-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.py
145 lines (108 loc) · 3.76 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
145
import os
import random
import pygame
from pygame.constants import K_DOWN, K_LEFT, K_RIGHT, K_UP, QUIT
pygame.init()
FPS = pygame.time.Clock()
HEIGHT = 800
WIDTH = 1200
FONT = pygame.font.SysFont("Verdana", 20)
bg = pygame.transform.scale(pygame.image.load("./img/background.png"), (WIDTH, HEIGHT))
bg_x = 0
bg_x2 = bg.get_width()
bg_move = 3
COLOR_WHITE = (255, 255, 255)
COLOR_BLACK = (0, 0, 0)
COLOR_BLUE = (0, 0, 255)
COLOR_GREEN = (0, 255, 0)
main_display = pygame.display.set_mode((WIDTH, HEIGHT))
IMAGE_PASS = "./img/goose"
PLAYER_IMG = os.listdir(IMAGE_PASS)
player_size = (15, 15)
player = pygame.image.load("./img/player.png").convert_alpha()
player_rect = player.get_rect()
player_rect.center = main_display.get_rect().center
player_move_down = [0, 4]
player_move_top = [0, -4]
player_move_right = [4, 0]
player_move_left = [-4, 0]
def create_enemy():
enemy = pygame.image.load("./img/enemy.png").convert_alpha()
enemy_width = enemy.get_height()
enemy_rect = pygame.Rect(
WIDTH, random.randint(enemy_width, HEIGHT - enemy_width), *enemy.get_size()
)
enemy_move = [random.randint(-8, -4), 0]
return [enemy, enemy_rect, enemy_move]
def create_bonus():
bonus = pygame.image.load("./img/bonus.png").convert_alpha()
bonus_width = bonus.get_width()
bonus_rect = pygame.Rect(
random.randint(bonus_width, WIDTH - bonus_width),
-bonus_width,
*bonus.get_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
img_indx = 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_PASS, PLAYER_IMG[img_indx]))
img_indx += 1
if img_indx >= len(PLAYER_IMG):
img_indx = 0
bg_x -= bg_move
bg_x2 -= bg_move
if bg_x < -bg.get_width():
bg_x = bg.get_width()
if bg_x2 < -bg.get_width():
bg_x2 = bg.get_width()
main_display.blit(bg, (bg_x, 0))
main_display.blit(bg, (bg_x2, 0))
keys = pygame.key.get_pressed()
if keys[K_UP] and player_rect.top > 0:
player_rect = player_rect.move(player_move_top)
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)
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(player, player_rect)
main_display.blit(FONT.render(str(score), True, COLOR_BLACK), (WIDTH - 50, 20))
pygame.display.flip()
for enemy in enemies:
if enemy[1].right < 0:
enemies.pop(enemies.index(enemy))
for bonus in bonuses:
if bonus[1].top > HEIGHT:
bonuses.pop(bonuses.index(bonus))