-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmain.cpp
More file actions
181 lines (151 loc) · 5.18 KB
/
Copy pathmain.cpp
File metadata and controls
181 lines (151 loc) · 5.18 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
#include "raylib.h"
#include <cstdlib>
#include <cstring>
#include <netdb.h>
#include <netinet/in.h>
#include <raymath.h>
#include <sys/socket.h>
#include <sys/types.h>
#define WINDOW_WIDTH 1000
#define WINDOW_HEIGHT 1000
#define TEXT_NORMAL 20
enum GameState { MENU, PLAY, END };
void menu(GameState &gs) {
int recBoxPadding = 20;
ClearBackground(BLACK);
DrawText("PongMulti", (WINDOW_WIDTH - MeasureText("PongMulti", 50)) / 2, 100,
50, WHITE);
Rectangle rec1 = {
(float)((WINDOW_WIDTH - MeasureText("PLAY", TEXT_NORMAL)) / 2.0) -
recBoxPadding,
(float)400 - 10,
(float)MeasureText("PLAY", TEXT_NORMAL) + (2 * recBoxPadding), 35};
DrawRectangleRounded(rec1, 10, 5, WHITE);
DrawText("PLAY", (WINDOW_WIDTH - MeasureText("PLAY", TEXT_NORMAL)) / 2, 400,
TEXT_NORMAL, BLACK);
Rectangle rec2 = {
(float)((WINDOW_WIDTH - MeasureText("EXIT", TEXT_NORMAL)) / 2.0) -
recBoxPadding,
(float)500 - 10,
(float)MeasureText("EXIT", TEXT_NORMAL) + (2 * recBoxPadding), 35};
DrawRectangleRounded(rec2, 10, 5, WHITE);
DrawText("EXIT", (WINDOW_WIDTH - MeasureText("EXIT", TEXT_NORMAL)) / 2, 500,
TEXT_NORMAL, BLACK);
if (IsMouseButtonReleased(MOUSE_BUTTON_LEFT)) {
if (CheckCollisionPointRec(GetMousePosition(), rec1)) {
TraceLog(LOG_INFO, "PLAY");
gs = GameState::PLAY;
} else if (CheckCollisionPointRec(GetMousePosition(), rec2)) {
TraceLog(LOG_INFO, "EXIT");
CloseWindow();
}
}
}
float playerPosY = (WINDOW_HEIGHT - 60.0) / 2;
Vector2 ballPos = {WINDOW_WIDTH / 2.0, WINDOW_HEIGHT / 2.0};
Vector2 ballDirection = Vector2Normalize({0.5, 0.5});
float ballSpeed = 4.0;
void play(GameState &gs) {
ClearBackground(BLACK);
const float platformWidth = 10.0;
const float platformHeight = 60.0;
const float speed = 6.0;
const float edgePadding = 4.0;
const float ballRadius = 15.0;
Rectangle player = {10, playerPosY, platformWidth, platformHeight};
DrawCircle(ballPos.x, ballPos.y, ballRadius, WHITE);
DrawRectangleRec(player, WHITE);
if (IsKeyDown(KEY_DOWN)) {
if (playerPosY + speed + platformHeight + edgePadding <= WINDOW_HEIGHT)
playerPosY += speed;
} else if (IsKeyDown(KEY_UP)) {
if (playerPosY - speed - edgePadding >= 0)
playerPosY -= speed;
}
if (CheckCollisionCircleRec(ballPos, ballRadius, player)) {
TraceLog(LOG_INFO, "BALL HIT");
ballDirection.x = -ballDirection.x;
float factor = (ballPos.y - player.y) / platformHeight;
factor *= 2.0;
factor -= 1.0;
ballDirection.y = factor;
ballDirection = Vector2Normalize(ballDirection);
}
if (ballPos.x + (ballDirection.x * ballSpeed) + edgePadding + ballRadius <
WINDOW_WIDTH)
ballPos.x += ballDirection.x * ballSpeed;
else {
ballDirection.x = -ballDirection.x;
}
if (ballPos.y + (ballDirection.y * ballSpeed) + edgePadding + ballRadius <
WINDOW_HEIGHT &&
(ballPos.y + (ballDirection.y * ballSpeed) - edgePadding > 0))
ballPos.y += ballDirection.y * ballSpeed;
else {
ballDirection.y = -ballDirection.y;
}
if (ballPos.x < 0) {
gs = GameState::END;
}
}
void end(GameState &gs) {
ClearBackground(BLACK);
playerPosY = (WINDOW_HEIGHT - 60.0) / 2;
ballPos = {WINDOW_WIDTH / 2.0, WINDOW_HEIGHT / 2.0};
ballDirection = Vector2Normalize({0.5, 0.5});
float recBoxPadding = 20;
DrawText("You Lost! Sikes",
(WINDOW_WIDTH - MeasureText("You Lost! Sikes", 50)) / 2, 100, 50,
WHITE);
Rectangle rec1 = {
(float)((WINDOW_WIDTH - MeasureText("PLAY AGAIN", TEXT_NORMAL)) / 2.0) -
recBoxPadding,
(float)400 - 10,
(float)MeasureText("PLAY AGAIN", TEXT_NORMAL) + (2 * recBoxPadding), 35};
DrawRectangleRounded(rec1, 10, 5, WHITE);
DrawText("PLAY AGAIN",
(WINDOW_WIDTH - MeasureText("PLAY AGAIN", TEXT_NORMAL)) / 2, 400,
TEXT_NORMAL, BLACK);
Rectangle rec2 = {
(float)((WINDOW_WIDTH - MeasureText("EXIT", TEXT_NORMAL)) / 2.0) -
recBoxPadding,
(float)500 - 10,
(float)MeasureText("EXIT", TEXT_NORMAL) + (2 * recBoxPadding), 35};
DrawRectangleRounded(rec2, 10, 5, WHITE);
DrawText("EXIT", (WINDOW_WIDTH - MeasureText("EXIT", TEXT_NORMAL)) / 2, 500,
TEXT_NORMAL, BLACK);
if (IsMouseButtonReleased(MOUSE_BUTTON_LEFT)) {
if (CheckCollisionPointRec(GetMousePosition(), rec1)) {
TraceLog(LOG_INFO, "PLAY AGAIN");
gs = GameState::PLAY;
} else if (CheckCollisionPointRec(GetMousePosition(), rec2)) {
TraceLog(LOG_INFO, "EXIT");
CloseWindow();
}
}
}
int main(int argc, char *argv[]) {
/* if (argc != 4) {
TraceLog(LOG_FATAL, "./a.out IpAddr_to_connect PORT");
std::exit(1);
}
*/
GameState gs = GameState::MENU;
SetTargetFPS(120);
InitWindow(WINDOW_WIDTH, WINDOW_HEIGHT, "PongMulti");
// setLocalSockFD(argv[3]);
// setConnectSocketFD(argv[1], argv[2]);
while (!WindowShouldClose()) {
BeginDrawing();
DrawFPS(WINDOW_WIDTH - MeasureText("XXX FPS", 20), 0);
if (gs == GameState::MENU) {
menu(gs);
} else if (gs == GameState::PLAY) {
play(gs);
} else if (gs == GameState::END) {
end(gs);
}
if (!WindowShouldClose())
EndDrawing();
}
}