From da381fa32c82c7a72be43aa1ecaa53e610c8179c Mon Sep 17 00:00:00 2001 From: me Date: Fri, 11 Apr 2025 08:06:43 +0300 Subject: [PATCH] c moving ball --- .gitignore | 4 ++ c-moving-ball/main.c | 137 +++++++++++++++++++++++++++++++++++++++++++ c-moving-ball/run.sh | 2 + license.txt | 19 ++++++ 4 files changed, 162 insertions(+) create mode 100644 .gitignore create mode 100644 c-moving-ball/main.c create mode 100755 c-moving-ball/run.sh create mode 100644 license.txt diff --git a/.gitignore b/.gitignore new file mode 100644 index 0000000..ba558bf --- /dev/null +++ b/.gitignore @@ -0,0 +1,4 @@ +c-moving-ball/raylib.h +c-moving-ball/wasm.sh +c-moving-ball/wasm +c-moving-ball/game diff --git a/c-moving-ball/main.c b/c-moving-ball/main.c new file mode 100644 index 0000000..a3602f7 --- /dev/null +++ b/c-moving-ball/main.c @@ -0,0 +1,137 @@ +/* + +Gist accompanying https://alloca.space/blog/c-lang.html + +Build and Run +------------- + +1. Install a C compiler and https://raylib.com on your system + +2. Build and run with: + + cc main.c -lraylib -lGL -lm -lpthread -ldl -lrt -lX11 -o game && ./game + +*/ + +#include +#include "raylib.h" + +/* Constants */ + +#define FPS 60 +#define SCREEN_WIDTH 640 +#define SCREEN_HEIGHT 480 +#define BALL_SPEED 200 +#define BALL_RADIUS 50 + +/* Data types */ + +typedef struct { + int x; + int y; +} V2; + +typedef struct { + V2 position; + float radius; + V2 direction; + Color color; +} Ball; + +/* Functions */ + +Ball* createBall(); +void swing(int *position, int *direction, int min, int max); +void swingBall(Ball* ball); +void applyKeys(Ball* ball); +void updateBall(Ball* ball); +void initialize(); + +/* Main */ + +int main(void) { + initialize(); + + Ball* ball = createBall(); + Color background_color = { 0x08, 0x18, 0x29, 0xff }; + + // Game loop + while (!WindowShouldClose()) { + // Update + updateBall(ball); + + // Draw + BeginDrawing(); + + ClearBackground(background_color); + DrawCircle((*ball).position.x, (*ball).position.y, (*ball).radius, (*ball).color); + + EndDrawing(); + } + + // De-Initialization + free(ball); + CloseWindow(); + + return 0; +} + +/* Initialize window */ + +void initialize() { + SetConfigFlags(FLAG_MSAA_4X_HINT); + InitWindow(SCREEN_WIDTH, SCREEN_HEIGHT, "alloca.space - The C Programming Language"); + SetTargetFPS(FPS); +} + +/* Ball */ + +Ball* createBall() { + Ball ball_value = { + .position = { + .x = SCREEN_WIDTH/3, + .y = SCREEN_HEIGHT/3, + }, + .radius = BALL_RADIUS, + .direction = { + .x = 1, + .y = 1, + }, + .color = { 0xf9, 0x92, 0x26, 0xff }, + }; + + Ball* ball = (Ball*)malloc(sizeof(Ball)); + *ball = ball_value; + + return ball; +} + +void updateBall(Ball* ball) { + applyKeys(ball); + swingBall(ball); +} + +void applyKeys(Ball* ball) { + if (IsKeyDown(KEY_D)) { (*ball).direction.x = 1; } + if (IsKeyDown(KEY_A)) { (*ball).direction.x = -1; } + if (IsKeyDown(KEY_S)) { (*ball).direction.y = 1; } + if (IsKeyDown(KEY_W)) { (*ball).direction.y = -1; } +} + +void swingBall(Ball* ball) { + swing(&(*ball).position.x, &(*ball).direction.x, 0 + (*ball).radius, SCREEN_WIDTH - (*ball).radius); + swing(&(*ball).position.y, &(*ball).direction.y, 0 + (*ball).radius, SCREEN_HEIGHT - (*ball).radius); +} + +void swing(int *position, int *direction, int min, int max) { + if (*position <= min) { + *position = min; + *direction = 1; + } + else if (*position >= max) { + *position = max; + *direction = -1; + } + float dt = GetFrameTime(); + *position += *direction * (BALL_SPEED * dt); +} diff --git a/c-moving-ball/run.sh b/c-moving-ball/run.sh new file mode 100755 index 0000000..7686d65 --- /dev/null +++ b/c-moving-ball/run.sh @@ -0,0 +1,2 @@ +#!/bin/bash +cc main.c -lraylib -lGL -lm -lpthread -ldl -lrt -lX11 -o game && ./game diff --git a/license.txt b/license.txt new file mode 100644 index 0000000..e359f5d --- /dev/null +++ b/license.txt @@ -0,0 +1,19 @@ +zlib License + +(C) 2025 alloca.space + +This software is provided 'as-is', without any express or implied +warranty. In no event will the authors be held liable for any damages +arising from the use of this software. + +Permission is granted to anyone to use this software for any purpose, +including commercial applications, and to alter it and redistribute it +freely, subject to the following restrictions: + +1. The origin of this software must not be misrepresented; you must not + claim that you wrote the original software. If you use this software + in a product, an acknowledgment in the product documentation would be + appreciated but is not required. +2. Altered source versions must be plainly marked as such, and must not be + misrepresented as being the original software. +3. This notice may not be removed or altered from any source distribution.