-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathdog-game.cpp
More file actions
154 lines (131 loc) · 2.91 KB
/
dog-game.cpp
File metadata and controls
154 lines (131 loc) · 2.91 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
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
#include "engine.h"
class Dog
{
public:
Dog(int x_, int y_)
{
x = x_;
y = y_;
}
int x;
int y;
void update()
{
int chance = random(10);
if (chance >= 5)
{
string random_move = random({"up","down","left","right"});
if (random_move == "up")
{
y = y - 1;
}
else if (random_move == "down")
{
y = y + 1;
}
else if (random_move == "left")
{
x = x - 1;
}
else if (random_move == "right")
{
x = x + 1;
}
}
}
};
class DogGame : public Engine
{
// player's (x,y) location
int x;
int y;
// goal's (x,y) location
int goal_x;
int goal_y;
// a vector of dog objects
vector<Dog> dogs;
string ICON_PLAYER = "🧍";
string ICON_DOG = "🐕";
string ICON_GOAL = "🏁";
void setup()
{
// prompt the user to enter how many dogs to
// put in the game?
cout << "How many dogs? ";
int n;
cin >> n;
// create n instances of dogs
for (int i = 0; i < n; i ++)
{
int x = random(12);
int y = random(12);
// initialize them
Dog dog(x,y);
dogs.push_back(dog);
}
createCanvas(12, 12);
goal_x = 11;
goal_y = 5;
}
void keyPressed(int keyCode)
{
switch (keyCode)
{
case 'd':
x = x + 1;
break;
case 'a':
x = x - 1;
break;
case 'w':
y = y - 1;
break;
case 's':
y = y + 1;
break;
}
}
void draw()
{
clear();
// draw the dog
stroke(ICON_GOAL);
point(goal_x, goal_y);
// draw the player
stroke(ICON_PLAYER);
point(x, y);
// draw all the dogs
for (Dog dog : dogs)
{
dog.update();
stroke(ICON_DOG);
point(dog.x, dog.y);
}
}
void console()
{
// for each dog in the vector of dog objects
for (Dog dog : dogs)
{
// if the player's position overlaps with this dog
if (y == dog.y && x == dog.x)
{
cout << "You are bitten by a dog. GAME OVER!! " << endl;
quit();
return;
}
}
// if the player's position is at the goal's position
if (x == goal_x && y == goal_y)
{
cout << "You survived!!" << endl;
quit();
return;
}
}
};
int main()
{
DogGame game;
game.play();
}