Game Programming: Coding for Fun 259
if event.type == pygame.MOUSEBUTTONDOWN:
if pygame.mouse.get_pressed()[0]: # Left mouse button, draw
mousedown = True
elif pygame.mouse.get_pressed()[2]: # Right mouse button, pop
pos = pygame.mouse.get_pos()
clicked_smileys = [s for s in sprite_list if
s.rect.collidepoint(pos)]
sprite_list.remove(clicked_smileys)
if len(clicked_smileys) > 0:
pop.play()
count_popped += len(clicked_smileys)
if event.type == pygame.MOUSEBUTTONUP:
mousedown = False
screen.fill(BLACK)
sprite_list.update()
sprite_list.draw(screen)
clock.tick(60)
draw_string = "Bubbles created: " + str(count_smileys)
draw_string += " - Bubbles popped: " + str(count_popped)
if (count_smileys > 0):
draw_string += " - Percent: "
draw_string += str(round(count_popped/count_smileys*100, 1))
draw_string += "%"
text = font.render(draw_string, True, WHITE)
text_rect = text.get_rect()
text_rect.centerx = screen.get_rect().centerx
text_rect.y = 10
screen.blit (text, text_rect)
pygame.display.update()
if mousedown:
speedx = random.randint(-5, 5)
speedy = random.randint(-5, 5)
newSmiley = Smiley(pygame.mouse.get_pos(), speedx, speedy)
sprite_list.add(newSmiley)
count_smileys += 1
pygame.quit()
The more programs you write, the better you’ll get at coding.
You may start by coding games that you find interesting, writing
an app that solves a problem you care about, or developing apps for
other people. Keep coding, solve more problems, and get better and
better at programming, and you’ll soon be able to help create prod-
ucts that benefit users around the world.
Whether you’re coding mobile games and apps; writing pro-
grams that control automobiles, robots, or drones; or building the