-
Notifications
You must be signed in to change notification settings - Fork 1
/
game_functions.py
65 lines (57 loc) · 2.25 KB
/
game_functions.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
import sys
import pygame
from player import Player
from bubble import Bubble
pygame.init()
ADDBUBBLE = pygame.USEREVENT + 1
pygame.time.set_timer(ADDBUBBLE, 250)
def check_events(game_settings, screen, player, bubbles, stats, play_button):
for event in pygame.event.get():
if event.type == pygame.QUIT:
sys.exit()
elif event.type == pygame.KEYDOWN:
if event.key == pygame.K_RIGHT:
player.moving_right = True
if event.key == pygame.K_LEFT:
player.moving_left = True
if event.key == pygame.K_UP:
player.moving_up = True
if event.key == pygame.K_DOWN:
player.moving_down = True
elif event.type == pygame.KEYUP:
if event.key == pygame.K_RIGHT:
player.moving_right = False
if event.key == pygame.K_LEFT:
player.moving_left = False
if event.key == pygame.K_UP:
player.moving_up = False
if event.key == pygame.K_DOWN:
player.moving_down = False
elif event.type == ADDBUBBLE:
create_bubble(game_settings, screen, bubbles)
elif event.type == pygame.MOUSEBUTTONDOWN:
mouse_x, mouse_y = pygame.mouse.get_pos()
check_play_button(stats, play_button, mouse_x, mouse_y)
def check_play_button(stats, play_button, mouse_x, mouse_y):
if play_button.rect.collidepoint(mouse_x, mouse_y):
stats.game_active = True
def create_bubble(game_settings, screen, bubbles):
new_bubble = Bubble(screen, game_settings)
bubbles.add(new_bubble)
def update_bubbles(player, bubbles, stats, sb):
hitted_bubble = pygame.sprite.spritecollideany(player, bubbles)
if hitted_bubble != None:
stats.score += hitted_bubble.bubble_radius
sb.prepare_score()
hitted_bubble.kill()
def update_screen(game_settings, screen, player, bubbles, clock, stats, play_button, sb):
screen.fill(game_settings.bg_color)
player.blit_me()
if len(bubbles) > 0:
for bubble in bubbles:
bubble.blit_me()
sb.draw_score()
clock.tick(30)
if not stats.game_active:
play_button.draw_button()
pygame.display.flip()