diff options
| -rw-r--r-- | src/Game.py | 4 | ||||
| -rw-r--r-- | src/Player.py | 1 | ||||
| -rw-r--r-- | src/map.py | 121 |
3 files changed, 124 insertions, 2 deletions
diff --git a/src/Game.py b/src/Game.py index aedfa0c..bef2b35 100644 --- a/src/Game.py +++ b/src/Game.py @@ -1,6 +1,7 @@ import Player from Direction import DIRECTION import settings as Settings +import map as Map import pygame class Game(): @@ -39,6 +40,7 @@ class Game(): def check_collision(dx, dy): return player.y + sprite_height + dy > Settings.settings.height or player.y + dy < 0 or player.x + sprite_width + dx > Settings.settings.width or player.x + dx < 0 + map = Map.Map() # Main game loop running = True @@ -77,7 +79,6 @@ class Game(): dy = 0 # Necssarry to move only horizontal or vertical - # print(player.direction) # Update the circle's position and checking for collisions if not check_collision(dx, dy): player.x += dx @@ -85,6 +86,7 @@ class Game(): screen.fill((0, 0, 0)) # Clear the screen + map.draw_map(screen) player.draw(screen, counter) # Update the screen diff --git a/src/Player.py b/src/Player.py index f8ebc06..fe24423 100644 --- a/src/Player.py +++ b/src/Player.py @@ -36,7 +36,6 @@ class Player(): def draw(self, screen, counter): pos = (self.x, self.y) - print(self.direction, DIRECTION.UP) if self.direction == DIRECTION.UP: screen.blit(pygame.transform.rotate(self.sprite[counter // 5], 270), pos) elif self.direction == DIRECTION.DOWN: @@ -0,0 +1,121 @@ +# the map is 2d array of with size (160, 120) +# 0 -> free +# 1 -> vertical line +# 2 -> + +import pygame +import settings as Settings + + +U = 1 +D = 4 +L = 8 +R = 2 + +LU = L | U +LD = L | D +RU = R | U +RD = R | D + + +class Map(): + """ + 0 -> free + 1 -> vertical line + 2 -> horizontal line + 3 -> right, down + + """ + def __init__(self): + self.map = [ + [LU, U, U, U, U, U, U, RU], + [L, 0, 0, 0, 0, 0, 0, R], + [L, 0, LU, 0, RU, 0, 0,R], + [L, 0, 0, 0, 0, 0, 0, R], + [L, 0, 0, 0, 0, 0, 0, R], + [LD, D, D, D, D, D, D, RD], + ] + self.line_length = Settings.settings.width // len(self.map) + self.line_color = (0, 0, 150) + self.line_stroke = 6 + + def consturct_map(self): + pass + + + def draw_wall(self, screen, flag , pos): + if flag & U: + pos1 = pos + pos2 = (pos[0] + self.line_length, pos[1]) + pygame.draw.line(screen, self.line_color, pos1, pos2, self.line_stroke) + if flag & D: + pos1 = (pos[0], pos[1] + self.line_length) + pos2 = (pos[0] + self.line_length, pos[1] + self.line_length) + pygame.draw.line(screen, self.line_color, pos1, pos2, self.line_stroke) + if flag & R: + pos1 = (pos[0] + self.line_length, pos[1]) + pos2 = (pos[0] + self.line_length, pos[1] + self.line_length) + pygame.draw.line(screen, self.line_color, pos1, pos2, self.line_stroke) + if flag & L: + pos1 = pos + pos2 = (pos[0], pos[1] + self.line_length) + pygame.draw.line(screen, self.line_color, pos1, pos2, self.line_stroke) + + + + + + def draw_map(self, screen): + rows = len(self.map) + cols = len(self.map[0]) + # ofs = Settings.settings.width // cols + # line_color = (0, 0, 150) + # line_stroke = 6 + for i in range(0, rows): + for j in range(cols): + pos = (j * self.line_length, i * self.line_length) + self.draw_wall(screen, self.map[i][j], pos) + # if self.map[i][j] == 1: + # if i == rows - 1: + # p1 = (j * ofs, (i + 1) * ofs) + # p2 = ((j + 1) * ofs, (i + 1) * ofs) + # else: + # p1 = (j * ofs, i * ofs) + # p2 = ((j + 1) * ofs, i * ofs) + # pygame.draw.line(screen, line_color, p1, p2, line_stroke) + # if self.map[i][j] == 2: + # if j == cols - 1: + # p1 = ((j + 1) * ofs, i * ofs) + # p2 = ((j + 1) * ofs, (i + 1) * ofs) + # else: + # p1 = (j * ofs, i * ofs) + # p2 = (j * ofs, (i + 1) * ofs) + # pygame.draw.line(screen, line_color, p1, p2, line_stroke) + # if self.map[i][j] == 3: + # p1_vertical = (j * ofs, i * ofs) + # p2_vertical = ((j + 1) * ofs, i * ofs) + # p1_horizontal = (j * ofs, i * ofs) + # p2_horizontal = (j * ofs, (i + 1) * ofs) + # pygame.draw.line(screen, line_color, p1_horizontal, p2_horizontal, line_stroke) + # pygame.draw.line(screen, line_color, p1_vertical, p2_vertical, line_stroke) + # if self.map[i][j] == 4: + # p1_vertical = (j * ofs, (i + 1) * ofs) + # p2_vertical = ((j + 1) * ofs, (i + 1) * ofs) + # p1_horizontal = (j * ofs, i * ofs) + # p2_horizontal = (j * ofs, (i + 1) * ofs) + # pygame.draw.line(screen, line_color, p1_horizontal, p2_horizontal, line_stroke) + # pygame.draw.line(screen, line_color, p1_vertical, p2_vertical, line_stroke) + # if self.map[i][j] == 5: + # p1_vertical = (j * ofs, (i + 1) * ofs) + # p2_vertical = ((j + 1) * ofs, (i + 1) * ofs) + # p1_horizontal = ((j + 1) * ofs, i * ofs) + # p2_horizontal = ((j + 1) * ofs, (i + 1) * ofs) + # pygame.draw.line(screen, line_color, p1_horizontal, p2_horizontal, line_stroke) + # pygame.draw.line(screen, line_color, p1_vertical, p2_vertical, line_stroke) + # if self.map[i][j] == 6: + # p1_vertical = (j * ofs, i * ofs) + # p2_vertical = ((j + 1) * ofs, i * ofs) + # p1_horizontal = ((j + 1) * ofs, i * ofs) + # p2_horizontal = ((j + 1) * ofs, (i + 1) * ofs) + # pygame.draw.line(screen, line_color, p1_horizontal, p2_horizontal, line_stroke) + # pygame.draw.line(screen, line_color, p1_vertical, p2_vertical, line_stroke) |
