SDL movement problem

Started by
24 comments, last by evillive2 19 years, 4 months ago
hi there, I am trying to create a pong game but I can't get my paddles to move. This is my code, the error should be somewhere here:

void Movement()
{
    bool GameOver = false;
    
    while(!GameOver)
    {
    SDL_Event event;
        while(SDL_PollEvent(&event))
        {
            switch(event.type)
            {
                case SDL_QUIT:
                   GameOver == true;
                   running = false;
                   break;
                case SDL_KEYDOWN:
                    switch(event.key.keysym.sym)
                    {
                        case SDLK_UP:    
                        if(paddle1Y > head1->h+20)
                        {                                             
                        paddle1X--;
                        }
                        break;
                        case SDLK_DOWN:
                        if(paddle1Y < screen->h-10)
                        {
                        paddle1Y++;
                        }
                        break;
                        case SDLK_ESCAPE:
                            GameOver== true;
                            break;
                   }  
                   break;  
            }
            SDL_Flip(screen);
        }       
    }
}                 

as soon as I call this function, input is ignoredm I don't know why. this is the full source if needed:

#include <iostream>
#include <stdlib.h>
#include <stdio.h>
#include <SDL/SDL.h>

using namespace std;

void CreateImage(SDL_Surface *dialog, int x, int y);
void LoadSurfaces();
void StartGame();
void Movement();

SDL_Surface *head1, *head2, *dialog, *screen, *background;
SDL_Surface *newgame1, *newgame2, *highscores1, *highscores2, *changechar1, *changechar2;
SDL_Surface *paddle1, *paddle2;

int paddle1X = 200, paddle1Y = 450, paddle2X = 600, paddle2Y =  450;
bool running = true;

int main(int argc, char *argv[])
{
    //Initiate SDL
  if((SDL_INIT_AUDIO|SDL_INIT_VIDEO) < 0)
  {
      cout<< "Error Initiating SDL\n";
  }
  atexit(SDL_Quit);
  
  //Create the screen surface
  screen = SDL_SetVideoMode(800, 600, 32, SDL_HWSURFACE|SDL_DOUBLEBUF);
  
  if(screen == NULL)
  {
      cout<< "Error setting video mode 640x480\n";
      return -1;
  }    
   
  
  int highlight = 1;
  
  //Load Surfaces
  LoadSurfaces();
  
  CreateImage(background, 0, 0);
  CreateImage(head1, 10, 10);
  CreateImage(head2, 580, 10);
  CreateImage(dialog, 10, 320);
  CreateImage(newgame2, 250, 50);
  CreateImage(highscores1, 250, 100);
  CreateImage(changechar1, 250, 150);
      
  while(running)
  {
      SDL_Event event;
      while(SDL_PollEvent(&event))
      {
          switch(event.type)
          {
              case SDL_QUIT:
                  running = false;
                  break;
              case SDL_KEYDOWN:
                  switch(event.key.keysym.sym)
                  {
                      case SDLK_ESCAPE:
                          running = false;
                          break;
                      case SDLK_UP:
                          if(highlight > 1){highlight--;}    
                          break;
                      case SDLK_DOWN:
                          if(highlight < 3){highlight++;}
                          break;
                      case SDLK_RETURN:
                          if(highlight == 1)
                          StartGame();
                          break;
                  }
                  break;    
          }
          
          switch(highlight)
          {
              case 1:
                  CreateImage(newgame2, 250, 50);
                  CreateImage(highscores1, 250, 100);
                  CreateImage(changechar1, 250, 150);
                  break;
              case 2:
                  CreateImage(newgame1, 250, 50);
                  CreateImage(highscores2, 250, 100);
                  CreateImage(changechar1, 250, 150);
                  break;
              case 3:
                  CreateImage(newgame1, 250, 50);
                  CreateImage(highscores1, 250, 100);
                  CreateImage(changechar2, 250, 150);
                  break;
          }    
          SDL_Flip(screen);
      }       
  }         
  return 0;
}



void CreateImage(SDL_Surface *image, int x, int y)
{
    SDL_Rect field;
    field.x = x;
    field.y = y;
    
    
    SDL_BlitSurface(image, NULL, screen, &field);
}    


