From e83dec026a0f439d6499b15bb92b63b737b2d55c Mon Sep 17 00:00:00 2001 From: Yadciel Date: Thu, 27 Apr 2023 13:21:11 +0200 Subject: [PATCH] loop, death, start --- game.py | 103 +++++++++++++++++++++++++++++---------------------- game_over.py | 47 +++++++++++++++++++++++ main.py | 2 +- player.py | 1 + 4 files changed, 108 insertions(+), 45 deletions(-) create mode 100644 game_over.py diff --git a/game.py b/game.py index e77bd54..1ab9cd8 100644 --- a/game.py +++ b/game.py @@ -2,6 +2,7 @@ import pygame import player import helicopter import object +import game_over import tilemap import sys @@ -9,11 +10,13 @@ import sys def start(): pygame.init() win = pygame.display.set_mode((800, 800)) - player1 = player.Player(0, 0) - enemy = helicopter.Helicopter(5, 5) + player1 = player.Player(500, 500) + enemy = helicopter.Helicopter(50, 50) Erzmine = object.Erz(600, 100) Tanken = object.Tank(160, 200) Ziele = object.Ziel(600, 900) + GameOver = False + GO = game_over.GameOver() tile_set = pygame.image.load("resources/roads2W.png").convert_alpha() @@ -30,54 +33,66 @@ def start(): if event.type == pygame.MOUSEBUTTONDOWN: player1.dest(pygame.mouse.get_pos()) - # Hintergrundfarbe - win.fill(white) - tile = tilemap.tilemap() - # Tilemap - tile_size = tile.tilesize - # Schleife durch das Tilemap-Array - for y in range(len(tile.map_array)): - for x in range(len(tile.map_array[y])): - # Überprüfen, ob der Wert in der aktuellen Zelle gleich 0 ist - if tile.map_array[y][x] == 0: - win.blit(tile.road_quer, (x * tile_size, y * tile_size)) - if tile.map_array[y][x] == 1: - win.blit(tile.road_senkrecht, (x * tile_size, y * tile_size)) - if tile.map_array[y][x] == 2: - win.blit(tile.road_btr, (x * tile_size, y * tile_size)) - if tile.map_array[y][x] == 3: - win.blit(tile.road_tbr, (x * tile_size, y * tile_size)) - - font = pygame.font.SysFont(None, 24) - tank_label = font.render('Tank: ' + str(round(player1.Tank, 2)), True, white) - win.blit(tank_label, (20, 20)) - - # debug ausgabe - # --------------------------- - debug_selfrect = font.render('self centerx: ' + str(player1.rect.centerx) + ' self centery: ' + str(player1.rect.centery), True, white) - win.blit(debug_selfrect, (20, 40)) - - debug_selfrect = font.render('dest x: ' + str(player1.dest_x) + ' dest y: ' + str(player1.dest_y), True, white) - win.blit(debug_selfrect, (20, 60)) - # --------------------------- - if player1.rect.colliderect(Tanken.rect): player1.Tank = 250 - Erzmine.update() - Erzmine.draw(win) + if player1.rect.colliderect(enemy.rect): + GameOver = True - Tanken.update() - Tanken.draw(win) + if GameOver == False: + # Hintergrundfarbe + win.fill(white) + tile = tilemap.tilemap() + # Tilemap + tile_size = tile.tilesize + # Schleife durch das Tilemap-Array + for y in range(len(tile.map_array)): + for x in range(len(tile.map_array[y])): + # Überprüfen, ob der Wert in der aktuellen Zelle gleich 0 ist + if tile.map_array[y][x] == 0: + win.blit(tile.road_quer, (x * tile_size, y * tile_size)) + if tile.map_array[y][x] == 1: + win.blit(tile.road_senkrecht, (x * tile_size, y * tile_size)) + if tile.map_array[y][x] == 2: + win.blit(tile.road_btr, (x * tile_size, y * tile_size)) + if tile.map_array[y][x] == 3: + win.blit(tile.road_tbr, (x * tile_size, y * tile_size)) - Ziele.update() - Ziele.draw(win) + Erzmine.update() + Erzmine.draw(win) - player1.update() - player1.draw(win) + Tanken.update() + Tanken.draw(win) - enemy.update(player1) - enemy.dest((player1.rect.centerx, player1.rect.centery)) - enemy.draw(win) + Ziele.update() + Ziele.draw(win) + + player1.update() + player1.draw(win) + + enemy.update(player1) + enemy.dest((player1.rect.centerx, player1.rect.centery)) + enemy.draw(win) + + # Game UI-------------------------------- + font = pygame.font.SysFont(None, 24) + tank_label = font.render('Tank: ' + str(round(player1.Tank, 2)), True, white) + win.blit(tank_label, (20, 20)) + font = pygame.font.SysFont(None, 24) + tank_label = font.render('Erz: ' + str(round(player1.Erz, 0)), True, white) + win.blit(tank_label, (20, 40)) + + # debug ausgabe + # --------------------------- + debug_selfrect = font.render('self centerx: ' + str(player1.rect.centerx) + ' self centery: ' + str(player1.rect.centery), True, white) + win.blit(debug_selfrect, (20, 60)) + + debug_selfrect = font.render('dest x: ' + str(player1.dest_x) + ' dest y: ' + str(player1.dest_y), True, white) + win.blit(debug_selfrect, (20, 80)) + # --------------------------- + # ---------------------------------------- + else: + GO.draw(win) + pass pygame.display.update() diff --git a/game_over.py b/game_over.py new file mode 100644 index 0000000..09bde52 --- /dev/null +++ b/game_over.py @@ -0,0 +1,47 @@ +import pygame +import game + + +class GameOver(pygame.sprite.Sprite): + def __init__(self): + super().__init__() + self.label = pygame.font.SysFont("Arial", 50).render("Game Over", True, (255, 0, 0)) + self.rect = self.label.get_rect(center=(400, 300)) + + def draw(self, win): + # Rechtecke + start_button = pygame.Rect(200, 400, 100, 50) + quit_button = pygame.Rect(500, 400, 120, 50) + + # Schriftart + font = pygame.font.Font(None, 32) + + # Text + start_text = font.render("Start", True, (0, 0, 0)) + quit_text = font.render("Beenden", True, (0, 0, 0)) + + win.fill((0, 0, 0)) # Black + win.blit(self.label, self.rect) + + # Buttons zeichnen + pygame.draw.rect(win, (100, 100, 100), start_button) + win.blit(start_text, (start_button.x + 25, start_button.y + 10)) + + pygame.draw.rect(win, (100, 100, 100), quit_button) + win.blit(quit_text, (quit_button.x + 10, quit_button.y + 10)) + + for event in pygame.event.get(): + if event.type == pygame.QUIT: + pygame.quit() + sys.exit() + if event.type == pygame.MOUSEBUTTONDOWN: + mouse_pos = event.pos + + # Start-Button + if start_button.collidepoint(mouse_pos): + game.start() + + # Beenden-Button + if quit_button.collidepoint(mouse_pos): + pygame.quit() + sys.exit() diff --git a/main.py b/main.py index b4b3652..872e412 100644 --- a/main.py +++ b/main.py @@ -14,7 +14,7 @@ gray = (128, 128, 128) # Rechtecke start_button = pygame.Rect(100, 100, 100, 50) -quit_button = pygame.Rect(300, 100, 100, 50) +quit_button = pygame.Rect(300, 100, 120, 50) # Schriftart font = pygame.font.Font(None, 32) diff --git a/player.py b/player.py index 597d8df..37dfe2f 100644 --- a/player.py +++ b/player.py @@ -15,6 +15,7 @@ class Player(Fahrzeug, pygame.sprite.Sprite): self.direction = pygame.math.Vector2(0, -1) self.angle = 0 self.Tank = 250 + self.Erz = 0 def draw(self, win):