Py学习  »  Python

python 3.2w/pygame崩溃

Lutmer • 6 年前 • 506 次点击  

好吧,我很高兴在pygame上开始学习python,但是当我在学习一些入门教程的时候,我注意到,在运行代码的时候,pygame窗口看不到响应,所以我放了一些“print”命令来查看它有多远,我注意到它在循环中停止了,有什么办法可以修复它吗?我把密码留在这里

import pygame
pygame.init()

print("First Fase")
win = pygame.display.set_mode((500,500)) 

pygame.display.set_caption("Test")

print("Second Fase")
x = 50
y =50
width = 40
height = 60
vel = 7

print("Third Fase")

done = False
while not done:
 pygame.time.delay(100)

for event in pygame.event.get():
 if event.type == pygame.QUIT:
  done = True

if event.type == KEYDOWN:
 if event.key == K_ESC:
  done = True

pygame.draw.rect(win, (255, 0, 0), (x, y, width, height))
pygame.display.update()
print("NoErrors")
Python社区是高质量的Python/Django开发社区
本文地址:http://www.python88.com/topic/42977
文章 [ 1 ]  |  最新文章 6 年前
Rabbid76
Reply   •   1 楼
Rabbid76    6 年前

它必须是 pygame.KEYDOWN pygame.K_ESC 而不是 KEYDOWN K_ESC .

但首先你要尊重 Indentation . 在以下代码中,循环没有嵌套:

done = False
while not done:
 pygame.time.delay(100)

for event in pygame.event.get():
 if event.type == pygame.QUIT:
  done = True

这个 for 循环不在 while ,它是 虽然 循环。

你必须这样格式化你的代码:

import pygame
pygame.init()

print("First Fase")
win = pygame.display.set_mode((500,500)) 

pygame.display.set_caption("Test")

print("Second Fase")
x = 50
y =50
width = 40
height = 60
vel = 7

print("Third Fase")

done = False
while not done:
    pygame.time.delay(100)

    for event in pygame.event.get():
        if event.type == pygame.QUIT:
            done = True

        if event.type == pygame.KEYDOWN:
            if event.key == pygame.K_ESC:
                done = True

    pygame.draw.rect(win, (255, 0, 0), (x, y, width, height))
    pygame.display.update()
    print("NoErrors")