-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.py
151 lines (115 loc) · 4.15 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
146
147
148
149
150
151
import random
import os
import pygame
from pygame.constants import QUIT, K_DOWN, K_UP, K_RIGHT, K_LEFT
# import game_obj
pygame.init()
FPS = pygame.time.Clock()
HEIGHT = 800
WIDTH = 1200
COLOR_WHITE = (255, 255, 255)
COLOR_BLACK = (0, 0, 0)
COLOR_BLUE = (0, 0, 255)
COLOR_RED = (255, 0, 0)
FONT = pygame.font.SysFont('Verdana', 20)
main_display = pygame.display.set_mode((WIDTH, HEIGHT))
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)
def create_enemy():
image = pygame.image.load('enemy.png').convert_alpha()
image_size = image.get_size()
enemy = pygame.transform.scale(image, (image_size[0] // 2, image_size[1] //2))
enemy_rect = pygame.Rect(WIDTH, random.randint(100, HEIGHT-100), *enemy.get_size())
enemy_move = [random.randint(-8, -4), 0]
return [enemy, enemy_rect, enemy_move]
def create_bonus():
image = pygame.image.load('bonus.png').convert_alpha()
image_size = image.get_size()
bonus = pygame.transform.scale(image, (image_size[0] // 2, image_size[1] //2))
bonus_rect = pygame.Rect(random.randint(100, WIDTH-100), 0, *bonus.get_size())
bonus_move = [0, random.randint(4, 8)]
return [bonus, bonus_rect, bonus_move]
# Player set
IMAGE_PASS = 'Goose'
PLAYER_IMAGES = os.listdir(IMAGE_PASS)
player = pygame.image.load('player.png').convert_alpha()
player_rect = player.get_rect()
player_rect.top = HEIGHT // 4
player_move_down = [0, 4]
player_move_right = [4, 0]
player_move_up = [0, -4]
player_move_left = [-4, 0]
# Backgroung set
bg = pygame.transform.scale(pygame.image.load('background.png'), (WIDTH, HEIGHT))
bg_x1 = 0
bg_x2 = bg.get_width()
bg_move = 3
# Game obj
enemies = list()
bonuses = list()
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_PASS, PLAYER_IMAGES[image_index])).convert_alpha()
image_index += 1
if image_index >= len(PLAYER_IMAGES):
image_index = 0
# Backround set
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))
# Key press
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)
# Enemy
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
# Write lebel "Game over" and button "restart"
# Add 3 life to duck with hearts
# Bonuses
for bonus in bonuses:
bonus[1] = bonus[1].move(bonus[2])
main_display.blit(bonus[0], bonus[1])
if player_rect.colliderect(bonus[1]):
bonuses.pop(bonuses.index(bonus))
score += 1
#Show player
main_display.blit(player, player_rect)
main_display.blit(FONT.render(str(score), True, COLOR_BLACK), (WIDTH - 50, 20))
pygame.display.flip()
# DELETE obj after them left main screen
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))