Jump to content
  • Advertisement
Sign in to follow this  
BKBen7

First Game(Its not a VB mmorpg!)

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

Anyway, I'm having trouble blitting a ball image onto a surface in my pong game, I have the source posted below, I thought maybe someone could try and help me figure out what I'm doing wrong, right now when I launch the program it flashes then shuts off, if I take out the blitting lines, it loads up fine. I'm sure this is something stupid I'm not doing right, but I can;t figure it out. (Dont mind some of the oddities in the program, right now its just set up for me testing it.) EDIT: I'm retarded, some how a 'return 0' got in there and was cutting the program out, it works now.
#include "SDL/SDL.h"
#include <stdlib.h>


#define SCREENCAPTION "SDLPong"
#define SCREENWIDTH 640
#define SCREENHEIGHT 480
#define SCREENBPP 0
#define SCREENFLAGS SDL_ANYFORMAT

Uint8 *keystate = SDL_GetKeyState(NULL);
int paddle1x = 9;
int paddle1y = 200;
int paddle1w = 16;
int paddle1h = 64;
int paddle2x = 618;
int paddle2y = 200;
int paddle2w = 16;
int paddle2h = 64;
int DownKeyPressed = 0;
int UpKeyPressed = 0;
double ballx = 304;
double bally = 224;
int BallMoving = 0;
double BallXSpeed = -.5;
double BallYSpeed = 0;
int twally = 0;
int bwally = 472;

   
int ResetGame()//stub
{
    return 0;
    
}

int main( int argc, char* argv[] )
{
    //initialize systems
    SDL_Init ( SDL_INIT_VIDEO ) ;
    //create a window
    SDL_Surface* pSurface = SDL_SetVideoMode ( SCREENWIDTH , SCREENHEIGHT , SCREENBPP, SCREENFLAGS ) ;
    //Name the window
    SDL_WM_SetCaption(SCREENCAPTION, 0);
    //declare event variable
    SDL_Event event ; 
    //message pump
    SDL_Surface* ballb = SDL_LoadBMP("ball.bmp"); 
    //yea this!
    return 0; 

    while (1)
    {
        SDL_Rect back ;
        back.x = 1;
        back.y = 1;
        back.w = SCREENWIDTH;
        back.h = SCREENHEIGHT; 
        SDL_FillRect ( pSurface , &back , SDL_MapRGB ( pSurface->format , 0 , 0 , 0) ) ;

        SDL_Rect twall ;
        twall.x = 0;
        twall.y = twally;
        twall.w = 640;
        twall.h = 8;

        SDL_FillRect ( pSurface , &twall , SDL_MapRGB ( pSurface->format , 255 , 255 , 255 ) ) ;

        SDL_Rect bwall ;
        bwall.x = 0;
        bwall.y = bwally;
        bwall.w = 640;
        bwall.h = 8;

        SDL_FillRect ( pSurface , &bwall , SDL_MapRGB ( pSurface->format , 255 , 255 , 255 ) ) ;


        SDL_Rect ball;
        ball.x = ballx;
        ball.y = bally;
        ball.w = 16;
        ball.h = 16;       
        SDL_FillRect ( pSurface , &ball , SDL_MapRGB ( pSurface->format , 200 , 200 , 200) ) ;	


        SDL_BlitSurface(ballb, 0, pSurface, &ball);


        //Prepare Player 1's Paddle
        SDL_Rect paddle1 ;
        paddle1.x = paddle1x;
        paddle1.y = paddle1y;
        paddle1.w = paddle1w;
        paddle1.h = paddle1h;

        //Draw it
        SDL_FillRect ( pSurface , &paddle1 , SDL_MapRGB ( pSurface->format , 255 , 255 , 255 ) ) ;

        //Prepare Player 2's Paddle
        SDL_Rect paddle2 ;
        paddle2.x = paddle2x;
        paddle2.y = paddle2y;
        paddle2.w = paddle2w;
        paddle2.h = paddle2h;

        SDL_FillRect ( pSurface , &paddle2 , SDL_MapRGB ( pSurface->format , 255 , 255 , 255 ) ) ;


        //look for an event
        if ( SDL_PollEvent ( &event ) )
        {
            //an event was found
            if (event.type == SDL_QUIT) 
            {
                break ;
            }

            if (event.type == SDL_KEYDOWN)
            {
                if ( keystate[SDLK_DOWN] == 1)
                {
                    DownKeyPressed = 1;
                }
                else
                {
                    DownKeyPressed = 0;
                }
                if ( keystate[SDLK_UP] == 1)
                {
                    UpKeyPressed = 1;
                }
                else
                {
                    UpKeyPressed = 0;
                }
                if ( keystate[SDLK_SPACE] == 1)
                {
                    if (BallMoving == 1) 
                    {
                        ResetGame();
                    }
                else
                    {
                        BallMoving = 1;
                    }
                }
            }
            else
            {
                UpKeyPressed = 0;
                DownKeyPressed = 0;
            }
        }



        if (DownKeyPressed == 1)
        {
           
            paddle1y += 1;
            paddle2y += 1;
            
        }

        if (UpKeyPressed == 1)
        {
           
            paddle1y -= 1;
            paddle2y -= 1;
        }

        if (paddle1y >= 410)
        {
            paddle1y = 409;
        }
        if (paddle1y <= 5)
        {
            paddle1y = 6;
        }
        
        if (paddle2y >= 410)
        {
            paddle2y = 409;
        }
        if (paddle2y <= 5)
        {
            paddle2y = 6;
        }
 //from here to line 227 is bugged a little, will be fixed in time.   
        if (BallMoving == 1)
        {
            ballx += BallXSpeed;
            bally += BallYSpeed;
        
            //Hit player 1's paddle?
            if (ballx <= paddle1x + 16 &&  bally + 16 >= paddle1y && bally <= paddle1y + paddle1h)
            {
                if (bally + 8 >= paddle1y + 42.6 && bally + 16 <= paddle1y + 64)
                {
                    BallYSpeed += .3;
                }
                if (bally + 8 >= paddle1y && bally + 16 <= paddle1y + 21.2)
                {
                    BallYSpeed -= .3;
                }
                BallXSpeed = -BallXSpeed;
            }

            //Hit player 2's paddle?
            if (ballx + 16 >= paddle2x && bally + 16 >= paddle2y && bally <= paddle2y + paddle2h)
            {
                if (bally + 8 >= paddle2y + 42.7 && bally + 16 <= paddle2y + 64)
                {
                    BallYSpeed += .3;
                }  
                if (bally + 8 >= paddle2y && bally + 16 <= paddle2y + 21.2)
                {
                    BallYSpeed -= .3;
                }                            
                BallXSpeed = -BallXSpeed;
            }
            //Hit Bottom or Top Wall?
            if (bally <= twally + 8 ||  bally + 16 >= bwally)
            {
                BallYSpeed = -BallYSpeed;
            }
            
        }
        //update the screen
        SDL_UpdateRect ( pSurface , 0 , 0 , 0 , 0 ) ;
    }//end of message pump
    //done
    return ( 0 ) ;
}



This is the complete source to the program. [Edited by - BKBen7 on August 23, 2005 1:49:29 PM]

Share this post


Link to post
Share on other sites
Advertisement
Sign in to follow this  

  • Advertisement
×

Important Information

By using GameDev.net, you agree to our community Guidelines, Terms of Use, and Privacy Policy.

We are the game development community.

Whether you are an indie, hobbyist, AAA developer, or just trying to learn, GameDev.net is the place for you to learn, share, and connect with the games industry. Learn more About Us or sign up!

Sign me up!