Allegro Pong Game

Started by
3 comments, last by itsdbest 16 years, 10 months ago
i've been quite bored lately so i decided to make another Pong game. the last one was pretty easy so i decideed to add acceleration and friction to my paddles/ball. unfortunatley none of my collisions so far have worked. help would be really, really, really good. #include <allegro.h> #define SCREEN_WIDTH 1400 #define SCREEN_HEIGHT 1050 BITMAP* buffer; void Move_Paddle(); void Move_Ball(); void Draw(); class Ball { public: int x; int y; int xv; int yv; int radius; }; class Paddle { public: int x; int y; int yv; int width; int length; }; Ball ball; Paddle player1; int main(int argc, char** argv[]) { allegro_init(); install_keyboard(); set_color_depth(16); set_gfx_mode(GFX_AUTODETECT, SCREEN_WIDTH, SCREEN_HEIGHT, 0, 0); buffer = create_bitmap(SCREEN_WIDTH, SCREEN_HEIGHT); ball.x = SCREEN_WIDTH/2; ball.y = SCREEN_HEIGHT/2; ball.xv = 2; ball.yv = 2; ball.radius = 20; player1.length = SCREEN_HEIGHT/6; player1.width = 20; player1.x = SCREEN_WIDTH - player1.width; player1.y = (SCREEN_HEIGHT/2) - (player1.length/2); player1.yv = 0; bool game_running = true; while(game_running == true) { if(key[KEY_ESC]) { game_running = false; } Move_Paddle(); Move_Ball(); Draw(); } return 0; } END_OF_MAIN(); void Move_Paddle() { player1.y += player1.yv; player1.yv *= 0.9; if(key[KEY_UP]) { player1.yv = -3; } else if(key[KEY_DOWN]) { player1.yv = 3; } if(key[KEY_UP] && player1.y + player1.yv + -3 != 0) { player1.yv += -3; } else if(key[KEY_DOWN] && player1.y + player1.yv + 3 != SCREEN_HEIGHT) { player1.yv += 3; } } void Move_Ball() { ball.x += ball.xv; ball.y += ball.yv; int h = 0; h++; if(h == 100) { if(ball.yv > 0) { ball.yv ++; } else if(ball.yv < 0) { ball.yv--; } if(ball.xv > 0) { ball.xv++; } else if(ball.xv < 0) { ball.xv--; } h = 0; } if(ball.x == 0 + ball.radius || ball.x == SCREEN_WIDTH - ball.radius) { ball.xv *= -1; } else if(ball.y == 0 + ball.radius || ball.y == SCREEN_HEIGHT - ball.radius) { ball.yv *= -1; } } void Draw() { clear_bitmap(buffer); rectfill(buffer, player1.x, player1.y, player1.x + player1.width, player1.y + player1.length, makecol(0, 0, 255)); circlefill(buffer, ball.x, ball.y, ball.radius, makecol(0, 255, 0)); draw_sprite(screen, buffer, 0, 0); } can anybody see any problems with my code?
Advertisement
One thing I noticed is that you could use classes more effectively. For example:

class Paddle {    private:       int x;       int y;       int yv;       int width;       int length;    public:       Move();       Draw();}

Wanna help spread Firefox? http://www.spreadfirefox.com/?q=user/register&r=126078
okay, thanks i'll change that now but do you know why my collisions aren't working?
Hey dude,

I think that this is where you're doing your collisions

if(ball.x == 0 + ball.radius || ball.x == SCREEN_WIDTH - ball.radius){    ball.xv *= -1;}    else if(ball.y == 0 + ball.radius || ball.y == SCREEN_HEIGHT - ball.radius){ball.yv *= -1;}


The first thing I notice is that you're using '==', I would definitely use < or > because it might never actually end up equalling whatever you're comparing to. So, for example,
if( (ball.x < (0 + ball.radius)) || (ball.x > (SCREEN_WIDTH - ball.radius)) )


By the way, doesn't Allegro use SCREEN_W instead of SCREEN_WIDTH?

And also, you can get the cool code windows by using (source)(/source) but replacing '(' with '['

- Goishin
There is no code for collision detection in your program.

The paddle moves out of the window, there is no collision being detected there.

You must also check when the ball will collide with the paddle. That is currently not being checked for in your program.

