-
Notifications
You must be signed in to change notification settings - Fork 0
/
pacman.cpp
128 lines (104 loc) · 3.9 KB
/
pacman.cpp
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
#include <QMatrix>
#include "pacman.h"
Pacman::Pacman(QGraphicsItem *parent) :
GameCharacter(parent)
{
width = 20;
height = 20;
setAnimationState(0);
startPos.setX(240); // Pacman starts game at point NN
startPos.setY(360);
goStartPos();
goStartDirection();
// Load Pacman images
pacmanClosed.load(":/Images/PacManClosed.png");
pacmanRight1.load(":/Images/PacManOpen1.png");
pacmanRight2.load(":/Images/PacManOpen2.png");
QMatrix rotationMatrix;
rotationMatrix.rotate(90);
pacmanDown1 = pacmanRight1.transformed(rotationMatrix);
pacmanDown2 = pacmanRight2.transformed(rotationMatrix);
pacmanLeft1 = pacmanDown1.transformed(rotationMatrix);
pacmanLeft2 = pacmanDown2.transformed(rotationMatrix);
pacmanUp1 = pacmanLeft1.transformed(rotationMatrix);
pacmanUp2 = pacmanLeft2.transformed(rotationMatrix);
pacmanDeath1.load(":/Images/PacManOpen1.png");
pacmanDeath2.load(":/Images/PacManOpen2.png");
pacmanDeath3.load(":/Images/PacManOpen3.png");
pacmanDeath4.load(":/Images/PacManOpen4.png");
pacmanDeath5.load(":/Images/PacManOpen5.png");
}
Pacman::~Pacman()
{
}
QRectF Pacman::boundingRect() const
{
return QRectF(-width/2,-height/2,width,height);
}
void Pacman::paint(QPainter *painter, const QStyleOptionGraphicsItem *option, QWidget *widget)
{
Q_UNUSED(widget); // prevent compiler from generating warnings regarding unused parameters
Q_UNUSED(option);
// Painter paints in local coordinates (independent of pos()!)
int topLeftX = - width/2;
int topLeftY = - height/2;
// Link right image to direction and animationState loop
if (animationState < 2){ // animationState 0,1
painter->drawPixmap(topLeftX,topLeftY,width,height,pacmanClosed);
}
else {
switch (direction){
case left:
if(animationState < 4){ // animationState 2,3
painter->drawPixmap(topLeftX,topLeftY,width,height,pacmanLeft1);
}else if(animationState < 6){ // animationState 4,5
painter->drawPixmap(topLeftX,topLeftY,width,height,pacmanLeft2);
}
break;
case up:
if(animationState < 4){ // animationState 2,3
painter->drawPixmap(topLeftX,topLeftY,width,height,pacmanUp1);
}else if(animationState < 6){ // animationState 4,5
painter->drawPixmap(topLeftX,topLeftY,width,height,pacmanUp2);
}
break;
case right:
if(animationState<4){ // animationState 2,3
painter->drawPixmap(topLeftX,topLeftY,width,height,pacmanRight1);
}else if(animationState<6){ // animationState 4,5
painter->drawPixmap(topLeftX,topLeftY,width,height,pacmanRight2);
}
break;
case down:
if(animationState<4){ // animationState 2,3
painter->drawPixmap(topLeftX,topLeftY,width,height,pacmanDown1);
}else if(animationState<6){ // animationState 4,5
painter->drawPixmap(topLeftX,topLeftY,width,height,pacmanDown2);
}
break;
}
}
// Could add death sequence, but tricky for timing
}
void Pacman::advance()
{
if (animationState > 3){ // should become: if > 5
animationState = 0;
}
else{
++animationState;
}
}
void Pacman::setDeathStatus(bool status)
{
deathStatus = status;
}
bool Pacman::isDead() const
{
return deathStatus;
}
void Pacman::goStartDirection()
{
setDirection(right); // initially Pacman points right (C< shape)
setNextDirection(right); // else unexpected turn at first crossing if no keystroke
}