From 5a5550b29b931f2b915c7e18f43acde924f5399e Mon Sep 17 00:00:00 2001 From: Yadciel Date: Wed, 19 Apr 2023 12:23:30 +0200 Subject: [PATCH] movement, und tilemap --- fahrzeug.py | 31 ++++++++++++++++++++++++++----- game.py | 10 ++++++++-- tilemap.py | 17 +++++++++++------ 3 files changed, 45 insertions(+), 13 deletions(-) diff --git a/fahrzeug.py b/fahrzeug.py index d77f17d..9fef426 100644 --- a/fahrzeug.py +++ b/fahrzeug.py @@ -23,7 +23,7 @@ class Fahrzeug(pygame.sprite.Sprite): self.dest_y = y self.direction = pygame.math.Vector2(0, -1) self.angle = 0 - self.Tank = 100 + self.Tank = 250 def draw(self, win): rotated_image = pygame.transform.rotate(self.image, self.angle) @@ -36,6 +36,14 @@ class Fahrzeug(pygame.sprite.Sprite): win.blit(rotated_image, rotated_rect) def update(self): + self.move() + + if self.rect.centerx != self.dest_x or self.rect.centery != self.dest_y: + if self.Tank > 0: + self.Tank -= 0.1 + self.angle = get_angle((self.rect.centerx, self.rect.centery), (self.dest_x, self.dest_y))-90 + + def move(self): if self.Tank > 0: if self.rect.centerx < self.dest_x: self.rect.centerx += self.speed @@ -46,10 +54,23 @@ class Fahrzeug(pygame.sprite.Sprite): if self.rect.centery > self.dest_y: self.rect.centery -= self.speed - if self.rect.centerx != self.dest_x or self.rect.centery != self.dest_y: - if self.Tank > 0: - self.Tank -= 0.1 - self.angle = get_angle((self.rect.centerx, self.rect.centery), (self.dest_x, self.dest_y))-90 + keys = pygame.key.get_pressed() + if keys[pygame.K_LEFT]: + self.rect.centerx -= self.speed + self.dest_x -= self.speed + self.angle = 90 + if keys[pygame.K_RIGHT]: + self.rect.centerx += self.speed + self.dest_x += self.speed + self.angle = 270 + if keys[pygame.K_UP]: + self.rect.centery -= self.speed + self.dest_y -= self.speed + self.angle = 0 + if keys[pygame.K_DOWN]: + self.rect.centery += self.speed + self.dest_y += self.speed + self.angle = 180 def dest(self, pos): self.dest_x, self.dest_y = pos diff --git a/game.py b/game.py index fe3cc53..ce02bcb 100644 --- a/game.py +++ b/game.py @@ -18,8 +18,6 @@ def start(): white = (255, 255, 255) blue = (0, 0, 255) - # Tilemap - tile_size = 64 while True: for event in pygame.event.get(): @@ -32,12 +30,20 @@ def start(): # 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) diff --git a/tilemap.py b/tilemap.py index 427da2b..b867c90 100644 --- a/tilemap.py +++ b/tilemap.py @@ -2,12 +2,17 @@ import pygame class tilemap(pygame.sprite.Sprite): def __init__(self): super().__init__() - self.image =pygame.image.load("roads2w.png").convert_alpha() + self.image = pygame.image.load("roads2w.png").convert_alpha() + self.scaled_image = pygame.transform.scale(self.image, (self.image.get_width()*4, self.image.get_height()*4)) self.rect = self.image.get_rect() self.rect = 0, 0 - self.road_quer = self.image.subsurface(64*2, 0, 64, 64) - self.map_array = [[0, 0, 0, 0, 0], - [0, 0, 0, 0, 0], - [0, 0, 0, 0, 0], - [0, 0, 0, 0, 0], + self.tilesize = self.scaled_image.get_width()/8 + self.road_quer = self.scaled_image.subsurface((self.scaled_image.get_width()/8)*2, 0, self.scaled_image.get_height()/3, self.scaled_image.get_width()/8) + self.road_senkrecht = self.scaled_image.subsurface((self.scaled_image.get_width()/8)*3, 0, self.scaled_image.get_height()/3, self.scaled_image.get_width()/8) + self.road_btr = self.scaled_image.subsurface((self.scaled_image.get_width()/8)*0, (self.scaled_image.get_height()/3)*1, self.scaled_image.get_height()/3, self.scaled_image.get_width()/8) + self.road_tbr = pygame.transform.flip(self.scaled_image.subsurface((self.scaled_image.get_width()/8)*2, (self.scaled_image.get_height()/3)*1, self.scaled_image.get_height()/3, self.scaled_image.get_width()/8), True, False) + self.map_array = [[2, 0, 0, 0, 0], + [1, 0, 0, 0, 0], + [3, 0, 0, 0, 0], + [1, 0, 0, 0, 0], [0, 0, 0, 0, 0]]