pong-clone/main.c
Lia Brüggemann 15ba5eb03c
Create main.c
2025-10-13 23:37:10 +02:00

106 lines
3.3 KiB
C

#include <asm-generic/errno.h>
#include <raylib.h>
#include <stdio.h>
#include <stdlib.h>
//------------------------------------------------------------------------------------
// Program main entry point
//------------------------------------------------------------------------------------
int main(void)
{
const int screenWidth = 1000;
const int screenHeight = 550;
InitWindow(screenWidth, screenHeight, "Pong Clone");
Vector2 ballPosition = { (float)screenWidth/2, (float)screenHeight/2 };
Vector2 rightBarPosition = { (float)screenWidth -100, (float)screenHeight/2 };
Rectangle rightBar = {rightBarPosition.x, rightBarPosition.y, 30,200};
Vector2 ballVelocity = {10.f, 0.0f};
SetTargetFPS(60); // Set our game to run at 60 frames-per-second
//--------------------------------------------------------------------------------------
const Color backroundColor = BLUE;
const Color barColor = GREEN;
int speed = 1;
// Main game loop
while (!WindowShouldClose()) // Detect window close button or ESC key
{
ballPosition.x = ballPosition.x + ballVelocity.x * GetFrameTime() * 10 * speed;
ballPosition.y = ballPosition.y + ballVelocity.y * GetFrameTime() * 10 * speed;
rightBar.x = rightBarPosition.x;
rightBar.y = rightBarPosition.y;
if (CheckCollisionCircleRec(ballPosition, 25, rightBar)) {
if (speed == 1) {
float value = rand() % (10 - -10 + 1) + -10;
ballVelocity.y = value;
}
ballVelocity.x = -10.0f;
speed++;
} if (ballPosition.x >= (float)screenWidth) {
return 0;
} if (ballPosition.y >= (float)screenHeight) {
ballVelocity.y = ballVelocity.y * -1;
} if (ballPosition.y <= 0) {
ballVelocity.y = ballVelocity.y * -1;
} if (ballPosition.x <= 141) {
ballVelocity.x = ballVelocity.x +10.0f;
ballVelocity.y = ballVelocity.y * -1;
}
// Update
//----------------------------------------------------------------------------------
if (IsKeyDown(KEY_UP)) rightBarPosition.y -= 2.0f * GetFrameTime() * 100;
if (IsKeyDown(KEY_DOWN)) rightBarPosition.y += 2.0f * GetFrameTime() * 100;
//----------------------------------------------------------------------------------
// Draw
//----------------------------------------------------------------------------------
char str[3];
BeginDrawing();
ClearBackground(backroundColor);
sprintf(str, "%d", speed);
DrawText(str, screenWidth / 2, screenHeight -50, 50, RED);
DrawRectangle(rightBarPosition.x, rightBarPosition.y, 30, 200, barColor);
DrawRectangle(0 + 100, ballPosition.y -100, 30, 200, barColor);
DrawCircleV(ballPosition, 25, ORANGE);
EndDrawing();
//----------------------------------------------------------------------------------
}
// De-Initialization
//--------------------------------------------------------------------------------------
CloseWindow(); // Close window and OpenGL context
//--------------------------------------------------------------------------------------
return 0;
}