From b70ebb007fb5c2c22f64f9ad7dd6cbb4e4c6398a Mon Sep 17 00:00:00 2001 From: Yadciel Date: Mon, 8 May 2023 22:11:59 +0200 Subject: [PATCH] win game,pre-release, alpha --- game.py | 58 ++++++++++++++++++++++++++++++++------------------- helicopter.py | 5 +++-- object.py | 12 ++++++++--- player.py | 22 ++++++++++++++----- win_screen.py | 47 +++++++++++++++++++++++++++++++++++++++++ 5 files changed, 112 insertions(+), 32 deletions(-) create mode 100644 win_screen.py diff --git a/game.py b/game.py index 0ff5bd7..e895bcd 100644 --- a/game.py +++ b/game.py @@ -3,6 +3,7 @@ import player import helicopter import object import game_over +import win_screen import tilemap import sys @@ -12,11 +13,14 @@ def start(): win = pygame.display.set_mode((800, 800)) player1 = player.Player(500, 500) enemy = helicopter.Helicopter(50, 50) - Erzmine = object.Erz(600, 40) - Tanken = object.Tank(200, 220) - Ziele = object.Ziel(600, 750) - GameOver = False - GO = game_over.GameOver() + erzmine = object.Erz(600, 40) + tanken = object.Tank(200, 220) + ziele = object.Ziel(600, 750) + gameover = '' + go_screen = game_over.GameOver() + w_screen = win_screen.WinScreen() + debug = False + ziel_erz = 0 tile_set = pygame.image.load("resources/roads2W.png").convert_alpha() @@ -33,20 +37,25 @@ def start(): if event.type == pygame.MOUSEBUTTONDOWN: player1.dest(pygame.mouse.get_pos()) - if player1.rect.colliderect(Tanken.rect): - player1.Tank = 250 + if player1.rect.colliderect(tanken.rect): + if player1.Tank < 250: + player1.Tank += 0.1 if player1.rect.colliderect(enemy.rect): - GameOver = True + gameover = 'gameover' - if player1.rect.colliderect(Erzmine.rect): + if player1.rect.colliderect(erzmine.rect): player1.Erz += 1 - if player1.rect.colliderect(Ziele.rect): + if player1.rect.colliderect(ziele.rect): if player1.Erz >= 1: player1.Erz -= 1 + ziel_erz += 1 - if GameOver == False: + if ziel_erz >= 1000: + gameover = 'win' + + if not gameover: # Hintergrundfarbe win.fill(white) tile = tilemap.tilemap() @@ -65,14 +74,14 @@ def start(): if tile.map_array[y][x] == 3: win.blit(tile.road_tbr, (x * tile_size, y * tile_size)) - Erzmine.update() - Erzmine.draw(win) + erzmine.update() + erzmine.draw(win) - Tanken.update() - Tanken.draw(win) + tanken.update() + tanken.draw(win) - Ziele.update() - Ziele.draw(win) + ziele.update() + ziele.draw(win) player1.update() player1.draw(win) @@ -91,15 +100,20 @@ def start(): # 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)) + if debug: + 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)) + 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) + if gameover == 'win': + w_screen.draw(win) + pass + if gameover == 'gameover': + go_screen.draw(win) pass pygame.display.update() diff --git a/helicopter.py b/helicopter.py index 08da79c..184be4e 100644 --- a/helicopter.py +++ b/helicopter.py @@ -22,6 +22,7 @@ class Helicopter(Fahrzeug, pygame.sprite.Sprite): self.speed = 1 self.duration_speed = 0.02 self.start_time = time.time() + self.debug = False def update(self, player): keys = pygame.key.get_pressed() @@ -67,8 +68,8 @@ class Helicopter(Fahrzeug, pygame.sprite.Sprite): # debug nicht anrühren # self.angle += 1 - pygame.draw.rect(win, (255, 0, 0), rotated_rect) - + if self.debug: + pygame.draw.rect(win, (0, 255, 0), self.rect) win.blit(rotated_image, rotated_rect) self.rotor.draw(win) diff --git a/object.py b/object.py index 3e34e84..2c35e6b 100644 --- a/object.py +++ b/object.py @@ -12,9 +12,11 @@ class Erz(pygame.sprite.Sprite): self.rect = self.scaled_image.get_rect(center=(x, y)) self.rect.centerx = x self.rect.centery = y + self.debug = False def draw(self, win): - pygame.draw.rect(win, (255, 0, 0), self.rect) + if self.debug: + pygame.draw.rect(win, (255, 0, 0), self.rect) win.blit(self.scaled_image, self.rect) @@ -27,9 +29,11 @@ class Tank(pygame.sprite.Sprite): self.rect = self.scaled_image.get_rect(center=(x, y)) self.rect.centerx = x self.rect.centery = y + self.debug = False def draw(self, win): - pygame.draw.rect(win, (255, 0, 0), self.rect) + if self.debug: + pygame.draw.rect(win, (255, 0, 0), self.rect) win.blit(self.scaled_image, self.rect) @@ -43,9 +47,11 @@ class Ziel(pygame.sprite.Sprite): self.rect = self.scaled_image.get_rect(center=(x, y)) self.rect.centerx = x self.rect.centery = y + self.debug = False def draw(self, win): - pygame.draw.rect(win, (255, 0, 0), self.rect) + if self.debug: + pygame.draw.rect(win, (255, 0, 0), self.rect) win.blit(self.scaled_image, self.rect) diff --git a/player.py b/player.py index df28202..556fd15 100644 --- a/player.py +++ b/player.py @@ -1,11 +1,12 @@ import pygame from fahrzeug import Fahrzeug + class Player(Fahrzeug, pygame.sprite.Sprite): def __init__(self, x, y): super().__init__(x, y) self.image = pygame.image.load("resources/player.png").convert_alpha() - self.image = pygame.transform.scale(self.image, (self.image.get_width()/1.2, self.image.get_height()/1.2)) + self.image = pygame.transform.scale(self.image, (self.image.get_width() / 1.2, self.image.get_height() / 1.2)) self.rect = self.image.get_rect(center=(x, y)) self.rect.centerx = x self.rect.centery = y @@ -16,15 +17,17 @@ class Player(Fahrzeug, pygame.sprite.Sprite): self.angle = 0 self.Tank = 250 self.Erz = 0 - + self.debug = False + self.b_move = False def draw(self, win): rotated_image = pygame.transform.rotate(self.image, self.angle) rotated_rect = rotated_image.get_rect(center=self.rect.center) - #debug, nicht anrühren - #self.angle += 1 - pygame.draw.rect(win, (255, 0, 0), rotated_rect) + # debug, nicht anrühren + # self.angle += 1 + if self.debug: + pygame.draw.rect(win, (255, 0, 0), rotated_rect) win.blit(rotated_image, rotated_rect) @@ -39,23 +42,32 @@ class Player(Fahrzeug, pygame.sprite.Sprite): if self.rect.centery > self.dest_y: self.rect.centery -= self.speed + #bug wenn beide methoden zeitgleich benutzt werden + + self.b_move = False keys = pygame.key.get_pressed() if keys[pygame.K_LEFT]: self.rect.centerx -= self.speed self.dest_x -= self.speed self.angle = 90 + self.b_move = True if keys[pygame.K_RIGHT]: self.rect.centerx += self.speed self.dest_x += self.speed self.angle = 270 + self.b_move = True if keys[pygame.K_UP]: self.rect.centery -= self.speed self.dest_y -= self.speed self.angle = 0 + self.b_move = True if keys[pygame.K_DOWN]: self.rect.centery += self.speed self.dest_y += self.speed self.angle = 180 + self.b_move = True + if self.b_move: + self.Tank -= 0.1 def dest(self, pos): self.dest_x, self.dest_y = pos diff --git a/win_screen.py b/win_screen.py new file mode 100644 index 0000000..299a31e --- /dev/null +++ b/win_screen.py @@ -0,0 +1,47 @@ +import pygame + + +class WinScreen(pygame.sprite.Sprite): + def __init__(self): + super().__init__() + self.label = pygame.font.SysFont("Arial", 50).render("Win", True, (255, 0, 0)) + self.rect = self.label.get_rect(center=(400, 300)) + + def draw(self, win): + win.fill((0, 0, 0)) # Black + win.blit(self.label, self.rect) + + self.start_quit(win) + + @staticmethod + def start_quit(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)) + # 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) or \ + start_text.get_rect(center=(start_button.x + 50, start_button.y + 25)).collidepoint(mouse_pos): + game.start() + + # Beenden-Button + if quit_button.collidepoint(mouse_pos) or \ + quit_text.get_rect(center=(quit_button.x + 60, quit_button.y + 25)).collidepoint(mouse_pos): + pygame.quit() + sys.exit()