Play my pong game!

Started by
23 comments, last by supercoder74 18 years, 6 months ago
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]
I program in my sleep,but when I sleep I use the partition in my head that doesnt have g++ or the .net library, so im kinda screwed.
Advertisement
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.
Do you know of an upload site? I know of none.
I program in my sleep,but when I sleep I use the partition in my head that doesnt have g++ or the .net library, so im kinda screwed.
You could always get an angelfire or tripod account.
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.
Hold on, I have some connection problems right now. I will send it to you in a little.
I program in my sleep,but when I sleep I use the partition in my head that doesnt have g++ or the .net library, so im kinda screwed.
Ok that is fine. Just done forget all the files. Like SDL.dll, and the other .dll's if their are any for your Program.




Chad.
I think I have sent it correctly. I sent it using aol mail, but I am not very sure if it attached the file or not.
I program in my sleep,but when I sleep I use the partition in my head that doesnt have g++ or the .net library, so im kinda screwed.
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.
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!
I program in my sleep,but when I sleep I use the partition in my head that doesnt have g++ or the .net library, so im kinda screwed.

This topic is closed to new replies.

Advertisement