0% found this document useful (0 votes)
91 views

Pong Source

1) This document contains the code for an Arduino Pong game. It defines constants for the paddle and ball positions and movement. 2) In the main loop, it checks the input, draws the game screen, handles ball movement and collisions with paddles/walls, and checks for scoring. 3) Game states include a menu, the game being played in two parts (drawing static and dynamic elements), and a game over screen. Scores are tracked and it checks if a player has won.

Uploaded by

Riley Lannon
Copyright
© Attribution Non-Commercial (BY-NC)
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as TXT, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
91 views

Pong Source

1) This document contains the code for an Arduino Pong game. It defines constants for the paddle and ball positions and movement. 2) In the main loop, it checks the input, draws the game screen, handles ball movement and collisions with paddles/walls, and checks for scoring. 3) Game states include a menu, the game being played in two parts (drawing static and dynamic elements), and a game over screen. Scores are tracked and it checks if a player has won.

Uploaded by

Riley Lannon
Copyright
© Attribution Non-Commercial (BY-NC)
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as TXT, PDF, TXT or read online on Scribd
You are on page 1/ 5

//ARDUINO PONG //2013 EnArch GDT #include <TVout.

h> #define WHEEL_ONE_PIN 0 //analog #define WHEEL_TWO_PIN 1 //analog //#define BUTTON_ONE_PIN 2 //digital to start game, not utilized at the moment // #define BUTTON_TWO_PIN 3 //digital to reset and go back to main menu #define PADDLE_HEIGHT 14 #define PADDLE_WIDTH 1 #define RIGHT_PADDLE_X (TV.horz_res()-4) #define LEFT_PADDLE_X 2 #define IN_GAMEA 0 //in game state - draw constants of the game box #define IN_GAMEB 0 //in game state - draw the dynamic part of the game #define IN_MENU 1 //in menu state #define GAME_OVER 2 //game over state #define LEFT_SCORE_X (TV.horz_res()/2-15) #define RIGHT_SCORE_X (TV.horz_res()/2+10) #define SCORE_Y 4 #define MAX_Y_VELOCITY 6 #define PLAY_TO 7 #define LEFT 0 #define RIGHT 1 TVout TV; unsigned char x,y; int led = 13; int button1Status = 0; // boolean button2Status = false; int wheelOnePosition = 0; int wheelTwoPosition = 0; int rightPaddleY = 0; int leftPaddleY = 0; unsigned char ballX = 0; unsigned char ballY = 0; char ballVolX = 2; char ballVolY = 2; int leftPlayerScore = 0; int rightPlayerScore = 0; int frame = 0; int state = IN_MENU; void processInputs() { wheelOnePosition = analogRead(WHEEL_ONE_PIN); // delay(50); wheelTwoPosition = analogRead(WHEEL_TWO_PIN); // delay(50);

//button1Status = analogRead(BUTTON_ONE_PIN); // button2Status = (digitalRead(BUTTON_TWO_PIN) == LOW); // button1Status = (digitalRead(BUTTON_ONE_PIN));

//cdubois //Serial.println(BUTTON_ONE_PIN); //Serial.println(BUTTON_TWO_PIN); // delay(50); //Serial.println(button1Status); // Serial.println(button2Status); //Serial.println(wheelOnePosition); //Serial.println(wheelTwoPosition); // delay(1000); } void drawGameScreen() { //TV.clear_screen(); //draw right paddle rightPaddleY = ((wheelOnePosition /8) * (TV.vert_res()-PADDLE_HEIGHT))/ 128; x = RIGHT_PADDLE_X; for(int i=0; i<PADDLE_WIDTH; i++) { TV.draw_line(x+i,rightPaddleY,x+i,rightPaddleY+PADDLE_HEIGHT,1); } //draw left paddle leftPaddleY = ((wheelTwoPosition /8) * (TV.vert_res()-PADDLE_HEIGHT))/ 128; x = LEFT_PADDLE_X; for(int i=0; i<PADDLE_WIDTH; i++) { TV.draw_line(x+i,leftPaddleY,x+i,leftPaddleY+PADDLE_HEIGHT,1); } //draw score TV.print_char(LEFT_SCORE_X,SCORE_Y,'0'+leftPlayerScore); TV.print_char(RIGHT_SCORE_X,SCORE_Y,'0'+rightPlayerScore); //draw ball TV.set_pixel(ballX, ballY, 2); } //player == LEFT or RIGHT void playerScored(byte player) { if(player == LEFT) leftPlayerScore++; if(player == RIGHT) rightPlayerScore++; //check for win if(leftPlayerScore == PLAY_TO || rightPlayerScore == PLAY_TO) { state = GAME_OVER; } ballVolX = -ballVolX; }

