【问题标题】:Python window opens and closesPython 窗口打开和关闭
【发布时间】:2018-08-02 22:57:45
【问题描述】:

我一直在关注here 的教程来构建一个小型 Python 游戏。 这就是它背后的代码:

import pygame

pygame.init()

display_width = 1280
display_height = 720

gameDisplay = pygame.display.set_mode((display_width,display_height))
pygame.display.set_caption("Racing Game")
clock = pygame.time.Clock()

black = (0,0,0)
white = (255,255,255)
carImg = pygame.image.load("racecar.png")

def car(x,y):
    gameDisplay.blit(carImg, (x,y))

x = display_width * 0.45
y = display_height * 0.8
x_change = 0
car_speed = 0

crashed = True
while crashed:
    for event in pygame.event.get():
        if event.type == pygame.QUIT:
            crashed = False
        ## <code to remove>
        if event.type == pygame.KEYDOWN:
            if event.key == pygame.K_LEFT:
                x_change = -5
            elif event.key == pygame.K_RIGHT:
                x_change = 5
        if event.type = pygame.KEYUP:
            if event.key == pygame.K_LEFT or event.key == pygame.K_RIGHT:
                x_change = 0
        ## </code to remove>
        print(event)
    x += x_change
    gameDisplay.fill((255,255,255))
    car(x,y)
    pygame.display.update()
    clock.tick(60)

pygame.display.quit()   
pygame.quit()
quit()

当我尝试运行它时,窗口会打开并立即关闭。但是,如果我删除两个 ## &lt;code to remove&gt;## &lt;/code to remove&gt; 之间的代码,一切正常。发生这种情况的那段代码是什么原因造成的?

【问题讨论】:

  • 这不能编译(无效语法):if event.type = pygame.KEYUP:.
  • 很适合我。注意我修复了@ggorlen 所述的问题。仅供参考:我正在使用带有 Python 3.5 的 Raspbian Stretch
  • 由无效语法错误引起。下次尝试通过终端(或 Windows 上的命令行)运行它,而不是查看实际的错误代码
  • @Gabriel Python 在启动解释器之前扫描语法,所以这完全是预运行时。

标签: python pygame


【解决方案1】:

这是由if event.type = pygame.KEYUP: 的语法错误引起的。打开文件会导致它立即关闭,但在解释器 (IDLE) 中运行它会显示该错误。只需将其更改为if event.type == pygame.KEYUP:,一切都会正常工作。

更新:

从文件而不是解释器 (IDLE) 运行代码不会总是打开。最好在 IDLE 中运行。

代码:

import pygame

pygame.init()

display_width = 1280
display_height = 720

gameDisplay = pygame.display.set_mode((display_width,display_height))
pygame.display.set_caption("Racing Game")
clock = pygame.time.Clock()

black = (0,0,0)
white = (255,255,255)
carImg = pygame.image.load("racecar.png")

def car(x,y):
    gameDisplay.blit(carImg, (x,y))

x = display_width * 0.45
y = display_height * 0.8
x_change = 0
car_speed = 0

crashed = True
while crashed:
    for event in pygame.event.get():
        if event.type == pygame.QUIT:
            crashed = False
        #############################
        if event.type == pygame.KEYDOWN:
            if event.key == pygame.K_LEFT:
                x_change = -5
            elif event.key == pygame.K_RIGHT:
                x_change = 5
        if event.type == pygame.KEYUP:
            if event.key == pygame.K_LEFT or event.key == pygame.K_RIGHT:
                x_change = 0
        #############################
        print(event)
    x += x_change
    gameDisplay.fill((255,255,255))
    car(x,y)
    pygame.display.update()
    clock.tick(60)

pygame.display.quit()   
pygame.quit()
quit()

【讨论】:

  • 如果您发现这个答案是解决方案,请不要忘记接受它。
  • @GhostCat 这是一个错误。谢谢你让我知道。我的意思是说“无法挽救”。
猜你喜欢
  • 1970-01-01
  • 2016-08-08
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2014-08-01
  • 2020-11-02
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多