Jump to content
  • Advertisement
Sign in to follow this  
flump9

Need Help Making a pong game

This topic is 2344 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

Recommended Posts

I am making a pong game and i am stumbled on how to make the ball collide with stuff, but aswell as that i dont think ive done the code very well.Please can you help me? Here is my code
#include "allegro.h"

int main()
{
allegro_init();

install_keyboard();

set_color_depth(8);
set_gfx_mode(GFX_AUTODETECT_WINDOWED, 800, 600, 0, 0) ;

BITMAP *bmp = create_bitmap(800, 600);
clear_bitmap(bmp);

BITMAP *bat = load_bitmap("bat.bmp", NULL);
BITMAP *ball = load_bitmap("ball.bmp", NULL);

int y=0;
int y1=0;

while (!key [KEY_ESC])
{
if(key [KEY_UP])--y;
if(key [KEY_DOWN])++y;
if(key [KEY_W])--y1;
if(key [KEY_S])++y1;
draw_sprite(bmp, bat, 700, y);
draw_sprite(bmp, bat, 50, y1);
draw_sprite(bmp, ball, 400, 300);
blit(bmp, screen, 0, 0, 0, 0, bmp->w, bmp->h);
clear_to_color(bmp, makecol (0, 0, 0));
}
destroy_bitmap(bmp);
destroy_bitmap(bat);
destroy_bitmap(ball);
return 0;
}

END_OF_MAIN()

I've edited a bit,but now it wont run,any reasons why?
#include "allegro.h"

int y=0;
int y1=0;

BITMAP *bmp = create_bitmap(800, 600);
BITMAP *bat = load_bitmap("sprites/bat.bmp", NULL);
BITMAP *ball = load_bitmap("sprites/ball.bmp", NULL);

void p1()
{
if(key [KEY_UP])--y;
if(key [KEY_DOWN])++y;
draw_sprite(bmp, bat, 50, y);
}

void p2()
{
if(key [KEY_W])--y1;
if(key [KEY_S])++y1;
draw_sprite(bmp, bat, 700, y1);
}

void pongball()
{
draw_sprite(bmp, ball, 400, 300);
}

int main()
{
allegro_init();
install_keyboard();
set_color_depth(32);
set_gfx_mode(GFX_AUTODETECT_WINDOWED, 800, 600, 0, 0) ;

clear_bitmap(bmp);

while (!key [KEY_ESC])
{
p1();
p2();
pongball();
blit(bmp, screen, 0, 0, 0, 0, bmp->w, bmp->h);
clear_to_color(bmp, makecol (0, 0, 0));
}
destroy_bitmap(bmp);
destroy_bitmap(bat);
destroy_bitmap(ball);
return 0;
}

END_OF_MAIN()


Share this post


Link to post
Share on other sites
Advertisement
The reason why your code doesn't work is that you use variables, that are defined in the main function, in other functions.
The key variable is the one that gives errors.

For your other question about collisions:
Circle rectangle collisions

Hope this helps.

Share this post


Link to post
Share on other sites

The reason why your code doesn't work is that you use variables, that are defined in the main function, in other functions.
The key variable is the one that gives errors.

For your other question about collisions:
Circle rectangle collisions

Hope this helps.


Please can you point those out for me?

Share this post


Link to post
Share on other sites
Well in your function p1() you use the variables key, y, bmp and bat.
Y, bmp and bat don't give errors because you defined them globally.
Global variables are defined outside the main function and can be used everywhere that comes after the declaration.
So you can use them in the functions.
Key is not defined globally but in the main function that means you can only use it in the main function.
If you want to use it in a function you should pass it as a parameter.


void p1( type key ){
if( key[ UP ] )
y++
// rest of code
...
}


type is the type of you key variable. Because I don't know how allegro works I don't exactly know which type it is.
if it's an array of booleans you could pass it with: void p1( bool key [ number of elements ] ).
if you want to know more about functions : function tutorial

Share this post


Link to post
Share on other sites
Sign in to follow this  

  • Advertisement
×

Important Information

By using GameDev.net, you agree to our community Guidelines, Terms of Use, and Privacy Policy.

Participate in the game development conversation and more when you create an account on GameDev.net!

Sign me up!