首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >为什么event.type函数返回名称错误?

为什么event.type函数返回名称错误?
EN

Stack Overflow用户
提问于 2019-10-24 12:31:23
回答 1查看 36关注 0票数 1

我试着用PyGame做一个简单的迷宫游戏作为初学者的项目,但是当我试着使用event.type时,它说,

代码语言:javascript
复制
 Traceback (most recent call last):
  File "simple_maze.py", line 22, in <module>
    if event.type == pygame.KEYDOWN:
NameError: name 'event' is not defined 

作为参考,这是我当前的代码。

代码语言:javascript
复制
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:

有什么想法吗?

EN

回答 1

Stack Overflow用户

发布于 2019-10-24 12:38:06

event不是一直存在的对象-您必须创建它

代码语言:javascript
复制
for event in pygame.event.get(): 
    if event.type == pygame.KEYDOWN:
        print(event) # do something with this information about key event

你应该在每个教程中都能看到这一点。Program Arcade Games With Python And Pygame

票数 1
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/58534171

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档