Web   ·   Wiki   ·   Activities   ·   Blog   ·   Lists   ·   Chat   ·   Meeting   ·   Bugs   ·   Git   ·   Translate   ·   Archive   ·   People   ·   Donate
summaryrefslogtreecommitdiffstats
path: root/dev/pacmath.activity/pacmanMovement.py
blob: 5ca8667f92a3d4ddd9802b5c2e42297c1eaa3f62 (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
#! /usr/bin/env python
"""
Creation of PacMath sprite and movement
@license: U{http://creativecommons.org/licenses/by-sa/3.0/us/}
"""

from basicMovement import basicMovement
import pygame
import sys

class pacmanMovement(pygame.sprite.Sprite):    
    def __init__(self):
        """
        Creates a PacMath sprite
        """
        pygame.sprite.Sprite.__init__(self)
        self.name = 'PacMath'
        self.ppl = False
        self.imageIndex = 1 #image index

        # Save a copy of the screen's rectangle
        self.screen = pygame.display.get_surface().get_rect()

        #initialize the move instance
        self.move = basicMovement()

        self.image = {}
        self.image[0] = pygame.image.load('./images/pacmanu.png').convert_alpha()
        self.image[1] = pygame.image.load('./images/pacmanr.png').convert_alpha()
        self.image[2] = pygame.image.load('./images/pacmand.png').convert_alpha()
        self.image[3] = pygame.image.load('./images/pacmanl.png').convert_alpha()
        self.image[4] = pygame.image.load('./images/pacman.png').convert_alpha()
        # Create a variable to store the previous position of the sprite
        self.old = self.image[0].get_rect()
        self.old.x = 300
        self.old.y = 350
        # Create a variable to store the current position of the sprite
        self.rect = self.image[0].get_rect()
        self.rect.x = 300
        self.rect.y = 350

    def update(self, screen, event, MAZE_SIZE, mazeObj):
        """
        Updates Pacmath sprite when directional key is pressed
        @param screen: the screen in which we will draw
        @type event: pygame.event.Event
        @param event: directional key
        @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 = [0, 0]
        #speed/incremental value
        speed = 25
        pacmanEvent = 0
        moving = self.move.getMoving()
        
        if moving == True:
            # Check for movement
            if event.type == pygame.KEYUP:
                #keep going in the same direction
                dx = self.rect.x - self.old.x
                dy = self.rect.y - self.old.y
                amount = [dx, dy]
                pacmanEvent = self.move.update(screen, self, amount, MAZE_SIZE, mazeObj)
                
        if event.type == pygame.KEYDOWN:
            if event.key == pygame.K_LEFT:
                self.move.moving = True
                amount = [-speed, 0]
                pacmanEvent = self.move.update(screen, self, amount, MAZE_SIZE, mazeObj)
                self.imageIndex = 3
            elif event.key == pygame.K_UP:
                self.move.moving = True
                amount = [0, -speed]
                pacmanEvent = self.move.update(screen, self, amount, MAZE_SIZE, mazeObj)
                self.imageIndex = 0
            elif event.key == pygame.K_RIGHT:
                self.move.moving = True
                amount = [speed, 0]
                pacmanEvent = self.move.update(screen, self, amount, MAZE_SIZE, mazeObj)
                self.imageIndex = 1
            elif event.key == pygame.K_DOWN:
                self.move.moving = True
                amount = [0, speed]
                pacmanEvent = self.move.update(screen, self, amount, MAZE_SIZE, mazeObj)
                self.imageIndex = 2           

            
        # Create a Surface the size of our character
        blank = pygame.Surface((self.rect.width, self.rect.height))
        blank.fill((0, 0, 0))

        # Erase the old position by putting our blank Surface on it
        screen.blit(blank, self.old)

        # Draw the new position
        screen.blit(self.image[self.imageIndex], self.rect)

        # Update ONLY the modified areas of the screen
        pygame.display.update([self.old, self.rect])
        return pacmanEvent

    def clearSelf(self, screen, mazeObj):
        """
        Draws the grid square occupied by pacman 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 pacman'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.images[self.imageIndex], self.rect)
        pygame.display.update(self.rect)