Sign in to follow this  
Joshnathan

SDL movement problem

Recommended Posts

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

Share this post


Link to post
Share on other sites
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.

Share this post


Link to post
Share on other sites
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?

Share this post


Link to post
Share on other sites
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);
}

Share this post


Link to post
Share on other sites
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?

Share this post


Link to post
Share on other sites
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 :)

Share this post


Link to post
Share on other sites
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.

Share this post


Link to post
Share on other sites
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))

Share this post


Link to post
Share on other sites
nope, once I added that the program only opened for a sec and then immediatly shuts down.

BTW I solved the trail problem. only have to solve my "exit problem" now :D

Share this post


Link to post
Share on other sites
initialize running to 'true'?

Edit: hmm... that seems to be done

What about this?

case SDL_QUIT:
GameOver == true;

[...]
case SDLK_ESCAPE:
GameOver== true;
break;


Looks like it should read:

GameOver = true;

Share this post


Link to post
Share on other sites
Quote:
Original post by Jan-Lieuwe
case SDL_QUIT:
GameOver == true;

Looks like it should read:

GameOver = true;


That must be it.

If you fixed that and still have problems post the updated code..

Share this post


Link to post
Share on other sites
I have one more question :D
atm I have this function:

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)
{
paddle1Y--;
}
break;
case SDLK_DOWN:
if(paddle1Y < screen->h-10)
{
paddle1Y++;
}
break;
case SDLK_ESCAPE:
GameOver = true;
break;
}
break;
}
SDL_FillRect(screen, NULL, 255);
DrawGame();
SDL_Flip(screen);
}
}
}


which works fine, but the movement isn't smooth (you gotta keep clicking the "up" button. So I thought I could do it like this:

void Movement()
{
bool GameOver = false;
while(!GameOver)
{
keys = SDL_GetKeyState(NULL);
if(keys[SDLK_UP]){paddle1Y--;}
if(keys[SDLK_DOWN]){paddle1Y++;}
if(keys[SDLK_ESCAPE]){GameOver = true;}
if(keys[SDL_QUIT]){GameOver = true; running = false;}
SDL_FillRect(screen, NULL, 255);
DrawGame();
SDL_Flip(screen);
}
}


but it won't work. Any idea why? it is ignoring the input again.

Share this post


Link to post
Share on other sites
try something like:

int moveDown = 1;
int moveUp = -1;

if (keyUP)
paddleY += moveUp;
if (keyDOWN)
paddleY += moveDown;



i remember reading somewhere that, that should work.
why exactly...i don't know.

Share this post


Link to post
Share on other sites
using your original method ...

SDL will put events into the queue when they happen. So when a key is pressed down, a keydown event will happen. Then maybe much later, the key will be released and a keyup event will happen.

So, you want to tell your program to keep moving the paddle in between and so only change the speed of the paddle (ie. 1 left, 0 not moving, -1 right) from user input and not the position.

Hope that makes sense :)

Share this post


Link to post
Share on other sites
finally made it work, but now I want to add speed. I am trying to increase the speed by hitting the shift button and decreasing speed by hitting ctrl buttun. what are the functions in SDL for that? SDLK_CTRL and SDLK_SHIFT didn't work :(

Share this post


Link to post
Share on other sites
Quote:
Original post by Joshnathan
lol, I just try'd some stuff and LSHIFT and LCTRL worked :D

That's the best way to learn, to try some stuff [smile].

Share this post


Link to post
Share on other sites
Quote:
Original post by Joshnathan
finally made it work, but now I want to add speed. I am trying to increase the speed by hitting the shift button and decreasing speed by hitting ctrl buttun. what are the functions in SDL for that? SDLK_CTRL and SDLK_SHIFT didn't work :(

i'm curious... how did you get it to work?
show code please.

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