Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include "raylib.h"
- #define KEY_KP_8 328
- #define KEY_KP_2 322
- #define SCREEN_WIDTH 640
- #define SCREEN_HEIGHT 400
- #define INITIAL_BALL_SPEED 2.0
- #define INITIAL_PADDLE_SPEED 6.0
- typedef struct Ball {
- // Speed of the ball
- float speed;
- // Position of the ball
- Vector2 position;
- // Direction of the ball
- Vector2 direction;
- // Size of the ball
- float size;
- // Color of the ball
- Color color;
- // Is the ball active
- bool active;
- } Ball;
- typedef struct Paddle {
- // Speed of the paddle
- float speed;
- // Position and Size of the paddle
- Rectangle rect;
- // Color of the paddle
- Color color;
- // Is the paddle active
- bool active;
- // Title position
- Vector2 titlePosition;
- // Score position
- Vector2 scorePosition;
- // Score
- int score;
- } Paddle;
- //------------------------------------------------------------------------------------
- // Global Variables Declaration
- //------------------------------------------------------------------------------------
- static Ball ball;
- static Paddle player1;
- static Paddle player2;
- static SpriteFont font;
- const char player1Name[64] = "Player 1";
- const char player2Name[64] = "Player 2";
- //------------------------------------------------------------------------------------
- // Module Functions Declaration (local)
- //------------------------------------------------------------------------------------
- static void InitGame(void); // Initialize game
- static void UpdateGame(void); // Update game (one frame)
- int main()
- {
- InitWindow(SCREEN_WIDTH, SCREEN_HEIGHT, "Simple pong Game");
- InitGame();
- SetTargetFPS(60);
- while(!WindowShouldClose())
- {
- UpdateGame();
- BeginDrawing();
- ClearBackground(BLACK);
- // Draw the middle line
- DrawLineEx((Vector2){ SCREEN_WIDTH / 2, 10.0f }, (Vector2){ SCREEN_WIDTH / 2, SCREEN_HEIGHT - 10.0f}, 5.0f, DARKGRAY);
- // Draw Player 1 HUD
- DrawTextEx(font, player1Name, player1.titlePosition, font.baseSize*1.0f, 2, player1.color);
- DrawTextEx(font, FormatText("%01i", player1.score), player1.scorePosition, font.baseSize*1.0f, 2, player1.color);
- // Draw Player 2 HUD
- DrawTextEx(font, player2Name, player2.titlePosition, font.baseSize*1.0f, 2, player2.color);
- DrawTextEx(font, FormatText("%01i", player2.score), player2.scorePosition, font.baseSize*1.0f, 2, player2.color);
- // Draw the ball
- DrawCircleV(ball.position, ball.size, ball.color);
- // Draw players
- DrawRectangleRec(player1.rect, player1.color);
- DrawRectangleRec(player2.rect, player2.color);
- EndDrawing();
- }
- CloseWindow();
- return 0;
- }
- static void InitGame(void)
- {
- // Initialize the ball
- ball.position.x = SCREEN_WIDTH / 2;
- ball.position.y = SCREEN_HEIGHT / 2;
- ball.direction.x = -1.0f;
- ball.direction.y = 1.0f;
- ball.size = 10;
- ball.speed = INITIAL_BALL_SPEED;
- ball.color = WHITE;
- ball.active = false;
- // Initialize the player 1
- player1.rect.x = 10;
- player1.rect.y = 160;
- player1.rect.width = 10;
- player1.rect.height = 80;
- player1.speed = INITIAL_PADDLE_SPEED;
- player1.color = BLUE;
- player1.active = false;
- player1.titlePosition.x = (SCREEN_WIDTH / 4) - 84.5;
- player1.titlePosition.y = 35.0f;
- player1.scorePosition.x = (SCREEN_WIDTH / 4) - 9.5;
- player1.scorePosition.y = 65.0f;
- player1.score = 0;
- // Initialize the player 2
- player2.rect.x = 620;
- player2.rect.y = 160;
- player2.rect.width = 10;
- player2.rect.height = 80;
- player2.speed = INITIAL_PADDLE_SPEED;
- player2.color = YELLOW;
- player2.active = false;
- player2.titlePosition.x = ((SCREEN_WIDTH / 4) * 3) - 84.5;
- player2.titlePosition.y = 35.0f;
- player2.scorePosition.x = ((SCREEN_WIDTH / 4) * 3) - 9.5;
- player2.scorePosition.y = 65.0f;
- player2.score = 0;
- // Initialize the font
- font = LoadSpriteFont("assets/fonts/3Dventure.ttf");
- }
- static void UpdateGame(void)
- {
- // Ball movements
- ball.position.x += ball.direction.x * ball.speed;
- ball.position.y += ball.direction.y * ball.speed;
- // Flip when hitting top or bottom
- if((ball.position.y < (ball.size / 2)) || (ball.position.y > (SCREEN_HEIGHT - (ball.size / 1))))
- ball.direction.y = -ball.direction.y;
- // Collision between ball and paddle
- if((CheckCollisionCircleRec(ball.position, ball.size, player1.rect)) || (CheckCollisionCircleRec(ball.position, ball.size, player2.rect))){
- ball.direction.x = -ball.direction.x;
- ball.speed *= 1.1; //
- }
- // Player 1 movements
- if(IsKeyDown(KEY_UP) && player1.rect.y > 10)
- player1.rect.y -= player1.speed;
- if(IsKeyDown(KEY_DOWN) && player1.rect.y < SCREEN_HEIGHT - player1.rect.height - 10)
- player1.rect.y += player1.speed;
- // Player 2 movements
- if(IsKeyDown(KEY_KP_8) && player2.rect.y > 10)
- player2.rect.y -= player2.speed;
- if(IsKeyDown(KEY_KP_2) && player2.rect.y < SCREEN_HEIGHT - player2.rect.height - 10)
- player2.rect.y += player2.speed;
- // If player 1 score
- if(ball.position.x > SCREEN_WIDTH) {
- player1.score++;
- ball.speed = INITIAL_BALL_SPEED;
- ball.position.x = SCREEN_WIDTH / 2;
- ball.position.y = SCREEN_HEIGHT / 2;
- ball.direction.x = -1.0f;
- ball.direction.y = 1.0f;
- }
- // If player 2 score
- if(ball.position.x < 0) {
- player2.score++;
- ball.speed = INITIAL_BALL_SPEED;
- ball.position.x = SCREEN_WIDTH / 2;
- ball.position.y = SCREEN_HEIGHT / 2;
- ball.direction.x = -1.0f;
- ball.direction.y = 1.0f;
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement