Sign in to follow this  
supercoder74

Play my pong game!

Recommended Posts

supercoder74    154
Hello, I have finished my first real game. I will just post my source code. entity.h:
#include <SDL/SDL.h>
#include <SDL/SDL_image.h>
#include <string>
#ifndef ENTITY_H
#define ENTITY_H

class entity
{
      public:
      entity(std::string imagefile,int startingx,int startingy){xpos=startingx;ypos=startingy;img=IMG_Load(imagefile.c_str());}
      bool movex(int xvel,int xmax,int xmin)
      {
           xpos+=xvel;
          
           if(xpos>xmax||xpos<xmin){
           xpos-=xvel;
           return false;
           }
           return true;
      }
      bool movey(int yvel,int ymax,int ymin)
      {
           ypos+=yvel;
           if(ypos>ymax||ypos<ymin){
           ypos-=yvel;
           return false;
           }
           return true;
      }
      void draw(SDL_Surface* screen)
      {
           SDL_Rect dest;
           dest.x=xpos;
           dest.y=ypos;
           SDL_BlitSurface(img,NULL,screen,&dest);
      }
      
      
      void setx(int x)
      {
           xpos=x;
      }
      void sety(int y)
      {
           ypos=y;
      }
      int getx()
      {
          return xpos;
      }
      int gety()
      {
          return ypos;
      }
      int getw()
      {
          return img->w;
      }
      int geth()
      {
          return img->h;
      }
      SDL_Surface* getsurface()
      {
         return img;
      }
      private:
              int xpos;
              int ypos;
              SDL_Surface* img;
};
#endif




pongmain.cpp:
#include <SDL/SDL.h>
#include <SDL/SDL_image.h>
#include <SDL/SDL_collide.h>
#include <windows.h>
#include "entity.h"
int SCREEN_W=628;//width of window
int SCREEN_H=348;//height of window
int xvel=1;//xvel of ball
int yvel=0;//yvel of ball
int paddleyvel=1;//how fast paddle goes
int leftgoal=0;//score of left player
int rightgoal=0;//score of right player
char windowtitle[100];
entity ball("pongball.bmp",SCREEN_W/2,SCREEN_H/2);// The ball
entity lpaddle("paddle.bmp",100,SCREEN_H/2);// The left paddle
entity rpaddle("paddle.bmp",SCREEN_W-100,SCREEN_H/2);//The right paddle
entity line("line.bmp",SCREEN_W/2,10);//The line in the middle
Uint8* keys;//used to check for keys
void initgame()
{
     SDL_Init(SDL_INIT_VIDEO);
     SDL_SetVideoMode(SCREEN_W,SCREEN_H,0,SDL_HWSURFACE|SDL_DOUBLEBUF);
     
}

void moveball() //check for collision against walls and paddles
{
     if(SDL_BoundingCollide(ball.getsurface(),ball.getx(),ball.gety(),lpaddle.getsurface(),lpaddle.getx(),lpaddle.gety()))
     {
     xvel=-xvel;
    
     }
     
     if(SDL_BoundingCollide(ball.getsurface(),ball.getx(),ball.gety(),rpaddle.getsurface(),rpaddle.getx(),rpaddle.gety()))
     xvel=-xvel;
     
     if(!ball.movex(xvel,SCREEN_W-20,0))
     xvel=-xvel;
     
     if(!ball.movey(yvel,SCREEN_H-20,0))
     yvel=-yvel;
     
     if(yvel>2)//make sure ball does not go so fast that it is impossible to hit
     yvel--;
     
     if(yvel<-2)
     yvel++;
}
void drawstuff()// draw the ball, the paddles, and the line in the middle, and update the window title which has the score in it
{
     ball.draw(SDL_GetVideoSurface());
     lpaddle.draw(SDL_GetVideoSurface());
     rpaddle.draw(SDL_GetVideoSurface());
     line.draw(SDL_GetVideoSurface());
     sprintf(windowtitle,"My pong game! Left player: %i Right player: %i",leftgoal,rightgoal);
     SDL_WM_SetCaption(windowtitle,NULL);
}
void checkgoals()//check if someone scored
{
     if(ball.getx()<lpaddle.getx()-20) //if ball is to the left of the left paddle
     {
     rightgoal++;// increase score of right player
     MessageBox(NULL,"Right player scored!!","Score!",MB_OK);
     ball.setx(SCREEN_W/2);// put entities back to their defaults
     ball.sety(SCREEN_H/2);
     lpaddle.setx(100);
     lpaddle.sety(SCREEN_H/2);
     rpaddle.setx(SCREEN_W-100);
     rpaddle.sety(SCREEN_H/2);
     }
     if(ball.getx()>rpaddle.getx()+20)// if ball is to the right of the right paddle
     {
     leftgoal++;// increase score of left player
     MessageBox(NULL,"Left player scored!!","Score!",MB_OK);
     ball.setx(SCREEN_W/2);
     ball.sety(SCREEN_H/2);
     lpaddle.setx(100);
     lpaddle.sety(SCREEN_H/2);
     rpaddle.setx(SCREEN_W-100);
     rpaddle.sety(SCREEN_H/2);
     }
}
     
int main(int argc,char* argv[])
{
    
   
    initgame(); //init the window and game
    atexit(SDL_Quit);
    SDL_Event event;
   char tmp[3];//used for deubugging
    int done=0;
     while(done==0)
    {
       keys=SDL_GetKeyState(NULL);
       SDL_PollEvent(&event);
       if(event.type==SDL_QUIT)
       exit(0);
       else if(event.type==SDL_KEYDOWN)
       {
       
         if(keys[SDLK_UP])
         {
         rpaddle.movey(-paddleyvel,SCREEN_H,0);
          if(SDL_BoundingCollide(ball.getsurface(),ball.getx(),ball.gety(),rpaddle.getsurface(),rpaddle.getx(),rpaddle.gety()))//bounce against paddles at angle
          yvel++;
          }
       
         if(keys[SDLK_DOWN])
         {
         rpaddle.movey(paddleyvel,SCREEN_H-75,0);
         if(SDL_BoundingCollide(ball.getsurface(),ball.getx(),ball.gety(),rpaddle.getsurface(),rpaddle.getx(),rpaddle.gety()))
         yvel--;
         }
         
         if(keys[SDLK_w])
         {
         lpaddle.movey(-paddleyvel,SCREEN_H,0);
         if(SDL_BoundingCollide(ball.getsurface(),ball.getx(),ball.gety(),lpaddle.getsurface(),lpaddle.getx(),lpaddle.gety()))
         yvel++;
         }
         
         if(keys[SDLK_s])
         {
         lpaddle.movey(paddleyvel,SCREEN_H-75,0);
         if(SDL_BoundingCollide(ball.getsurface(),ball.getx(),ball.gety(),lpaddle.getsurface(),lpaddle.getx(),lpaddle.gety()))
         yvel--;
         }
         
         if(keys[SDLK_y])//deubugging purpuses; prints the y velocity of ball
         {
          itoa(yvel,tmp,10);
          MessageBox(NULL,tmp,NULL,MB_OK);
          }
       
      }
       moveball(); //move the ball according to the velocities
       drawstuff();// draw the things
       checkgoals();//check if the ball is past a paddle
       SDL_Flip(SDL_GetVideoSurface());
       SDL_FillRect(SDL_GetVideoSurface(),NULL,0x000000);//put here because of some wierd bug with the paddles not getting updated
       SDL_Delay(2);//make game slower
    }
}




here is the data: pongball.bmp paddle.bmp line.bmp You need to link with Sdl and Sdl_image to compile. If you use it, if you sent some kind of feedback it would be appreciated. [EDIT] you need to right click and save-as for the paddle and line, because the images are white. Changed source. [/EDIT]

Share this post


Link to post
Share on other sites
Chad Smith    1344
Why don't you upload the .exe somewhere and post the link here. I don't have time to go through all of that process.



Just put the files, in a ZIP folder. don't forget SDL.dll. You will probaly get more people to play, and leave their comments that way.




Chad.

Share this post


Link to post
Share on other sites
Chad Smith    1344
Send me the .zip, and I can host it for about a week. ok. Just send me the .zip to fearspear08@yahoo.com, and post here that you have sent it. I will check my E-Mail, and then put it on my site and it will then be hosted. I will take it of their after a week or 2 though. Sorry.


Chad.

Share this post


Link to post
Share on other sites
Chad Smith    1344
Yeah I got it.


It is now uploaded.


To download it everyone, then just go here....

Pong!



Just tell me if the download goes down. I have done some work on my server, so I hope it stays up now. It should!



Chad.

Share this post


Link to post
Share on other sites
supercoder74    154
Quote:
Original post by Chad Smith
Yeah I got it.


It is now uploaded.


To download it everyone, then just go here....

Pong!



Just tell me if the download goes down. I have done some work on my server, so I hope it stays up now. It should!



Chad.


Thanks!

Share this post


Link to post
Share on other sites
Chad Smith    1344
Your Welcome!


I am creating a Game Development site, so I am deleting everything on my server except that. So now, other people can upload their game to my server for free. Expect more news later on!




Also since I am now doing some stuff to my Server, your game will be on their longer. Right now it will be on their for 2 weeks though.



Chad.


Share this post


Link to post
Share on other sites
Programmer16    2321
All in all, it is a nice game. Only problem is that every now and then it was going straight back and forth (no angle). Congratulations on finishing your game and keep up the good work!

Share this post


Link to post
Share on other sites
Lost    238
Quote:
Original post by DigiDude
A bit simplistic but pretty good! Did you open the main window from a console window?
He is using SDL which handles all the Windows stuff.

Share this post


