Web   ·   Wiki   ·   Activities   ·   Blog   ·   Lists   ·   Chat   ·   Meeting   ·   Bugs   ·   Git   ·   Translate   ·   Archive   ·   People   ·   Donate
summaryrefslogtreecommitdiffstats
path: root/dev/pacmath.activity/GhostMovement.py
blob: 7282dea938b580ccc90308e43cd24c5cf7cc2981 (plain)
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
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
#! /usr/bin/env python
"""
Creation 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, g, q):
        """
        Creates a Enemy sprite
        @type g: number 1-4
        @param g: The number identifying this ghost, used to associate image
        @type q: question object
        @param q: The question object (unused?)
        """
        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()
        
        self.freezeImage = pygame.image.load('./images/ghost_freeze.png').convert_alpha()
        # Save a copy of the screen's rectangle
        self.screen = pygame.display.get_surface().get_rect()
        self.freezeTime = 0
        #initialize the move instance
        self.move = basicMovement()

        # 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
        @rtype: [dx, dy]
        @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
        @type screen: pygame.display
        @param screen: the screen in which we will draw
        @param MAZE_SIZE: size of maze
        @type mazeObj: Maze
        @param mazeObj: an instance of the maze
        """
        # call basic movement and add any other update
        amount = self.randomMovement()
        if self.freezeTime > 0:
            amount = [0,0]
            self.move.moving = False
        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
        if self.freezeTime > 0:
            if self.freezeTime%2 == 1 and self.freezeTime > 5:
                screen.blit(self.freezeImage, self.rect)
            else:
                screen.blit(self.image, self.rect)
            self.freezeTime -= 1
        else:
            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):
        """
        Draws the grid square occupied by this ghost at the location in 'rect'
        @type screen: pygame.display
        @param screen: the screen in which we will draw
        @type mazeObj: Maze
        @param mazeObj: an instance of the maze
        """
        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):
        """
        Draws this ghost's image onto the screen at the location in 'rect'
        @type screen: pygame.display
        @param screen: the screen in which we will draw
        """
        screen.blit(self.image, self.rect)
        pygame.display.update(self.rect)

    def freeze(self, time):
        """
        Adds freezetime to this ghost, not letting it move for a given number
        of 'update' calls
        @param time: the number of update calls for which it should freeze
        """
        self.freezeTime += time

    def eaten(self):
        """
        Called when this ghost is eaten, moving it back to spawn
        """
        self.rect.x = 300
        self.rect.y = 300
        self.move.moving = False