We closed this forum 18 June 2010. It has served us well since 2005 as the ALPHA forum did before it from 2002 to 2005. New discussions are ongoing at the new URL http://forum.processing.org. You'll need to sign up and get a new user account. We're sorry about that inconvenience, but we think it's better in the long run. The content on this forum will remain online.
IndexProgramming Questions & HelpPrograms › Air Hockey (Pong) Game
Page Index Toggle Pages: 1
Air Hockey (Pong) Game (Read 558 times)
Air Hockey (Pong) Game
Dec 11th, 2008, 1:30am
 
I was trying to make an "air hockey" type of pong game. I am using the collusion example from the library, but I have no idea how to incorporate a "computer" player on the opposite side of the screen. Please help me in telling me what to put, and where to put it. Thanks.


// Global variables for the ball
float ball_x;
float ball_y;
float ball_dir = 1;
float ball_size = 10;  // Radius
float dy = 0;  // Direction

// Global variables for the paddle
int paddle_width = 5;
int paddle_height = 30;

int dist_wall = 7;

void setup()
{
 size(300, 300);
 rectMode(RADIUS);
 ellipseMode(RADIUS);
 noStroke();
 smooth();
 ball_y = height/2;
 ball_x = 1;
}

void draw()
{
 background(255);
 
 
 ball_x += ball_dir * 1.0;
 ball_y += dy;
 if(ball_x > width+ball_size) {
   ball_x = -width/2 - ball_size;
   ball_y = random(0, height);
   dy = 0;
 }
 
 // Constrain paddle to screen
 float paddle_y = constrain(mouseY, paddle_height, height-paddle_height);

 // Test to see if the ball is touching the paddle
 float py = width-dist_wall-paddle_width-ball_size;
 if(ball_x == py
    && ball_y > paddle_y - paddle_height - ball_size
    && ball_y < paddle_y + paddle_height + ball_size) {
   ball_dir *= -1;
   if(mouseY != pmouseY) {
     dy = (mouseY-pmouseY)/2.0;
     if(dy >  5) { dy =  5; }
     if(dy < -5) { dy = -5; }
   }
 }
 
 // If ball hits paddle or back wall, reverse direction
 if(ball_x < ball_size && ball_dir == -1) {
   ball_dir *= -1;
 }
 
 // If the ball is touching top or bottom edge, reverse direction
 if(ball_y > height-ball_size) {
   dy = dy * -1;
 }
 if(ball_y < ball_size) {
   dy = dy * -1;
 }

 // Draw ball
 fill(0);
 ellipse(ball_x, ball_y, ball_size, ball_size);
 
 // Draw the paddle
 fill(255, 0, 0);
 ellipse(width-dist_wall, paddle_y, paddle_width, paddle_height);  
}
Re: Air Hockey (Pong) Game
Reply #1 - Dec 11th, 2008, 10:17pm
 
one way to do it is to have the computer follow the ball at a set speed.  here is a very simple pong game i made a while ago. it's very unfinished, but it does illustrate this concept:

Code:

int ballx,bally,dir,pad_width,pad_height,ball_size,winner,hits;
float ball_speed,pad,Xpad;

void setup() {
size(800,600);
PFont font = loadFont("256Bytes-48.vlw");
textFont(font);
ballx=int(random(25, width-25));
bally=height/2-25;
pad=width/2-25;
Xpad=width/2-25;
dir = int(random(4));
pad_width=50;
pad_height = 10;
ball_size=50;
ball_speed = 5;
hits = 0;
}


void draw() {
update_display();
player_move();
com_move();
winner = ball_move();
println(hits);
if (winner == 1) {
text("You Win!",100,height/2) ;
}
if (winner == 2) {
text("Computer Wins!",80,height/2) ;
}
}

void update_display() {
background(0);
fill(255,50,50);
ellipse(ballx,bally,ball_size,ball_size);
fill(50,255,50);
rect(pad,height-10,pad_width,pad_height);
fill(50,50,255);
rect(Xpad,0,pad_width,pad_height);
}

void keyPressed() {
if (key == CODED) {
if (keyCode == LEFT)
pad_right();
if (keyCode == RIGHT)
pad_left();
}
}

void pad_right() {
if (pad > 0)
pad -= 5;
}

void pad_left() {
if (pad < width-pad_width)
pad += 5;
}

void com_move() {
if (ballx - ball_size/2 > Xpad) {
Xpad+=8;
}
if (ballx - ball_size/2 < Xpad)
Xpad-=8;
}

int ball_move() {
switch (dir) {
case 0:
ballx -= ball_speed;
bally -= ball_speed;
break;
case 1:
ballx += ball_speed;
bally -= ball_speed;
break;
case 2:
ballx += ball_speed;
bally += ball_speed;
break;
case 3:
ballx -= ball_speed;
bally += ball_speed;
break;
}
if (ballx <= ball_size/2){
if (dir == 0)
dir = 1;
if (dir == 3)
dir = 2;
}
if (ballx >= width - ball_size/2) {
if (dir == 1)
dir = 0;
if (dir == 2)
dir = 3;
}
if (bally - ball_size/2 <= 10) {
if ((ballx >= Xpad) && (ballx <= Xpad + pad_width)) {
if (dir == 0)
dir = 3;
if (dir == 1)
dir = 2;
}
else {
return 1;
}
hits++;
speed_change();
}
if (bally + ball_size/2 >= height - 10) {
if ((ballx >= pad) && (ballx <= pad + pad_width)) {
if (dir == 3)
dir = 0;
if (dir == 2)
dir = 1;
}
else {
return 2;
}
hits++;
speed_change();
}
return 0;
}

