Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- /*Original 'Pong' code taken from the Gamebuino libraries example sketches under the Intermediate section.
- *
- Edits made by Dale Maunder, 8/8/2017, edits add basic 1-axis joystick functionality via the I2C pins on the MAKERbuino console.
- A compatible joystick can be found at https://www.adafruit.com/product/512 or other region specific stores,
- (I personally used a Duinotech joystick module from Jaycar Electronics, Australia).
- Edits made by myself are indicated by lines of #'s surrounding the specific code, both original and added deprecated code has been commented out.
- */
- #include <SPI.h>
- #include <Gamebuino.h>
- Gamebuino gb;
- boolean paused = false;
- //player variables
- int player_score = 0;
- int player_h = 16;
- int player_w = 3;
- int player_x = 0;
- int player_y = (LCDHEIGHT-player_h)/2;
- int player_vy = 3; //Original value = 2, a higher value makes the player move faster.
- //oponent variables
- int oponent_score = 0;
- int oponent_h = 16;
- int oponent_w = 3;
- int oponent_x = LCDWIDTH-oponent_w;
- int oponent_y = (LCDHEIGHT-oponent_h)/2;
- int oponent_vy = 2;
- //ball variables
- int ball_size = 6;
- int ball_x = LCDWIDTH - ball_size - oponent_w - 1;
- int ball_y = (LCDHEIGHT-ball_size)/2;
- int ball_vx = 3;
- int ball_vy = 3;
- extern const byte font5x7[]; //get the default large font
- //############################################################
- const int JoyY_pin = A5; // Analog pin connected to joystick's Y axis output
- //const int JoyX_pin = A4; //X axis is not used but has been left in for reference
- int JoyY_pos = 0; // Variable to store position of joystick's Y axis
- //############################################################
- void setup() {
- gb.begin();
- gb.display.setFont(font5x7); //change the font to the large one
- gb.titleScreen(F("Pong Solo"));
- gb.pickRandomSeed(); //pick a different random seed each time for games to be different
- gb.battery.show = false; //hide the battery indicator
- //############################################################
- pinMode(JoyY_pin, INPUT); //Configures pin on microcontroller
- //pinMode(JoyX_pin, INPUT);
- //############################################################
- }
- void loop() {
- //############################################################
- if(analogRead(JoyY_pin) >= 700){
- JoyY_pos = 1; //Joystick is in down position
- }
- else if(analogRead(JoyY_pin) <= 600){
- JoyY_pos = 2; //Joystick is in up position
- }
- else{
- JoyY_pos = 0; //Joystick is inside dead spot at or near its home positon
- }
- //############################################################
- if(gb.update()){
- //pause the game if C is pressed
- if(gb.buttons.pressed(BTN_C)){
- gb.titleScreen(F("Pong Solo"));
- gb.battery.show = false;
- gb.display.fontSize = 2;
- }
- /*
- //original move the player
- if(gb.buttons.repeat(BTN_UP, 1)){
- player_y = max(0, player_y - player_vy);
- }
- if(gb.buttons.repeat(BTN_DOWN, 1)){
- player_y = min(LCDHEIGHT - player_h, player_y + player_vy);
- }
- */
- //############################################################
- //edited move the player
- if(JoyY_pos == 2){
- player_y = max(0, player_y - player_vy);
- }
- if(JoyY_pos == 1){
- player_y = min(LCDHEIGHT - player_h, player_y + player_vy);
- }
- //#######################################################################
- //move the ball
- ball_x = ball_x + ball_vx;
- ball_y = ball_y + ball_vy;
- //check for ball collisions
- //collision with the top border
- if(ball_y < 0){
- ball_y = 0;
- ball_vy = -ball_vy;
- gb.sound.playTick();
- }
- //collision with the bottom border
- if((ball_y + ball_size) > LCDHEIGHT){
- ball_y = LCDHEIGHT - ball_size;
- ball_vy = -ball_vy;
- gb.sound.playTick();
- }
- //collision with the player
- if(gb.collideRectRect(ball_x, ball_y, ball_size, ball_size, player_x, player_y, player_w, player_h)){
- ball_x = player_x + player_w;
- ball_vx = -ball_vx;
- gb.sound.playTick();
- }
- //collision with the oponent
- if(gb.collideRectRect(ball_x, ball_y, ball_size, ball_size, oponent_x, oponent_y, oponent_w, oponent_h)){
- ball_x = oponent_x - ball_size;
- ball_vx = -ball_vx;
- gb.sound.playTick();
- }
- //collision with the left side
- if(ball_x < 0){
- oponent_score = oponent_score + 1;
- gb.sound.playCancel();
- ball_x = LCDWIDTH - ball_size - oponent_w -1;
- ball_vx = -abs(ball_vx);
- ball_y = random(0,LCDHEIGHT-ball_size);
- }
- //collision with the right side
- if((ball_x + ball_size) > LCDWIDTH){
- player_score = player_score + 1;
- gb.sound.playOK();
- ball_x = LCDWIDTH - ball_size - oponent_w - 16; //place the ball on the oponent side
- ball_vx = -abs(ball_vx);
- ball_y = random(0,LCDHEIGHT-ball_size);
- }
- //reset score when 10 is reached
- if((player_score == 10) || (oponent_score == 10)){
- player_score = 0;
- oponent_score = 0;
- }
- //move the oponent
- if((oponent_y+(oponent_h/2)) < (ball_y+(ball_size/2))){ //if the ball is below the oponent
- oponent_y = oponent_y + oponent_vy; //move down
- oponent_y = min(LCDHEIGHT-oponent_h, oponent_y); //don't go out of the screen
- }
- else {
- oponent_y = oponent_y - oponent_vy; //move up
- oponent_y = max(0, oponent_y); //don't go out of the screen
- }
- }
- //draw the score
- gb.display.fontSize = 2;
- gb.display.cursorX = 15;
- gb.display.cursorY = 16;
- gb.display.print(player_score);
- gb.display.cursorX = 57;
- gb.display.cursorY = 16;
- gb.display.print(oponent_score);
- //draw the ball
- gb.display.fillRect(ball_x, ball_y, ball_size, ball_size);
- //draw the player
- gb.display.fillRect(player_x, player_y, player_w, player_h);
- //draw the oponent
- gb.display.fillRect(oponent_x,oponent_y, oponent_w, oponent_h);
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement