movement, und tilemap

This commit is contained in:
Yadciel 2023-04-19 12:23:30 +02:00
parent d964caf807
commit 5a5550b29b
3 changed files with 45 additions and 13 deletions

View File

@ -23,7 +23,7 @@ class Fahrzeug(pygame.sprite.Sprite):
self.dest_y = y self.dest_y = y
self.direction = pygame.math.Vector2(0, -1) self.direction = pygame.math.Vector2(0, -1)
self.angle = 0 self.angle = 0
self.Tank = 100 self.Tank = 250
def draw(self, win): def draw(self, win):
rotated_image = pygame.transform.rotate(self.image, self.angle) rotated_image = pygame.transform.rotate(self.image, self.angle)
@ -36,6 +36,14 @@ class Fahrzeug(pygame.sprite.Sprite):
win.blit(rotated_image, rotated_rect) win.blit(rotated_image, rotated_rect)
def update(self): 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.Tank > 0:
if self.rect.centerx < self.dest_x: if self.rect.centerx < self.dest_x:
self.rect.centerx += self.speed self.rect.centerx += self.speed
@ -46,10 +54,23 @@ class Fahrzeug(pygame.sprite.Sprite):
if self.rect.centery > self.dest_y: if self.rect.centery > self.dest_y:
self.rect.centery -= self.speed self.rect.centery -= self.speed
if self.rect.centerx != self.dest_x or self.rect.centery != self.dest_y: keys = pygame.key.get_pressed()
if self.Tank > 0: if keys[pygame.K_LEFT]:
self.Tank -= 0.1 self.rect.centerx -= self.speed
self.angle = get_angle((self.rect.centerx, self.rect.centery), (self.dest_x, self.dest_y))-90 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): def dest(self, pos):
self.dest_x, self.dest_y = pos self.dest_x, self.dest_y = pos

10
game.py
View File

@ -18,8 +18,6 @@ def start():
white = (255, 255, 255) white = (255, 255, 255)
blue = (0, 0, 255) blue = (0, 0, 255)
# Tilemap
tile_size = 64
while True: while True:
for event in pygame.event.get(): for event in pygame.event.get():
@ -32,12 +30,20 @@ def start():
# Hintergrundfarbe # Hintergrundfarbe
win.fill(white) win.fill(white)
tile = tilemap.tilemap() tile = tilemap.tilemap()
# Tilemap
tile_size = tile.tilesize
# Schleife durch das Tilemap-Array # Schleife durch das Tilemap-Array
for y in range(len(tile.map_array)): for y in range(len(tile.map_array)):
for x in range(len(tile.map_array[y])): for x in range(len(tile.map_array[y])):
# Überprüfen, ob der Wert in der aktuellen Zelle gleich 0 ist # Überprüfen, ob der Wert in der aktuellen Zelle gleich 0 ist
if tile.map_array[y][x] == 0: if tile.map_array[y][x] == 0:
win.blit(tile.road_quer, (x * tile_size, y * tile_size)) 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) font = pygame.font.SysFont(None, 24)
tank_label = font.render('Tank: ' + str(round(player1.Tank, 2)), True, white) tank_label = font.render('Tank: ' + str(round(player1.Tank, 2)), True, white)

View File

@ -3,11 +3,16 @@ class tilemap(pygame.sprite.Sprite):
def __init__(self): def __init__(self):
super().__init__() 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 = self.image.get_rect()
self.rect = 0, 0 self.rect = 0, 0
self.road_quer = self.image.subsurface(64*2, 0, 64, 64) self.tilesize = self.scaled_image.get_width()/8
self.map_array = [[0, 0, 0, 0, 0], 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)
[0, 0, 0, 0, 0], 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)
[0, 0, 0, 0, 0], 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)
[0, 0, 0, 0, 0], 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]] [0, 0, 0, 0, 0]]