void speed_change() {
ball_speed += .02;
}

void player_move() {
pad = mouseX;
}
Re: Air Hockey (Pong) Game
Reply #2 - Dec 11th, 2008, 11:07pm
 
Running the program doesn't actually show anything, maybe could you isolate which part would illustrate what it is I'm looking for?
Re: Air Hockey (Pong) Game
Reply #3 - Dec 11th, 2008, 11:38pm
 
Comment out the two lines after size (with 'font' in them) and you should see the ball and paddles moving.

Beside, you were supposed to analyze the code, it isn't that long... Smiley Hint: com_move (computer move) is probably what you are looking for.
Re: Air Hockey (Pong) Game
Reply #4 - Dec 12th, 2008, 1:14am
 
PhiLho  wrote on Dec 11th, 2008, 11:38pm:
Comment out the two lines after size (with 'font' in them) and you should see the ball and paddles moving.


Ooops! Sorry about that. You must not have that font. And thanks Philho. We make a good team. Smiley
Re: Air Hockey (Pong) Game
Reply #5 - Dec 12th, 2008, 2:55am
 
hey guys thanks for the input. my code is complete, except for one more thing. I would like to add a score count, and instead of using text() I'm just using a println()  function. Is there any way to make processing keep score? thanks =)
Re: Air Hockey (Pong) Game
Reply #6 - Dec 12th, 2008, 8:35am
 
sure! use 2 integers, one for player score one for computer score. increment the score on a win.

to print:

Code:

println("Your Score: " + player_score);
println("Computer's Score: " + computer_score);
Re: Air Hockey (Pong) Game
Reply #7 - Dec 16th, 2008, 3:31am
 
is it possible for someone to add comments on to the code below so i can understand what it means? I'm pretty much a noob at this stuff, and I have to be able to explain it to someone next week and I want to know it!!! thanks :)

case 0:
   ballx -= ball_speed;
   bally -= ball_speed;
   break;
 case 1:
   ballx += ball_speed;
   bally -= ball_speed;
   break;
 case 2:
   ballx += ball_speed;
   bally += ball_speed;
   break;
 case 3:
   ballx -= ball_speed;
   bally += ball_speed;
   break;
 }
 if (ballx <= ball_size/2){
   if (dir == 0)
dir = 1;
   if (dir == 3)
dir = 2;
 }
 if (ballx >= width - ball_size/2) {
   if (dir == 1)
dir = 0;
   if (dir == 2)
dir = 3;
 }
 if (bally - ball_size/2 <= 10) {
   if ((ballx >= Xpad) && (ballx <= Xpad + pad_width)) {
if (dir == 0)
  dir = 3;
if (dir == 1)
  dir = 2;
   }  
   else {
return 1;
   }
   hits++;
   speed_change();
 }
 if (bally + ball_size/2 >= height - 10) {
   if ((ballx >= pad) && (ballx <= pad + pad_width)) {
if (dir == 3)
  dir = 0;
if (dir == 2)
  dir = 1;
   }
   else {
return 2;  
   }
   hits++;
   speed_change();
 }
 return 0;
}
Re: Air Hockey (Pong) Game
Reply #8 - Dec 16th, 2008, 8:18am
 
sorry. once you've been programming a while code starts to read like english, and you forget that it might not be so obvious to other people.

Code:

//this function moves the ball to a new location
//returns 1 if player won, returns 2 if computer won
int ball_move() {
//the ball can move in 1 of 4 directions
switch (dir) {

//up and to the left
case 0:
ballx -= ball_speed;
bally -= ball_speed;
break;

//up and to the right
case 1:
ballx += ball_speed;
bally -= ball_speed;
break;

//down and to the right
case 2:
ballx += ball_speed;
bally += ball_speed;
break;

//down and to the left
case 3:
ballx -= ball_speed;
bally += ball_speed;
break;
}

//if the ball is against the left wall
if (ballx <= ball_size/2){
//if it's going up and to the left, set it up and to the right
if (dir == 0)
dir = 1;
//if it's going down and to the left, set it down and to the right
if (dir == 3)
dir = 2;
}
//if the ball is against the right wall
if (ballx >= width - ball_size/2) {
//if it's going up and to the right, set it to up and to the left
if (dir == 1)
dir = 0;
//if it's going down and the right, set it to down and to the left
if (dir == 2)
dir = 3;
}
//if the ball is at the paddle's y position
if (bally - ball_size/2 <= 10) {
//if it's at the paddle's x position, change the direction
if ((ballx >= Xpad) && (ballx <= Xpad + pad_width)) {
if (dir == 0)
dir = 3;
if (dir == 1)
dir = 2;
}
//if it's not at the paddle's y, game over
else {
return 1;
}
//make the ball faster
hits++;
speed_change();
}
//same as above but for other paddle
if (bally + ball_size/2 >= height - 10) {
if ((ballx >= pad) && (ballx <= pad + pad_width)) {
if (dir == 3)
dir = 0;
if (dir == 2)
dir = 1;
}
else {
return 2;
}
hits++;
speed_change();
}
return 0;
}


if you don't know what a switch case is, look it up.
Re: Air Hockey (Pong) Game
Reply #9 - Dec 16th, 2008, 5:28pm
 
thanks so much!!!
Page Index Toggle Pages: 1