-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathhorse.cpp
More file actions
64 lines (60 loc) · 2.34 KB
/
horse.cpp
File metadata and controls
64 lines (60 loc) · 2.34 KB
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
/* Jacoby King
* 11/08/2022
* CIS164 Final Project
*/
#include "horse.h"
//Horse object constructor, takes in the different QPixmaps and speed since the Horses have different colors and speeds
Horse::Horse(QPixmap firstPosition, QPixmap secondPosition, int speed)
{
//Setting the Horse object to the first QPixmap position
horseImagePosition = 0;
setPixmap(firstPosition);
//Setting attributes
this->speed = speed;
this->firstPosition = firstPosition;
this->secondPosition = secondPosition;
//Initializing and connecting the horseImageTimer to the updatePixmap() slot
horseImageTimer = new QTimer();
connect(horseImageTimer, SIGNAL(timeout()), this, SLOT(updatePixmap()));
//Initializing and connecting the horseMovementTimer to the move() slot
horseMovementTimer = new QTimer();
connect(horseMovementTimer, SIGNAL(timeout()), this, SLOT(move()));
}
//Slot, used to update the position of the Horse on the QGraphicsView scene everytime the horseMovementTimer iterates
void Horse::move()
{
//If the Horse is not at the edge of the view
if(x() < 540){
//Change x position by the current x pos + speed
setPos(x() + speed, y());
}
else{
//If the Horse is at the edge of the view, stop movement and image timers
horseImageTimer->stop();
horseMovementTimer->stop();
//Emits a signal to the widget when whichever Horse it is connected to (the slowest) reaches the end
emit finishRace();
}
}
//Slot, used to update the Pixmap of the object
void Horse::updatePixmap()
{
//If the horseImagePosition is true, that means it is in the secondPosition, therefor change it to the firstPosition and make horseImagePosition false
if(horseImagePosition){
setPixmap(firstPosition);
horseImagePosition = 0;
}
//If the horseImagePosition is false, that means it is in the firstPosition, therefor change it to the secondPosition and make horseImagePosition true
else{
setPixmap(secondPosition);
horseImagePosition = 1;
}
}
//Called for each Horse object when the race begins, starts slot connected Horse timers
void Horse::startTimers()
{
//Animates Horse object
horseImageTimer->start(180);
//Moves Horse object
horseMovementTimer->start(100);
}