-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathUndertale.cpp
More file actions
237 lines (204 loc) · 8.71 KB
/
Undertale.cpp
File metadata and controls
237 lines (204 loc) · 8.71 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
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
#include <raylib.h>
#include <string>
using namespace std;
class GColors {
public:
Color Purple0 = (Color){ 31, 0, 71, 255 };
Color Purple1 = (Color){ 34, 2, 77, 255 };
Color Purple2 = (Color){ 49, 3, 110, 255 };
Color Purple3 = (Color){ 67, 5, 150, 255 };
Color Purple4 = (Color){ 86, 6, 194, 255 };
Color Purple5 = (Color){ 110, 10, 245, 255 };
};
class SimpleGame {
public:
const int screenWidth = 1000;
const int screenHeight = 600;
string GameTitle = "Undertale PingPong";
Texture2D background;
Texture2D ballTexture;
Texture2D leftPaddleTexture;
Texture2D rightPaddleTexture;
Texture2D startMenuBg;
const float paddleWidth = 60.0f;
const float paddleHeight = 120.0f;
GColors gc;
Rectangle LeftPad = {10, static_cast<float>(screenHeight / 2 - paddleHeight / 2), paddleWidth, paddleHeight};
Rectangle RightPad = {screenWidth - paddleWidth - 10, static_cast<float>(screenHeight / 2 - paddleHeight / 2), paddleWidth, paddleHeight};
Rectangle TopBAR = {0, 0, static_cast<float>(screenWidth), 5};
Rectangle BottomBAR = {0, static_cast<float>(screenHeight - 5), static_cast<float>(screenWidth), 5};
Rectangle BALL = {static_cast<float>(screenWidth / 2 - 15), static_cast<float>(screenHeight / 2 - 15), 30, 30};
float BALLSPEED_X = 400.0f;
float BALLSPEED_Y = 200.0f;
float speedIncrement = 20.0f;
bool LeftHit = true;
int LeftPoint = 0;
int RightPoint = 0;
string Points = " | Left Player: 0 | Right Player: 0";
Sound HitSound;
Music BGMusic;
Sound Point;
string PlayerWin;
bool GameOver = false;
bool GameStarted = false;
void ResetPaddles() {
LeftPad = {10, static_cast<float>(screenHeight / 2 - paddleHeight / 2), paddleWidth, paddleHeight};
RightPad = {screenWidth - paddleWidth - 10, static_cast<float>(screenHeight / 2 - paddleHeight / 2), paddleWidth, paddleHeight};
}
void ResetBall() {
BALL.x = static_cast<float>(screenWidth / 2 - BALL.width / 2);
BALL.y = static_cast<float>(screenHeight / 2 - BALL.height / 2);
BALLSPEED_X = (LeftHit ? 400.0f : -400.0f);
BALLSPEED_Y = (GetRandomValue(0, 1) == 0 ? 200.0f : -200.0f);
}
void UpdatePoints() {
Points = " | Left Player: " + to_string(LeftPoint) + " | Right Player: " + to_string(RightPoint);
}
void GameUpdate() {
if (!GameStarted) {
if (IsKeyPressed(KEY_SPACE)) {
GameStarted = true;
ResetPaddles();
ResetBall();
}
} else {
if (GameOver) {
if (IsKeyPressed(KEY_R)) {
GameOver = false;
LeftPoint = 0;
RightPoint = 0;
UpdatePoints();
ResetPaddles();
ResetBall();
}
} else {
if (LeftPoint == 5) {
PlayerWin = "Left Player Win!";
GameOver = true;
} else if (RightPoint == 5) {
PlayerWin = "Right Player Win!";
GameOver = true;
}
UpdateMusicStream(BGMusic);
if (IsKeyDown(KEY_W) && LeftPad.y > 0) {
LeftPad.y -= 400 * GetFrameTime ();
}
if (IsKeyDown(KEY_S) && LeftPad.y < screenHeight - paddleHeight) {
LeftPad.y += 400 * GetFrameTime();
}
if (IsKeyDown(KEY_Z) && LeftPad.y > 0) {
LeftPad.y -= 400 * GetFrameTime();
}
if (IsKeyDown(KEY_UP) && RightPad.y > 0) {
RightPad.y -= 400 * GetFrameTime();
}
if (IsKeyDown(KEY_DOWN) && RightPad.y < screenHeight - paddleHeight) {
RightPad.y += 400 * GetFrameTime();
}
BALL.x += BALLSPEED_X * GetFrameTime();
BALL.y += BALLSPEED_Y * GetFrameTime();
if (CheckCollisionRecs(LeftPad, BALL) && BALLSPEED_X < 0) {
PlaySound(HitSound);
BALL.x = LeftPad.x + LeftPad.width;
BALLSPEED_X = labs(BALLSPEED_X) + speedIncrement;
float paddleCenterY = LeftPad.y + LeftPad.height / 2;
float ballCenterY = BALL.y + BALL.height / 2;
BALLSPEED_Y += (ballCenterY - paddleCenterY) * 0.1f;
} else if (CheckCollisionRecs(RightPad, BALL) && BALLSPEED_X > 0) {
PlaySound(HitSound);
BALL.x = RightPad.x - BALL.width;
BALLSPEED_X = -labs(BALLSPEED_X) - speedIncrement;
}
if (BALL.y < 0) {
PlaySound(HitSound);
BALL.y = 0;
BALLSPEED_Y = labs(BALLSPEED_Y);
} else if (BALL.y + BALL.height > screenHeight) {
PlaySound(HitSound);
BALL.y = screenHeight - BALL.height;
BALLSPEED_Y = -labs(BALLSPEED_Y);
}
if (BALL.x + BALL.width < 0) {
RightPoint++;
UpdatePoints();
PlaySound(Point);
ResetBall();
LeftHit = true;
} else if (BALL.x > screenWidth) {
LeftPoint++;
UpdatePoints();
PlaySound(Point);
ResetBall();
LeftHit = false;
}
}
}
}
void DrawStartMenu() {
DrawTexture(startMenuBg, 0, 0, WHITE);
DrawText("Undertale PingPong!", screenWidth / 2 - 200, screenHeight / 2 - 50, 40, WHITE);
DrawText("Press SPACE to Start", screenWidth / 2 - 150, screenHeight / 2 + 10, 20, WHITE);
}
void GameDraw() {
if (!GameStarted) {
DrawStartMenu();
} else {
DrawTexture(background, 0, 0, WHITE);
DrawTexture(ballTexture, static_cast<int>(BALL.x), static_cast<int>(BALL.y), WHITE);
DrawTexture(leftPaddleTexture, static_cast<int>(LeftPad.x), static_cast<int>(LeftPad.y), WHITE);
DrawTexture(rightPaddleTexture, static_cast<int>(RightPad.x), static_cast<int>(RightPad.y), WHITE);
if (GameOver) {
DrawText(PlayerWin.c_str(), 10, 10, 30, gc.Purple5);
DrawText("Press R to play Again!!", screenWidth / 2 - 200, screenHeight / 2, 60, gc.Purple5);
} else {
DrawRectangle(TopBAR.x, TopBAR.y, TopBAR.width, TopBAR.height, gc.Purple3);
DrawRectangle(BottomBAR.x, BottomBAR.y, BottomBAR.width, BottomBAR.height, gc.Purple3);
DrawText(Points.c_str(), 240, 10, 30, gc.Purple5);
DrawFPS(10, 10);
}
}
DrawText("Fangame made by Zaghdane Ihab", screenWidth / 2 - 150, screenHeight - 40, 20 , WHITE);
DrawText("Original game : Undertale By Toby Fox", screenWidth / 2 - 150, screenHeight - 20, 20, WHITE);
}
void UnloadALL() {
UnloadSound(HitSound);
UnloadSound(Point);
UnloadMusicStream(BGMusic);
CloseAudioDevice();
UnloadTexture(background);
UnloadTexture(ballTexture);
UnloadTexture(leftPaddleTexture);
UnloadTexture(rightPaddleTexture);
UnloadTexture(startMenuBg);
}
void GameLoop() {
InitWindow(screenWidth, screenHeight, GameTitle.c_str());
ballTexture = LoadTexture("Build\\custom_ball.png");
leftPaddleTexture = LoadTexture("Build\\sans_paddle.png");
rightPaddleTexture = LoadTexture("Build\\sans_paddle.png");
SetTargetFPS(60);
InitAudioDevice();
HitSound = LoadSound("Build\\hitSound.wav");
BGMusic = LoadMusicStream("Build\\music.mp3");
Point = LoadSound("Build\\point.wav");
SetSoundVolume(HitSound, 0.2f);
SetMusicVolume(BGMusic, 0.5f);
PlayMusicStream(BGMusic);
background = LoadTexture("Build/undertale.png");
startMenuBg = LoadTexture("Build\\start.png");
while (!WindowShouldClose()) {
GameUpdate();
BeginDrawing();
ClearBackground(gc.Purple0);
GameDraw();
EndDrawing();
}
UnloadALL();
CloseWindow();
}
};
int main(void) {
SimpleGame GC;
GC.GameLoop();
return 0;
}