Link to post
Share on other sites
supercoder74    154
Quote:
Original post by Kevinator
This thing is buggy. Did you actually test it with two players? The players can stop each other's paddles.


I have noticed this,yes. I have not actually got someone with a mind to play my game, but I have tested it as much as I can with two hands. I think I can solve this problem with threads.

Share this post


Link to post
Share on other sites
dudedbz1    100
Why threads? I have a mario clone that uses two places for input and one never stops the other... I didnt look at your code, so sorry I am mistaken, but are you using a

Uint8 *somenameforyourkeys/*mine is just "keys"*/;
keys = SDL_GetKeyState(0);
? If not, try it.

Edit: I looked, and it seems you did... Do you know where the bug is? I mean, wont my mario clone mess up too if they use the same kind of input?

Share this post


Link to post
Share on other sites
supercoder74    154
Quote:
Original post by dudedbz1
Why threads? I have a mario clone that uses two places for input and one never stops the other... I didnt look at your code, so sorry I am mistaken, but are you using a

Uint8 *somenameforyourkeys/*mine is just "keys"*/;
keys = SDL_GetKeyState(0);
? If not, try it.

Edit: I looked, and it seems you did... Do you know where the bug is? I mean, wont my mario clone mess up too if they use the same kind of input?


Really? Could you some of your code that deals with the keyboard?

Share this post


Link to post
Share on other sites
dudedbz1    100
Yeah, sure! Here:

Uint8 *keys = SDL_GetKeyState(NULL);

SDL_Event event;

if (SDL_PollEvent(&event) == true)
{
if (event.type == SDL_QUIT && Quit())
isRunning = false;
}

if (keys[SDLK_ESCAPE])
isRunning = false;

if ((Mario.x + Mario.Char->w) < Luigi.x)
Luigi.Char = Luigi.CharLeft;
if (Mario.x > (Luigi.x + Luigi.Char->w))
Luigi.Char = Luigi.CharRight;

//------------------------------//
// MARIO //
//------------------------------//
if (keys[SDLK_UP])
{
Mario.Jump = true;
}
if (keys[SDLK_LEFT] &&
Mario.x >= 0 &&
Mario.CollideOnRight(Luigi) == false)
{
Mario.x -= 3;

Mario.Char = Mario.CharLeft;
}
if (keys[SDLK_RIGHT] &&
(Mario.x + Mario.Char->w) <= 800 &&
Mario.CollideOnLeft(Luigi) == false)
{
Mario.x += 3;

Mario.Char = Mario.CharRight;
}

//------------------------------//
// Luigi //
//------------------------------//
if (keys[SDLK_KP8])
{
Luigi.Jump = true;
}
... Same input as mario
... Just with the keypad(keys[SDLK_KP4] .. etc.)
}


Thats pretty much it. I'm making my pong clone to be two-player too, so when I finish I will tell you if the same problem persists. Good-luck.
I dont know if that'll help you much though...

Share this post


Link to post
Share on other sites
supercoder74    154
Quote:
Original post by dudedbz1
Yeah, sure! Here:
*** Source Snippet Removed ***
Thats pretty much it. I'm making my pong clone to be two-player too, so when I finish I will tell you if the same problem persists. Good-luck.
I dont know if that'll help you much though...


Hmm. It looks the same. The problem may lie somewhere else.
Nice code, by the way! I might try some 2d scroller sometime. I bet I could reuse that entity code for it..

Share this post


Link to post
Share on other sites
dudedbz1    100
Yeah, maybe so... Uhhh... Could it be in your velocities? Cuz I never understood them and I never used them. In my pong I have this:

if (Keys[SDLK_LEFT])
P1_Paddle.Setx(P1_Paddle.Getx() - 1);
if (Keys[SDLK_RIGHT])
P1_Paddle.Setx(P1_Paddle.Getx() + 1);

So maybe you should check those. And I reuse my old code too. Much of the initializing is from my mario clone. Just different variables. Good luck.

Share this post


Link to post
Share on other sites
supercoder74    154
Quote:
Original post by dudedbz1
Yeah, maybe so... Uhhh... Could it be in your velocities? Cuz I never understood them and I never used them. In my pong I have this:

if (Keys[SDLK_LEFT])
P1_Paddle.Setx(P1_Paddle.Getx() - 1);
if (Keys[SDLK_RIGHT])
P1_Paddle.Setx(P1_Paddle.Getx() + 1);

So maybe you should check those. And I reuse my old code too. Much of the initializing is from my mario clone. Just different variables. Good luck.


What you just used right there are velocities. Well, kinda. You add the velocity to the x and y. so if the velocity is -1, and you add it to x, it makes x go down by one. The reason this is useful is that if you just change the line that declares the velocity everything instantly moves faster if you add to it, or moves slower if you decrease from it. I dont think that is the problem, because the paddle velocity remains constant.

Share this post


Link to post
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now

Sign in to follow this