-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathcatch_game.py
68 lines (51 loc) · 1.88 KB
/
catch_game.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
66
67
68
import pygame
from settings import Settings
from character import Character
import game_functions as gf
from ball import Ball
from pygame.sprite import Group
from game_stats import GameStats
from time import sleep
def run_game():
pygame.init()
c_settings = Settings()
screen = pygame.display.set_mode(
(c_settings.screen_width, c_settings.screen_height)
)
pygame.display.set_caption("Catch")
# Make a character
character = Character(c_settings, screen)
# Make a group stores the ball in.
balls = Group()
new_ball = Ball(c_settings, screen)
balls.add(new_ball)
stats = GameStats(c_settings)
while True:
gf.check_events(c_settings, screen, character)
if stats.game_active:
character.update()
new_ball.update()
# Check if the character collides with any ball in the group.
# If so, get rid of the ball and regenerate it again.
# Otherwise, check if the ball has go down to the screen.
# If so, decrement tries left and reset the game.
if pygame.sprite.spritecollideany(character, balls):
new_ball.kill()
new_ball = Ball(c_settings, screen)
balls.add(new_ball)
if new_ball.rect.y > c_settings.screen_height:
# Decrement tryes left.
if stats.left_tryes > 0:
stats.left_tryes -= 1
new_ball.kill()
new_ball = Ball(c_settings, screen)
balls.add(new_ball)
character.character_center()
character.update()
new_ball.update()
sleep(0.5)
else:
stats.game_active = False
gf.update_screen(c_settings, screen, character, new_ball)
if __name__ == "__main__":
run_game()