Jump to content
  • Advertisement
Sign in to follow this  
Breander

My first game!! PONG!! lol

This topic is 4611 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

Hi this is my first post on these forums but I've been around for about a month just reading posts. Well anyways I wanted to share the first game I've ever made that I just finished and to get peoples feed back on it. I used C and the Allegro lib using DevC++. This is a really simple game of pong there are no sounds or anything fancy. There is really no end game but there is a scoring function. I have been learning Allegro from Game Programing All In One by Jonathan S. Harbour. Download I think you need to take the allegro42.dll file and place it in your windows/win32 directory in order for it to work. Also controls are: Padle 1 Left and Right arrow keys Padle 2 A and D keys Here is the source files for the game. pong.h file
#ifndef _PONG_H
#define _PONG_H

#include "allegro.h"

//define game constants
#define MODE GFX_AUTODETECT_WINDOWED
#define WIDTH 640
#define HEIGHT 480
#define ORANGE makecol(255,189,73)

//define paddles structure
struct tagPaddle
{
       int x, y, x1, y1, score;
} paddles[2];

struct tagBall
{
       int x, y, xdir, ydir, radius, alive;
} balls[2];


//function prototypes
void drawpaddle(int num);
void erasepaddle(int num);
void setuppaddle();
void movepaddle(int num);
void updateball(int num);
void setupball();

#endif

main.c
#include "pong.h"

void drawpaddle(int num)
{
     int x = paddles[num].x;
     int y = paddles[num].y;
     int x1 = paddles[num].x1;
     int y1 = paddles[num].y1;
     
     rectfill(screen, x, y, x1, y1, ORANGE);
}

void erasepaddle(int num)
{
     int x = paddles[num].x;
     int y = paddles[num].y;
     int x1 = paddles[num].x1;
     int y1 = paddles[num].y1;
     
     rectfill(screen, x, y, x1, y1, 0);
}

void setuppaddle()
{
     //player 1
     paddles[0].x = 400;
     paddles[0].y = 450;
     paddles[0].x1 = 450;
     paddles[0].y1 = 460;
     paddles[0].score = 0;
     
     //player 2
     paddles[1].x = 50;
     paddles[1].y = 20;
     paddles[1].x1 = 100;
     paddles[1].y1 = 30;
     paddles[1].score = 0;
}

void setupball()
{
     balls[0].x = 300;
     balls[0].y = 250;
     balls[0].radius = 5;
     balls[0].xdir = 5;
     balls[0].ydir = 5;
     balls[0].alive = 1;
}

void movepaddle(int num)
{
         //player 1
         if (key[KEY_LEFT]){
            if (paddles[0].x == WIDTH - WIDTH + 10 || paddles[0].x < WIDTH - WIDTH +10){
                  paddles[0].x = paddles[0].x;
                  paddles[0].x1 = paddles[0].x1;
            }
            else{
               paddles[0].x = paddles[0].x - 3;
               paddles[0].x1 = paddles[0].x1 - 3;
            }
         }
         
         if (key[KEY_RIGHT]){
            if (paddles[0].x1 == WIDTH - 10 || paddles[0].x1 > WIDTH - 10){
               paddles[0].x = paddles[0].x;
               paddles[0].x1 = paddles[0].x1;
            }
            else{
                 paddles[0].x = paddles[0].x + 3;
                 paddles[0].x1 = paddles[0].x1 + 3;
            }
         }
         
         //player 2
         if (key[KEY_A]){
            if (paddles[1].x == WIDTH - WIDTH + 10 || paddles[1].x < WIDTH - WIDTH +10){
                  paddles[1].x = paddles[1].x;
                  paddles[1].x1 = paddles[1].x1;
            }
            else{
               paddles[1].x = paddles[1].x - 3;
               paddles[1].x1 = paddles[1].x1 - 3;
            }
         }
         
         if (key[KEY_D]){
            if (paddles[1].x1 == WIDTH - 10 || paddles[1].x1 > WIDTH - 10){
               paddles[1].x = paddles[1].x;
               paddles[1].x1 = paddles[1].x1;
            }
            else{
                 paddles[1].x = paddles[1].x + 3;
                 paddles[1].x1 = paddles[1].x1 + 3;
            }
         }
}

