0

I'm currently working on a chess programm in Python. I use Sublime Text 3 as an IDE, but it doesn't seem to work properly for pygame. I created a chess board and whenever I run it in Sublime Text the board doesn't appear. Somehow it seems similar to the problem that I cannot have input in Sublime Text. (Running the script from the Console works though, the board appears as it should). I'll attach my code below in case you want to have a look, but the problem isn't code dependend I think.. So my question is whether there is a way to run python scripts from Sublime Text such that user input and these functions using pygame properly work?

Thanks!

"""
This is the main driver file. It is responsible for handling user input and displaying current GameState object
"""
import pygame as p
import chess_engine

p.init()
WIDTH = HEIGHT = 512 ##400 is another option
DIMENSION = 8
SQ_SIZE = HEIGHT // DIMENSION
MAX_FPS = 15
IMAGES = {}

'''
Initialize a global dictionary of images. This will be called exactly once in the main! It is its own method (not in the main)
so we are flexible
'''
def loadImages():
    pieces = ["wP", "wR", "wN", "wB", "wK", "wQ", "bP", "bR", "bN", "bB", "bK", "bQ"]
    for piece in pieces:
        IMAGES[piece] = p.transform.scale(p.image.load("pieces/" + piece + ".png"), (SQ_SIZE, SQ_SIZE)) ##load pictures into IMAGES dictionary
    #pygame.image.load() function returns us a Surface with the ball data
    #Note: we can access an image by saying 'IMAGES['wp']'
    #we also scale the images so it fits the square nicely

'''
The main driver for the code. It handles user inpute and updates the graphics.
'''
def main():
    p.init()
    screen = p.display.set_mode((WIDTH, HEIGHT))
    #creates a new Surface object that represents the actual displayed graphics. Any drawing you do to this Surface will become visible on the monitor.
    clock = p.time.Clock()
    screen.fill(p.Color("white"))
    gs = chess_engine.GameState()
    #print(gs.board)
    loadImages() #only do this once, before the while loop

    ##starting here, program is inizalized and ready to run
    ##inside an infinite loop we check if a QUIT event has happened. If so, we exit the programm
    running = True
    while running:
        for e in p.event.get():
            if e.type == p.QUIT:
                running = False
        drawGameState(screen, gs)
        clock.tick(MAX_FPS)
        p.display.flip()
    # This makes everything we have drawn on the screen Surface become visible
    # This buffering makes sure we only see completely drawn frames on the screen. 
    # Without it, the user would see the half completed parts of the screen as they are being created.

'''
Responsible for all the graphics within a current game state.
'''
def drawGameState(screen, gs):
    #order in which we call is important! First draw the board, then the pieces!!
    drawBoard(screen) #draw squares on the board
    #add in piece highlighting for move suggestions (later)
    drawPieces(screen, gs.board) #draw pieces on top of those squares

'''
Draw the squares on the board. The top left square is always light (no matter the perspective)
'''
def drawBoard(screen):
    colors = [p.Color("white"), p.Color("gray")]
    for r in range(DIMENSION):
        for c in range(DIMENSION):
            color = colors[((r+c) % 2)] #sum of tuple of coordinates of white squares are always even! (0,0), (0,2), (1,1),...
            p.draw.rect(screen, color, p.Rect(c*SQ_SIZE, r*SQ_SIZE, SQ_SIZE, SQ_SIZE))

'''
Draw the pieces on the board using the urrent GameState.board
'''
def drawPieces(screen, board):
    for r in range(DIMENSION):
        for c in range(DIMENSION):
            piece = board[r][c]
            if piece != "--": #not empty square
                screen.blit(IMAGES[piece], p.Rect(c*SQ_SIZE, r*SQ_SIZE, SQ_SIZE, SQ_SIZE))


if __name__ == "__main__":
    main()
"""
This class is responsible for storing all the information about the current state of a chess game.
"""

class GameState():
    def __init__(self):
        #board is a 8x8 2d list, each element has 2 chars
        #first char represents color of piece
        #second char represents type
        #"--" represents an empty space
        self.board = [
            ["bR", "bN", "bB", "bQ", "bK", "bB", "bN", "bR"],
            ["bP", "bP", "bP", "bP", "bP", "bP", "bP", "bP"],
            ["--", "--", "--", "--", "--", "--", "--", "--"],
            ["--", "--", "--", "--", "--", "--", "--", "--"],
            ["--", "--", "--", "--", "--", "--", "--", "--"],
            ["--", "--", "--", "--", "--", "--", "--", "--"],
            ["wP", "wP", "wP", "wP", "wP", "wP", "wP", "wP"],
            ["wR", "wN", "wB", "wQ", "wK", "wB", "wN", "wR"]
        ]
        self.whiteToMove = True
        self.moveLog = []
  • It doesn't look like you're calling `input()` or friends here; is your problem that when you run this from within Sublime, no window appears at all? if so, are you using a `sublime-build` file on Windows that uses `cmd` to specify what to execute? – OdatNurd Jun 07 '21 at 19:07
  • Yes, it's the problem that no window appears at all. But the editor was right, it's actually a duplicate problem, since it's the same problem for input. I solved it making a new sublime-build with SublimeREPL. Thanks! – cactus_splines Jun 08 '21 at 06:03
  • 1
    For the record, the reason this happens on windows is that build systems that use `cmd` on Windows tell the OS specifically "Do not allow this program to create a window". Using `shell_cmd` OR `cmd` in conjunction with `"shell": true` in the build allows programs on Windows to open a WIndow normally. Your problem is most likely solved incidentally due to the change you made; this has nothing to do with requesting Input in the general case – OdatNurd Jun 08 '21 at 13:24

0 Answers0