Advertisement
Guest User

Ball demo

a guest
Jun 18th, 2018
75
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 1.31 KB | None | 0 0
  1. #include <Gamebuino-Meta.h>
  2.  
  3. // This is arbitrary, feel free to mess around
  4. const float GRAVITY = 0.18;  
  5. const float AIR_FRICTION = 0.05;
  6.  
  7. float ball_pos_x = 10;
  8. float ball_pos_y = 10;
  9. float ball_speed_x = 0;
  10. float ball_speed_y = 0;
  11. uint8_t ball_size = 2;
  12.  
  13. void setup() {
  14.     gb.begin();
  15. }
  16.  
  17. void loop() {
  18.     while (!gb.update());
  19.  
  20.     // Inputs
  21.     if (gb.buttons.released(BUTTON_UP)) {
  22.         ball_speed_y -= 3.5;
  23.     }
  24.     else if (gb.buttons.released(BUTTON_DOWN)) {
  25.         ball_speed_y += 3.5;
  26.     }
  27.     if (gb.buttons.repeat(BUTTON_RIGHT, 1)) {
  28.         ball_speed_x += 0.2;
  29.     }
  30.     if (gb.buttons.repeat(BUTTON_LEFT, 1)) {
  31.         ball_speed_x -= 0.2;
  32.     }
  33.  
  34.     // Acceleration
  35.     ball_speed_y += GRAVITY;
  36.     ball_speed_x *= 1 - AIR_FRICTION;
  37.    
  38.     // Speed
  39.     ball_pos_x += ball_speed_x;
  40.     ball_pos_y += ball_speed_y;
  41.  
  42.     // Collitions
  43.     if (ball_pos_x < 0) {
  44.         ball_pos_x = 0;
  45.         ball_speed_x *= -0.7;
  46.     }
  47.     if (ball_pos_x > gb.display.width() - ball_size) {
  48.         ball_pos_x = gb.display.width() - ball_size;
  49.         ball_speed_x *= -0.7;
  50.     }
  51.     if (ball_pos_y > gb.display.height() - ball_size - 2) {
  52.         ball_pos_y = gb.display.height() - ball_size - 2;
  53.         ball_speed_y *= -0.7;
  54.     }
  55.  
  56.     // DRAW
  57.     gb.display.clear();
  58.     gb.display.fillRect(0, gb.display.height() - 2, gb.display.width(), 2);
  59.     gb.display.fillRect((int)ball_pos_x, (int)ball_pos_y, 2, 2);
  60. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement