Allegro.cc - Online Community

Allegro.cc Forums » Game Design & Concepts » Pong problems

This thread is locked; no one can reply to it. rss feed Print
Pong problems
Danex!
Member #7,759
September 2006
avatar

Hi all, i am new to allegro, just started around 3 or 4 days ago, i went through tutorials that i could find and learned sum stuff and looked at their code for pong and other games and decided to try and make my own pong clone useing what i had learned, so i set about it and got so far as to make 2 paddles and a ball. Both paddles move fine but i cannot figure out how to make them reflect the ball. i also cannot figure out how to stop the paddles from going off the map or make the ball reflect off the top and bottom walls, can anyone tell me how to set up boundaries for the walls and the paddle to reflect the ball?

CGamesPlay
Member #2,559
July 2002
avatar

Well, you have 2 problems: making the paddles stay on screen, and making the ball bounce.

First, the paddles are on the screen if their Y value is 0 or greater, right? The paddle starts at Y, and goes down to Y + height. So, make sure that both paddles always have a Y greater than or equal to 0. Also, if the bottom of the paddle, Y + height, every goes off the bottom of the screen, SCREEN_H, then you need to stop it. That means that, if you detect that Y + height of either paddle is greater than SCREEN_H, you should set that paddle's Y value to SCREEN_H - height (it's an equation: Y + height = SCREEN_H, solve for Y).

The ball should bounce off the top and bottom of the screen if its Y - radius goes under 0 (the top of the screen), or if its Y + radius goes greater than the SCREEN_H (the bottom of the screen). When you detect that that has happened, you should switch the directrion of the ball to moving the opposite direction up or down.

You can tell if the ball is hitting a paddle because the X value is equal to the paddles X value, and the ball's Y is both larger than the paddle's Y value (the ball is lower than the top of the paddle), and less than the paddle's Y value + height (the ball is higher than the bottom of the paddle). When this happens, just switch the direction that the ball is going, left or right.

--
Tomasu: Every time you read this: hugging!

Ryan Patterson - <http://cgamesplay.com/>

Danex!
Member #7,759
September 2006
avatar

cool thx i will try that and post back results
Danex!

CGamesPlay
Member #2,559
July 2002
avatar

If that's the case then you shouldn't respond to this post until after you have the results, because you can't post twice in a row ;)

--
Tomasu: Every time you read this: hugging!

Ryan Patterson - <http://cgamesplay.com/>

Danex!
Member #7,759
September 2006
avatar

1)
ok, i get that part about the paddle logic, i set the paddles to not go below 0 or above 480 with
if (paddle_1_y < 0){
paddle_1_y = 0;}
if (paddle_2_y < 0){
paddle_2_y = 0;}
if (paddle_1_y > 480){
paddle_1_y = 480;}
if (paddle_2_y > 480){
paddle_2_y = 480;}
part of it works becuase the paddle will not go below 0, but the paddle goes alittle below my screen where i can see part but part is off the screen, i think it may be a problem with my screen becuase the paddle diffinently stops and won't move any more
2) i cannot get the ball to move right???, thats my move ball function below, it also has my logic for bounceing the ball off the paddle

void move_ball(){

if (ball_x == 630) {
if (ball_y > paddle_2_y, paddle_2_y + 79 > ball_y)
{ ball_x--; }
else if(ball_x==635) {
ball_x--;
textout_ex( buffer, font, "Player 1 Wins!", 320, 240, makecol( 255, 0, 0), makecol( 0, 0, 0)); }
}

if (ball_x == 5){
if (ball_y > paddle_2_y, paddle_2_y + 79 > ball_y)
{ ball_x--; }
else if (ball_x==0){
ball_x++;
textout_ex( buffer, font, "Player 2 Wins!", 320, 240, makecol( 255, 0, 0), makecol( 0, 0, 0));
} }
circlefill( buffer, ball_x, ball_y , 5, makecol( 0,255, 0));
}

CGamesPlay
Member #2,559
July 2002
avatar

If the paddle's Y value can become 480, that means that thetop of the paddle will be the very bottom row on the screen, with the rest of the paddle off of the screen. You need to make sure that the paddle's Y value never goes below the screen height - the paddle height.

