Advertisement
Guest User

Untitled

a guest
Jul 24th, 2017
74
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C 6.05 KB | None | 0 0
  1. #include "raylib.h"
  2.  
  3. #define KEY_KP_8 328
  4. #define KEY_KP_2 322
  5.  
  6. #define SCREEN_WIDTH 640
  7. #define SCREEN_HEIGHT 400
  8.  
  9. #define INITIAL_BALL_SPEED 2.0
  10. #define INITIAL_PADDLE_SPEED 6.0
  11.  
  12. typedef struct Ball {
  13.    
  14.     // Speed of the ball
  15.     float speed;
  16.    
  17.     // Position of the ball
  18.     Vector2 position;
  19.  
  20.     // Direction of the ball
  21.     Vector2 direction;
  22.    
  23.     // Size of the ball
  24.     float size;
  25.    
  26.     // Color of the ball
  27.     Color color;
  28.    
  29.     // Is the ball active
  30.     bool active;
  31.    
  32. } Ball;
  33.  
  34. typedef struct Paddle {
  35.    
  36.     // Speed of the paddle
  37.     float speed;
  38.    
  39.     // Position and Size of the paddle
  40.     Rectangle rect;
  41.    
  42.     // Color of the paddle
  43.     Color color;
  44.    
  45.     // Is the paddle active
  46.     bool active;
  47.    
  48.     // Title position
  49.     Vector2 titlePosition;
  50.    
  51.     // Score position
  52.     Vector2 scorePosition;
  53.    
  54.     // Score
  55.     int score;
  56.        
  57. } Paddle;
  58.  
  59. //------------------------------------------------------------------------------------
  60. // Global Variables Declaration
  61. //------------------------------------------------------------------------------------
  62. static Ball ball;
  63. static Paddle player1;
  64. static Paddle player2;
  65. static SpriteFont font;
  66. const char player1Name[64] = "Player 1";
  67. const char player2Name[64] = "Player 2";
  68.  
  69. //------------------------------------------------------------------------------------
  70. // Module Functions Declaration (local)
  71. //------------------------------------------------------------------------------------
  72. static void InitGame(void);         // Initialize game
  73. static void UpdateGame(void);       // Update game (one frame)
  74.  
  75. int main()
  76. {
  77.      
  78.     InitWindow(SCREEN_WIDTH, SCREEN_HEIGHT, "Simple pong Game");
  79.    
  80.     InitGame();
  81.    
  82.     SetTargetFPS(60);
  83.    
  84.     while(!WindowShouldClose())
  85.     {
  86.        
  87.         UpdateGame();
  88.         BeginDrawing();
  89.         ClearBackground(BLACK);
  90.        
  91.         // Draw the middle line
  92.         DrawLineEx((Vector2){ SCREEN_WIDTH / 2, 10.0f }, (Vector2){ SCREEN_WIDTH / 2, SCREEN_HEIGHT - 10.0f}, 5.0f, DARKGRAY);
  93.        
  94.        
  95.         // Draw Player 1 HUD
  96.         DrawTextEx(font, player1Name, player1.titlePosition, font.baseSize*1.0f, 2, player1.color);
  97.         DrawTextEx(font, FormatText("%01i", player1.score), player1.scorePosition, font.baseSize*1.0f, 2, player1.color);
  98.        
  99.         // Draw Player 2 HUD
  100.         DrawTextEx(font, player2Name, player2.titlePosition, font.baseSize*1.0f, 2, player2.color);
  101.         DrawTextEx(font, FormatText("%01i", player2.score), player2.scorePosition, font.baseSize*1.0f, 2, player2.color);
  102.        
  103.         // Draw the ball
  104.         DrawCircleV(ball.position, ball.size, ball.color);
  105.        
  106.         // Draw players
  107.         DrawRectangleRec(player1.rect, player1.color);
  108.         DrawRectangleRec(player2.rect, player2.color);
  109.        
  110.         EndDrawing();
  111.     }
  112.    
  113.     CloseWindow();
  114.    
  115.     return 0;
  116. }
  117.  
  118. static void InitGame(void)
  119. {
  120.    
  121.     // Initialize the ball
  122.     ball.position.x = SCREEN_WIDTH / 2;
  123.     ball.position.y = SCREEN_HEIGHT / 2;
  124.     ball.direction.x = -1.0f;
  125.     ball.direction.y = 1.0f;
  126.     ball.size = 10;
  127.     ball.speed = INITIAL_BALL_SPEED;
  128.     ball.color = WHITE;
  129.     ball.active = false;
  130.    
  131.    
  132.     // Initialize the player 1
  133.     player1.rect.x = 10;
  134.     player1.rect.y = 160;
  135.     player1.rect.width = 10;
  136.     player1.rect.height = 80;
  137.     player1.speed = INITIAL_PADDLE_SPEED;
  138.     player1.color = BLUE;
  139.     player1.active = false;
  140.     player1.titlePosition.x = (SCREEN_WIDTH / 4) - 84.5;
  141.     player1.titlePosition.y = 35.0f;
  142.     player1.scorePosition.x = (SCREEN_WIDTH / 4) - 9.5;
  143.     player1.scorePosition.y = 65.0f;
  144.     player1.score = 0;
  145.    
  146.    
  147.     // Initialize the player 2
  148.     player2.rect.x = 620;
  149.     player2.rect.y = 160;
  150.     player2.rect.width = 10;
  151.     player2.rect.height = 80;
  152.     player2.speed = INITIAL_PADDLE_SPEED;
  153.     player2.color = YELLOW;
  154.     player2.active = false;
  155.     player2.titlePosition.x = ((SCREEN_WIDTH / 4) * 3) - 84.5;
  156.     player2.titlePosition.y = 35.0f;
  157.     player2.scorePosition.x = ((SCREEN_WIDTH / 4) * 3) - 9.5;
  158.     player2.scorePosition.y = 65.0f;
  159.     player2.score = 0;
  160.    
  161.    
  162.     // Initialize the font
  163.     font = LoadSpriteFont("assets/fonts/3Dventure.ttf");
  164.    
  165. }
  166.  
  167. static void UpdateGame(void)
  168. {
  169.     // Ball movements
  170.     ball.position.x += ball.direction.x * ball.speed;
  171.     ball.position.y += ball.direction.y * ball.speed;
  172.    
  173.     // Flip when hitting top or bottom
  174.     if((ball.position.y  < (ball.size / 2)) || (ball.position.y > (SCREEN_HEIGHT - (ball.size / 1))))
  175.         ball.direction.y = -ball.direction.y;
  176.    
  177.    
  178.     // Collision between ball and paddle
  179.     if((CheckCollisionCircleRec(ball.position, ball.size, player1.rect)) || (CheckCollisionCircleRec(ball.position, ball.size, player2.rect))){
  180.         ball.direction.x = -ball.direction.x;
  181.         ball.speed *= 1.1; //
  182.     }
  183.        
  184.        
  185.     // Player 1 movements
  186.     if(IsKeyDown(KEY_UP) && player1.rect.y > 10)
  187.         player1.rect.y -= player1.speed;
  188.     if(IsKeyDown(KEY_DOWN) && player1.rect.y < SCREEN_HEIGHT - player1.rect.height - 10)
  189.         player1.rect.y += player1.speed;
  190.    
  191.    
  192.     // Player 2 movements
  193.     if(IsKeyDown(KEY_KP_8) && player2.rect.y > 10)
  194.         player2.rect.y -= player2.speed;
  195.     if(IsKeyDown(KEY_KP_2) && player2.rect.y < SCREEN_HEIGHT - player2.rect.height - 10)
  196.         player2.rect.y += player2.speed;
  197.    
  198.    
  199.     // If player 1 score
  200.     if(ball.position.x > SCREEN_WIDTH) {
  201.         player1.score++;
  202.         ball.speed = INITIAL_BALL_SPEED;
  203.         ball.position.x = SCREEN_WIDTH / 2;
  204.         ball.position.y = SCREEN_HEIGHT / 2;
  205.         ball.direction.x = -1.0f;
  206.         ball.direction.y = 1.0f;
  207.     }
  208.    
  209.     // If player 2 score
  210.     if(ball.position.x < 0) {
  211.         player2.score++;
  212.         ball.speed = INITIAL_BALL_SPEED;
  213.         ball.position.x = SCREEN_WIDTH / 2;
  214.         ball.position.y = SCREEN_HEIGHT / 2;
  215.         ball.direction.x = -1.0f;
  216.         ball.direction.y = 1.0f;
  217.     }
  218. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement