aboutsummaryrefslogtreecommitdiff
path: root/src
diff options
context:
space:
mode:
Diffstat (limited to 'src')
-rw-r--r--src/clyde.py12
-rw-r--r--src/game.py9
-rw-r--r--src/ghost.py39
-rw-r--r--src/inky.py16
-rw-r--r--src/pinky.py12
-rw-r--r--src/player.py37
-rw-r--r--src/timer.py7
7 files changed, 109 insertions, 23 deletions
diff --git a/src/clyde.py b/src/clyde.py
index d17a583..140ce16 100644
--- a/src/clyde.py
+++ b/src/clyde.py
@@ -1,4 +1,5 @@
from ghost import Ghost
+import random
import pygame
from settings import settings
from mode import MODE
@@ -34,6 +35,12 @@ class Clyde(Ghost):
forbidden = inv_dir[self.last_move]
+ rand_pos = (0, 0)
+
+ if pacman.powerup:
+ self.mode = MODE.FRIGHETENED
+ rand_pos = random.randint(0, 900), random.randint(0, 990)
+
for i in range(len(dx)):
nx = self.x + dx[i] * self.speed
ny = self.y + dy[i] * self.speed
@@ -42,7 +49,10 @@ class Clyde(Ghost):
if self.mode == MODE.SCATTERED:
ret[i] = self.heuristic(
(nx, ny), default_tile[0], default_tile[1])
- else:
+ elif self.mode == MODE.FRIGHETENED:
+ ret[i] = self.heuristic(
+ (nx, ny), rand_pos[0], rand_pos[1])
+ elif self.mode == MODE.CHASING:
if self.is_eight_tiles_away(pacman):
ret[i] = self.heuristic(
(nx, ny), bottom_left_corner[0], bottom_left_corner[1])
diff --git a/src/game.py b/src/game.py
index 0478b28..8e4ce37 100644
--- a/src/game.py
+++ b/src/game.py
@@ -116,7 +116,6 @@ class Game():
ty = 0 # Necssarry to move only horizontal or vertical
# Check for the timer event
if event.type == timer_event:
- print("Finished")
pinky.mode = MODE.CHASING
inky.mode = MODE.CHASING
blinky.mode = MODE.CHASING
@@ -174,10 +173,10 @@ class Game():
# Draw the player and the ghosts
player.draw(screen, counter)
- blinky.draw(screen)
- pinky.draw(screen)
- inky.draw(screen)
- clyde.draw(screen)
+ blinky.draw(screen, player.powerup, counter)
+ pinky.draw(screen, player.powerup, counter)
+ inky.draw(screen, player.powerup, counter)
+ clyde.draw(screen, player.powerup, counter)
# Update the screen
pygame.display.flip()
diff --git a/src/ghost.py b/src/ghost.py
index 43c0c4a..cfd3068 100644
--- a/src/ghost.py
+++ b/src/ghost.py
@@ -1,8 +1,10 @@
import pygame
+import random
import math
from util import get_sprites
from settings import settings
from mode import MODE
+from direction import DIRECTION
import map as Map
dx = [1, 0, -1, 0] # right, down, left, up
@@ -16,6 +18,8 @@ class Ghost():
def __init__(self, sprite_sheet, color, x, y):
self.x = x
self.y = y
+ self.sprite_sheet = sprite_sheet
+ self.name = "blinky"
self.sprite = get_sprites(sprite_sheet)
self.color = color
self.last_move = 3 # this represents the direction based on the dx, dy arrays
@@ -67,6 +71,14 @@ class Ghost():
forbidden = inv_dir[self.last_move]
+ rand_pos = (0, 0)
+
+ if pacman.powerup:
+ self.mode = MODE.FRIGHETENED
+ pacman.sprite = get_sprites(pygame.image.load(
+ '../assets/blinky.png').convert_alpha())
+ rand_pos = random.randint(0, 900), random.randint(0, 990)
+
for i in range(len(dx)):
nx = self.x + dx[i] * self.speed
ny = self.y + dy[i] * self.speed
@@ -75,8 +87,11 @@ class Ghost():
if self.mode == MODE.SCATTERED:
ret[i] = self.heuristic(
(nx, ny), default_tile[0], default_tile[1])
- else:
+ elif self.mode == MODE.CHASING:
ret[i] = self.heuristic((nx, ny), pacman.x, pacman.y)
+ elif self.mode == MODE.FRIGHETENED:
+ ret[i] = self.heuristic(
+ (nx, ny), rand_pos[0], rand_pos[1])
if settings.debug:
pygame.draw.line(screen, self.color, (pacman.x, pacman.y),
(self.x, self.y), 1)
@@ -103,9 +118,23 @@ class Ghost():
self.x %= 900 # The logic of the portal
self.last_move = min_idx
- def draw(self, screen):
+ def draw(self, screen, powerup, counter):
radius = 30 // 2
pos = (self.x - radius, self.y - radius)
- image = pygame.transform.scale(
- self.sprite[sprite_sheet[self.last_move]], (40, 40))
- screen.blit(image, pos)
+ if powerup:
+ self.sprite = get_sprites(pygame.image.load(
+ f'../assets/pacman_{self.color}.png').convert_alpha())
+ image = pygame.transform.scale(self.sprite[counter // 5], (35, 35))
+ if self.last_move == DIRECTION.UP.value:
+ screen.blit(pygame.transform.rotate(image, 270), pos)
+ elif self.last_move == DIRECTION.DOWN.value:
+ screen.blit(pygame.transform.rotate(image, 90), pos)
+ elif self.last_move == DIRECTION.RIGHT.value:
+ screen.blit(pygame.transform.flip(image, True, False), pos)
+ elif self.last_move == DIRECTION.LEFT.value:
+ screen.blit(image, pos)
+ else:
+ self.sprite = get_sprites(self.sprite_sheet)
+ 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 070faa0..1989048 100644
--- a/src/inky.py
+++ b/src/inky.py
@@ -1,4 +1,5 @@
import math
+import random
from typing_extensions import override
from direction import DIRECTION
from mode import MODE
@@ -47,7 +48,7 @@ class Inky(Ghost):
return target
@override
- def get_next_move(self, target, maze, screen, blinky):
+ def get_next_move(self, pacman, maze, screen, blinky):
default_tile = self.get_default_tile()
dx = [1, 0, -1, 0]
@@ -66,9 +67,15 @@ class Inky(Ghost):
if self.last_move == 3:
forbidden = 1
- inter_tile = self.get_intermediate_tile(target)
+ inter_tile = self.get_intermediate_tile(pacman)
target = self.get_target(inter_tile, blinky)
+ rand_pos = (0, 0)
+
+ if pacman.powerup:
+ self.mode = MODE.FRIGHETENED
+ rand_pos = random.randint(0, 900), random.randint(0, 990)
+
# y = mx + c
if settings.debug:
@@ -83,7 +90,10 @@ class Inky(Ghost):
if self.mode == MODE.SCATTERED:
ret[i] = self.heuristic(
(nx, ny), default_tile[0], default_tile[1])
- else:
+ elif self.mode == MODE.FRIGHETENED:
+ ret[i] = self.heuristic(
+ (nx, ny), rand_pos[0], rand_pos[1])
+ elif self.mode == MODE.CHASING:
ret[i] = self.heuristic(
(nx, ny), target[0], target[1])
diff --git a/src/pinky.py b/src/pinky.py
index 688163b..416e7d8 100644
--- a/src/pinky.py
+++ b/src/pinky.py
@@ -1,4 +1,5 @@
import pygame
+import random
from typing_extensions import override
from direction import DIRECTION
from mode import MODE
@@ -61,6 +62,8 @@ class Pinky(Ghost):
if self.last_move == 3:
forbidden = 1
+ rand_pos = (0, 0)
+
new_target = self.get_four_tiles_ahead_of_pacman(target)
if settings.debug:
pygame.draw.circle(screen, self.color,
@@ -68,6 +71,10 @@ class Pinky(Ghost):
pygame.draw.circle(screen, self.color,
default_tile, 15)
+ if target.powerup:
+ self.mode = MODE.FRIGHETENED
+ rand_pos = random.randint(0, 900), random.randint(0, 990)
+
for i in range(len(dx)):
if i != forbidden:
nx = self.x + dx[i] * self.speed
@@ -76,7 +83,10 @@ class Pinky(Ghost):
if self.mode == MODE.SCATTERED:
ret[i] = self.heuristic(
(nx, ny), default_tile[0], default_tile[1])
- else:
+ elif self.mode == MODE.FRIGHETENED:
+ ret[i] = self.heuristic(
+ (nx, ny), rand_pos[0], rand_pos[1])
+ elif self.mode == MODE.CHASING:
ret[i] = self.heuristic(
(nx, ny), new_target[0], new_target[1])
if settings.debug:
diff --git a/src/player.py b/src/player.py
index 903c358..97d2ea5 100644
--- a/src/player.py
+++ b/src/player.py
@@ -1,4 +1,5 @@
from direction import DIRECTION
+from timer import Timer
import map as Map
from util import get_sprites
import pygame
@@ -8,9 +9,12 @@ class Player():
def __init__(self, sprite_sheet):
self.x = 30 * 17 - 15
self.y = 30 * 25 - 15
+ self.sprite_sheet = sprite_sheet
self.sprite = get_sprites(sprite_sheet)
self.speed = 6
self.direction = DIRECTION.LEFT
+ self.powerup = False
+ self.timer = None
# checks if the current position of pacman is either a dot, big dot or free
def is_valid(self, maze, x, y):
@@ -20,6 +24,9 @@ class Player():
is_free = maze.maze[y][x] == 0
if is_dot or is_big_dot:
maze.maze[y][x] = 0
+ if is_big_dot:
+ self.powerup = True
+ self.timer = Timer(5 * 1000)
return (is_dot or is_free or is_big_dot)
return True
@@ -42,14 +49,28 @@ class Player():
return True
def draw(self, screen, counter):
+ if self.timer is not None:
+ elapsed_time = pygame.time.get_ticks() - self.timer.start
+ if elapsed_time > self.timer.duration:
+ self.powerup = False
+
radius = 30 // 2
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)
- elif self.direction == DIRECTION.DOWN:
- screen.blit(pygame.transform.rotate(image, 90), pos)
- elif self.direction == DIRECTION.RIGHT:
- screen.blit(pygame.transform.flip(image, True, False), pos)
- elif self.direction == DIRECTION.LEFT:
+ sprite_sheet = [2, 0, 3, 1]
+ if self.powerup:
+ self.sprite = get_sprites(pygame.image.load(
+ '../assets/pacman_as_ghost.png').convert_alpha())
+ image = pygame.transform.scale(
+ self.sprite[sprite_sheet[self.direction.value]], (40, 40))
screen.blit(image, pos)
+ else:
+ self.sprite = get_sprites(self.sprite_sheet)
+ image = pygame.transform.scale(self.sprite[counter // 5], (35, 35))
+ if self.direction == DIRECTION.UP:
+ screen.blit(pygame.transform.rotate(image, 270), pos)
+ elif self.direction == DIRECTION.DOWN:
+ screen.blit(pygame.transform.rotate(image, 90), pos)
+ elif self.direction == DIRECTION.RIGHT:
+ screen.blit(pygame.transform.flip(image, True, False), pos)
+ elif self.direction == DIRECTION.LEFT:
+ screen.blit(image, pos)
diff --git a/src/timer.py b/src/timer.py
new file mode 100644
index 0000000..769534b
--- /dev/null
+++ b/src/timer.py
@@ -0,0 +1,7 @@
+import pygame
+
+
+class Timer():
+ def __init__(self, duration):
+ self.start = pygame.time.get_ticks()
+ self.duration = duration