Advertisement
Guest User

Untitled

a guest
Nov 18th, 2019
119
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 5.40 KB | None | 0 0
  1. #include "raylib.h"
  2. #include <stdio.h>
  3. #include <stdlib.h>
  4.  
  5. int main(void)
  6. {
  7. // Initialization
  8. //--------------------------------------------------------------------------------------
  9. const int screenWidth = 1280;
  10. const int screenHeight = 720;
  11. const int velocidady = 8;
  12. const int ballSize = 25;
  13. const int maxVelocity = 20;
  14. const int minVelocity = 8;
  15.  
  16. bool pause = false;
  17.  
  18. int score1p = 0;
  19. int score2p = 0;
  20.  
  21. Rectangle paladerecha;
  22.  
  23. paladerecha.width = 20;
  24. paladerecha.height = 100;
  25. paladerecha.x = screenWidth - 50 - paladerecha.width;
  26. paladerecha.y = screenHeight/2 - paladerecha.height/2;
  27.  
  28.  
  29. Rectangle palaizquierda;
  30.  
  31. palaizquierda.width = 20;
  32. palaizquierda.height = 100;
  33. palaizquierda.x = 50;
  34. palaizquierda.y = screenHeight/2 - palaizquierda.height/2;
  35.  
  36.  
  37. Vector2 ball;
  38. ball.x = screenWidth/2;
  39. ball.y = screenHeight/2;
  40.  
  41. Vector2 ballVelocity;
  42. ballVelocity.x = minVelocity;
  43. ballVelocity.y = minVelocity;
  44.  
  45. InitWindow(screenWidth, screenHeight, "raylib [core] example - basic window");
  46.  
  47. SetTargetFPS(60); // Set our game to run at 60 frames-per-second
  48. //--------------------------------------------------------------------------------------
  49.  
  50. // Main game loop
  51. while (!WindowShouldClose()) // Detect window close button or ESC key
  52. {
  53. // Update
  54. //----------------------------------------------------------------------------------
  55. // Gestiono pulsaciones botones
  56. if(!pause){
  57. if (IsKeyDown(KEY_Q)){
  58. palaizquierda.y -= velocidady;
  59. }
  60.  
  61. if (IsKeyDown(KEY_A)){
  62. palaizquierda.y += velocidady;
  63. }
  64.  
  65. if (IsKeyDown(KEY_UP)){
  66. paladerecha.y -= velocidady;
  67. }
  68.  
  69. if (IsKeyDown(KEY_DOWN)){
  70. paladerecha.y += velocidady;
  71. }
  72. }
  73.  
  74. if (IsKeyPressed(KEY_P)){
  75. pause = !pause;
  76. }
  77.  
  78. //Consulto Limites
  79. if(palaizquierda.y<0){
  80. palaizquierda.y = 0;
  81. }
  82.  
  83. if(palaizquierda.y > (screenHeight - palaizquierda.height)){
  84. palaizquierda.y = screenHeight - palaizquierda.height;
  85. }
  86.  
  87. if(paladerecha.y<0){
  88. paladerecha.y = 0;
  89. }
  90.  
  91. if(paladerecha.y > (screenHeight - paladerecha.height)){
  92. paladerecha.y = screenHeight - paladerecha.height;
  93. }
  94.  
  95. //Gestionamos el movimiento de la Bola
  96. if(!pause){
  97. ball.x += ballVelocity.x;
  98. ball.y += ballVelocity.y;
  99. }
  100.  
  101. //Aqui marcamos gol
  102. if(ball.x > screenWidth - ballSize){
  103. //Marca la pala izquierda
  104. score1p++;
  105. ball.x = screenWidth/2;
  106. ball.y = screenHeight/2;
  107. ballVelocity.x = -minVelocity;
  108. ballVelocity.y = minVelocity;
  109.  
  110. }else if(ball.x < ballSize){
  111. //Marca la pala derecha
  112. score2p++;
  113. ball.x = screenWidth/2;
  114. ball.y = screenHeight/2;
  115. ballVelocity.x = minVelocity;
  116. ballVelocity.y = minVelocity;
  117. }
  118.  
  119. if((ball.y > screenHeight - ballSize) || (ball.y < ballSize) ){
  120. ballVelocity.y *=-1;
  121. }
  122.  
  123.  
  124. if(CheckCollisionCircleRec(ball, ballSize, paladerecha)){
  125. if(ballVelocity.x>0){
  126. if(abs(ballVelocity.x)<maxVelocity){
  127. ballVelocity.x *=-1.5;
  128. ballVelocity.y *= 1.5;
  129. }else{
  130. ballVelocity.x *=-1;
  131. }
  132. }
  133. }
  134.  
  135. if(CheckCollisionCircleRec(ball, ballSize, palaizquierda)){
  136. if(ballVelocity.x<0){
  137. if(abs(ballVelocity.x)<maxVelocity){
  138. ballVelocity.x *=-1.5;
  139. ballVelocity.y *= 1.5;
  140. }else{
  141. ballVelocity.x *=-1;
  142. }
  143. }
  144. }
  145.  
  146. //----------------------------------------------------------------------------------
  147.  
  148. // Draw
  149. //----------------------------------------------------------------------------------
  150. BeginDrawing();
  151.  
  152. ClearBackground(BLUE);
  153.  
  154. // Pinto Pala Derecha
  155. DrawRectangleRec(paladerecha, GREEN);
  156.  
  157. // Pinto Pala Izquierda
  158. DrawRectangleRec(palaizquierda, GREEN);
  159.  
  160. // Pintamos la pelota
  161. DrawCircleV(ball, ballSize, GREEN);
  162.  
  163. // Texto ejemplo
  164. DrawText(FormatText("SCORE 1P: %d",score1p), 10, 10, 40, RED);
  165. DrawText(FormatText("SCORE 2P: %d",score2p), screenWidth - 300, 10, 40, RED);
  166.  
  167. EndDrawing();
  168. //----------------------------------------------------------------------------------
  169. }
  170.  
  171. // De-Initialization
  172. //--------------------------------------------------------------------------------------
  173. CloseWindow(); // Close window and OpenGL context
  174. //--------------------------------------------------------------------------------------
  175.  
  176. return 0;
  177. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement