aboutsummaryrefslogtreecommitdiff
path: root/src
diff options
context:
space:
mode:
Diffstat (limited to 'src')
-rw-r--r--src/clyde.py2
-rw-r--r--src/game.py15
-rw-r--r--src/ghost.py42
-rw-r--r--src/inky.py70
-rw-r--r--src/map.py5
-rw-r--r--src/pinky.py5
-rw-r--r--src/player.py25
7 files changed, 121 insertions, 43 deletions
diff --git a/src/clyde.py b/src/clyde.py
index 560c7cb..117f71a 100644
--- a/src/clyde.py
+++ b/src/clyde.py
@@ -2,7 +2,7 @@ from ghost import Ghost
class Clyde(Ghost):
def __init__(self, sprite_sheet, x, y):
- super().__init__(sprite_sheet, "red", x, y)
+ super().__init__(sprite_sheet, "orange", x, y)
diff --git a/src/game.py b/src/game.py
index b2ff3b7..5b85c39 100644
--- a/src/game.py
+++ b/src/game.py
@@ -61,11 +61,11 @@ class Game():
pygame.mixer.music.play()
siren_sound.play(-1)
- running = True
+ is_game_over = [False]
# Main game loop
- while running:
+ while not is_game_over[0]:
# setting game fps
clock.tick(settings.fps)
@@ -84,7 +84,7 @@ class Game():
# Handling events
for event in pygame.event.get():
if event.type == pygame.QUIT:
- running = False
+ is_game_over = False
elif event.type == pygame.KEYDOWN:
# Move the circle based on the pressed key
if event.key == pygame.K_w:
@@ -143,13 +143,14 @@ class Game():
if player.check_collision(maze, dx, dy, TILE_WIDTH, TILE_HEIGHT):
player.x += dx
player.y += dy
+ player.x %= 900
# Move ghosts
- blinky.move(maze.maze, player)
- pinky.move(maze.maze, player)
- inky.move(maze.maze, player)
- clyde.move(maze.maze, player)
+ blinky.move(maze.maze, player, screen, is_game_over)
+ pinky.move(maze.maze, player, screen, is_game_over)
+ inky.move(maze.maze, player, screen, is_game_over)
+ clyde.move(maze.maze, player, screen, is_game_over)
# Draw the map on each frame
maze.draw_map(screen)
diff --git a/src/ghost.py b/src/ghost.py
index 98b7deb..e810bfe 100644
--- a/src/ghost.py
+++ b/src/ghost.py
@@ -1,9 +1,10 @@
+import pygame
import math
from util import get_sprites
from settings import settings
import map as Map
-dx = [1, 0, -1, 0]
+dx = [1, 0, -1, 0] # right, down, left, up
dy = [0, 1, 0, -1]
inv_dir = [2, 3, 0, 1]
@@ -15,7 +16,7 @@ class Ghost():
self.y = y
self.sprite = get_sprites(sprite_sheet)
self.color = color
- self.last_move = 3
+ self.last_move = 3 # this represents the direction based on the dx, dy arrays
self.speed = 3
def in_bounds(self, pos):
@@ -28,10 +29,12 @@ class Ghost():
# checks if the current position of pacman is either a dot, big dot or free
def is_valid(self, maze, x, y):
- is_dot = maze[y][x] == Map.D
- is_big_dot = maze[y][x] == Map.BD
- is_free = maze[y][x] == 0
- return (is_dot or is_free or is_big_dot)
+ if x >= 0 and x < 30:
+ is_dot = maze[y][x] == Map.D
+ is_big_dot = maze[y][x] == Map.BD
+ is_free = maze[y][x] == 0
+ 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
@@ -45,38 +48,45 @@ class Ghost():
py = ny + direct_y[i] * 14
x = px // gx
y = py // gy
- if not self.in_bounds((px, py)) or not self.is_valid(maze, x, y):
+ if not self.is_valid(maze, x, y):
return False
return True
- def get_next_move(self, pacman, maze):
+ def get_next_move(self, pacman, maze, screen):
ret = len(dx) * [math.inf]
forbidden = inv_dir[self.last_move]
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):
+ nx = self.x + dx[i] * self.speed
+ ny = self.y + dy[i] * self.speed
+ if self.check_collision(nx, ny, 30, 30, maze):
+ if i != forbidden:
ret[i] = self.heuristic((nx, ny), pacman.x, pacman.y)
- min_idx = ret.index(min(ret))
+ min_h = min(ret)
+ if min_h == ret[3] and min_h != math.inf:
+ return 3
+ min_idx = ret.index(min_h)
return min_idx
- def move(self, maze, pacman):
- min_idx = self.get_next_move(pacman, maze)
+ def move(self, maze, pacman, screen, game_over):
+ if abs(pacman.x - self.x) <= 15 and abs(pacman.y - self.y) <= 15:
+ game_over[0] = True
+ min_idx = self.get_next_move(pacman, maze, screen)
new_dx = dx[min_idx] * self.speed
new_dy = dy[min_idx] * self.speed
self.x += new_dx
self.y += new_dy
+ self.x %= 900
self.last_move = min_idx
def draw(self, screen):
radius = 30 // 2
pos = (self.x - radius , self.y - radius)
- screen.blit(self.sprite[sprite_sheet[self.last_move]], pos)
+ image = pygame.transform.scale(self.sprite[sprite_sheet[self.last_move]], (40, 40))
+ screen.blit(image, pos)
diff --git a/src/inky.py b/src/inky.py
index 95eb80b..2f886a6 100644
--- a/src/inky.py
+++ b/src/inky.py
@@ -1,9 +1,73 @@
+import math
+from direction import DIRECTION
from ghost import Ghost
class Inky(Ghost):
def __init__(self, sprite_sheet, x, y):
- super().__init__(sprite_sheet, "red", x, y)
-
-
+ super().__init__(sprite_sheet, "cyan", x, y)
+ # def get_intermediate_tile(self, pacman):
+ # if pacman.direction == DIRECTION.UP:
+ # new_target = (pacman.x - 30 * 2, pacman.y - 30 * 4)
+ # 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_vector_blinky_it(self, blinky, pacman):
+ # it = self.get_intermediate_tile(pacman)
+ # return (it[0] - blinky.x, it[1], blinky.y)
+ #
+ # @override
+ # def get_next_move(self, target, maze, screen):
+ # 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_intermediate_tile(target)
+ # pygame.draw.circle(screen, self.color, (new_target[0], new_target[1]), 15)
+ #
+ # 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
+ #
+ #
+ #
+ #
diff --git a/src/map.py b/src/map.py
index 34f066a..2ccfa1f 100644
--- a/src/map.py
+++ b/src/map.py
@@ -12,7 +12,8 @@ TL = 32
BL = 64
BR = 128
G = 256
-P = 512
+LP = 512
+RP = 1024
PI = math.pi
@@ -35,7 +36,7 @@ class Map():
[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],
- [P, 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, P],
+ [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],
diff --git a/src/pinky.py b/src/pinky.py
index 8b2bee1..42b5d91 100644
--- a/src/pinky.py
+++ b/src/pinky.py
@@ -1,6 +1,6 @@
+import pygame
from typing_extensions import override
from direction import DIRECTION
-from settings import settings
import math
from ghost import Ghost
@@ -36,7 +36,7 @@ class Pinky(Ghost):
return (pacman.x, pacman.y)
@override
- def get_next_move(self, target, maze):
+ def get_next_move(self, target, maze, screen):
dx = [1, 0, -1, 0]
dy = [0, 1, 0, -1]
@@ -54,6 +54,7 @@ class Pinky(Ghost):
forbidden = 1
new_target = self.get_four_tiles_ahead_of_pacman(target)
+ pygame.draw.circle(screen, self.color, (new_target[0], new_target[1]), 15)
for i in range(len(dx)):
if i != forbidden:
diff --git a/src/player.py b/src/player.py
index 34dc4a4..2c4c4f4 100644
--- a/src/player.py
+++ b/src/player.py
@@ -15,13 +15,14 @@ class Player():
# checks if the current position of pacman is either a dot, big dot or free
def is_valid(self,maze, x, y):
- is_dot = maze.maze[y][x] == Map.D
- is_big_dot = maze.maze[y][x] == Map.BD
- is_free = maze.maze[y][x] == 0
- if is_dot or is_big_dot:
- maze.maze[y][x] = 0
-
- return (is_dot or is_free or is_big_dot)
+ if x >= 0 and x < 30:
+ is_dot = maze.maze[y][x] == Map.D
+ is_big_dot = maze.maze[y][x] == Map.BD
+ is_free = maze.maze[y][x] == 0
+ if is_dot or is_big_dot:
+ maze.maze[y][x] = 0
+ 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
@@ -44,12 +45,12 @@ class Player():
def draw(self, screen, counter):
radius = 30 // 2
pos = (self.x - radius , self.y - radius)
- # pygame.draw.circle(screen, 'green', pos, radius)
+ image = pygame.transform.scale(self.sprite[counter // 5], (35, 35))
if self.direction == DIRECTION.UP:
- screen.blit(pygame.transform.rotate(self.sprite[counter // 5], 270), pos)
+ screen.blit(pygame.transform.rotate(image, 270), pos)
elif self.direction == DIRECTION.DOWN:
- screen.blit(pygame.transform.rotate(self.sprite[counter // 5], 90), pos)
+ screen.blit(pygame.transform.rotate(image, 90), pos)
elif self.direction == DIRECTION.RIGHT:
- screen.blit(pygame.transform.flip(self.sprite[counter // 5], True, False), pos)
+ screen.blit(pygame.transform.flip(image, True, False), pos)
elif self.direction == DIRECTION.LEFT:
- screen.blit(self.sprite[counter // 5], pos)
+ screen.blit(image, pos)