我目前正在使用 Python 编写国际象棋程序。我使用 Sublime Text 3 作为 IDE,但它似乎不适用于 pygame。我创建了一个棋盘,每当我在 Sublime Text 中运行它时,棋盘都不会出现。不知何故,这似乎类似于我无法在 Sublime Text 中输入的问题。(虽然从控制台运行脚本可以工作,但板子应该会出现)。我会在下面附上我的代码,以防你想看看,但我认为问题不在于代码依赖。所以我的问题是是否有办法从 Sublime Text 运行 python 脚本,以便用户输入和这些使用 pygame 的功能是否正常工作?
谢谢!
"""
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 = []