Add tilemap support, add map border collision
This commit is contained in:
parent
d0d7376ad8
commit
63cc9c0ab2
55
grid.py
Normal file
55
grid.py
Normal file
@ -0,0 +1,55 @@
|
|||||||
|
from copy import copy, deepcopy
|
||||||
|
|
||||||
|
class Grid:
|
||||||
|
#def __init__(self, width, height, locationGrid=[[]], graph={}, translationGrid)=:
|
||||||
|
def __init__(self, width, height):
|
||||||
|
self.width = width
|
||||||
|
self.heigth = height
|
||||||
|
self.locationGrid = [[0 for x in range(width)] for y in range(height)]
|
||||||
|
#self.locationGrid = []
|
||||||
|
self.graph={}
|
||||||
|
self.translationGrid = [[0 for x in range(width)] for y in range(height)]
|
||||||
|
|
||||||
|
def fillTraslationGrid(self):
|
||||||
|
self.translationGrid[0] = ['0x0', '0x1', '0x2', '0x3', '0x4', '0x5']
|
||||||
|
self.translationGrid[1] = ['1x0', '1x1', '1x2', '1x3', '1x4', '1x5']
|
||||||
|
self.translationGrid[2] = ['2x0', '2x1', '2x2', '2x3', '2x4', '2x5']
|
||||||
|
self.translationGrid[3] = ['3x0', '3x1', '3x2', '3x3', '3x4', '3x5']
|
||||||
|
self.translationGrid[4] = ['4x0', '4x1', '4x2', '4x3', '4x4', '4x5']
|
||||||
|
self.translationGrid[5] = ['5x0', '5x1', '5x2', '5x3', '5x4', '5x5']
|
||||||
|
|
||||||
|
|
||||||
|
def fillLocationGrid(self):
|
||||||
|
pass
|
||||||
|
"""self.locationGrid[0] =
|
||||||
|
self.locationGrid[1] =
|
||||||
|
self.locationGrid[2] =
|
||||||
|
self.locationGrid[3] =
|
||||||
|
self.locationGrid[4] =
|
||||||
|
self.locationGrid[5] = """
|
||||||
|
|
||||||
|
|
||||||
|
def printLocationGrid(self):
|
||||||
|
for r in self.locationGrid:
|
||||||
|
for c in r:
|
||||||
|
print(c, end = " ")
|
||||||
|
print()
|
||||||
|
|
||||||
|
def copyLocationGrid(self, x):
|
||||||
|
self.locationGrid = deepcopy(x)
|
||||||
|
|
||||||
|
|
||||||
|
def printTranslationGrid(self):
|
||||||
|
for r in self.translationGrid:
|
||||||
|
for c in r:
|
||||||
|
print(c, end = " ")
|
||||||
|
print()
|
||||||
|
|
||||||
|
def printGraph(self):
|
||||||
|
print(self.graph)
|
||||||
|
|
||||||
|
def setLocationGridCell(self, x, y, value):
|
||||||
|
self.locationGrid[x][y] = value
|
||||||
|
|
||||||
|
def setTranslationGridCell(self, x, y, value):
|
||||||
|
self.translationGrid[x][y] = value
|
37
main.py
37
main.py
@ -1,5 +1,7 @@
|
|||||||
import pygame as pg
|
import pygame as pg
|
||||||
import sys
|
import sys
|
||||||
|
from os import path
|
||||||
|
from grid import *
|
||||||
from settings import *
|
from settings import *
|
||||||
from sprites import *
|
from sprites import *
|
||||||
|
|
||||||
@ -13,23 +15,26 @@ class Game:
|
|||||||
self.load_data()
|
self.load_data()
|
||||||
|
|
||||||
def load_data(self):
|
def load_data(self):
|
||||||
pass
|
game_folder = path.dirname(__file__)
|
||||||
|
self.map_data = []
|
||||||
|
with open(path.join(game_folder, 'map.txt'), 'rt') as f:
|
||||||
|
for line in f:
|
||||||
|
self.map_data.append(line)
|
||||||
|
|
||||||
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
|
||||||
self.all_sprites = pg.sprite.Group()
|
self.all_sprites = pg.sprite.Group()
|
||||||
self.walls = pg.sprite.Group()
|
self.walls = pg.sprite.Group()
|
||||||
self.player = Player(self, 1, 1)
|
for row, tiles in enumerate(self.map_data):
|
||||||
self.mine1 = Mine(self, 8, 6)
|
for col, tile in enumerate(tiles):
|
||||||
self.mine2 = Mine(self, 7, 6)
|
if tile == '2':
|
||||||
self.mine3 = Mine(self, 2, 3)
|
Mine(self, col, row)
|
||||||
self.grenade1 = Grenade(self, 1, 8)
|
if tile == '3':
|
||||||
self.grenade2 = Grenade(self, 3, 4)
|
Bomb(self, col, row)
|
||||||
self.grenade3 = Grenade(self, 9, 5)
|
if tile == '4':
|
||||||
self.bomb1 = Bomb(self, 6, 7)
|
Grenade(self, col, row)
|
||||||
self.bomb2 = Bomb(self, 3, 3)
|
if tile == 'A':
|
||||||
self.bomb3 = Bomb(self, 4, 9)
|
self.player = Player(self, col, row)
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
def run(self):
|
def run(self):
|
||||||
@ -55,14 +60,6 @@ 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_grid(self):
|
|
||||||
for x in range(0, WIDTH, TILESIZE):
|
|
||||||
pg.draw.line(self.screen, LIGHTGREY, (x, 0), (x, HEIGHT))
|
|
||||||
for y in range(0, WIDTH, TILESIZE):
|
|
||||||
pg.draw.line(self.screen, LIGHTGREY, (0, y), (WIDTH, y))
|
|
||||||
|
|
||||||
|
|
||||||
def draw(self):
|
def draw(self):
|
||||||
self.screen.fill(BGCOLOR)
|
self.screen.fill(BGCOLOR)
|
||||||
self.draw_grid()
|
self.draw_grid()
|
||||||
|
@ -8,12 +8,14 @@ RED = (255, 0, 0)
|
|||||||
YELLOW = (255, 255, 0)
|
YELLOW = (255, 255, 0)
|
||||||
|
|
||||||
# game settings
|
# game settings
|
||||||
WIDTH = 800 # 16 * 64 or 32 * 32 or 64 * 16
|
MAP_SIZE = 6
|
||||||
HEIGHT = 600 # 16 * 48 or 32 * 24 or 64 * 12
|
TILESIZE = 100
|
||||||
|
WIDTH = TILESIZE * MAP_SIZE # 16 * 64 or 32 * 32 or 64 * 16
|
||||||
|
HEIGHT = TILESIZE * MAP_SIZE # 16 * 48 or 32 * 24 or 64 * 12
|
||||||
FPS = 60
|
FPS = 60
|
||||||
TITLE = "Sweeper Demo"
|
TITLE = "Sweeper Demo"
|
||||||
BGCOLOR = DARKGREY
|
BGCOLOR = DARKGREY
|
||||||
|
|
||||||
TILESIZE = 50
|
|
||||||
GRIDWIDTH = WIDTH / TILESIZE
|
GRIDWIDTH = WIDTH / TILESIZE
|
||||||
GRIDHEIGHT = HEIGHT / TILESIZE
|
GRIDHEIGHT = HEIGHT / TILESIZE
|
21
sprites.py
21
sprites.py
@ -22,19 +22,23 @@ class Player(pg.sprite.Sprite):
|
|||||||
if direction == self.direction:
|
if direction == self.direction:
|
||||||
if self.direction == Direction.Right.name:
|
if self.direction == Direction.Right.name:
|
||||||
if dx > 0:
|
if dx > 0:
|
||||||
self.x += dx
|
if self.check_border(dx):
|
||||||
|
self.x += dx
|
||||||
|
|
||||||
if self.direction == Direction.Up.name:
|
if self.direction == Direction.Up.name:
|
||||||
if dy < 0:
|
if dy < 0:
|
||||||
self.y += dy
|
if self.check_border(0, dy):
|
||||||
|
self.y += dy
|
||||||
|
|
||||||
if self.direction == Direction.Down.name:
|
if self.direction == Direction.Down.name:
|
||||||
if dy > 0:
|
if dy > 0:
|
||||||
self.y += dy
|
if self.check_border(0, dy):
|
||||||
|
self.y += dy
|
||||||
|
|
||||||
if self.direction == Direction.Left.name:
|
if self.direction == Direction.Left.name:
|
||||||
if dx < 0:
|
if dx < 0:
|
||||||
self.x += dx
|
if self.check_border(dx):
|
||||||
|
self.x += dx
|
||||||
|
|
||||||
elif direction != self.direction:
|
elif direction != self.direction:
|
||||||
self.direction = direction
|
self.direction = direction
|
||||||
@ -48,6 +52,15 @@ class Player(pg.sprite.Sprite):
|
|||||||
if self.direction == Direction.Left.name:
|
if self.direction == Direction.Left.name:
|
||||||
self.image == pg.transform.rotate(self.baseImage, -180)"""
|
self.image == pg.transform.rotate(self.baseImage, -180)"""
|
||||||
|
|
||||||
|
def check_border(self, dx=0, dy=0):
|
||||||
|
if (self.x + dx) < 0 or (self.y + dy) < 0 or (self.x + dx) >= MAP_SIZE or (self.y + dy) >= MAP_SIZE :
|
||||||
|
return False
|
||||||
|
else:
|
||||||
|
return True
|
||||||
|
|
||||||
|
|
||||||
|
def collide_with_walls(self, dx=0, dy=0):
|
||||||
|
pass
|
||||||
|
|
||||||
def update(self):
|
def update(self):
|
||||||
self.rect.x = self.x * TILESIZE
|
self.rect.x = self.x * TILESIZE
|
||||||
|
Loading…
Reference in New Issue
Block a user