aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorOmar Magdy <99906646+omagdy7@users.noreply.github.com>2023-03-22 01:14:19 +0200
committerGitHub <noreply@github.com>2023-03-22 01:14:19 +0200
commit4076674216ea9e3989adbf8d87df5a474ee81701 (patch)
tree9b4c41fb94dd1411aae40ffcf71c664909f9f1e5
parent87aa85cdbe3bb90705ba5919206f84d7d6bf9a3d (diff)
parent86b685ec600ed0bffc5dd8ec94850e89a3e7137b (diff)
downloadMacpan-4076674216ea9e3989adbf8d87df5a474ee81701.tar.xz
Macpan-4076674216ea9e3989adbf8d87df5a474ee81701.zip
Merge pull request #1 from omagdy7/Map
Map
-rw-r--r--README.md14
-rw-r--r--assets/feh_189639_000001_Sprites.pngbin0 -> 14183 bytes
-rw-r--r--src/Game.py22
-rw-r--r--src/Player.py7
-rw-r--r--src/map.py122
-rw-r--r--src/settings.py4
6 files changed, 156 insertions, 13 deletions
diff --git a/README.md b/README.md
index c909b77..d137568 100644
--- a/README.md
+++ b/README.md
@@ -15,12 +15,22 @@ python3 macpan.py
- [X] Replace the yellow circle with a pacman sprite
- [X] Setup the sprite animation for pacman
- [X] Setup collision with walls
-- [ ] Load the proper sprites for the map
+- [X] Load the proper sprites for the map
+- [ ] Setup collision with the map
- [ ] Add the ghosts in the game
-- [ ] Develop an algorithm to make the ghosts find pacman in the map(BFS, A*)
- [ ] Setup the sprite animation for the ghosts
+- [ ] Develop an algorithm to make the ghosts find pacman in the map(BFS, A*)
+- [ ] Add the Algorithm for Blinky (Normal A*)
+- [ ] Add the Algorithm for Pinky (Normal A* + 4 spaces ahead of pacman)
+- [ ] Add the Algorithm for Inky (Ambush)
+- [ ] Add the Algorithm for Clyde (Same as Blinky excepts when he gets 8 tiles close to pacman he retreats, So he is basically useless)
## Optional
- [ ] Setup a menu for the game
+- [ ] Setup a simple score system
- [ ] Setup a proper sfx/audio for the game
- [ ] Add powerups
+- [ ] Add firghtening mode for the ghosts
+
+# EXTRA BONUS
+- [ ] Make a nueral network agent that fully plays the game alone using reinforcment learning and PyTorch
diff --git a/assets/feh_189639_000001_Sprites.png b/assets/feh_189639_000001_Sprites.png
new file mode 100644
index 0000000..2cb9b8b
--- /dev/null
+++ b/assets/feh_189639_000001_Sprites.png
Binary files differ
diff --git a/src/Game.py b/src/Game.py
index aedfa0c..9d93786 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():
@@ -28,16 +29,27 @@ class Game():
clock = pygame.time.Clock()
- # Sprite sheet for pacman
- sprite_sheet = pygame.image.load('../assets/pacman_left_sprite.png').convert_alpha();
-
sprite_width, sprite_height = 32, 32
+ map = Map.Map()
+
+ grid_x = Settings.settings.width // len(map.maze[0])
+ grid_y = (Settings.settings.height - 50) // len(map.maze)
+
# Checks collision with walls
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
+ print(grid_x, grid_y)
+ x = int((player.x + dx) / grid_x)
+ y = int((player.y + dy) / grid_y)
+ print(x, y)
+ print(map.maze[x][y])
+ is_dot = map.maze[x][y] == Map.D
+ is_big_dot = map.maze[x][y] == Map.BD
+ is_free = map.maze[x][y] == 0
+ return not (is_dot or is_free or is_big_dot)
+
# Main game loop
running = True
@@ -77,7 +89,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 +96,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..e0128d9 100644
--- a/src/Player.py
+++ b/src/Player.py
@@ -28,15 +28,14 @@ def get_sprites(sprite_sheet) -> List:
class Player():
def __init__(self, sprite_sheet):
- self.x = Settings.settings.width // 2;
- self.y = Settings.settings.height // 2;
+ self.x = 450
+ self.y = 663
self.sprite = get_sprites(sprite_sheet)
- self.speed = 5
+ self.speed = 10
self.direction = DIRECTION.LEFT
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:
diff --git a/src/map.py b/src/map.py
index e69de29..e05fc2f 100644
--- a/src/map.py
+++ b/src/map.py
@@ -0,0 +1,122 @@
+import pygame
+import math
+import settings as Settings
+
+
+
+H = 1
+V = 2
+D = 4
+BD = 8
+TR = 16
+TL = 32
+BL = 64
+BR = 128
+G = 256
+
+PI = math.pi
+
+
+class Map():
+ def __init__(self):
+ self.maze = [
+ [TL, H, H, H, H, H, H, H, H, H, H, H, H, H, H, H, H, H, H, H, H, H, H, H, H, H, H, H, H, TR],
+ [V, TL, H, H, H, H, H, H, H, H, H, H, H, H, TR, TL, H, H, H, H, H, H, H, H, H, H, H, H, TR, V],
+ [V, V, D, D, D, D, D, D, D, D, D, D, D, D, V, V, D, D, D, D, D, D, D, D, D, D, D, D, V, V],
+ [V, V, D, TL, H, H, TR, D, TL, H, H, H, TR, D, V, V, D, TL, H, H, H, TR, D, TL, H, H, TR, D, V, V],
+ [V, V, BD, V, 0, 0, V, D, V, 0, 0, 0, V, D, V, V, D, V, 0, 0, 0, V, D, V, 0, 0, V, BD, V, V],
+ [V, V, D, BL, H, H, BR, D, BL, H, H, H, BR, D, BL, BR, D, BL, H, H, H, BR, D, BL, H, H, BR, D, V, V],
+ [V, V, D, D, D, D, D, D, D, D, D, D, D, D, D, D, D, D, D, D, D, D, D, D, D, D, D, D, V, V],
+ [V, V, D, TL, H, H, TR, D, TL, TR, D, TL, H, H, H, H, H, H, TR, D, TL, TR, D, TL, H, H, TR, D, V, V],
+ [V, V, D, BL, H, H, BR, D, V, V, D, BL, H, H, TR, TL, H, H, BR, D, V, V, D, BL, H, H, BR, D, V, V],
+ [V, V, D, D, D, D, D, D, V, V, D, D, D, D, V, V, D, D, D, D, V, V, D, D, D, D, D, D, V, V],
+ [V, BL, H, H, H, H, TR, D, V, BL, H, H, TR, 0, V, V, 0, TL, H, H, BR, V, D, TL, H, H, H, H, BR, V],
+ [V, 0, 0, 0, 0, 0, V, D, V, TL, H, H, BR, 0, BL, BR, 0, BL, H, H, TR, V, D, V, 0, 0, 0, 0, 0, V],
+ [V, 0, 0, 0, 0, 0, V, D, V, V, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, V, V, D, V, 0, 0, 0, 0, 0, V],
+ [BR, 0, 0, 0, 0, 0, V, D, V, V, 0, TL, H, H, G, G, H, H, TR, 0, V, V, D, V, 0, 0, 0, 0, 0, BL],
+ [H, H, H, H, H, H, BR, D, BL, BR, 0, V, 0, 0, 0, 0, 0, 0, V, 0, BL, BR, D, BL, H, H, H, H, H, H],
+ [0, 0, 0, 0, 0, 0, 0, D, 0, 0, 0, V, 0, 0, 0, 0, 0, 0, V, 0, 0, 0, D, 0, 0, 0, 0, 0, 0, 0],
+ [H, H, H, H, H, H, TR, D, TL, TR, 0, V, 0, 0, 0, 0, 0, 0, V, 0, TL, TR, D, TL, H, H, H, H, H, H],
+ [TR, 0, 0, 0, 0, 0, V, D, V, V, 0, BL, H, H, H, H, H, H, BR, 0, V, V, D, V, 0, 0, 0, 0, 0, TL],
+ [V, 0, 0, 0, 0, 0, V, D, V, V, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, V, V, D, V, 0, 0, 0, 0, 0, V],
+ [V, 0, 0, 0, 0, 0, V, D, V, V, 0, TL, H, H, H, H, H, H, TR, 0, V, V, D, V, 0, 0, 0, 0, 0, V],
+ [V, TL, H, H, H, H, BR, D, BL, BR, 0, BL, H, H, TR, TL, H, H, BR, 0, BL, BR, D, BL, H, H, H, H, TR, V],
+ [V, V, D, D, D, D, D, D, D, D, D, D, D, D, V, V, D, D, D, D, D, D, D, D, D, D, D, D, V, V],
+ [V, V, D, TL, H, H, TR, D, TL, H, H, H, TR, D, V, V, D, TL, H, H, H, TR, D, TL, H, H, TR, D, V, V],
+ [V, V, D, BL, H, TR, V, D, BL, H, H, H, BR, D, BL, BR, D, BL, H, H, H, BR, D, V, TL, H, BR, D, V, V],
+ [V, V, BD, D, D, V, V, D, D, D, D, D, D, D, D, D, D, D, D, D, D, D, D, V, V, D, D, BD, V, V],
+ [V, BL, H, TR, D, V, V, D, TL, TR, D, TL, H, H, H, H, H, H, TR, D, TL, TR, D, V, V, D, TL, H, BR, V],
+ [V, TL, H, BR, D, BL, BR, D, V, V, D, BL, H, H, TR, TL, H, H, BR, D, V, V, D, BL, BR, D, BL, H, TR, V],
+ [V, V, D, D, D, D, D, D, V, V, D, D, D, D, V, V, D, D, D, D, V, V, D, D, D, D, D, D, V, V],
+ [V, V, D, TL, H, H, H, H, BR, BL, H, H, TR, D, V, V, D, TL, H, H, BR, BL, H, H, H, H, TR, D, V, V],
+ [V, V, D, BL, H, H, H, H, H, H, H, H, BR, D, BL, BR, D, BL, H, H, H, H, H, H, H, H, BR, D, V, V],
+ [V, V, D, D, D, D, D, D, D, D, D, D, D, D, D, D, D, D, D, D, D, D, D, D, D, D, D, D, V, V],
+ [V, BL, H, H, H, H, H, H, H, H, H, H, H, H, H, H, H, H, H, H, H, H, H, H, H, H, H, H, BR, V],
+ [BL, H, H, H, H, H, H, H, H, H, H, H, H, H, H, H, H, H, H, H, H, H, H, H, H, H, H, H, H, BR]
+ ]
+ self.dot_color = (255, 255, 255) # white
+ self.small_dot_radius = 4
+ self.big_dot_radius = 8
+ self.line_color = (0, 0, 255) # Blue
+ self.line_vertical = Settings.settings.height // len(self.maze)
+ self.line_horizontal = Settings.settings.width // len(self.maze[0])
+ self.line_stroke = 3
+
+ def consturct_map(self):
+ pass
+
+
+ def draw_wall(self, screen, flag , pos):
+ if flag & V:
+ pos1 = (pos[0] + self.line_vertical * 0.5, pos[1])
+ pos2 = (pos1[0], pos1[1] + self.line_horizontal)
+ pygame.draw.line(screen, self.line_color, pos1, pos2, self.line_stroke)
+ if flag & H:
+ pos1 = (pos[0], pos[1] + self.line_vertical * 0.5)
+ pos2 = (pos1[0] + self.line_horizontal, pos1[1])
+ pygame.draw.line(screen, self.line_color, pos1, pos2, self.line_stroke)
+ if flag & D:
+ pos1 = (pos[0] + self.line_vertical * 0.5, pos[1] + self.line_horizontal * 0.5)
+ pygame.draw.circle(screen, self.dot_color, pos1, self.small_dot_radius)
+ if flag & BD:
+ pos1 = (pos[0] + self.line_vertical * 0.5, pos[1] + self.line_horizontal * 0.5)
+ pygame.draw.circle(screen, self.dot_color, pos1, self.big_dot_radius)
+ if flag & TR:
+ pos1 = (pos[0] - self.line_vertical * 0.5, pos[1] + self.line_horizontal * 0.5)
+ arc_rect = pygame.Rect(pos1[0], pos1[1], self.line_vertical, self.line_horizontal)
+ pygame.draw.arc(screen, self.line_color, arc_rect, 0, PI / 2, self.line_stroke)
+ if flag & TL:
+ pos1 = (pos[0] + self.line_vertical * 0.5, pos[1] + self.line_horizontal * 0.5)
+ arc_rect = pygame.Rect(pos1[0], pos1[1], self.line_vertical, self.line_horizontal)
+ pygame.draw.arc(screen, self.line_color, arc_rect, PI / 2, PI, self.line_stroke)
+ if flag & BL:
+ pos1 = (pos[0] + self.line_vertical * 0.5, pos[1] - self.line_horizontal * 0.5)
+ arc_rect = pygame.Rect(pos1[0], pos1[1], self.line_vertical, self.line_horizontal)
+ pygame.draw.arc(screen, self.line_color, arc_rect, PI, 3*PI / 2, self.line_stroke)
+ if flag & BR:
+ pos1 = (pos[0] - self.line_vertical * 0.5, pos[1] - self.line_horizontal * 0.5)
+ arc_rect = pygame.Rect(pos1[0], pos1[1], self.line_vertical, self.line_horizontal)
+ pygame.draw.arc(screen, self.line_color, arc_rect, 3*PI / 2, PI * 2, self.line_stroke)
+
+
+
+
+
+ def draw_map(self, screen):
+ rows = len(self.maze)
+ cols = len(self.maze[0])
+ for i in range(0, rows):
+ for j in range(cols):
+ pos = (j * self.line_horizontal, i * self.line_vertical)
+ self.draw_wall(screen, self.maze[i][j], pos)
+
+
+
+
+
+
+
+
+
+
+
diff --git a/src/settings.py b/src/settings.py
index ba62736..cdda2a4 100644
--- a/src/settings.py
+++ b/src/settings.py
@@ -1,7 +1,7 @@
class Settings():
def __init__(self):
- self.width = 640
- self.height = 480
+ self.width = 900
+ self.height = 950
self.fps = 60
settings = Settings()