void drawBox() { TV.clear_screen(); //draw net for(int i=1; i<TV.vert_res() - 4; i+=6) { TV.draw_line(TV.horz_res()/2,i,TV.horz_res()/2,i+3,1); } // had to make box a bit smaller to fit tv TV.draw_line(0, 0, 0,95,1 ); // left TV.draw_line(0, 0, 126,0,1 ); // top TV.draw_line(126, 0, 126,95,1 ); // right TV.draw_line(0, 95, 126,95,1 ); // bottom state = IN_GAMEB; } void drawMenu() { x = 0; y = 0; char volX =3; char volY = 3; TV.clear_screen(); TV.select_font(_8X8); TV.print_str(10, 5, "Arduino Pong"); TV.select_font(_5X7); TV.print_str(22, 35, "Please Wait"); TV.print_str(30, 45, "To Start"); delay(10000); TV.clear_screen(); //while(button1Status<500) { processInputs(); TV.delay_frame(3); if(x + volX < 1 || x + volX > TV.horz_res() - 1) volX = -volX; if(y + volY < 1 || y + volY > TV.vert_res() - 1) volY = -volY; if(TV.get_pixel(x + volX, y + volY)) { TV.set_pixel(x + volX, y + volY, 0); if(TV.get_pixel(x + volX, y - volY) == 0) { volY = -volY; } else if(TV.get_pixel(x - volX, y + volY) == 0) { volX = -volX; } else { volX = -volX; volY = -volY; } } TV.set_pixel(x, y, 0); x += volX; y += volY; TV.set_pixel(x, y, 1); // }

TV.select_font(_5X7); state = IN_GAMEA;

} void setup() { Serial.begin(9600); x=0; y=0; TV.start_render(_NTSC); in(_NTSC,128,56) ballX = TV.horz_res() / 2; ballY = TV.vert_res() / 2; // pinMode(BUTTON_ONE_PIN, INPUT); } void loop() { processInputs(); if(state == IN_MENU) { drawMenu(); digitalWrite(led, LOW); } if(state == IN_GAMEA) { drawBox(); digitalWrite(led, HIGH); } if(state == IN_GAMEB) { if(frame % 3 == 0) { //every third frame ballX += ballVolX; ballY += ballVolY; digitalWrite(led, HIGH); // change if hit top or bottom if(ballY <= 1 || ballY >= TV.vert_res()-1) { ballVolY = -ballVolY; delay(100); TV.tone( 2000,50 ); } // test left side for wall hit if(ballVolX < 0 && ballX == LEFT_PADDLE_X+PADDLE_WIDTH-1 && ballY >= leftP addleY && ballY <= leftPaddleY + PADDLE_HEIGHT) { ballVolX = -ballVolX; ballVolY += 2 * ((ballY - leftPaddleY) - (PADDLE_HEIGHT / 2)) / (PADDLE_ HEIGHT / 2); delay(100); TV.tone(2000,50 ); } // test right side for wall hit if(ballVolX > 0 && ballX == RIGHT_PADDLE_X && ballY >= rightPaddleY && bal lY <= rightPaddleY + PADDLE_HEIGHT) { ballVolX = -ballVolX; ballVolY += 2 * ((ballY - rightPaddleY) - (PADDLE_HEIGHT / 2)) / (PADDLE _HEIGHT / 2); delay(100); TV.tone( 2000,50 ); } // sets the digital pin as output

//for devices with only 1k sram(m168) use TV.beg

//limit vertical speed if(ballVolY > MAX_Y_VELOCITY) ballVolY = MAX_Y_VELOCITY; if(ballVolY < -MAX_Y_VELOCITY) ballVolY = -MAX_Y_VELOCITY; // Scoring if(ballX <= 1) { playerScored(RIGHT); // sound delay(1000); TV.tone( 500,500 ); } if(ballX >= TV.horz_res() - 1) { playerScored(LEFT); // sound delay(1000); TV.tone( 500,500 ); } } // } if(state == GAME_OVER) { drawGameScreen(); TV.select_font(_8X8); TV.print_str(29,25,"GAME"); TV.print_str(68,25,"OVER"); TV.select_font(_5X7); TV.print_str(32,50,"Reset"); TV.print_str(70,50,"Game"); /* } TV.select_font(_5X7); //reset the font //reset the scores leftPlayerScore = 0; rightPlayerScore = 0; state = IN_MENU; */ digitalWrite(led, LOW); } TV.delay_frame(1); if(++frame == 60) frame = 0; //increment and/or reset frame counter } if(button1Status) Serial.println((int)ballVolX); drawGameScreen();

You might also like