So keeping it simple:
1. Check for the paddle and ball remaining in the window
2. Check for the ball and paddle colliding with each other.

Decided to have a go at the code...
I removed the paddle friction, as it kinda confused me at the start...
A very basic collision detection is in my code
for more details look up http://nehe.gamedev.net/data/lessons/lesson.asp?lesson=30

Here is the sample code:
#include <allegro.h>#define SCREEN_WIDTH 640#define SCREEN_HEIGHT 480//we will have seperate scoring window and the actual game play happens in the game window#define GAME_WIDTH 640#define GAME_HEIGHT 440BITMAP* buffer;//adding a window to check the number of collisionsBITMAP* scorewindow;int hit = 0, miss = 0;void DetectnDraw();class Ball{private:       int x;       int y;       int xv;       int yv;       int radius;       friend void DetectnDraw();public:       void Move_Ball();       void Ball_Init();};class Paddle{private:       int x;       int y;       int yv;       int width;       int length;       friend void DetectnDraw();public:       void Move_Paddle();       void Paddle_Init();};Ball ball;Paddle player1;int main(int argc, char** argv[]){allegro_init();//install_timer();install_keyboard();set_color_depth(16);set_gfx_mode(GFX_AUTODETECT, SCREEN_WIDTH, SCREEN_HEIGHT, 0, 0);buffer = create_bitmap(GAME_WIDTH, GAME_HEIGHT);scorewindow = create_bitmap(GAME_WIDTH, SCREEN_HEIGHT - GAME_HEIGHT);ball.Ball_Init();player1.Paddle_Init();bool game_running = true;while(game_running == true){if(key[KEY_ESC]){game_running = false;}player1.Move_Paddle();ball.Move_Ball();DetectnDraw();}return 0;}END_OF_MAIN();void Ball::Ball_Init(){ ball.x = SCREEN_WIDTH/2; ball.y = SCREEN_HEIGHT/2; ball.xv = 2; ball.yv = 2; ball.radius = 10;}void Paddle::Paddle_Init(){     player1.length = GAME_HEIGHT/8;     player1.width = 20;     player1.x = GAME_WIDTH - player1.width;     player1.y = (GAME_HEIGHT/2) - (player1.length);     player1.yv = 0;}void Paddle::Move_Paddle(){//move the paddle only if it is within limitsif((key[KEY_UP]) && (player1.y > 0)){player1.y--;}else if((key[KEY_DOWN]) && (player1.y + player1.length < GAME_HEIGHT)){player1.y++;}}void Ball::Move_Ball(){int h = 0;h++;for (long i=1; i < 400000; i++){}    if(h == 100)    {     if(ball.yv > 0)     {     ball.yv ++;     }     else if(ball.yv < 0)     {     ball.yv--;     }     if(ball.xv > 0)     {     ball.xv++;     }     else if(ball.xv < 0)     {     ball.xv--;     }     h = 0;    }if ((ball.x < (0 + ball.radius)) || (ball.x > (GAME_WIDTH - 20 - ball.radius)) ){ball.xv *= -1;}else if((ball.y < (0 + ball.radius)) || (ball.y > (GAME_HEIGHT - ball.radius))){ball.yv *= -1;}ball.x += ball.xv;ball.y += ball.yv;//rest(0.5);}void DetectnDraw(){//dectect collision between paddle and ball codeif (ball.x + ball.radius >= GAME_WIDTH - player1.width) //ball has crossed the paddle line   {            miss++; // increase the miss counter            //check if the ball has hit the paddle, not the most perfect collison detection bit            if (player1.y < ball.y && player1.y+player1.length > ball.y)            {                          hit++;                          miss--;            }               }clear_bitmap(buffer);clear_bitmap(scorewindow);rectfill(buffer, player1.x, player1.y, player1.x + player1.width, player1.y + player1.length, makecol(0, 0, 255));circlefill(buffer, ball.x, ball.y, ball.radius, makecol(0, 255, 0));textprintf_ex(scorewindow, font, 10, 10, makecol(255, 100, 200), -1, "Hit: %d, Miss: %d", hit/3, miss/3);draw_sprite(screen, buffer, 0, 0);draw_sprite(screen, scorewindow, 0, GAME_HEIGHT);show_video_bitmap(screen);}


[Edited by - itsdbest on June 2, 2007 9:24:55 AM]

This topic is closed to new replies.

Advertisement