From a4b37d49e96c3166d105a939262fa2b04f26f7c3 Mon Sep 17 00:00:00 2001 From: Hri7566 Date: Thu, 25 Jan 2024 02:41:39 -0500 Subject: [PATCH] init --- .gitignore | 1 + Makefile | 7 +++ cong.c | 142 +++++++++++++++++++++++++++++++++++++++++++++++++++++ 3 files changed, 150 insertions(+) create mode 100644 .gitignore create mode 100644 Makefile create mode 100644 cong.c diff --git a/.gitignore b/.gitignore new file mode 100644 index 0000000..4548ab6 --- /dev/null +++ b/.gitignore @@ -0,0 +1 @@ +/cong diff --git a/Makefile b/Makefile new file mode 100644 index 0000000..7a71cf1 --- /dev/null +++ b/Makefile @@ -0,0 +1,7 @@ +SOURCES := cong.c +TARGET := cong +CFLAGS := -O2 -lraylib + +.PHONY: all +all: + $(CC) -o $(TARGET) $(SOURCES) $(CFLAGS) diff --git a/cong.c b/cong.c new file mode 100644 index 0000000..95c51ad --- /dev/null +++ b/cong.c @@ -0,0 +1,142 @@ +#include +#include +#include + +typedef struct { + int x; + int y; +} IVector2; + +typedef struct { + IVector2 position; + IVector2 size; +} ITransform; + +typedef struct { + ITransform transform; +} GameObject; + +#define WIDTH 640 +#define HEIGHT 480 + +void initPlayer(GameObject *player); +void initBall(GameObject *ball); +void initWall(GameObject *wall); + +int main(void) { + GameObject player; + GameObject ball; + GameObject wall; + + IVector2 ballVel; + ballVel.x = 250; + ballVel.y = 250; + + int playerSpeed = 250; + int score = 0; + float dt = 0.0f; + char scoreText[2048]; + char isPaused = 0; + + initPlayer(&player); + initBall(&ball); + initWall(&wall); + + InitWindow(WIDTH, HEIGHT, "cong"); + SetWindowMonitor(0); + SetTargetFPS(60); + + while (!WindowShouldClose()) { + /** + * Update + */ + + if (IsKeyPressed(KEY_SPACE)) { + if (isPaused) + isPaused = 0; + else + isPaused = 1; + } + + dt = GetFrameTime(); + + if (isPaused == 0) { + // Ball physics + ball.transform.position.x += ballVel.x * dt; + ball.transform.position.y += ballVel.y * dt; + + if (ball.transform.position.y + ball.transform.size.y >= HEIGHT || ball.transform.position.y <= 0) { + ballVel.y = -ballVel.y; + } + + if (ball.transform.position.x + ball.transform.size.x >= wall.transform.position.x && ballVel.x > 0) { + ballVel.x = GetRandomValue(250, 250 + (score * 50)); + ballVel.x = -ballVel.x; + // ballVel.y = GetRandomValue(250, score * 1000); + } + + if (ball.transform.position.x < player.transform.position.x + player.transform.size.x && ballVel.x < 0 && ball.transform.position.y + ball.transform.size.y > player.transform.position.y && ball.transform.position.y < player.transform.position.y + player.transform.size.y) { + ballVel.x = -ballVel.x; + if (ballVel.x < 250) ballVel.x = 250; + score++; + } + + if (ball.transform.position.x + ball.transform.size.x < 0) { + ball.transform.position.x = (WIDTH / 2) - (ball.transform.size.x / 2); + ballVel.x = -ballVel.x; + score = 0; + } + + // Player movement + if (IsKeyDown(KEY_W) && player.transform.position.y > 0) { + player.transform.position.y -= playerSpeed * dt; + } else if (IsKeyDown(KEY_S) && player.transform.position.y + player.transform.size.y < HEIGHT) { + player.transform.position.y += playerSpeed * dt; + } + } + + // Score text + sprintf(scoreText, "%d", score); + + /** + * Draw + */ + + BeginDrawing(); + + ClearBackground(RAYWHITE); + + DrawRectangle(player.transform.position.x, player.transform.position.y, player.transform.size.x, player.transform.size.y, BLACK); + DrawRectangle(ball.transform.position.x, ball.transform.position.y, ball.transform.size.x, ball.transform.size.y, BLACK); + DrawRectangle(wall.transform.position.x, wall.transform.position.y, wall.transform.size.x, wall.transform.size.y, BLACK); + DrawText(scoreText, 50, 10, 50, BLACK); + if (isPaused) DrawText("PAUSED", 10, HEIGHT - 50, 50, BLACK); + + EndDrawing(); + } + + CloseWindow(); + + return 0; +} + +void initPlayer(GameObject *player) { + player->transform.position.x = 10; + player->transform.position.y = 10; + player->transform.size.x = 16; + player->transform.size.y = 96; +} + +void initBall(GameObject *ball) { + ball->transform.size.x = 16; + ball->transform.size.y = 16; + ball->transform.position.x = (WIDTH / 2) - (ball->transform.size.x / 2); + ball->transform.position.y = (HEIGHT / 2) - (ball->transform.size.y / 2); +} + +void initWall(GameObject *wall) { + wall->transform.size.x = 16; + wall->transform.size.y = HEIGHT; + wall->transform.position.x = WIDTH - wall->transform.size.x - 10; + wall->transform.position.y = 0; +}