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
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
|
#! /usr/bin/env python
"""
Creationg of enemy sprites and movement
@license: U{http://creativecommons.org/licenses/by-sa/3.0/us/}
"""
from basicMovement import basicMovement
import pygame
import sys
import random
class ghostMovement(pygame.sprite.Sprite):
def __init__(self, position, g, q):
"""
Creates a Enemy sprite
@param position: location of enemy in maze
"""
pygame.sprite.Sprite.__init__(self)
self.name = 'Ghost'
self.question = q
if g == 0:
file = './images/ghost.png'
elif g == 1:
file = './images/ghost2.png'
elif g == 2:
file = './images/ghost3.png'
elif g == 3:
file = './images/ghost4.png'
self.image = pygame.image.load(file).convert_alpha()
# Save a copy of the screen's rectangle
self.screen = pygame.display.get_surface().get_rect()
#initialize the move instance
self.move = basicMovement(self)
# Create a variable to store the previous position of the sprite
self.old = self.image.get_rect()
self.old.x = 300
self.old.y = 300
# Create a variable to store the current position of the sprite
self.rect = self.image.get_rect()
self.rect.x = 300
self.rect.y = 300
def randomMovement(self):
"""
Randomly generates the movement of the enemy
@return: returns the amount to move by
"""
on = 1 #0 is off and 1 is on
#dx and dy are delta x and y for the change in x and y position
dx = 0
dy = 0
#speed/incremental value
speed = 25
amount = [0, 0]
# simply move around randomly
# random x and random y
while dx == 0:
dx = random.randint(-1, 1)
while dy == 0:
dy = random.randint(-1, 1)
dx = dx * speed * on
dy = dy * speed * on
#for determining which direction to update
movementVal = random.randint(1, 10)
moving = self.move.getMoving()
if moving == False:
#half the time it will update the x and the other half the y
if movementVal > 5:
amount = [dx, 0]
self.move.moving = True
else:
amount = [0, dy]
self.move.moving = True
elif moving == True:
#keep going in the same direction
dx = self.rect.x - self.old.x
dy = self.rect.y - self.old.y
amount = [dx, dy]
# return
return amount
def update(self, screen, MAZE_SIZE, mazeObj):
"""
Updates enemy sprite using random movement
@param screen: the screen in which we will draw
@param MAZE_SIZE: size of maze
@param mazeObj: an instance of the maze
"""
# call basic movement and add any other update
amount = self.randomMovement()
self.move.update(screen, self, amount, MAZE_SIZE, mazeObj)
# Create a Surface the size of our character
blank = pygame.Surface((self.rect.width, self.rect.height))
blank.fill((0, 0, 0))
i = 0 # image index
# Erase the old position by putting our blank Surface on it
screen.blit(blank, self.old)
#draw whatever we erased because there may be a pellet there
gridX = int(round(self.old.x/MAZE_SIZE, 0) )
gridY = int(round(self.old.y/MAZE_SIZE, 0) )
mazeObj.drawPoint(screen, gridX, gridY)
# Draw the new position
screen.blit(self.image, self.rect)
# Update ONLY the modified areas of the screen
pygame.display.update([self.old, self.rect])
def clearSelf(self, screen, mazeObj):
gridX = int(round(self.rect.x/MAZE_SIZE, 0) )
gridY = int(round(self.rect.y/MAZE_SIZE, 0) )
mazeObj.drawPoint(screen, gridX, gridY)
pygame.display.update(self.rect)
def drawSelf(self, screen):
screen.blit(self.image, self.rect)
pygame.display.update(self.rect)
|