python - pygame event AttributeError: 'Event' 对象没有属性 'MOUSEBUTTONUP'
问题描述
我正在关注一个关于使用 pymunk 和 pygame 模拟物理的教程,但是当我使用 的部分时event.MOUSEBUTTONDOWN
,它一直被 pycharm 突出显示并显示“':'预期”。将其从 = 更改为 == 解决了这个问题,但现在每当它运行时都会出现错误。
if event.type == event.MOUSEBUTTONDOWN:
pps.append(create_pp(space,event.pos))
AttributeError:“事件”对象没有属性“鼠标按钮”
import pygame,sys,pymunk
def create_pp(space,pos):
body = pymunk.Body(1,100,body_type = pymunk.Body.DYNAMIC)
body.position = pos
shape = pymunk.Circle(body,80)
space.add(body,shape)
return shape
def draw_pps(pps):
for pp in pps:
pos_x = int(pp.body.position.x)
pos_y = int(pp.body.position.y)
pp_rect = pp_surface.get_rect(center = (pos_x,pos_y))
screen.blit(pp_surface,pp_rect)
def static_mouth(space):
body = pymunk.Body(body_type = pymunk.Body.STATIC)
body.position = (430,400)
shape = pymunk.Circle(body, 50)
space.add(body,shape)
return shape
def draw_static_mouth(mouths):
for mouth in mouths:
pos_x = int(mouth.body.position.x)
pos_y = int(mouth.body.position.y)
mouth_rect = mouth_surface.get_rect(center = (pos_x,pos_y))
screen.blit(mouth_surface, mouth_rect)
pygame.init() #initiating pygame
screen = pygame.display.set_mode((600,600)) #creates display surface
clock = pygame.time.Clock() #creating game clock
space = pymunk.Space()
space.gravity = (0,400)
pp_surface = pygame.image.load('Drawing.png')
mouth_surface = pygame.image.load('Mouth.png')
pps = []
mouths = []
mouths.append(static_mouth(space))
while True: #game loop
for event in pygame.event.get(): #checks for user input
if event.type == pygame.QUIT:
pygame.quit()
sys.exit()
if event.type == event.MOUSEBUTTONDOWN:
pps.append(create_pp(space,event.pos))
screen.fill((217,217,217)) #backgroun color
draw_pps(pps)
draw_static_mouth(mouths)
space.step(1/50)
pygame.display.update() #renders frame
clock.tick(120) #limiting fps to 120
解决方案
我认为这部分出了问题if event.type == event.MOUSEBUTTONDOWN:
我想你可以试试这个
if event.type == pygame.MOUSEBUTTONDOWN:
推荐阅读
- java - 如何修复 maven org.apache.maven.surefire.testset.TestSetFailedException:
- android - CoordinatorLayout 在设计视图中不可见
- gradle - 如何在自定义 gradle 插件中获取 project.sourceSets.test.runtimeClasspath
- database - Magento 2 InstallSchema 方法未执行
- angular - 当我导航时,如何使选择的第一个组件的 client_id 不被删除
- c# - 有没有更好的方法从 redis 中获取所有值?
- wordpress - Shell Backupscript 弄乱了目录
- npm - 如何使用 xlsx npm 包写入特定单元格
- json - Spring rest 模板无法从 text/html 转换
- go - 迭代复数