Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <Gamebuino-Meta.h>
- // This is arbitrary, feel free to mess around
- const float GRAVITY = 0.18;
- const float AIR_FRICTION = 0.05;
- float ball_pos_x = 10;
- float ball_pos_y = 10;
- float ball_speed_x = 0;
- float ball_speed_y = 0;
- uint8_t ball_size = 2;
- void setup() {
- gb.begin();
- }
- void loop() {
- while (!gb.update());
- // Inputs
- if (gb.buttons.released(BUTTON_UP)) {
- ball_speed_y -= 3.5;
- }
- else if (gb.buttons.released(BUTTON_DOWN)) {
- ball_speed_y += 3.5;
- }
- if (gb.buttons.repeat(BUTTON_RIGHT, 1)) {
- ball_speed_x += 0.2;
- }
- if (gb.buttons.repeat(BUTTON_LEFT, 1)) {
- ball_speed_x -= 0.2;
- }
- // Acceleration
- ball_speed_y += GRAVITY;
- ball_speed_x *= 1 - AIR_FRICTION;
- // Speed
- ball_pos_x += ball_speed_x;
- ball_pos_y += ball_speed_y;
- // Collitions
- if (ball_pos_x < 0) {
- ball_pos_x = 0;
- ball_speed_x *= -0.7;
- }
- if (ball_pos_x > gb.display.width() - ball_size) {
- ball_pos_x = gb.display.width() - ball_size;
- ball_speed_x *= -0.7;
- }
- if (ball_pos_y > gb.display.height() - ball_size - 2) {
- ball_pos_y = gb.display.height() - ball_size - 2;
- ball_speed_y *= -0.7;
- }
- // DRAW
- gb.display.clear();
- gb.display.fillRect(0, gb.display.height() - 2, gb.display.width(), 2);
- gb.display.fillRect((int)ball_pos_x, (int)ball_pos_y, 2, 2);
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement