aboutsummaryrefslogtreecommitdiff
path: root/src
diff options
context:
space:
mode:
authoromagdy7 <omar.professional8777@gmail.com>2023-05-08 13:07:48 +0300
committeromagdy7 <omar.professional8777@gmail.com>2023-05-08 13:07:48 +0300
commitc7c473177086399a8fb97936b4c3c2b67a43fce0 (patch)
tree382cf7c0aacc90fd34b2989fb68ea34261d3a5b0 /src
parent14be09cd58292f36c1922eddcfb43b750237751a (diff)
downloadMacpan-c7c473177086399a8fb97936b4c3c2b67a43fce0.tar.xz
Macpan-c7c473177086399a8fb97936b4c3c2b67a43fce0.zip
Finished inky's algorithm and also added an option in settings to disable audio
Diffstat (limited to 'src')
-rw-r--r--src/game.py9
-rw-r--r--src/inky.py102
-rw-r--r--src/player.py9
-rw-r--r--src/settings.py1
4 files changed, 78 insertions, 43 deletions
diff --git a/src/game.py b/src/game.py
index 5a41f88..c4f5597 100644
--- a/src/game.py
+++ b/src/game.py
@@ -61,8 +61,9 @@ class Game():
siren_sound = pygame.mixer.Sound('../assets/sfx/siren_1.wav')
munch_sound = pygame.mixer.Sound('../assets/sfx/munch_1.wav')
- pygame.mixer.music.play()
- siren_sound.play(-1)
+ if settings.sound:
+ pygame.mixer.music.play()
+ siren_sound.play(-1)
is_game_over = [False]
# Main game loop
@@ -148,7 +149,7 @@ class Game():
# Move ghosts
blinky.move(maze.maze, player, screen, is_game_over, blinky)
pinky.move(maze.maze, player, screen, is_game_over, blinky)
- # inky.move(maze.maze, player, screen, is_game_over, blinky)
+ inky.move(maze.maze, player, screen, is_game_over, blinky)
clyde.move(maze.maze, player, screen, is_game_over, blinky)
# Draw the map on each frame
@@ -158,7 +159,7 @@ class Game():
player.draw(screen, counter)
blinky.draw(screen)
pinky.draw(screen)
- # inky.draw(screen)
+ inky.draw(screen)
clyde.draw(screen)
# Update the screen
diff --git a/src/inky.py b/src/inky.py
index e1c44b0..86ac156 100644
--- a/src/inky.py
+++ b/src/inky.py
@@ -10,37 +10,71 @@ class Inky(Ghost):
def __init__(self, sprite_sheet, x, y):
super().__init__(sprite_sheet, "orange", x, y)
- # @override
- # def get_next_move(self, target, maze, screen, blinky):
- # dx = [1, 0, -1, 0]
- # dy = [0, 1, 0, -1]
- #
- # ret = len(dx) * [math.inf]
- #
- # forbidden = 0
- #
- # if self.last_move == 0:
- # forbidden = 2
- # if self.last_move == 1:
- # forbidden = 3
- # if self.last_move == 2:
- # forbidden = 0
- # if self.last_move == 3:
- # forbidden = 1
- #
- # new_target = self.get_target(target, blinky)
- #
- # if settings.debug:
- # pygame.draw.line(screen, self.color, (new_target),
- # (blinky.x, blinky.y), 1)
- #
- # for i in range(len(dx)):
- # if i != forbidden:
- # nx = self.x + dx[i] * self.speed
- # ny = self.y + dy[i] * self.speed
- # if self.check_collision(nx, ny, 30, 30, maze):
- # ret[i] = self.heuristic(
- # (nx, ny), new_target[0], new_target[1])
- #
- # min_idx = ret.index(min(ret))
- # return min_idx
+ def get_intermediate_tile(self, pacman):
+ if pacman.direction == DIRECTION.UP:
+ new_target = (pacman.x - 30 * 2, pacman.y - 30 * 2)
+ if self.in_bounds(new_target):
+ return new_target
+ else:
+ return (pacman.x, pacman.y)
+ elif pacman.direction == DIRECTION.DOWN:
+ new_target = (pacman.x, pacman.y + 30 * 2)
+ if self.in_bounds(new_target):
+ return new_target
+ else:
+ return (pacman.x, pacman.y)
+ elif pacman.direction == DIRECTION.RIGHT:
+ new_target = (pacman.x + 30 * 2, pacman.y)
+ if self.in_bounds(new_target):
+ return new_target
+ else:
+ return (pacman.x, pacman.y)
+ elif pacman.direction == DIRECTION.LEFT:
+ new_target = (pacman.x - 30 * 2, pacman.y)
+ if self.in_bounds(new_target):
+ return new_target
+ else:
+ return (pacman.x, pacman.y)
+
+ def get_target(self, inter_tile, blinky):
+ target = (inter_tile[0] - (blinky.x - inter_tile[0]),
+ inter_tile[1] - (blinky.y - inter_tile[1]))
+ return target
+
+ @override
+ def get_next_move(self, target, maze, screen, blinky):
+ dx = [1, 0, -1, 0]
+ dy = [0, 1, 0, -1]
+
+ ret = len(dx) * [math.inf]
+
+ forbidden = 0
+
+ if self.last_move == 0:
+ forbidden = 2
+ if self.last_move == 1:
+ forbidden = 3
+ if self.last_move == 2:
+ forbidden = 0
+ if self.last_move == 3:
+ forbidden = 1
+
+ inter_tile = self.get_intermediate_tile(target)
+ target = self.get_target(inter_tile, blinky)
+
+ # y = mx + c
+
+ if settings.debug:
+ pygame.draw.line(screen, self.color, (target),
+ (self.x, self.y), 1)
+
+ for i in range(len(dx)):
+ if i != forbidden:
+ nx = self.x + dx[i] * self.speed
+ ny = self.y + dy[i] * self.speed
+ if self.check_collision(nx, ny, 30, 30, maze):
+ ret[i] = self.heuristic(
+ (nx, ny), target[0], target[1])
+
+ min_idx = ret.index(min(ret))
+ return min_idx
diff --git a/src/player.py b/src/player.py
index 2c4c4f4..903c358 100644
--- a/src/player.py
+++ b/src/player.py
@@ -1,4 +1,3 @@
-from typing import List
from direction import DIRECTION
import map as Map
from util import get_sprites
@@ -14,7 +13,7 @@ class Player():
self.direction = DIRECTION.LEFT
# checks if the current position of pacman is either a dot, big dot or free
- def is_valid(self,maze, x, y):
+ def is_valid(self, maze, x, y):
if x >= 0 and x < 30:
is_dot = maze.maze[y][x] == Map.D
is_big_dot = maze.maze[y][x] == Map.BD
@@ -24,8 +23,8 @@ class Player():
return (is_dot or is_free or is_big_dot)
return True
-
- # checks collision with pacman and obstacles returns false if there is a collision and true otherwise
+ # checks collision with pacman and obstacles returns false
+ # if there is a collision and true otherwise
def check_collision(self, maze, dx, dy, tile_width, tile_height):
direct_x = [1, 0, -1, 0, 1, 1, -1, -1]
direct_y = [0, 1, 0, -1, -1, 1, -1, 1]
@@ -44,7 +43,7 @@ class Player():
def draw(self, screen, counter):
radius = 30 // 2
- pos = (self.x - radius , self.y - radius)
+ pos = (self.x - radius, self.y - radius)
image = pygame.transform.scale(self.sprite[counter // 5], (35, 35))
if self.direction == DIRECTION.UP:
screen.blit(pygame.transform.rotate(image, 270), pos)
diff --git a/src/settings.py b/src/settings.py
index 94eabd8..d1cc93c 100644
--- a/src/settings.py
+++ b/src/settings.py
@@ -4,6 +4,7 @@ class Settings():
self.height = 990
self.fps = 60
self.debug = True
+ self.sound = False
settings = Settings()