-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmain.py
144 lines (112 loc) · 5 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 = 500 #параметри екрану
WIDTH = 700
FONT = pygame.font.SysFont("Verdana", 20)
COLOR_WHITE = (255, 255, 255)
COLOR_BLACK = (0, 0, 0)
COLOR_BLUE = (0, 0, 255)
COLOR_GREEN = (0, 128, 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 = pygame.Surface(player_size) #клас який містить сутності якими можна керувати, розміщувати на правому полі
#player.fill(COLOR_BLACK) #гравець кольору
player_rect = player.get_rect() #повертає координати х,у
player_move_down = [0, 4] #функція яка описує рух гравця донизу
player_move_right = [4, 0]
player_move_up = [0, -4]
player_move_left = [-4, 0]
def create_enemy():
enemy_size = (30, 30)
enemy = pygame.image.load('enemy.png').convert_alpha()
#enemy = pygame.Surface(enemy_size)
#enemy.fill(COLOR_BLUE)
enemy_rect = pygame.Rect(WIDTH, random.randint(enemy.get_height(), HEIGHT- enemy.get_height()), *enemy_size)
enemy_move = [random.randint(-8, -4), 0]
return [enemy, enemy_rect, enemy_move] #те що повертаємо
def creat_bonus():
bonus_size = (20, 20)
bonus = pygame.image.load('bonus.png').convert_alpha()
#bonus = pygame.Surface(bonus_size)
#bonus.fill(COLOR_GREEN)
bonus_rect = pygame.Rect(random.randint(bonus.get_width(), WIDTH - bonus.get_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, 1500)
CHANCE_IMAGE = pygame.USEREVENT + 3
pygame.time.set_timer(CHANCE_IMAGE, 200)
enemies = []
bonuses = []
score = 0
image_index = 0
playing = True
while playing: #нескінченний цикл, for скінченний
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(creat_bonus())
if event.type == CHANCE_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
#main_display.fill(COLOR_BLACK) #зафарбування ігрового поля
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_UP] and player_rect.top > 0:
player_rect = player_rect.move(player_move_up)
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(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].top > HEIGHT:
bonuses.pop(bonuses.index(bonus))
# for bonus in bonuses:
# if bonus[1].top > HEIGHT: 500
# if bonus[2].top > WIDTH 700
# bonuses.pop(bonuses.index(bonus))