-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmain.py
More file actions
249 lines (202 loc) · 7.7 KB
/
main.py
File metadata and controls
249 lines (202 loc) · 7.7 KB
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
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
import os.path
import random
import sys
import xml.etree.ElementTree as ET
import main
from timeloop import Timeloop
from datetime import timedelta
import pygame
pygame.init()
ran = random.Random()
tl = Timeloop()
WIDTH, HEIGHT = 900, 500
FPS = 60
spawn_enemy = pygame.USEREVENT + 1
spaceship_hit = pygame.USEREVENT + 2
score_add = pygame.USEREVENT + 3
SPACESHIP_WIDTH, SPACESHIP_HEIGHT = 35, 35
VEL = 5
SPACESHIP_MAX_BULLETS = 3
SPACESHIP_MAX_HEALTH = 3
spaceship_bullets = []
ENEMY_WIDTH, ENEMY_HEIGHT = 55, 55
VEL_ENEMY = 1
enemies = []
enemy_bullets = []
SPAWN_INTERVAL = 1.5
FIRE_INTERVAL = 1.1
VEL_BULLETS = 8
BLACK = (0, 0, 0)
WHITE = (255, 255, 255)
RED = (255, 0, 0)
BLUE = (0, 0, 255)
GRAY = (100, 100, 100)
END_FONT = pygame.font.SysFont("comicsans", 100)
NORM_FONT = pygame.font.SysFont("comicsans", 40)
SMALL_NORM_FONT = pygame.font.SysFont("comicsans", 30)
ENEMY_IMAGE = pygame.image.load(os.path.join('Assets', 'Default', 'enemy_D.png'))
ENEMY = pygame.transform.rotate((pygame.transform.scale(ENEMY_IMAGE, (ENEMY_WIDTH, ENEMY_HEIGHT))), 90)
SPACESHIP_IMAGE = pygame.image.load(os.path.join('Assets', 'Default', 'ship_G.png'))
SPACESHIP = pygame.transform.rotate(pygame.transform.scale(SPACESHIP_IMAGE, (SPACESHIP_WIDTH, SPACESHIP_HEIGHT)), 270)
WIN = pygame.display.set_mode((WIDTH, HEIGHT))
pygame.display.set_caption("Test Game")
FILE_NAME = 'scoreboard.xml'
dom = ET.parse(FILE_NAME)
root = dom.getroot()
player = dom.findall('player')
def draw_window(spaceship, spaceship_bullets, enemies, enemy_bullets, score, health):
score_text = f"score: {score}"
health_text = f"health: {health}/{SPACESHIP_MAX_HEALTH}"
draw_text = NORM_FONT.render(score_text, True, WHITE)
draw_text2 = NORM_FONT.render(health_text, True, WHITE)
WIN.fill(BLACK)
WIN.blit(draw_text, (20, 20))
WIN.blit(draw_text2, (WIDTH - 20 - draw_text2.get_width(), 20))
WIN.blit(SPACESHIP, (spaceship.x, spaceship.y))
for bullet in spaceship_bullets:
pygame.draw.rect(WIN, BLUE, bullet)
for enemy in enemies:
WIN.blit(ENEMY, (enemy.x, enemy.y))
for bullet in enemy_bullets:
pygame.draw.rect(WIN, RED, bullet)
pygame.display.update()
def draw_end_text(scoreboard, score):
y = 140
place = 1
draw_text = END_FONT.render("GAME OVER", True, WHITE)
draw2_text = NORM_FONT.render(f"Your score {score}", True, WHITE)
WIN.blit(draw_text, (WIDTH / 2 - draw_text.get_width() / 2, 20))
WIN.blit(draw2_text, (WIDTH / 2 - draw2_text.get_width() / 2, 100))
for p in scoreboard:
draw_scoreboard = NORM_FONT.render(f"{place}. {p.get('name')} - {p.get('score')}", True, WHITE)
WIN.blit(draw_scoreboard, (WIDTH / 2 - draw_scoreboard.get_width() / 2, y))
y += 40
place += 1
pygame.display.update()
pygame.time.delay(5000)
def control_spaceship(spaceship):
keys_pressed = pygame.key.get_pressed()
if keys_pressed[pygame.K_w] and not spaceship.y - VEL < 0: # Up
spaceship.y -= VEL
if keys_pressed[pygame.K_s] and not spaceship.y + SPACESHIP_HEIGHT + VEL > HEIGHT: # Down
spaceship.y += VEL
if keys_pressed[pygame.K_a] and not spaceship.x - VEL < 0: # Left
spaceship.x -= VEL
if keys_pressed[pygame.K_d] and not spaceship.x + SPACESHIP_WIDTH + VEL > WIDTH: # Right
spaceship.x += VEL
def handle_enemies(enemies):
for enemy in enemies:
enemy.x -= VEL_ENEMY
@tl.job(interval=timedelta(seconds=SPAWN_INTERVAL))
def spawn_enemy():
enemy = pygame.Rect(WIDTH - 20, ran.randint(20, (HEIGHT - 70)), ENEMY_WIDTH, ENEMY_HEIGHT)
enemies.append(enemy)
@tl.job(interval=timedelta(seconds=FIRE_INTERVAL))
def enemy_fire():
for enemy in enemies:
enemy_bullet = pygame.Rect(enemy.x + ENEMY_WIDTH, enemy.y + ENEMY_HEIGHT / 2 - 2, 10, 5)
enemy_bullets.append(enemy_bullet)
def handle_bullets(spaceship_bullets, enemy_bullets, spaceship, enemies):
for bullet in spaceship_bullets:
bullet.x += VEL_BULLETS
if bullet.x > WIDTH:
spaceship_bullets.remove(bullet)
for enemy in enemies:
if enemy.colliderect(bullet):
spaceship_bullets.remove(bullet)
enemies.remove(enemy)
pygame.event.post(pygame.event.Event(score_add))
for bullet in enemy_bullets:
bullet.x -= VEL_BULLETS
if bullet.x < 0:
enemy_bullets.remove(bullet)
elif spaceship.colliderect(bullet):
enemy_bullets.remove(bullet)
pygame.event.post(pygame.event.Event(spaceship_hit))
def name_input():
user_text = ''
input_rect = pygame.Rect(200, 200, (WIDTH / 2) - 200, (HEIGHT / 2) - 200)
while True:
for event in pygame.event.get():
if event.type == pygame.KEYDOWN:
if event.key == pygame.K_BACKSPACE:
user_text = user_text[:-1]
elif event.key == pygame.K_SPACE:
return user_text
else:
user_text += event.unicode
pygame.draw.rect(WIN, GRAY, input_rect)
text_surface = SMALL_NORM_FONT.render(user_text, True, BLACK)
WIN.blit(text_surface, (input_rect.x + 5, input_rect.y + 5))
input_rect.w = max(100, text_surface.get_width() + 10)
pygame.display.flip()
def is_int(s):
try:
int(s)
return True
except ValueError:
return False
def edit_XML(score, name):
scoreboard = []
run_XML = True
old_score = 0
old_name = ""
for elem in root.iter('player'):
if score > int(elem.get('score')) and run_XML:
old_score = int(elem.get('score'))
old_name = elem.get('name')
elem.set('score', str(score))
elem.set('name', name)
run_XML = False
dom.write('scoreboard.xml')
elif old_score > int(elem.get('score')):
temp_score = int(elem.get('score'))
temp_name = elem.get('name')
elem.set('score', str(old_score))
elem.set('name', old_name)
dom.write('scoreboard.xml')
old_score = temp_score
old_name = temp_name
for p in player:
scoreboard.append(p)
return scoreboard
def main():
spaceship = pygame.Rect(20, HEIGHT // 2, SPACESHIP_WIDTH, SPACESHIP_HEIGHT)
score = 0
spaceship_health = SPACESHIP_MAX_HEALTH
clock = pygame.time.Clock()
run = True
while run:
clock.tick(FPS)
for event in pygame.event.get():
if event.type == pygame.QUIT:
run = False
pygame.quit()
sys.exit()
if event.type == pygame.KEYDOWN:
if event.key == pygame.K_SPACE and len(spaceship_bullets) < SPACESHIP_MAX_BULLETS:
bullet = pygame.Rect(spaceship.x + SPACESHIP_WIDTH, spaceship.y + SPACESHIP_HEIGHT / 2 - 2, 10, 5)
spaceship_bullets.append(bullet)
if event.type == pygame.KEYDOWN:
if event.key == pygame.K_ESCAPE:
run = False
pygame.quit()
sys.exit()
if event.type == spaceship_hit:
spaceship_health -= 1
if event.type == score_add:
score += 1
if spaceship_health <= 0:
tl.stop()
name = name_input()
scoreboard = edit_XML(score, name)
draw_end_text(scoreboard, score)
pygame.quit()
sys.exit()
control_spaceship(spaceship)
handle_enemies(enemies)
handle_bullets(spaceship_bullets, enemy_bullets, spaceship, enemies)
draw_window(spaceship, spaceship_bullets, enemies, enemy_bullets, score, spaceship_health)
if __name__ == "__main__":
tl.start()
main()