<math>y_{paddle} + h_{paddle} = h_{screen}</math>

The move_ball function will need to be written again. In order to properly handle this, you need to have 4 variables. x, y, x_speed, and y_speed. Every time the function is called, add x_speed to x and y_speed to y. To make the ball move up, set y_speed to a negative number. To make the ball move down, set y_speed to a positive number. Same applies to x_speed and moving left/right.

After you have that working properly, when the ball's y is less than 0, set y_speed = -y_speed, and when the ball's y is greater than the screen height, sey y_speed = -y_speed;

--
Tomasu: Every time you read this: hugging!

Ryan Patterson - <http://cgamesplay.com/>

Danex!
Member #7,759
September 2006
avatar

ok so first off i need to change --->
if (paddle_1_y < 0){
paddle_1_y = 0;}
if (paddle_2_y < 0){
paddle_2_y = 0;}
if (paddle_1_y > 480){
paddle_1_y = 480;}
if (paddle_2_y > 480){
paddle_2_y = 480;}
to something like --->
if (paddle_1_y + paddle_1_hight > 480){
paddle_1_y = 480;}

and second i need to scrap the whole move ball function and restart with 4 variables of x,y,x_speed,y_speed
in the second part i do not understand why that makes the ball move, am i supposed to update x_speed to update x?
Danex!
P.S. thx for all ur help and understanding that i am ignorant of allegro logic

CGamesPlay
Member #2,559
July 2002
avatar

if (paddle_1_y < 0)
{
    paddle_1_y = 0;
}
if (paddle_1_y + paddle_1_height > 480)
{
    paddle_1_y = 480 - paddle_1_height;
}

That is how it should look for 1 paddle :)

Quote:

i need to scrap the whole move ball function and restart with 4 variables of x,y,x_speed,y_speed
in the second part i do not understand why that makes the ball move, am i supposed to update x_speed to update x?

Well, if you have x_speed = 1; and every move_ball you set x = x + x_speed; then the ball will move to the right 1 pixel every time move_ball is called. If x_speed is -1, then the ball will move left 1 pixel every time move_ball is called. You can make it move faster, too. x_speed = 1 means it moves 2 pixels right each call, etc. All of this also applies to the y_speed.

Quote:

P.S. thx for all ur help and understanding that i am ignorant of allegro logic

Well, to be technical, this is just math ;)

--
Tomasu: Every time you read this: hugging!

Ryan Patterson - <http://cgamesplay.com/>

Danex!
Member #7,759
September 2006
avatar

mk i set up the boundaries for the paddle like you said and if worked perfectly
;D thanks alot, can you give kudos or karma or something on this forum?
also is it a good idea to mix primitives like
"circlefill( buffer, ball_x, ball_y , 5, makecol( 0,255, 0));"
with bitmaps, or should i just make another bitmap of a ball?
P.S. working on the moving ball and i am currently using a primative for the ball and don't want that to mess my program

CGamesPlay
Member #2,559
July 2002
avatar

Quote:

thanks alot, can you give kudos or karma or something on this forum?

If there isn't a check box that says "this question has been answered to my satisfaction", then no, there isn't. You have to specify the kind of thread as one "with a specific answer" when you make the thread.

Quote:

also is it a good idea to mix primitives like
"circlefill( buffer, ball_x, ball_y , 5, makecol( 0,255, 0));"
with bitmaps, or should i just make another bitmap of a ball?

Use whatever is easiest, because both are fine.

--
Tomasu: Every time you read this: hugging!

Ryan Patterson - <http://cgamesplay.com/>

Danex!
Member #7,759
September 2006
avatar

