map, tilemap

This commit is contained in:
Yadciel 2023-04-19 10:59:13 +02:00
parent 15b3fd2046
commit d964caf807
3 changed files with 33 additions and 23 deletions

41
game.py
View File

@ -1,6 +1,7 @@
import pygame import pygame
import fahrzeug import fahrzeug
import background import background
import tilemap
import sys import sys
def start(): def start():
@ -11,19 +12,14 @@ def start():
Tanken = background.Tank() Tanken = background.Tank()
Ziele = background.Ziel() Ziele = background.Ziel()
tile_set = pygame.image.load("roads2W.png").convert_alpha()
# Farben # Farben
white = (255, 255, 255) white = (255, 255, 255)
blue = (0, 0, 255) blue = (0, 0, 255)
# Tilemap # Tilemap
tile_size = 16 tile_size = 64
map_width = 200
map_height = 200
tilemap = [[0 for y in range(map_height)] for x in range(map_width)]
for x in range(map_width):
for y in range(map_height):
tilemap[x][y] = 1
while True: while True:
for event in pygame.event.get(): for event in pygame.event.get():
@ -35,25 +31,26 @@ def start():
# Hintergrundfarbe # Hintergrundfarbe
win.fill(white) win.fill(white)
tile = tilemap.tilemap()
# Tilemap zeichnen # Schleife durch das Tilemap-Array
for x in range(map_width): for y in range(len(tile.map_array)):
for y in range(map_height): for x in range(len(tile.map_array[y])):
if tilemap[x][y] == 1: # Überprüfen, ob der Wert in der aktuellen Zelle gleich 0 ist
pygame.draw.rect(win, blue, (x * tile_size, y * tile_size, tile_size, tile_size)) if tile.map_array[y][x] == 0:
win.blit(tile.road_quer, (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)
win.blit(tank_label, (20, 20)) win.blit(tank_label, (20, 20))
#debug ausgabe # debug ausgabe
#--------------------------- # ---------------------------
#debug_selfrect = font.render('self centerx: ' + str(player1.rect.centerx) + ' self centery: ' + str(player1.rect.centery), True, white) # debug_selfrect = font.render('self centerx: ' + str(player1.rect.centerx) + ' self centery: ' + str(player1.rect.centery), True, white)
#win.blit(debug_selfrect, (20, 40)) # win.blit(debug_selfrect, (20, 40))
#debug_selfrect = font.render('dest x: ' + str(player1.dest_x) + ' dest y: ' + str(player1.dest_y), True, white) # debug_selfrect = font.render('dest x: ' + str(player1.dest_x) + ' dest y: ' + str(player1.dest_y), True, white)
#win.blit(debug_selfrect, (20, 60)) # win.blit(debug_selfrect, (20, 60))
#--------------------------- # ---------------------------
Erzmine.update() Erzmine.update()
Erzmine.draw(win) Erzmine.draw(win)

BIN
roads2W.png Normal file

Binary file not shown.

After

Width:  |  Height:  |  Size: 9.0 KiB

13
tilemap.py Normal file
View File

@ -0,0 +1,13 @@
import pygame
class tilemap(pygame.sprite.Sprite):
def __init__(self):
super().__init__()
self.image =pygame.image.load("roads2w.png").convert_alpha()
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],
[0, 0, 0, 0, 0]]