diff --git a/grid.py b/grid.py new file mode 100644 index 0000000..4fac112 --- /dev/null +++ b/grid.py @@ -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 \ No newline at end of file diff --git a/main.py b/main.py index 245cba8..3a793b5 100644 --- a/main.py +++ b/main.py @@ -1,5 +1,7 @@ import pygame as pg import sys +from os import path +from grid import * from settings import * from sprites import * @@ -13,23 +15,26 @@ class Game: self.load_data() 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): # initialize all variables and do all the setup for a new game self.all_sprites = pg.sprite.Group() self.walls = pg.sprite.Group() - self.player = Player(self, 1, 1) - self.mine1 = Mine(self, 8, 6) - self.mine2 = Mine(self, 7, 6) - self.mine3 = Mine(self, 2, 3) - self.grenade1 = Grenade(self, 1, 8) - self.grenade2 = Grenade(self, 3, 4) - self.grenade3 = Grenade(self, 9, 5) - self.bomb1 = Bomb(self, 6, 7) - self.bomb2 = Bomb(self, 3, 3) - self.bomb3 = Bomb(self, 4, 9) - + for row, tiles in enumerate(self.map_data): + for col, tile in enumerate(tiles): + if tile == '2': + Mine(self, col, row) + if tile == '3': + Bomb(self, col, row) + if tile == '4': + Grenade(self, col, row) + if tile == 'A': + self.player = Player(self, col, row) def run(self): @@ -55,14 +60,6 @@ class Game: for y in range(0, HEIGHT, TILESIZE): 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): self.screen.fill(BGCOLOR) self.draw_grid() diff --git a/map.txt b/map.txt new file mode 100644 index 0000000..c842675 --- /dev/null +++ b/map.txt @@ -0,0 +1,4 @@ +.A.... +..222. +.222.. +...... \ No newline at end of file diff --git a/settings.py b/settings.py index fc700fc..7a22523 100644 --- a/settings.py +++ b/settings.py @@ -8,12 +8,14 @@ RED = (255, 0, 0) YELLOW = (255, 255, 0) # game settings -WIDTH = 800 # 16 * 64 or 32 * 32 or 64 * 16 -HEIGHT = 600 # 16 * 48 or 32 * 24 or 64 * 12 +MAP_SIZE = 6 +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 TITLE = "Sweeper Demo" BGCOLOR = DARKGREY -TILESIZE = 50 + GRIDWIDTH = WIDTH / TILESIZE GRIDHEIGHT = HEIGHT / TILESIZE \ No newline at end of file diff --git a/sprites.py b/sprites.py index bfc1696..6c319ee 100644 --- a/sprites.py +++ b/sprites.py @@ -22,19 +22,23 @@ class Player(pg.sprite.Sprite): if direction == self.direction: if self.direction == Direction.Right.name: if dx > 0: - self.x += dx + if self.check_border(dx): + self.x += dx if self.direction == Direction.Up.name: if dy < 0: - self.y += dy + if self.check_border(0, dy): + self.y += dy if self.direction == Direction.Down.name: if dy > 0: - self.y += dy + if self.check_border(0, dy): + self.y += dy if self.direction == Direction.Left.name: if dx < 0: - self.x += dx + if self.check_border(dx): + self.x += dx elif direction != self.direction: self.direction = direction @@ -47,7 +51,16 @@ class Player(pg.sprite.Sprite): self.image == pg.transform.rotate(self.baseImage, -90) if self.direction == Direction.Left.name: 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): self.rect.x = self.x * TILESIZE @@ -60,7 +73,7 @@ class Direction(enum.Enum): Down = 3; Right = 4; - + class Mine(pg.sprite.Sprite): def __init__(self, game, x, y): self.groups = game.all_sprites