OK made a new move_ball()
void move_ball(){
int x_speed=1;
int y_speed=1;
ball_x += x_speed;
ball_y += y_speed;

if (ball_y < 0){
ball_y = 0;}
if (ball_y > 475){
ball_y = 475;}
if (ball_x == 5 && ball_y >= paddle_1_y && ball_y <= paddle_1_y){
x_speed = -1;
y_speed = -1; }
if (ball_x == 635 && ball_y >= paddle_2_y && ball_y <= paddle_2_y){
x_speed = -1;
y_speed = -1; }
i am gettin somewhere at least cause the ball moves down and to the right
and i tried the greator than or equal to math but i am gettin no reflection
On, the plus side i applied the same logic used in the paddles to make the ball not able to leave the map
However, the ball merely sticks to the bottom and straight lines to the end
i believe this is due to me not makeing the ball bounce with this --->
if (ball_y < 0){
ball_y = 0;}
if (ball_y > 475){
ball_y = 475;}

what should i add so that

if (ball_y < 0){
ball_y = 0;
---->switch direction of incoming ball(bounce)}
if (ball_y > 475){
ball_y = 475;
---->switch direction of incoming ball(bounce)}

CGamesPlay
Member #2,559
July 2002
avatar

Please read this

Your speed variables are local vairables. Even if you set them to -1, the will get set to 1 when you call the function again. Make them globals :)

--
Tomasu: Every time you read this: hugging!

Ryan Patterson - <http://cgamesplay.com/>

nonnus29
Member #2,606
August 2002
avatar

Quote:

Your speed variables are local vairables. Even if you set them to -1, the will get set to 1 when you call the function again. Make them globals :)

OMFG!!!!111

Stop the madness!! CGames is telling a newb to use globals!!! He's out of control, someone stop him!!!!

Danex!, when you use global variables, it makes kittens cry. :'(

Kikaru
Member #7,616
August 2006
avatar

[in scary robot voice] "Use global variables or I make you cry!" [/in scary robot voice]

CGamesPlay
Member #2,559
July 2002
avatar

In Soviet Russia, global variables use you!

--
Tomasu: Every time you read this: hugging!

Ryan Patterson - <http://cgamesplay.com/>

Danex!
Member #7,759
September 2006
avatar

Whats wrong with globals? half my coding is globals ha ha, well not half but alot
Danex!
also l o l makes I'm Dumb!...thats pretty funny
anyway i made them globals but the ball still doesn't bounce :(
the following is my entire program as u can see lots of globals

1#include <allegro.h>
2#include <cstdlib>
3#include <time.h>
4BITMAP *buffer;
5void move_ball();
6volatile long speed_counter;
7void increment_speed_counter() {
8speed_counter ++; }
9END_OF_FUNCTION(increment_speed_counter)
10int ball_x = 320;
11int ball_y = 240;
12int random;
13int paddle_1_y = 240;
14int paddle_1_height = 79;
15int paddle_2_height = 79;
16int paddle_2_y = 240;
17int paddle_1_x = 0;
18int paddle_2_x = 0;
19int x_speed=2;
20int y_speed=1;
21int main(int argc, char argv []) {
22 allegro_init();
23 install_timer();
24 LOCK_VARIABLE(speed_counter);
25 LOCK_FUNCTION(increment_speed_counter);
26 install_int_ex(increment_speed_counter,BPS_TO_TIMER(60));
27 install_keyboard();
28 set_color_depth(16);
29 set_gfx_mode(GFX_AUTODETECT,640,480,0,0);
30
31 BITMAP *paddle_1;
32 BITMAP *paddle_2;
33 paddle_1 = load_bitmap("paddle1.bmp",NULL);
34 paddle_2 = load_bitmap("paddle2.bmp",NULL);
35 buffer = create_bitmap(640,480);
36 void setup();
37 while (!key[KEY_ESC])
38 {
39
40 while (speed_counter >0)
41 {
42
43
44 if (key[KEY_LEFT]){
45 paddle_1_y ++;
46 paddle_1_y ++; }
47 else if (key[KEY_RIGHT]){
48 paddle_1_y --;
49 paddle_1_y --; }
50 if (key[KEY_A]) {
51 paddle_2_y ++;
52 paddle_2_y ++; }
53 else if (key[KEY_D]){
54 paddle_2_y --;
55 paddle_2_y --; }
56 speed_counter --;
57 if (paddle_1_y < 0){
58 paddle_1_y = 0;}
59 if (paddle_2_y < 0){
60 paddle_2_y = 0;}
61 if (paddle_1_y + paddle_1_height > 480){
62 paddle_1_y = 480 - paddle_1_height;}
63 if (paddle_2_y + paddle_2_height > 480){
64 paddle_2_y = 480 - paddle_2_height;}
65
66 }
67
68 move_ball();
69 draw_sprite(buffer, paddle_1, 0, paddle_1_y);
70 draw_sprite(buffer, paddle_2, 620, paddle_2_y);
71 blit(buffer,screen,0,0,0,0,640,480);
72 clear_bitmap(buffer); }
73destroy_bitmap(paddle_1);
74destroy_bitmap(paddle_2);
75destroy_bitmap(buffer);
76return 0;}
77END_OF_MAIN()
78 
79 
80
81void move_ball(){
82ball_x = ball_x + x_speed;
83ball_y = ball_y + y_speed;
84 
85if (ball_y < 0){
86ball_y = 0;}
87if (ball_y > 475){
88ball_y = 475;}
89if (ball_x == 5 && ball_y >= paddle_1_y && ball_y <= paddle_1_y){
90 x_speed = 1;
91 y_speed = 1; }
92if (ball_x == 635 && ball_y >= paddle_2_y && ball_y <= paddle_2_y){
93 x_speed = -1;
94 y_speed = -1; }
95if (ball_x == 640) {
96ball_x =0;
97textout_ex( screen, font, "Player 1 Wins!", 320, 240, makecol( 255, 0, 0), makecol( 0, 0, 0));
98rest(5000);
99exit(EXIT_FAILURE);
100}
101if (ball_x == 0){
102ball_x = 0;
103textout_ex( screen, font, "Player 2 Wins!", 320, 240, makecol( 255, 0, 0), makecol( 0, 0, 0));
104rest(5000);
105exit(EXIT_FAILURE);
106}
107circlefill( buffer, ball_x, ball_y , 5, makecol( 0,255, 0)); }

