1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
|
# Pacman.activity
# A pac-man clone for the XO laptop.
#
# Thanks to Raphael Reinig for testing
#
# Copyright (C) 2008 Tristan Hoffmann, tristanhoffmann@boxbe.com
# Thanks to Joshua Minor, I used a lot of code from his Maze.activity
#
# This file is part of Pacman.activity
#
# Pacman.activity is free software: you can redistribute it and/or modify
# it under the terms of the GNU General Public License as published by
# the Free Software Foundation, either version 3 of the License, or
# (at your option) any later version.
#
# Pacman.activity is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
# GNU General Public License for more details.
#
# You should have received a copy of the GNU General Public License
# along with Pacman.activity. If not, see <http://www.gnu.org/licenses/>.
import pygame
import random
from pygame import sprite as sprite
from utils import get_dir
class Ghost:
def __init__(self, color, seed, x, y):
self.seed = seed
self.generator = random.Random(seed)
self.color = color
self.sprites = sprite.RenderUpdates()
self.sprite = sprite.Sprite()
self.dirName = get_dir()
self.sprite.image = pygame.image.load(self.dirName + "ghost_" + color + ".png")
self.sprites.add(self.sprite)
self.position = (x, y)
self.startposition = self.position
self.reset()
def draw(self, screen, bounds, size):
rect = pygame.Rect(bounds.x+self.position[0]*size, bounds.y+self.position[1]*size, size, size)
self.sprite.rect = rect
self.sprites.draw(screen)
def reset(self):
self.direction = (0,0)
self.previous = self.startposition
self.elapsed = None
def animate(self, maze):
directions = []
if (self.canGo ((0,1), maze)):
directions.append ((0,1))
if (self.direction != (0,-1)):
directions.append ((0,1)) # raise the probability that the ghost doesn't go back
if (self.canGo ((0,-1), maze)):
directions.append ((0,-1))
if (self.direction != (0,1)):
directions.append ((0,-1)) # raise the probability that the ghost doesn't go back
if (self.canGo ((1,0), maze)):
directions.append ((1,0))
if (self.direction != (-1,0)):
directions.append ((1,0)) # raise the probability that the ghost doesn't go back
if (self.canGo ((-1,0), maze)):
directions.append ((-1,0))
if (self.direction != (1,0)):
directions.append ((-1,0)) # raise the probability that the ghost doesn't go back
if (self.canGo (self.direction, maze)): # don't go back if we can go forward
backDirection = (-self.direction[0], -self.direction[1])
directions.append (self.direction) # raise the probability that the ghost keeps it's direction
directions.remove (backDirection) # remove possibility to go back
self.direction = self.generator.choice (directions)
self.move (self.direction, maze)
def move(self, direction, maze):
"""Move the ghost in a given direction (deltax,deltay)"""
if (direction[0] > 1):
print "Direction[0] is greater than 1!"
if (direction[1] > 1):
print "Direction[1] is greater than 1!"
if (direction == (1, 1)):
print "Direction is (1, 1)"
newposition = (self.position[0]+direction[0], self.position[1]+direction[1])
self.position = newposition
def canGo(self, direction, maze):
"""Can the ghost go in this direction without bumping into something?"""
if (direction == (0,0)):
return 0 # make sure the ghost isn't sleeping :)
newposition = (self.position[0]+direction[0], self.position[1]+direction[1])
return maze.validMove(newposition[0], newposition[1])
|