[英]Why does the event.type function return a name error?
我正在嘗試使用 PyGame 作為初學者項目制作一個簡單的迷宮游戲,但是當我嘗試使用event.type
時,它說,
Traceback (most recent call last):
File "simple_maze.py", line 22, in <module>
if event.type == pygame.KEYDOWN:
NameError: name 'event' is not defined
作為參考,這是我當前的代碼。
import pygame
screendim = (500, 500)
blu = (0, 0, 255)
red = (255, 0, 0)
sc = (0, 0)
pr = 50
pygame.init()
screen = pygame.display.set_mode(screendim)
pygame.draw.circle(screen, blu, (250, 10), 10)
pygame.draw.rect(screen, red, pygame.Rect(0, 25, 450, 15))
pygame.display.flip()
play = True
while play:
if event.type == pygame.KEYDOWN:
有任何想法嗎?
event
不是一直存在的 object - 你必須創建它
for event in pygame.event.get():
if event.type == pygame.KEYDOWN:
print(event) # do something with this information about key event
您應該在每個教程中看到這一點 - 即。 使用 Python 和 Pygame 對街機游戲進行編程。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.