Go Down

Topic: Arduino Pong (Read 56980 times) previous topic - next topic

cybertim

i made a version with the use of three buttons that can be made from some foil; check the playground for more info; you will need this library:
http://www.arduino.cc/playground/Main/CapSense

code:
(only one control for both paddles; need to upgrade the code if you want two independent controls)

Code: [Select]
#include <TVout.h>
#include <CapSense.h>

#define PADDLE_HEIGHT 12
#define PADDLE_WIDTH 2

#define RIGHT_PADDLE_X (TV.horz_res()-4)
#define LEFT_PADDLE_X 2

#define IN_GAME 0 //in game state
#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 3
#define PLAY_TO 7

#define LEFT 0
#define RIGHT 1

CapSense rightButton = CapSense(5,4);        // 10M resistor between pins 4 & 2, pin 2 is sensor pin, add a wire and or foil if desired
CapSense leftButton = CapSense(5,3);        // 10M resistor between pins 4 & 6, pin 6 is sensor pin, add a wire and or foil
CapSense startButton = CapSense(5,2);        // 10M resistor between pins 4 & 8, pin 8 is sensor pin, add a wire and or foil

TVout TV;
unsigned char x,y;

boolean buttonStatus = false;

int wheelOnePosition = 0;
int wheelTwoPosition = 0;
int rightPaddleY = 0;
int leftPaddleY = 0;

unsigned char ballX = 0;
unsigned char ballY = 0;
char ballVolX = 1;
char ballVolY = 1;

int leftPlayerScore = 0;
int rightPlayerScore = 0;

int frame = 0;
int state = IN_MENU;

int basicInput = 50;

void processInputs() {
 long total1 =  startButton.capSense(30);
 if(total1 > basicInput) {
   buttonStatus = true;
 } else {
   buttonStatus = false;
 }
 
 long total2 =  leftButton.capSense(30);
 long total3 =  rightButton.capSense(30);

 if(total2 > basicInput) {
   wheelOnePosition = wheelOnePosition + 3;
   wheelTwoPosition = wheelTwoPosition + 3;
 }

 if(total3 > basicInput) {
   wheelOnePosition = wheelOnePosition - 3;
   wheelTwoPosition = wheelTwoPosition - 3;
 }

leftPaddleY = wheelOnePosition;
if(leftPaddleY < 0) {
 wheelOnePosition = leftPaddleY = 0;
}
if(leftPaddleY > TV.vert_res()-PADDLE_HEIGHT) {
 wheelOnePosition = leftPaddleY = TV.vert_res()-PADDLE_HEIGHT;
}

rightPaddleY = wheelTwoPosition;
if(rightPaddleY < 0) {
 wheelTwoPosition = rightPaddleY = 0;
}
if(rightPaddleY > TV.vert_res()-PADDLE_HEIGHT) {
 wheelTwoPosition = rightPaddleY = TV.vert_res()-PADDLE_HEIGHT;
}


   Serial.print(total1);                  // print sensor output 1
   Serial.print("\t");
   Serial.print(total2);                  // print sensor output 2
   Serial.print("\t");
   Serial.println(total3);                // print sensor output 3

}

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 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);
 }

 //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 drawMenu() {
 x = 0;
 y = 0;
 char volX = 1;
 char volY = 1;
 TV.clear_screen();
 TV.select_font(_8X8);
 TV.print_str(10, 5, "Arduino Pong");
 TV.select_font(_5X7);
 TV.print_str(22, 35, "Press Button");
 TV.print_str(50, 45, "To Start");
 delay(1000);
 while(!buttonStatus) {
   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_GAME;
}

void setup() {
 Serial.begin(9600);
 x=0;
 y=0;
 TV.start_render(_PAL);//for devices with only 1k sram(m168) use TV.begin(_NTSC,128,56)

 ballX = TV.horz_res() / 2;
 ballY = TV.vert_res() / 2;
}

void loop() {
 processInputs();

 if(state == IN_MENU) {
   drawMenu();
 }
 if(state == IN_GAME) {
   if(frame % 3 == 0) { //every third frame
     ballX += ballVolX;
     ballY += ballVolY;

     if(ballY <= 1 || ballY >= TV.vert_res()-1) ballVolY = -ballVolY;
     if(ballVolX < 0 && ballX == LEFT_PADDLE_X+PADDLE_WIDTH-1 && ballY >= leftPaddleY && ballY <= leftPaddleY + PADDLE_HEIGHT) {
       ballVolX = -ballVolX;
       ballVolY += 2 * ((ballY - leftPaddleY) - (PADDLE_HEIGHT / 2)) / (PADDLE_HEIGHT / 2);
     }
     if(ballVolX > 0 && ballX == RIGHT_PADDLE_X && ballY >= rightPaddleY && ballY <= rightPaddleY + PADDLE_HEIGHT) {
       ballVolX = -ballVolX;
       ballVolY += 2 * ((ballY - rightPaddleY) - (PADDLE_HEIGHT / 2)) / (PADDLE_HEIGHT / 2);
     }

     //limit vertical speed
     if(ballVolY > MAX_Y_VELOCITY) ballVolY = MAX_Y_VELOCITY;
     if(ballVolY < -MAX_Y_VELOCITY) ballVolY = -MAX_Y_VELOCITY;

     if(ballX <= 1) {
       playerScored(RIGHT);
     }
     if(ballX >= TV.horz_res() - 1) {
       playerScored(LEFT);
     }
   }
   if(buttonStatus) Serial.println((int)ballVolX);

   drawGameScreen();
 }
 if(state == GAME_OVER) {
   drawGameScreen();
   TV.select_font(_8X8);
   TV.print_str(29,25,"GAME");
   TV.print_str(68,25,"OVER");
   while(!buttonStatus) {
     processInputs();
     delay(50);
   }
   TV.select_font(_5X7); //reset the font
   //reset the scores
   leftPlayerScore = 0;
   rightPlayerScore = 0;
   state = IN_MENU;
 }

 TV.delay_frame(1);
 if(++frame == 60) frame = 0; //increment and/or reset frame counter
}


pic:

robot797

do you need a kristal for this
(build in 8000hz kristal)
becaus i have an old vieuw vinder that this would be awsome for

inbead this in the vieuw finder

but is this duo player or single?

Go Up
 


Please enter a valid email to subscribe

Confirm your email address

We need to confirm your email address.
To complete the subscription, please click the link in the email we just sent you.

Thank you for subscribing!

Arduino
via Egeo 16
Torino, 10131
Italy