basically when i run it the ball move down and right then hits the wall i set up prints "Player 1 Wins!" then exits
that means i have the exit running right but even if u place the paddle in the way of the ball it doesn't do anything, and if the ball touches the way it goes on a straight path from the wall to the end
so i need something to cause the ball to change directions when it touches a wall or a paddle
Danex!

CGamesPlay
Member #2,559
July 2002
avatar

In order for the ball to bounce off the top of the screen, you need to see when the ball's y is less than 0
if(ball_y < 0)
And if it is, then make it move down instead.
y_speed = 1;

Do the opposite for downward movement.

[edit]
Fixed.

--
Tomasu: Every time you read this: hugging!

Ryan Patterson - <http://cgamesplay.com/>

Danex!
Member #7,759
September 2006
avatar

sweet it works, and you can reflect the ball and all but now it randomly just changes direction in mid screen, whats goin on there?
Danex!
[edit]
i was wrong it does not bounce off the paddle, it just randomly changed directions right before paddle so now i have dirctions errors and no bounce paddle

CGamesPlay
Member #2,559
July 2002
avatar

It shouldn't randomly bounce mid-screen if your code is what you have up there in addition to what I said.

About the ball going through paddles: you compare that the ball's y is >= the paddle's y, and that the ball's y is <= the paddle's y. This means you have to hit the very top of the paddle in order to bounce.

You should be checking that the ball's y is >= the paddle's y, and the ball's y is <= the paddle's y + height. That is, the ball needs to be in between the top of the paddle and the bottom of it.

--
Tomasu: Every time you read this: hugging!

Ryan Patterson - <http://cgamesplay.com/>

Danex!
Member #7,759
September 2006
avatar

cool that works now the paddles bounce and all and i found the problem which was the ball is not bounceing randomly, sum how paddle 2 affects the ball as far as 3/4 of the map away from itself, (its like their is a virtual paddle at mid screen that does the same thing as paddle 2) i am goin thru my code to see how this is happening, also now i have the problem of the ball always moveing in the same direction when it hits a paddle or a wall so i need to add a angle of attack sum how, any ideas or suggestions?
Danex!
[edit]
fixed the problem with the extened reach of paddle, i had a '>' sign backwards ha ha::)

Go to: