I was following a tutorial on simulating physics with pymunk and pygame, however when i got to the part using event.MOUSEBUTTONDOWN
, it keeps being highlighted by pycharm and says "':' expected". Changing it from = to == fixed this issue but now whenever it runs it gives the error.
if event.type == event.MOUSEBUTTONDOWN:
pps.append(create_pp(space,event.pos))
AttributeError: 'Event' object has no attribute 'MOUSEBUTTONUP'
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
I think is this part went wrong if event.type == event.MOUSEBUTTONDOWN:
I think u could try this
if event.type == pygame.MOUSEBUTTONDOWN:
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.