所以继承我的剧本:
import pygame
pygame.init()
gameDisplay = pygame.display.set_mode((600,600))
pygame.display.set_caption('Doge Adventures')
gameexit = False
move_x = 300
move_y = 300
move_x_change = 0
move_y_change = 0
while not gameexit:
for event in pygame.event.get():
if event.type == pygame.QUIT:
gameexit = True
if event.type == pygame.KEYDOWN:
if event.key == pygame.K_LEFT:
move_x_change = -10
if event.key == pygame.K_RIGHT:
move_y_change = 10
move_x += move_x_change
gameDisplay.fill(white)
pygame.draw.rect(gameDisplay, black, [move_x, move_y, 10, 10])
pygame.display.update()
pygame.quit()
quit()
问题在于,当我运行它时,没有任何反应,没有错误。只是闲置弹出,但没有窗口。我想知道我的代码是否有任何问题。我正在使用pygame 2.6运行python 2.6
答案 0 :(得分:0)
正如我所说,你需要缩进所有内容,但我在你的代码中发现了很多问题,这是我用它做的一个版本:
import pygame
pygame.init()
gameDisplay = pygame.display.set_mode((600,600))
pygame.display.set_caption('Doge Adventures')
gameexit = False
move_x = 300
move_y = 300
white = (255, 255, 255)
black = (0, 0, 0)
while not gameexit:
for event in pygame.event.get():
if event.type == pygame.QUIT:
gameexit = True
if event.type == pygame.KEYDOWN:
if event.key == pygame.K_LEFT:
move_x -= 10
elif event.key == pygame.K_RIGHT:
move_x += 10
elif event.key == pygame.K_UP:
move_y -= 10
elif event.key == pygame.K_DOWN:
move_y += 10
gameDisplay.fill(white)
pygame.draw.rect(gameDisplay, black, [move_x, move_y, 10, 10])
pygame.display.update()
pygame.quit()
quit()
我推荐你的Python / Pygame的Al Sweigart书。这是一本很棒的书。