Uh oh!
There was an error while loading. Please reload this page.
- Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathPong.cpp
More file actions
Latest commit
142 lines (109 loc) · 2.49 KB
/
Copy pathPong.cpp
File metadata and controls
142 lines (109 loc) · 2.49 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
#defineCONSOLE_GAME_ENGINE_IMPLEMENTATION
#include"ConsoleGameEngine.hpp"
structObject
{
float px, py;
float vx, vy;
float sx, sy;
};
classCPong : publicConsoleGameEngine
{
public:
CPong()
{
sAppName = L"Pong";
}
private:
Object bats[2];
Object ball;
int score[2];
protected:
staticboolRectVsRect(
float px1, float py1, float sx1, float sy1,
float px2, float py2, float sx2, float sy2)
{
return px1 + sx1 > px2 &&
px1 < px2 + sx2 &&
py1 + sy1 > py2 &&
py1 < py2 + sy2;
}
voidResetBall()
{
ball.px = float(ScreenWidth() / 2);
ball.py = float(ScreenHeight() / 2);
ball.vx = -1.0f;
ball.vy = 1.0f;
}
boolOnUserCreate() override
{
bats[0].px = 2.0f;
bats[1].px = float(ScreenWidth() - 3);
for (int i = 0; i < 2; i++)
{
bats[i].py = (float)ScreenHeight() * 0.3f;
bats[i].sx = 1.0f;
bats[i].sy = (float)ScreenHeight() * 0.2f;
score[i] = 0;
}
ResetBall();
ball.sx = 2.0f;
ball.sy = 2.0f;
returntrue;
}
boolOnUserUpdate(floatfDeltaTime) override
{
if (GetKey(L'W').bHeld) bats[0].vy = -1.0f;
if (GetKey(L'S').bHeld) bats[0].vy = +1.0f;
if (GetKey(VK_UP).bHeld) bats[1].vy = -1.0f;
if (GetKey(VK_DOWN).bHeld) bats[1].vy = +1.0f;
for (int i = 0; i < 2; i++)
{
if (RectVsRect(bats[i].px, bats[i].py, bats[i].sx, bats[i].sy,
ball.px, ball.py, ball.sx, ball.sy))
{
ball.vx *= -1.0f;
}
bats[i].px += 50.0f * bats[i].vx * fDeltaTime;
bats[i].py += 50.0f * bats[i].vy * fDeltaTime;
bats[i].vx = 0.0f;
bats[i].vy = 0.0f;
}
ball.px += 20.0f * ball.vx * fDeltaTime;
ball.py += 20.0f * ball.vy * fDeltaTime;
if (ball.py + ball.sy >= ScreenHeight() || ball.py < 0.0f)
{
ball.vy *= -1.0f;
ball.py += ball.vy;
}
if (ball.px < 0.0f)
{
score[1]++;
ResetBall();
}
if (ball.px >= ScreenWidth())
{
score[0]++;
ResetBall();
}
Clear(PIXEL_SOLID, FG_BLACK);
for (int y = 0; y < ScreenHeight(); y++)
{
if (y % 2 == 0)
Draw(ScreenWidth() / 2, y, PIXEL_SOLID, FG_WHITE);
}
DrawString(1, 1, std::to_wstring(score[0]), FG_WHITE);
std::wstring score2 = std::to_wstring(score[1]);
DrawString(ScreenWidth() - score2.length() - 1, 1, score2, FG_WHITE);
for (int i = 0; i < 2; i++)
FillRectangle(bats[i].px, bats[i].py, bats[i].sx, bats[i].sy, PIXEL_SOLID, FG_YELLOW);
FillRectangle(ball.px, ball.py, ball.sx, ball.sy, PIXEL_HALF, FG_RED);
returntrue;
}
};
intmain()
{
CPong app;
if (app.ConstructConsole(80, 40, 12, 12) == RC_OK)
app.Run();
return0;
}