pong.c (6578B)
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 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 |
/* Copyright (c) 2024 Lou Woell */
#include <math.h>
#include <raylib.h>
#include <sys/param.h>
#if defined(PLATFORM_WEB)
#include <emscripten/emscripten.h>
#endif
// Color Scheme of my Website
#define DAY_COLOR CLITERAL(Color) { 238, 114, 241, 255}
#define NIGHT_COLOR CLITERAL(Color) { 33, 32, 44, 255}
// Alternative Neutral Colors
/* #define DAY_COLOR SKYBLUE */
/* #define NIGHT_COLOR DARKBLUE */
#define FONTSIZE 30
#define PAUSE_TEXT "PAUSED"
#define RECS 30
#define REC_SIZE 20
typedef enum {
DAY,
NIGHT,
} status;
typedef struct {
Vector2 Position;
Vector2 Direction;
float speed;
status team;
float radius;
} bBall;
typedef struct {
// Pause related
int framesCounter;
bool pause;
int BoardDim;
RenderTexture2D target;
// Board
status board[RECS][RECS];
// Bouncing Balls
bBall DayBall;
bBall NightBall;
} Game;
Game game = {0};
int pause_text_width = 0;
void MakeBoard()
{
for (int y = 0; y < RECS; y++)
{
for (int x = 0; x < RECS; x++)
{
if (RECS / 2 > x)
game.board[y][x] = DAY;
else
game.board[y][x] = NIGHT;
}
}
}
void DrawBoard()
{
ClearBackground(NIGHT_COLOR);
for (int j = 0; j < RECS; j++)
{
for (int i = 0; i < RECS; i++)
{
if (game.board[i][j]){
DrawRectangle(REC_SIZE * j,
REC_SIZE * i,
REC_SIZE,
REC_SIZE,
DAY_COLOR);
}
}
}
}
int RandomOffset(int offset)
{
return GetRandomValue(-(offset), (offset));
}
void MakeBouncingBall(bBall *ball, status team, float startx, float starty)
{
float xspeed;
float yspeed;
if (startx > game.BoardDim / 2.0f)
{
xspeed = -1;
yspeed = 1;
}
else
{
xspeed = 1;
yspeed = -1;
}
ball->Position = (Vector2){ startx, starty};
ball->Direction = (Vector2){ xspeed, yspeed };
ball->team = team;
ball->radius = REC_SIZE* 0.5;
ball->speed = REC_SIZE * 0.5 - 4;
}
void BouncingBallPosition(bBall *ball)
{
ball->Position.x += ball->Direction.x * ball->speed;
ball->Position.y += ball->Direction.y * ball->speed;
// Check walls collision
if ((ball->Position.x > (game.BoardDim - ball->radius)) ||
(ball->Position.x < ball->radius))
{
ball->Direction.x *= -1;
ball->Position.x += ball->Direction.x*ball->speed;
}
if ((ball->Position.y > (game.BoardDim - ball->radius)) ||
(ball->Position.y < ball->radius))
{
ball->Direction.y *= -1;
ball->Position.y += ball->Direction.y*ball->speed;
}
// Check Colour collision
for (double angle = 0; angle < (2 * PI); angle += (PI / 4))
{
int i = floor((ball->Position.x + cos(angle) * ball->radius) /
REC_SIZE);
int j = floor((ball->Position.y + sin(angle) * ball->radius) /
REC_SIZE);
if (i >= 0 && i < RECS && j >= 0 && j < RECS)
{
if (game.board[j][i] == ball->team)
{
game.board[j][i] = !game.board[j][i];
// Determine bounce direction based on the angle
if (fabs(cos(angle)) > fabs(sin(angle)))
ball->Direction.x *= -1;
else
ball->Direction.y *= -1;
}
}
}
}
void DrawBouncingBall(bBall *ball)
{
/* Draw Bouncing Ball */
DrawCircleV(ball->Position, ball->radius, (ball->team ? DAY_COLOR : NIGHT_COLOR));
}
void SetGame()
{
MakeBoard();
MakeBouncingBall(
&game.DayBall,
DAY,
(game.BoardDim / 4.0f) * 3 + RandomOffset(game.BoardDim / 4),
game.BoardDim/2.0f + RandomOffset(game.BoardDim/2));
MakeBouncingBall(
&game.NightBall,
NIGHT,
(game.BoardDim / 4.0f) + RandomOffset(game.BoardDim/ 4),
game.BoardDim/2.0f + RandomOffset(game.BoardDim/2));
}
void DrawGame()
{
float scale = MIN((float)GetScreenWidth()/game.BoardDim,
(float)GetScreenHeight()/game.BoardDim);
BeginTextureMode(game.target);
DrawBoard();
DrawBouncingBall(&game.DayBall);
DrawBouncingBall(&game.NightBall);
// On pause, we draw a blinking message
if (game.pause && ((game.framesCounter/30)%2))
{
DrawText(PAUSE_TEXT,
(game.BoardDim - pause_text_width)/2,
game.BoardDim/3,
FONTSIZE,
WHITE);
}
EndTextureMode();
BeginDrawing();
ClearBackground(BLACK);
DrawTexturePro(
game.target.texture,
(Rectangle){
0.0f,
0.0f,
(float)game.target.texture.width,
(float)-game.target.texture.height
},
(Rectangle){
(GetScreenWidth() - ((float)game.BoardDim*scale))*0.5f,
(GetScreenHeight() - ((float)game.BoardDim*scale))*0.5f,
(float)game.BoardDim*scale,
(float)game.BoardDim*scale
},
(Vector2){0, 0},
0.0f,
WHITE);
EndDrawing();
}
void handleInput()
{
// Press P to Pause
if (IsKeyPressed(KEY_P)) game.pause = !game.pause;
// Press R to reset
if (IsKeyPressed(KEY_R)) SetGame();
}
void UpdateDrawFrame()
{
handleInput();
/* ScaleGame(); */
if (!game.pause)
{
BouncingBallPosition(&game.DayBall);
BouncingBallPosition(&game.NightBall);
}
else game.framesCounter++;
DrawGame();
}
int main()
{
game.BoardDim = RECS*REC_SIZE;
SetConfigFlags(FLAG_VSYNC_HINT | FLAG_WINDOW_RESIZABLE);
InitWindow(game.BoardDim,game.BoardDim, "Pong Wars");
game.target = LoadRenderTexture(game.BoardDim, game.BoardDim);
SetTextureFilter(game.target.texture, TEXTURE_FILTER_POINT);
pause_text_width = MeasureText(PAUSE_TEXT, FONTSIZE);
SetGame();
#if defined(PLATFORM_WEB)
emscripten_set_main_loop(UpdateDrawFrame, 0, 1);
#else
SetTargetFPS(60);
while (!WindowShouldClose())
{
UpdateDrawFrame();
}
#endif
CloseWindow();
return 0;
}
|