map_gen_refactor #17
29
main.py
29
main.py
@ -9,6 +9,8 @@ from map import *
|
|||||||
# from house import House
|
# from house import House
|
||||||
from sprites import *
|
from sprites import *
|
||||||
from settings import *
|
from settings import *
|
||||||
|
from map_new import map_new
|
||||||
|
from map_new import map_utils
|
||||||
from SearchBfs import *
|
from SearchBfs import *
|
||||||
import math
|
import math
|
||||||
|
|
||||||
@ -35,9 +37,15 @@ class Game():
|
|||||||
self.wall_img = pg.image.load(path.join(img_folder, WALL_IMG)).convert_alpha()
|
self.wall_img = pg.image.load(path.join(img_folder, WALL_IMG)).convert_alpha()
|
||||||
self.wall_img = pg.transform.scale(self.wall_img, (TILESIZE, TILESIZE))
|
self.wall_img = pg.transform.scale(self.wall_img, (TILESIZE, TILESIZE))
|
||||||
|
|
||||||
|
# self.new_map = map_new.getMap()
|
||||||
|
|
||||||
|
|
||||||
def new(self):
|
def new(self):
|
||||||
# initialize all variables and do all the setup for a new game
|
# initialize all variables and do all the setup for a new game
|
||||||
|
|
||||||
|
# sprite groups and map array
|
||||||
|
(self.roadTiles, self.wallTiles), self.mapArray = map_new.getTiles()
|
||||||
|
|
||||||
self.all_sprites = pg.sprite.Group()
|
self.all_sprites = pg.sprite.Group()
|
||||||
self.walls = pg.sprite.Group()
|
self.walls = pg.sprite.Group()
|
||||||
|
|
||||||
@ -47,10 +55,10 @@ class Game():
|
|||||||
if tile_object.name == 'wall':
|
if tile_object.name == 'wall':
|
||||||
Obstacle(self, tile_object.x, tile_object.y, tile_object.width, tile_object.height)
|
Obstacle(self, tile_object.x, tile_object.y, tile_object.width, tile_object.height)
|
||||||
|
|
||||||
# self.screen.blit(self.map_img, (0,0))
|
self.camera = Camera(MAP_WIDTH_PX, MAP_HEIGHT_PX)
|
||||||
self.camera = Camera(self.map.width, self.map.height)
|
|
||||||
self.draw_debug = False
|
self.draw_debug = False
|
||||||
|
|
||||||
|
|
||||||
# self.screen.blit(self.map_img, self.camera.apply_rect(self.map_rect))
|
# self.screen.blit(self.map_img, self.camera.apply_rect(self.map_rect))
|
||||||
|
|
||||||
def run(self):
|
def run(self):
|
||||||
@ -78,22 +86,16 @@ class Game():
|
|||||||
for y in range(0, HEIGHT, TILESIZE):
|
for y in range(0, HEIGHT, TILESIZE):
|
||||||
pg.draw.line(self.screen, LIGHTGREY, (0, y), (WIDTH, y))
|
pg.draw.line(self.screen, LIGHTGREY, (0, y), (WIDTH, y))
|
||||||
|
|
||||||
# def draw(self, drawable_object, pos):
|
|
||||||
# # pos => (x, y)
|
|
||||||
# # drawable object must have .image field inside class
|
|
||||||
# self.screen.blit(drawable_object.image, pos )
|
|
||||||
|
|
||||||
def draw(self):
|
def draw(self):
|
||||||
pg.display.set_caption("{:.2f}".format(self.clock.get_fps()))
|
pg.display.set_caption("{:.2f}".format(self.clock.get_fps()))
|
||||||
self.screen.blit(self.map_img, self.camera.apply_rect(self.map_rect))
|
|
||||||
|
map_new.renderTiles(self.roadTiles, self.screen, self.camera)
|
||||||
|
map_new.renderTiles(self.wallTiles, self.screen, self.camera, self.draw_debug)
|
||||||
|
|
||||||
for sprite in self.all_sprites:
|
for sprite in self.all_sprites:
|
||||||
self.screen.blit(sprite.image, self.camera.apply(sprite))
|
self.screen.blit(sprite.image, self.camera.apply(sprite))
|
||||||
if self.draw_debug:
|
if self.draw_debug:
|
||||||
pg.draw.rect(self.screen, CYAN, self.camera.apply_rect(sprite.hit_rect), 1)
|
pg.draw.rect(self.screen, CYAN, self.camera.apply_rect(sprite.hit_rect), 1)
|
||||||
if self.draw_debug:
|
|
||||||
for wall in self.walls:
|
|
||||||
pg.draw.rect(self.screen, CYAN, self.camera.apply_rect(wall.rect), 1)
|
|
||||||
|
|
||||||
pg.display.flip()
|
pg.display.flip()
|
||||||
|
|
||||||
@ -117,11 +119,6 @@ class Game():
|
|||||||
def show_go_screen(self):
|
def show_go_screen(self):
|
||||||
pass
|
pass
|
||||||
|
|
||||||
# def reloadMap(self):
|
|
||||||
# #self.screen.fill(pygame.Color(self.BACKGROUND_COLOR))
|
|
||||||
# self.screen.blit(self.map_img, (0,0))
|
|
||||||
|
|
||||||
|
|
||||||
# def main():
|
# def main():
|
||||||
# game = WalleGame()
|
# game = WalleGame()
|
||||||
# game.update_window()
|
# game.update_window()
|
||||||
|
16
map_new/map_new.py
Normal file
16
map_new/map_new.py
Normal file
@ -0,0 +1,16 @@
|
|||||||
|
from map_new import map_utils
|
||||||
|
from map_new import map_pattern
|
||||||
|
import pygame as pg
|
||||||
|
from settings import *
|
||||||
|
|
||||||
|
def getTiles():
|
||||||
|
array = map_utils.generateMap()
|
||||||
|
pattern = map_pattern.getPattern()
|
||||||
|
tiles = map_utils.getSprites(array, pattern)
|
||||||
|
return tiles, array
|
||||||
|
|
||||||
|
def renderTiles(tiles, screen, camera, debug=False):
|
||||||
|
for tile in tiles:
|
||||||
|
screen.blit(tile.image, camera.apply_rect(tile.rect))
|
||||||
|
if debug:
|
||||||
|
pg.draw.rect(screen, RED, camera.apply_rect(tile.rect), 1)
|
17
map_new/map_pattern.py
Normal file
17
map_new/map_pattern.py
Normal file
@ -0,0 +1,17 @@
|
|||||||
|
import os
|
||||||
|
import pygame as pg
|
||||||
|
|
||||||
|
HERE_DIR = os.path.abspath(os.path.dirname(__file__))
|
||||||
|
TEXTURES_DIR = HERE_DIR.rpartition(os.sep)[0]+"\\resources\\textures"
|
||||||
|
|
||||||
|
ROAD_DIR = TEXTURES_DIR+"\\road\\"
|
||||||
|
BUILDING_DIR = TEXTURES_DIR+"\\buliding\\"
|
||||||
|
|
||||||
|
def loadImg(path):
|
||||||
|
return pg.image.load(path)
|
||||||
|
|
||||||
|
def getPattern():
|
||||||
|
return {
|
||||||
|
0: loadImg(ROAD_DIR+"GTA2_TILE_257.bmp"),
|
||||||
|
1: loadImg(BUILDING_DIR+"GTA2_TILE_187.bmp"),
|
||||||
|
}
|
45
map_new/map_utils.py
Normal file
45
map_new/map_utils.py
Normal file
@ -0,0 +1,45 @@
|
|||||||
|
import random
|
||||||
|
import pygame as pg
|
||||||
|
from settings import *
|
||||||
|
from map_new.tile import Tile
|
||||||
|
|
||||||
|
# tworzenie pustego arraya o podanych wymiarach
|
||||||
|
def getBlankMapArray():
|
||||||
|
map = [[0 for x in range(0,MAP_WIDTH)] for y in range (0,MAP_HEIGHT)]
|
||||||
|
return map
|
||||||
|
|
||||||
|
# generowanie obiektow na mapie
|
||||||
|
def generateMap():
|
||||||
|
map = getBlankMapArray()
|
||||||
|
for i in range(0, 20):
|
||||||
|
x = random.randint(0, MAP_WIDTH-1)
|
||||||
|
y = random.randint(0, MAP_HEIGHT-1)
|
||||||
|
map[y][x] = 1
|
||||||
|
return map
|
||||||
|
|
||||||
|
# tworzenie grup sprite'ow
|
||||||
|
def getSprites(map, pattern):
|
||||||
|
roadTiles = pg.sprite.Group()
|
||||||
|
wallTiles = pg.sprite.Group()
|
||||||
|
|
||||||
|
#objechanie tablicy i generowanie tile'a na danych kordach
|
||||||
|
for i in range(len(map)):
|
||||||
|
offsetY = i * TILE_SIZE_PX
|
||||||
|
for j in range(len(map[i])):
|
||||||
|
offsetX = j * TILE_SIZE_PX
|
||||||
|
tileId = map[i][j]
|
||||||
|
tile = Tile(pattern[tileId], offsetX, offsetY, TILE_SIZE_PX, TILE_SIZE_PX)
|
||||||
|
if tileId == 0:
|
||||||
|
roadTiles.add(tile)
|
||||||
|
else:
|
||||||
|
wallTiles.add(tile)
|
||||||
|
|
||||||
|
return roadTiles, wallTiles
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|
16
map_new/tile.py
Normal file
16
map_new/tile.py
Normal file
@ -0,0 +1,16 @@
|
|||||||
|
from re import X
|
||||||
|
import pygame as pg
|
||||||
|
|
||||||
|
class Tile(pg.sprite.Sprite):
|
||||||
|
def __init__(self, img, x, y, width, height):
|
||||||
|
super().__init__()
|
||||||
|
|
||||||
|
self.x = x
|
||||||
|
self.y = y
|
||||||
|
self.width = width
|
||||||
|
self.height = height
|
||||||
|
|
||||||
|
self.image = pg.Surface([width, height])
|
||||||
|
self.image.blit(img, (0,0))
|
||||||
|
|
||||||
|
self.rect = pg.Rect(x, y, width, height)
|
@ -5,6 +5,7 @@ vec = pg.math.Vector2
|
|||||||
#colors
|
#colors
|
||||||
LIGHTGREY = (100, 100, 100)
|
LIGHTGREY = (100, 100, 100)
|
||||||
CYAN = (0, 255, 255)
|
CYAN = (0, 255, 255)
|
||||||
|
RED = (255, 0, 0)
|
||||||
|
|
||||||
|
|
||||||
#game settings
|
#game settings
|
||||||
@ -26,3 +27,11 @@ PLAYER_IMG = 'garbagetruck/trashmaster_v2.png'
|
|||||||
PLAYER_HIT_RECT = pg.Rect(0, 0, 50, 50)
|
PLAYER_HIT_RECT = pg.Rect(0, 0, 50, 50)
|
||||||
PLAYER_WIDTH = 64
|
PLAYER_WIDTH = 64
|
||||||
PLAYER_HEIGHT = 32
|
PLAYER_HEIGHT = 32
|
||||||
|
|
||||||
|
#map settings
|
||||||
|
MAP_WIDTH = 20
|
||||||
|
MAP_HEIGHT = 20
|
||||||
|
|
||||||
|
TILE_SIZE_PX = 64
|
||||||
|
MAP_WIDTH_PX = MAP_WIDTH * TILE_SIZE_PX
|
||||||
|
MAP_HEIGHT_PX = MAP_HEIGHT * TILE_SIZE_PX
|
@ -64,9 +64,9 @@ class Player(pg.sprite.Sprite):
|
|||||||
self.rect.center = self.pos
|
self.rect.center = self.pos
|
||||||
self.pos += self.vel * self.game.dt
|
self.pos += self.vel * self.game.dt
|
||||||
self.hit_rect.centerx = self.pos.x
|
self.hit_rect.centerx = self.pos.x
|
||||||
collide_with_walls(self, self.game.walls, 'x')
|
collide_with_walls(self, self.game.wallTiles, 'x')
|
||||||
self.hit_rect.centery = self.pos.y
|
self.hit_rect.centery = self.pos.y
|
||||||
collide_with_walls(self, self.game.walls, 'y')
|
collide_with_walls(self, self.game.wallTiles, 'y')
|
||||||
self.rect.center = self.hit_rect.center
|
self.rect.center = self.hit_rect.center
|
||||||
|
|
||||||
class Dump(pg.sprite.Sprite):
|
class Dump(pg.sprite.Sprite):
|
||||||
|
Loading…
Reference in New Issue
Block a user