void LoadSurfaces()
{
    head1 = SDL_LoadBMP("heads.bmp");
    head2 = SDL_LoadBMP("heads.bmp");
    dialog = SDL_LoadBMP("dialog.bmp");
    background = SDL_LoadBMP("background.bmp");
    newgame1 = SDL_LoadBMP("newgame1.bmp");
    newgame2 = SDL_LoadBMP("newgame2.bmp");
    highscores1 = SDL_LoadBMP("highscores1.bmp");
    highscores2 = SDL_LoadBMP("highscores2.bmp");
    changechar1 = SDL_LoadBMP("changechar1.bmp");
    changechar2 = SDL_LoadBMP("changechar2.bmp");
    paddle1 = SDL_LoadBMP("paddle.bmp");
    paddle2 = SDL_LoadBMP("paddle.bmp");
}    

void StartGame()
{
  CreateImage(paddle1, paddle1X, paddle1Y);
  CreateImage(paddle2, paddle2X, paddle2Y);
  Movement();
}    

void Movement()
{
    bool GameOver = false;
    
    while(!GameOver)
    {
    SDL_Event event;
        while(SDL_PollEvent(&event))
        {
            switch(event.type)
            {
                case SDL_QUIT:
                   GameOver == true;
                   running = false;
                   break;
                case SDL_KEYDOWN:
                    switch(event.key.keysym.sym)
                    {
                        case SDLK_UP:    
                        if(paddle1Y > head1->h+20)
                        {                                             
                        paddle1X--;
                        }
                        break;
                        case SDLK_DOWN:
                        if(paddle1Y < screen->h-10)
                        {
                        paddle1Y++;
                        }
                        break;
                        case SDLK_ESCAPE:
                            GameOver== true;
                            break;
                   }  
                   break;  
            }
            SDL_Flip(screen);
        }       
    }
}                 

hope anyone can help me out, Joshua
-----------------------------Sismondi GamesStarted c++ in October 2004...
Advertisement
Quote:Original post by Joshnathan
hi there, I am trying to create a pong game but I can't get my paddles to move. This is my code, the error should be somewhere here:
*** Source Snippet Removed ***
as soon as I call this function, input is ignoredm I don't know why. this is the full source if needed:
*** Source Snippet Removed ***
hope anyone can help me out,
Joshua


Where are you calling your Movement() function? Seems like you aren't calling Movement() anywhere.
Ad: Ancamnia
ok well i new to programming so i dont know.
I call Movement() in the StartGame() function
-----------------------------Sismondi GamesStarted c++ in October 2004...
Quote:Original post by Joshnathan
I call Movement() in the StartGame() function


Could you be a bit more specific about the problem? Try adding eg. cout << "Key down"; etc. into the different cases and see whether or not they're called.. That's an easy way to do debugging. See where the code doesn't do what it should.

EDIT: Are you drawing your screen in Movement()? Might that be the problem?
Ad: Ancamnia
You are not redrawing the paddle each loop. You only draw the paddles once (in StartGame). Also, your function name 'CreateImage' is wrong as to what it does. It blits a surface to the screen, so it should be called something like DrawSurface or DrawImage. And you have to draw each surface each loop around:

while(!GameOver) {
//other stuff

DrawGame();
Movement();

SDL_Flip(screen);
}

void DrawGame() {
DrawImage(paddle1, paddle1X, paddle1Y);
DrawImage(paddle2, paddle2X, paddle2Y);
}
yep, works now. I still have 2 problems though:
normally when I push "Escape" it should exit, but it doesn't. neither does it close the game when I try closeing it the normal way(pushing the little "X" in the right corner of the screen).
2nd is that I leave a trail, how can I erase my trail?
-----------------------------Sismondi GamesStarted c++ in October 2004...
Quote:Original post by Joshnathan
yep, works now. I still have 2 problems though:
normally when I push "Escape" it should exit, but it doesn't. neither does it close the game when I try closeing it the normal way(pushing the little "X" in the right corner of the screen).
2nd is that I leave a trail, how can I erase my trail?


Use SDL_FillRect() to clear the screen before drawing. See this post for an example:
http://www.gamedev.net/community/forums/topic.asp?topic_id=284173&whichpage=1�

When clicking on the 'X' you'll get SDL_QUIT event.. but it seems you're handling that. Once again, add debug messages etc. You'll learn better if you debug your code yourself even though it takes time :)
Ad: Ancamnia
the thing is that the code is executed normally even after that part as I can see my paddle move. so somehow there is a mistake in the switch(event.key.keysym.sym) I guess, but I can't find it out.
-----------------------------Sismondi GamesStarted c++ in October 2004...
Try changing it to this:

//while(running) /* remove this line */
{
SDL_Event event;
while(/*new*/ running && /*new*/ SDL_PollEvent(&event))
{

Same here:

while(!GameOver)
{
SDL_Event event;
while(/*new*/ running && /*new*/ SDL_PollEvent(&event))

This topic is closed to new replies.

Advertisement