|
| 1 | +import os.path |
| 2 | + |
| 3 | +import random |
| 4 | +import sys |
| 5 | +import xml.etree.ElementTree as ET |
| 6 | + |
| 7 | +from timeloop import Timeloop |
| 8 | +from datetime import timedelta |
| 9 | + |
| 10 | +import pygame |
| 11 | + |
| 12 | +pygame.init() |
| 13 | + |
| 14 | +ran = random.Random() |
| 15 | +tl = Timeloop() |
| 16 | + |
| 17 | +WIDTH, HEIGHT = 900, 500 |
| 18 | +FPS = 60 |
| 19 | + |
| 20 | +spawn_enemy = pygame.USEREVENT + 1 |
| 21 | +spaceship_hit = pygame.USEREVENT + 2 |
| 22 | +score_add = pygame.USEREVENT + 3 |
| 23 | + |
| 24 | +SPACESHIP_WIDTH, SPACESHIP_HEIGHT = 35, 35 |
| 25 | +VEL = 6 |
| 26 | +SPACESHIP_MAX_BULLETS = 4 |
| 27 | +SPACESHIP_MAX_HEALTH = 10 |
| 28 | +spaceship_bullets = [] |
| 29 | + |
| 30 | +ENEMY_WIDTH, ENEMY_HEIGHT = 55, 55 |
| 31 | +VEL_ENEMY = 1 |
| 32 | +enemies = [] |
| 33 | +enemy_bullets = [] |
| 34 | +SPAWN_INTERVAL = 2 |
| 35 | +FIRE_INTERVAL = 1.2 |
| 36 | + |
| 37 | +VEL_BULLETS = 8 |
| 38 | + |
| 39 | +BLACK = (0, 0, 0) |
| 40 | +WHITE = (255, 255, 255) |
| 41 | +RED = (255, 0, 0) |
| 42 | +BLUE = (0, 0, 255) |
| 43 | +GRAY = (100, 100, 100) |
| 44 | + |
| 45 | +END_FONT = pygame.font.SysFont("comicsans", 100) |
| 46 | +NORM_FONT = pygame.font.SysFont("comicsans", 40) |
| 47 | +SMALL_NORM_FONT = pygame.font.SysFont("comicsans", 30) |
| 48 | + |
| 49 | +ENEMY_IMAGE = pygame.image.load(os.path.join('Assets', 'Default', 'enemy_D.png')) |
| 50 | +ENEMY = pygame.transform.rotate((pygame.transform.scale(ENEMY_IMAGE, (ENEMY_WIDTH, ENEMY_HEIGHT))), 90) |
| 51 | +SPACESHIP_IMAGE = pygame.image.load(os.path.join('Assets', 'Default', 'ship_G.png')) |
| 52 | +SPACESHIP = pygame.transform.rotate(pygame.transform.scale(SPACESHIP_IMAGE, (SPACESHIP_WIDTH, SPACESHIP_HEIGHT)), 270) |
| 53 | + |
| 54 | +WIN = pygame.display.set_mode((WIDTH, HEIGHT)) |
| 55 | +pygame.display.set_caption("Test Game") |
| 56 | + |
| 57 | +FILE_NAME = 'scoreboard.xml' |
| 58 | +dom = ET.parse(FILE_NAME) |
| 59 | +root = dom.getroot() |
| 60 | + |
| 61 | + |
| 62 | +player = dom.findall('player') |
| 63 | + |
| 64 | + |
| 65 | +def draw_window(spaceship, spaceship_bullets, enemies, enemy_bullets, score, health): |
| 66 | + score_text = f"score: {score}" |
| 67 | + health_text = f"health: {health}/{SPACESHIP_MAX_HEALTH}" |
| 68 | + draw_text = NORM_FONT.render(score_text, True, WHITE) |
| 69 | + draw_text2 = NORM_FONT.render(health_text, True, WHITE) |
| 70 | + WIN.fill(BLACK) |
| 71 | + WIN.blit(draw_text, (20, 20)) |
| 72 | + WIN.blit(draw_text2, (WIDTH - 20 - draw_text2.get_width(), 20)) |
| 73 | + WIN.blit(SPACESHIP, (spaceship.x, spaceship.y)) |
| 74 | + for bullet in spaceship_bullets: |
| 75 | + pygame.draw.rect(WIN, BLUE, bullet) |
| 76 | + for enemy in enemies: |
| 77 | + WIN.blit(ENEMY, (enemy.x, enemy.y)) |
| 78 | + for bullet in enemy_bullets: |
| 79 | + pygame.draw.rect(WIN, RED, bullet) |
| 80 | + pygame.display.update() |
| 81 | + |
| 82 | + |
| 83 | +def draw_end_text(scoreboard, score): |
| 84 | + y = 140 |
| 85 | + place = 1 |
| 86 | + draw_text = END_FONT.render("GAME OVER", True, WHITE) |
| 87 | + draw2_text = NORM_FONT.render(f"Your score {score}", True, WHITE) |
| 88 | + WIN.blit(draw_text, (WIDTH / 2 - draw_text.get_width() / 2, 20)) |
| 89 | + WIN.blit(draw2_text, (WIDTH / 2 - draw2_text.get_width() / 2, 100)) |
| 90 | + for p in scoreboard: |
| 91 | + draw_scoreboard = NORM_FONT.render(f"{place}. {p.get('name')} - {p.get('score')}", True, WHITE) |
| 92 | + WIN.blit(draw_scoreboard, (WIDTH / 2 - draw_scoreboard.get_width() / 2, y)) |
| 93 | + y += 40 |
| 94 | + place += 1 |
| 95 | + pygame.display.update() |
| 96 | + pygame.time.delay(5000) |
| 97 | + |
| 98 | + |
| 99 | +def control_spaceship(spaceship): |
| 100 | + keys_pressed = pygame.key.get_pressed() |
| 101 | + if keys_pressed[pygame.K_w] and not spaceship.y - VEL < 0: # Up |
| 102 | + spaceship.y -= VEL |
| 103 | + if keys_pressed[pygame.K_s] and not spaceship.y + SPACESHIP_HEIGHT + VEL > HEIGHT: # Down |
| 104 | + spaceship.y += VEL |
| 105 | + if keys_pressed[pygame.K_a] and not spaceship.x - VEL < 0: # Left |
| 106 | + spaceship.x -= VEL |
| 107 | + if keys_pressed[pygame.K_d] and not spaceship.x + SPACESHIP_WIDTH + VEL > WIDTH: # Right |
| 108 | + spaceship.x += VEL |
| 109 | + |
| 110 | + |
| 111 | +def handle_enemies(enemies): |
| 112 | + for enemy in enemies: |
| 113 | + enemy.x -= VEL_ENEMY |
| 114 | + |
| 115 | + |
| 116 | +@tl.job(interval=timedelta(seconds=SPAWN_INTERVAL)) |
| 117 | +def spawn_enemy(): |
| 118 | + enemy = pygame.Rect(WIDTH - 20, ran.randint(20, (HEIGHT - 70)), ENEMY_WIDTH, ENEMY_HEIGHT) |
| 119 | + enemies.append(enemy) |
| 120 | + |
| 121 | + |
| 122 | +@tl.job(interval=timedelta(seconds=FIRE_INTERVAL)) |
| 123 | +def enemy_fire(): |
| 124 | + for enemy in enemies: |
| 125 | + enemy_bullet = pygame.Rect(enemy.x + ENEMY_WIDTH, enemy.y + ENEMY_HEIGHT / 2 - 2, 10, 5) |
| 126 | + enemy_bullets.append(enemy_bullet) |
| 127 | + |
| 128 | + |
| 129 | +def handle_bullets(spaceship_bullets, enemy_bullets, spaceship, enemies): |
| 130 | + for bullet in spaceship_bullets: |
| 131 | + bullet.x += VEL_BULLETS |
| 132 | + if bullet.x > WIDTH: |
| 133 | + spaceship_bullets.remove(bullet) |
| 134 | + for enemy in enemies: |
| 135 | + if enemy.colliderect(bullet): |
| 136 | + spaceship_bullets.remove(bullet) |
| 137 | + enemies.remove(enemy) |
| 138 | + pygame.event.post(pygame.event.Event(score_add)) |
| 139 | + for bullet in enemy_bullets: |
| 140 | + bullet.x -= VEL_BULLETS |
| 141 | + if bullet.x < 0: |
| 142 | + enemy_bullets.remove(bullet) |
| 143 | + elif spaceship.colliderect(bullet): |
| 144 | + enemy_bullets.remove(bullet) |
| 145 | + pygame.event.post(pygame.event.Event(spaceship_hit)) |
| 146 | + |
| 147 | + |
| 148 | +def name_input(): |
| 149 | + user_text = '' |
| 150 | + input_rect = pygame.Rect(200, 200, (WIDTH / 2) - 200, (HEIGHT / 2) - 200) |
| 151 | + |
| 152 | + while True: |
| 153 | + for event in pygame.event.get(): |
| 154 | + if event.type == pygame.KEYDOWN: |
| 155 | + if event.key == pygame.K_BACKSPACE: |
| 156 | + user_text = user_text[:-1] |
| 157 | + elif event.key == pygame.K_KP_ENTER: |
| 158 | + return user_text |
| 159 | + else: |
| 160 | + user_text += event.unicode |
| 161 | + |
| 162 | + pygame.draw.rect(WIN, GRAY, input_rect) |
| 163 | + text_surface = SMALL_NORM_FONT.render(user_text, True, BLACK) |
| 164 | + WIN.blit(text_surface, (input_rect.x + 5, input_rect.y + 5)) |
| 165 | + input_rect.w = max(100, text_surface.get_width() + 10) |
| 166 | + pygame.display.flip() |
| 167 | + |
| 168 | + |
| 169 | +def is_int(s): |
| 170 | + try: |
| 171 | + int(s) |
| 172 | + return True |
| 173 | + except ValueError: |
| 174 | + return False |
| 175 | + |
| 176 | + |
| 177 | +def edit_XML(score, name): |
| 178 | + scoreboard = [] |
| 179 | + run_XML = True |
| 180 | + old_score = 0 |
| 181 | + old_name = "" |
| 182 | + for elem in root.iter('player'): |
| 183 | + if score > int(elem.get('score')) and run_XML: |
| 184 | + old_score = int(elem.get('score')) |
| 185 | + old_name = elem.get('name') |
| 186 | + elem.set('score', str(score)) |
| 187 | + elem.set('name', name) |
| 188 | + run_XML = False |
| 189 | + dom.write('scoreboard.xml') |
| 190 | + elif old_score > int(elem.get('score')): |
| 191 | + temp_score = int(elem.get('score')) |
| 192 | + temp_name = elem.get('name') |
| 193 | + elem.set('score', str(old_score)) |
| 194 | + elem.set('name', old_name) |
| 195 | + dom.write('scoreboard.xml') |
| 196 | + old_score = temp_score |
| 197 | + old_name = temp_name |
| 198 | + |
| 199 | + for p in player: |
| 200 | + scoreboard.append(p) |
| 201 | + return scoreboard |
| 202 | + |
| 203 | + |
| 204 | +def main(): |
| 205 | + spaceship = pygame.Rect(20, HEIGHT // 2, SPACESHIP_WIDTH, SPACESHIP_HEIGHT) |
| 206 | + |
| 207 | + score = 0 |
| 208 | + spaceship_health = SPACESHIP_MAX_HEALTH |
| 209 | + |
| 210 | + clock = pygame.time.Clock() |
| 211 | + run = True |
| 212 | + |
| 213 | + while run: |
| 214 | + clock.tick(FPS) |
| 215 | + for event in pygame.event.get(): |
| 216 | + if event.type == pygame.QUIT: |
| 217 | + run = False |
| 218 | + pygame.quit() |
| 219 | + sys.exit() |
| 220 | + if event.type == pygame.KEYDOWN: |
| 221 | + if event.key == pygame.K_SPACE and len(spaceship_bullets) < SPACESHIP_MAX_BULLETS: |
| 222 | + bullet = pygame.Rect(spaceship.x + SPACESHIP_WIDTH, spaceship.y + SPACESHIP_HEIGHT / 2 - 2, 10, 5) |
| 223 | + spaceship_bullets.append(bullet) |
| 224 | + if event.type == pygame.KEYDOWN: |
| 225 | + if event.key == pygame.K_ESCAPE: |
| 226 | + run = False |
| 227 | + pygame.quit() |
| 228 | + sys.exit() |
| 229 | + if event.type == spaceship_hit: |
| 230 | + spaceship_health -= 1 |
| 231 | + if event.type == score_add: |
| 232 | + score += 1 |
| 233 | + if spaceship_health <= 0: |
| 234 | + tl.stop() |
| 235 | + name = name_input() |
| 236 | + scoreboard = edit_XML(score, name) |
| 237 | + draw_end_text(scoreboard, score) |
| 238 | + pygame.quit() |
| 239 | + sys.exit() |
| 240 | + |
| 241 | + control_spaceship(spaceship) |
| 242 | + handle_enemies(enemies) |
| 243 | + handle_bullets(spaceship_bullets, enemy_bullets, spaceship, enemies) |
| 244 | + draw_window(spaceship, spaceship_bullets, enemies, enemy_bullets, score, spaceship_health) |
| 245 | + |
| 246 | + |
| 247 | +if __name__ == "__main__": |
| 248 | + tl.start() |
| 249 | + main() |
0 commit comments