Web   ·   Wiki   ·   Activities   ·   Blog   ·   Lists   ·   Chat   ·   Meeting   ·   Bugs   ·   Git   ·   Translate   ·   Archive   ·   People   ·   Donate
summaryrefslogtreecommitdiffstats
path: root/dev/pacmath.activity/pacmanMovement.py
blob: 5c2fce3f876997f8f525af5ae1bf9a6e6e51e9b5 (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
#! /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, position):
        """
        Creates a PacMath sprite
        @param position: location of pacmath in maze
        """
        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)

        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
        @param event: directional key
        @param MAZE_SIZE: size of maze
        @param mazeObj: an instance of the maze
        """
        # call basic movement and add any other update 

        amount = [0, 0]
        #speed/incremental value
        speed = 25

        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]
                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]
                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]
                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]
                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]
                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])