首页 > 解决方案 > 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    

标签: python

解决方案


我认为这部分出了问题if event.type == event.MOUSEBUTTONDOWN:

我想你可以试试这个

if event.type == pygame.MOUSEBUTTONDOWN:

推荐阅读