Jump to content

  • Log In with Google      Sign In   
  • Create Account


Need Help Making a pong game


Old topic!
Guest, the last post of this topic is over 60 days old and at this point you may not reply in this topic. If you wish to continue this conversation start a new topic.

  • You cannot reply to this topic
3 replies to this topic

#1 Flump9   Members   -  Reputation: 100

Like
0Likes
Like

Posted 20 December 2011 - 06:14 AM

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()



Sponsor:

#2 Sepii   Members   -  Reputation: 200

Like
0Likes
Like

Posted 21 December 2011 - 06:00 AM

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.

#3 Flump9   Members   -  Reputation: 100

Like
0Likes
Like

Posted 21 December 2011 - 02:48 PM

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?

#4 Sepii   Members   -  Reputation: 200

Like
0Likes
Like

Posted 22 December 2011 - 07:19 AM

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




Old topic!
Guest, the last post of this topic is over 60 days old and at this point you may not reply in this topic. If you wish to continue this conversation start a new topic.



PARTNERS