void updateball(int num)
{
     int x = balls[num].x;
     int y = balls[num].y;
     int radius = balls[num].radius;
     int xdir = balls[num].xdir;
     int ydir = balls[num].ydir;
     
     //keep within screen
     
     if (x > WIDTH-radius)
     {
            balls[num].xdir = -5;
            balls[num].x = WIDTH-radius;
     }
     if (x < radius)
     {
            balls[num].xdir = 5;
            balls[num].x = radius;
     }

     //reset ball if past paddles and update player scores
     
     if (y <= paddles[1].y +8)
     {
            balls[num].alive = 0;
            paddles[0].score++;
     }
     if (y >= paddles[0].y1 -8)
     {
            balls[num].alive = 0;
            paddles[1].score++;
     }
     //update x and y position
     balls[num].x += xdir;
     balls[num].y += ydir;
     
     //check to see if ball has hit player one
     if (x > paddles[0].x && x < paddles[0].x1-25 && y > paddles[0].y-15)
     {
           balls[num].xdir = -5;
           balls[num].ydir = -5;
     }
     if (x > paddles[0].x+25 && x < paddles[0].x1 && y > paddles[0].y-15)
     {
              balls[num].xdir = 5;
              balls[num].ydir = -5;
     }
        
     //check to see if ball has hit player two
     if (x > paddles[1].x && x < paddles[1].x1-25 && y < paddles[1].y1+15)
     {
           balls[num].xdir = -5;
           balls[num].ydir = 5;
     }
     if (x > paddles[1].x+25 && x < paddles[1].x1 && y < paddles[1].y1+15)
     {
              balls[num].xdir = 5;
              balls[num].ydir = 5;
     }
     //see if ball is alive
     if (balls[num].alive == 0)
     {
         balls[num].alive = 1;
         balls[num].x = 300;
         balls[num].y = 250;
     }
         //draw ball
         circlefill(screen, x, y, radius, 5);
         rest(20);
         //erase ball
         circlefill(screen, x, y, radius, 0);
}


void main()
{

     
     //initialize allegro, keyboard, screeen res
     allegro_init();
     install_keyboard();
     
     int ret = set_gfx_mode(MODE, WIDTH, HEIGHT, 0, 0);
     if (ret != 0) {
             allegro_message(allegro_error);
             return;
     }
     
     
     
     setuppaddle();
     setupball();
     
     while(!key[KEY_ESC])
     {
         //initialize player scores
         int p1score = paddles[0].score;
         int p2score = paddles[1].score;
         textprintf_ex(screen, font, 0, 0, 15, 0, "PONG!! ----- PRESS ESC to exit --- score: P1: %i P2: %i", p1score, p2score);
         
         //erase paddles
         erasepaddle(0);
         erasepaddle(1);
         
         //move paddles
         movepaddle(0);
         movepaddle(1);
         
         //draw paddles
         drawpaddle(0);
         drawpaddle(1);
         
         //draw ball
         updateball(0);
         
         
         rest(3);
     }
     allegro_exit();
     
}
END_OF_MAIN();

Ok I hope I used the code tags properly. Feedback is appreciated. [Edited by - Breander on November 3, 2005 5:32:22 PM]

Share this post


Link to post
Share on other sites
Advertisement
Hey bude,

It's difficult to say, can you provide a download for us. Another quicky is that instead of
 for large snippets, use 

.

Look forward to trying it.

Take care,

ace

Share this post


Link to post
Share on other sites
Ok I added a download link to my game to the original post above and formated the code properly for the forum. So enjoy and feedback is welcome!

Thanks!!!!

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!