-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.py
144 lines (101 loc) · 3.6 KB
/
main.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
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
import random
import os
import pygame
from pygame.constants import QUIT, K_DOWN, K_UP, K_LEFT, K_RIGHT
pygame.init()
FPS = pygame.time.Clock()
HEIGHT = 800
WIDTH = 1200
FONT = pygame.font.SysFont('Verdana', 20)
COLOR_WHITE = (255, 255, 255)
COLOR_BLACK = (0, 0, 0)
COLOR_BLUE = (0, 0, 255)
COLOR_GOLD = (255, 215, 0)
main_display = pygame.display.set_mode((WIDTH, HEIGHT))
bg = pygame.transform.scale(pygame.image.load('background.png'), (WIDTH, HEIGHT))
bg_X1 = 0
bg_X2 = bg.get_width()
bg_move = 3
IMAGE_PATH = 'Goose'
PLAYER_IMAGES = os.listdir(IMAGE_PATH)
player_size = (20, 20)
player = pygame.image.load('player.png').convert_alpha()
player_rect = player.get_rect()
player_move_down = [0, 4]
player_move_right = [4, 0]
player_move_left = [-4, 0]
player_move_top = [0, -4]
def create_enemy():
enemy_size = (30, 30)
enemy = pygame.image.load('enemy.png').convert_alpha()
enemy_rect = pygame.Rect(WIDTH, random.randint(0, HEIGHT), *enemy_size)
enemy_move = [random.randint(-8, -4), 0]
return [enemy, enemy_rect, enemy_move]
def create_bonus():
bonus_size = (20, 20)
bonus = pygame.image.load('bonus.png').convert_alpha()
bonus_rect = pygame.Rect(random.randint(0, WIDTH), 0, *bonus_size)
bonus_move = [0, random.randint(4, 8)]
return [bonus, bonus_rect, bonus_move]
CREATE_ENEMY = pygame.USEREVENT + 1
pygame.time.set_timer(CREATE_ENEMY, 1500)
CREATE_BONUS = pygame.USEREVENT + 2
pygame.time.set_timer(CREATE_BONUS, 3000)
CHANGE_IMAGE = pygame.USEREVENT + 3
pygame.time.set_timer(CHANGE_IMAGE, 200)
enemies = []
bonuses = []
score = 0
image_index = 0
playing = True
while playing:
FPS.tick(120)
for event in pygame.event.get():
if event.type == QUIT:
playing = False
if event.type == CREATE_ENEMY:
enemies.append(create_enemy())
if event.type == CREATE_BONUS:
bonuses.append(create_bonus())
if event.type == CHANGE_IMAGE:
player = pygame.image.load(os.path.join(IMAGE_PATH, PLAYER_IMAGES[image_index ]))
image_index += 1
if image_index >= len(PLAYER_IMAGES):
image_index = 0
bg_X1 -= bg_move
bg_X2 -= bg_move
if bg_X1 < -bg.get_width():
bg_X1 = bg.get_width()
if bg_X2 < -bg.get_width():
bg_X2 = bg.get_width()
main_display.blit(bg,( bg_X1, 0))
main_display.blit(bg,( bg_X2, 0))
keys = pygame.key.get_pressed()
if keys[K_DOWN] and player_rect.bottom < HEIGHT:
player_rect = player_rect.move(player_move_down)
if keys[K_RIGHT] and player_rect.right < WIDTH:
player_rect = player_rect.move(player_move_right)
if keys[K_LEFT] and player_rect.left > 0:
player_rect = player_rect.move(player_move_left)
if keys[K_UP] and player_rect.top > 0:
player_rect = player_rect.move(player_move_top)
for enemy in enemies:
enemy[1] = enemy[1].move(enemy[2])
main_display.blit(enemy[0], enemy[1])
if player_rect.colliderect(enemy[1]):
playing = False
for bonus in bonuses:
bonus[1] = bonus[1].move(bonus[2])
main_display.blit(bonus[0], bonus[1])
if player_rect.colliderect(bonus[1]):
score += 1
bonuses.pop(bonuses.index(bonus))
main_display.blit(FONT.render(str(score), True, COLOR_BLACK), (WIDTH-50, 20))
main_display.blit(player, player_rect)
pygame.display.flip()
for enemy in enemies:
if enemy[1].left < 0:
enemies.pop(enemies.index(enemy))
for bonus in bonuses:
if bonus[1].bottom > HEIGHT:
bonuses.pop(bonuses.index(bonus))