Pycharm Window 显示不正确
Pycharm Window Not Displaying Correctly
我按照 Youtube 教程在 Python 代码中创建了一个国际象棋引擎,但它在 Pycharm 中不起作用?我可以看到正确的棋盘已创建,但只有在我关闭弹出窗口时才会看到 window。最初它显示为黑屏,然后当我关闭 window 时它也会在关闭前短暂出现。所以我知道代码按预期工作,但手头还有一些其他问题。
以下是代码以备不时之需:
import pygame as p
from Chess import engine
WIDTH = HEIGHT = 800
DIMENSION = 8
SQ_SIZE = HEIGHT // DIMENSION
MAX_FPS = 15
IMAGES = {}
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("images/" + piece + ".png"), (SQ_SIZE, SQ_SIZE))
# any icon image can be accessed using ICONS['__']
def main():
p.init()
screen = p.display.set_mode((WIDTH, HEIGHT))
clock = p.time.Clock()
screen.fill(p.Color("white"))
gs = engine.State()
loadImages()
running = True
while running:
for e in p.event.get():
if e.type == p.QUIT:
running = False
visualise(screen, gs)
clock.tick(MAX_FPS)
p.display.flip()
def visualise(screen, gs):
drawBoard(screen)
drawPieces(screen, gs.board)
def drawBoard(screen):
colours = [p.Color("white"), p.Color("gray")]
for row in range(DIMENSION):
for column in range(DIMENSION):
tile = colours[((row + column) % 2)]
p.draw.rect(screen, tile, p.Rect(column * SQ_SIZE, row * SQ_SIZE, SQ_SIZE, SQ_SIZE))
def drawPieces(screen, board):
for row in range(DIMENSION):
for column in range(DIMENSION):
piece = board[row][column]
if piece != "--":
screen.blit(IMAGES[piece], p.Rect(column * SQ_SIZE, row * SQ_SIZE, SQ_SIZE, SQ_SIZE))
if __name__ == "__main__":
main()
// engine.py file
class State():
def __init__(self):
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 = []
这是Indentation的事情。您必须在应用程序循环中而不是在应用程序循环之后更新显示:
def main():
p.init()
screen = p.display.set_mode((WIDTH, HEIGHT))
clock = p.time.Clock()
screen.fill(p.Color("white"))
gs = engine.State()
loadImages()
running = True
while running:
for e in p.event.get():
if e.type == p.QUIT:
running = False
# INDENTATION
#-->|
visualise(screen, gs)
clock.tick(MAX_FPS)
p.display.flip()
我按照 Youtube 教程在 Python 代码中创建了一个国际象棋引擎,但它在 Pycharm 中不起作用?我可以看到正确的棋盘已创建,但只有在我关闭弹出窗口时才会看到 window。最初它显示为黑屏,然后当我关闭 window 时它也会在关闭前短暂出现。所以我知道代码按预期工作,但手头还有一些其他问题。
以下是代码以备不时之需:
import pygame as p
from Chess import engine
WIDTH = HEIGHT = 800
DIMENSION = 8
SQ_SIZE = HEIGHT // DIMENSION
MAX_FPS = 15
IMAGES = {}
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("images/" + piece + ".png"), (SQ_SIZE, SQ_SIZE))
# any icon image can be accessed using ICONS['__']
def main():
p.init()
screen = p.display.set_mode((WIDTH, HEIGHT))
clock = p.time.Clock()
screen.fill(p.Color("white"))
gs = engine.State()
loadImages()
running = True
while running:
for e in p.event.get():
if e.type == p.QUIT:
running = False
visualise(screen, gs)
clock.tick(MAX_FPS)
p.display.flip()
def visualise(screen, gs):
drawBoard(screen)
drawPieces(screen, gs.board)
def drawBoard(screen):
colours = [p.Color("white"), p.Color("gray")]
for row in range(DIMENSION):
for column in range(DIMENSION):
tile = colours[((row + column) % 2)]
p.draw.rect(screen, tile, p.Rect(column * SQ_SIZE, row * SQ_SIZE, SQ_SIZE, SQ_SIZE))
def drawPieces(screen, board):
for row in range(DIMENSION):
for column in range(DIMENSION):
piece = board[row][column]
if piece != "--":
screen.blit(IMAGES[piece], p.Rect(column * SQ_SIZE, row * SQ_SIZE, SQ_SIZE, SQ_SIZE))
if __name__ == "__main__":
main()
// engine.py file
class State():
def __init__(self):
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 = []
这是Indentation的事情。您必须在应用程序循环中而不是在应用程序循环之后更新显示:
def main():
p.init()
screen = p.display.set_mode((WIDTH, HEIGHT))
clock = p.time.Clock()
screen.fill(p.Color("white"))
gs = engine.State()
loadImages()
running = True
while running:
for e in p.event.get():
if e.type == p.QUIT:
running = False
# INDENTATION
#-->|
visualise(screen, gs)
clock.tick(MAX_FPS)
p.display.flip()