Sign in to follow this  

Why isn't this moving?

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

I am trying to create a program that will randomly generate rooms as the user presses the arrow keys, and it's been working fine, but I've hit a bit of a snag.  I'm trying to get the key press commands working, but no matter how long I look at the code, I can't figure out why the little figure I have made isn't moving.

 

I've included the entire file for the program, but here is where I think the problem is taking place:

 


		while(SDL_PollEvent(&Event))
		{
			if(Event.type == SDL_KEYDOWN)
			{
				if (Event.key.keysym.sym)
				{
					if (SDL_KEYDOWN == SDLK_UP) 
					{
						if (PlayerLocationy == 0)
						{
							Message = DeniedMessage;
							ApplySurface((ScreenWidth - Message->w)/2,(ScreenHeight - Message->h + 420)/2,Message,Screen,NULL);
							Message == NULL;
						}
						else
						{
							PlayerLocationy =- 42;
							ApplySurface(0,421,Sprites,Screen,&TextBox);
						}
					}
					if (SDL_KEYDOWN == SDLK_DOWN) 
					{
						if (PlayerLocationy == 378)
						{
							Message = DeniedMessage;
							ApplySurface((ScreenWidth - Message->w)/2,(ScreenHeight - Message->h + 420)/2,Message,Screen,NULL);
							Message == NULL;
						}
						else
						{
							PlayerLocationy =+ 42;
							ApplySurface(0,421,Sprites,Screen,&TextBox);
						}
					}
				if (SDL_KEYDOWN == SDLK_LEFT) 
					{
						if (PlayerLocationx == 0)
						{
							Message = DeniedMessage;
							ApplySurface((ScreenWidth - Message->w)/2,(ScreenHeight - Message->h + 420)/2,Message,Screen,NULL);
							Message == NULL;
						}
						else
						{
							PlayerLocationx =- 42;
							ApplySurface(0,421,Sprites,Screen,&TextBox);
						}
					}
				if (SDL_KEYDOWN == SDLK_RIGHT) 
					{
						if (PlayerLocationy == 0)
						{
							Message = DeniedMessage;
							ApplySurface((ScreenWidth - Message->w)/2,(ScreenHeight - Message->h + 420)/2,Message,Screen,NULL);
							Message == NULL;
						}
						else
						{
							PlayerLocationy =+ 42;
							ApplySurface(0,421,Sprites,Screen,&TextBox);
						}
					}
				if (SDL_KEYDOWN == SDLK_UP) 
					{
						Message == EscapeMessage;
						ApplySurface((ScreenWidth - Message->w)/2,(ScreenHeight - Message->h + 420)/2,Message,Screen,NULL);

						if (SDL_KEYDOWN == SDLK_y)
						{
							QUIT == true;
						}
					}
				}
			}
			
			if(Event.type == SDL_QUIT)
			{
				QUIT = true;
			}
		}

Any help that could be given would be very much appreciated.  I have a feeling it's something smal like a missing = sign that I can't find, but maybe another programmer would be able to see what I have not.

 

Thank you in advance

-Programmer557

 

[attachment=15821:Randomized Maze Game.zip]

Share this post


Link to post
Share on other sites

if (Event.key.keysym.sym)
{
if (SDL_KEYDOWN == SDLK_UP)
{

 

I'm not familiar with SDL, but that just looks wrong. Shouldn't it be more along those lines?

 

 

if (Event.key.keysym.sym == SDLK_UP)
{

 

Right now you are appearantly comparing the event-constant with the key-constant in the latter if, and as for the first one, I don't think that is necessary - is "Event.key.keysym.sym" a pointer? You are basically checking if this variable is any other value than 0, can't tell if this is what you intended or not.

Edited by Juliean

Share this post


Link to post
Share on other sites

if (Event.key.keysym.sym)
{
if (SDL_KEYDOWN == SDLK_UP)
{

 

I'm not familiar with SDL, but that just looks wrong. Shouldn't it be more along those lines?

 

 

>if (Event.key.keysym.sym == SDLK_UP)
{

 

Right now you are appearantly comparing the event-constant with the key-constant in the latter if, and as for the first one, I don't think that is necessary - is "Event.key.keysym.sym" a pointer? You are basically checking if this variable is any other value than 0, can't tell if this is what you intended or not.

 

Yes, you're correct. SDL_KEYDOWN is a constant used to determine the type of the event.

Event.key.keysym.sym is an integer containing the key symbol representing which key was pressed.

Share this post


Link to post
Share on other sites

if (PlayerLocationy == 378)
{
Message = DeniedMessage;
ApplySurface((ScreenWidth - Message->w)/2,(ScreenHeight - Message->h + 420)/2,Message,Screen,NULL);
Message == NULL;
}

if (SDL_KEYDOWN == SDLK_y)
{
QUIT == true;
}

I am almost 100% positive you mean to use a single equals sign here. Double equals compares them for equality, and then the result is discarded here with nothing happening.

 

else
{
PlayerLocationy =+ 42;
ApplySurface(0,421,Sprites,Screen,&TextBox);
}

else
{
PlayerLocationx =- 42;
ApplySurface(0,421,Sprites,Screen,&TextBox);
}

I can't quite place what language you must be getting this from, but in compound assignment operators, the equals sign always comes last. "PlayerLocationy =+ 42;" is parsed the same as "PlayerLocationy = +42;", which isn't what you wanted. Likewise, "PlayerLocationy =- 42;" is the same as "PlayerLocationy = -42;".

Share this post


Link to post
Share on other sites

Also, you should use

int x;

if( x == 0)
{
}
else if(x == 1)
{
}

for mutually exclusive conditions. Or better yet:

 

int x;

switch(x)
{
case 0:
// ...
break;
case 1:
// ...
break;
}

as long as you have an enumarable variable (which you should have with the SDL-event, just change "x" with Event.type and use "SDL_KEYDOWN" etc... in the "case" statement). Both of these will not only help performance, but also make your code more understandable.

Share this post


Link to post
Share on other sites

I found a little bit later that I had forgotten to flip the screen, but even after adding that the program still doesen't do what I want it to.  Just thought I'd add that before anyone made the comment.

Share this post


Link to post
Share on other sites

I think you're going to save yourself a lot of time, now and in the future, if you immediately remove all the repetition here. As it is, any change you make to the core routine here has to be made 4 times, and that means 4 times the probability that you'll make another mistake.
 
DRY

Share this post


Link to post
Share on other sites

.

That's exactly what I was saying. It was clear that he wanted to add to and subtract from the variables, but the order of the two symbols in compound operators is standardized. The equals sign must come last, otherwise, whenever he moves, it will only allow him to move to -42 or 42 on either axis, which I can guarantee is not what he wanted to do for a room generator, unless he only wants 7 rooms maximum, some he can only visit once.

I'll accept the downvote, anyway.

Share this post


Link to post
Share on other sites


if (SDL_KEYDOWN == SDLK_UP)
{
Message == EscapeMessage;
ApplySurface((ScreenWidth - Message->w)/2,(ScreenHeight - Message->h + 420)/2,Message,Screen,NULL);

if (SDL_KEYDOWN == SDLK_y)
{
QUIT == true;
}

}


I just notice on another read-through, that this condition will never be true. A key press event only contains information on one key press; to get another key press, you have to check for the next event. Even if we were checking which key was pressed correctly here, the nested condition will never be true, because we just checked to see if the same value was equal to SDLK_UP before entering the if-block.

Share this post


Link to post
Share on other sites

All right, I've made these slight changes and the sprite moves now, but it will only move down, and it will not move more than once.  The message "You Cannot Move Here" also appears.  This isn't supposed to happen unless the player is at the edge of the screen and tries to move in the direction of the edge.  Here is the altered code:

 

 

if (SDLK_UP) 
					{
						if (PlayerLocationy == 0)
						{
							Message = DeniedMessage;
							ApplySurface((ScreenWidth - Message->w)/2,(ScreenHeight - Message->h + 420)/2,Message,Screen,NULL);
							Message = NULL;
						}
						else
						{
							PlayerLocationy = PlayerLocationy - 42;
							ApplySurface(0,421,Sprites,Screen,&TextBox);
						}
					}
					
					if (SDLK_DOWN) 
					{
						if (PlayerLocationy == 378)
						{
							Message = DeniedMessage;
							ApplySurface((ScreenWidth - Message->w)/2,(ScreenHeight - Message->h + 420)/2,Message,Screen,NULL);
							Message = NULL;
						}
						else
						{
							PlayerLocationy = PlayerLocationy + 42;
							ApplySurface(0,421,Sprites,Screen,&TextBox);
							break;
						}
					}
					
					if (SDLK_LEFT) 
					{
						if (PlayerLocationx == 0)
						{
							Message = DeniedMessage;
							ApplySurface((ScreenWidth - Message->w)/2,(ScreenHeight - Message->h + 420)/2,Message,Screen,NULL);
							Message = NULL;
						}
						else
						{
							PlayerLocationx = PlayerLocationx - 42;
							ApplySurface(0,421,Sprites,Screen,&TextBox);
						}
					}
					
					if (SDLK_RIGHT) 
					{
						if (PlayerLocationx == 378)
						{
							Message = DeniedMessage;
							ApplySurface((ScreenWidth - Message->w)/2,(ScreenHeight - Message->h + 420)/2,Message,Screen,NULL);
							Message = NULL;
						}
						else
						{
							PlayerLocationx = PlayerLocationx + 42;
							ApplySurface(0,421,Sprites,Screen,&TextBox);
						}
					}
					
					if (SDLK_ESCAPE) 
					{
						Message = EscapeMessage;
						ApplySurface((ScreenWidth - Message->w)/2,(ScreenHeight - Message->h + 420)/2,Message,Screen,NULL);

						if (SDL_KEYDOWN == SDLK_y)
						{
							QUIT = true;
						}
					}

I noticed that Khatharr had said something about not repeating the same functions, but I don't know how to do that quite yet.

Share this post


Link to post
Share on other sites

if (SDLK_UP)


This isn't quite right, either. SDLK_UP, SDLK_DOWN, SDLK_LEFT, SDLK_RIGHT, and SDLK_ESCAPE are non-zero constants; all of these compare true no matter what. You want to test if the key symbol from the event is equal to these constants.

Additionally, you should use if-else-if blocks, because an event cannot be contain than one key press at a time, so it is a waste of time for your machine to check if it is each symbol after it already found which one it is. This would be perfect for a switch statement, if you are proficient in their use.

 

if (SDLK_ESCAPE) { Message = EscapeMessage; ApplySurface((ScreenWidth - Message->w)/2,(ScreenHeight - Message->h + 420)/2,Message,Screen,NULL); if (SDL_KEYDOWN == SDLK_y) { QUIT = true; } }


There's still this problem, here.

Share this post


Link to post
Share on other sites
This code is just in response to what you asked me about. There's still problems here that other people have touched on. Additionally, your render code seems too close to your logic. You should try to separate these into different sections unless the program structure is exceedingly simplistic.
const int MOVE_DIST = 42;
const int BOUNDARY_MIN = 0;
const int BOUNDARY_MAX = 378;

//Handle movement (see other people's comments about testing against SDLK_ constants)
if(SDLK_UP)    {PlayerLocationy -= MOVE_DIST;}
if(SDLK_DOWN)  {PlayerLocationy += MOVE_DIST;}
if(SDLK_LEFT)  {PlayerLocationx -= MOVE_DIST;}
if(SDLK_RIGHT) {PlayerLocationx += MOVE_DIST;}

//Correct out-of-bounds condition
Message = NULL;
if(PlayerLocationy < BOUNDARY_MIN) {
  Message = DeniedMessage;
  PlayerLocationy = BOUNDARY_MIN;
}
if(PlayerLocationy > BOUNDARY_MAX) {
  Message = DeniedMessage;
  PlayerLocationy = BOUNDARY_MAX;
}
if(PlayerLocationx < BOUNDARY_MIN) {
  Message = DeniedMessage;
  PlayerLocationx = BOUNDARY_MIN;
}
if(PlayerLocationy > BOUNDARY_MAX) {
  Message = DeniedMessage;
  PlayerLocationx = BOUNDARY_MAX;
}

//Render
if(Message != NULL) {
  int mess_x = (ScreenWidth - Message->w) / 2;
  //This next line is not clear. Get rid of the magic number and clarify order of operations
  int mess_y = (ScreenHeight - Message->h + 420) / 2;

  ApplySurface(mess_x,mess_y,Message,Screen,NULL);

  Message = NULL;
}
else {
  ApplySurface(0,421,Sprites,Screen,&TextBox);
}
 
The key here is that the things you're likely to change are easy to change and it's clear what's going on. You can change the constants at the top or the render stuff at the bottom and not have to worry about changing it all 4 times. The organization and alignment of the first set of tests also makes it easier to spot if there's any errors lurking there, such as having PlayerLocationy where there should be a PlayerLocationx, or a - where there should be a +. Edited by Khatharr

Share this post


Link to post
Share on other sites

Additionally, your render code seems too close to your logic. You should try to separate these into different sections unless the program structure is exceedingly simplistic.


+1 for separation between logic and rendering.

Share this post


Link to post
Share on other sites

This code is just in response to what you asked me about. There's still problems here that other people have touched on. Additionally, your render code seems too close to your logic. You should try to separate these into different sections unless the program structure is exceedingly simplistic.

const int MOVE_DIST = 42;
const int BOUNDARY_MIN = 0;
const int BOUNDARY_MAX = 378;

//Handle movement (see other people's comments about testing against SDLK_ constants)
if(SDLK_UP)    {PlayerLocationy -= MOVE_DIST;}
if(SDLK_DOWN)  {PlayerLocationy += MOVE_DIST;}
if(SDLK_LEFT)  {PlayerLocationx -= MOVE_DIST;}
if(SDLK_RIGHT) {PlayerLocationx += MOVE_DIST;}

//Correct out-of-bounds condition
Message = NULL;
if(PlayerLocationy < BOUNDARY_MIN) {
  Message = DeniedMessage;
  PlayerLocationy = BOUNDARY_MIN;
}
if(PlayerLocationy > BOUNDARY_MAX) {
  Message = DeniedMessage;
  PlayerLocationy = BOUNDARY_MAX;
}
if(PlayerLocationx < BOUNDARY_MIN) {
  Message = DeniedMessage;
  PlayerLocationx = BOUNDARY_MIN;
}
if(PlayerLocationy > BOUNDARY_MAX) {
  Message = DeniedMessage;
  PlayerLocationx = BOUNDARY_MAX;
}

//Render
if(Message != NULL) {
  int mess_x = (ScreenWidth - Message->w) / 2;
  //This next line is not clear. Get rid of the magic number and clarify order of operations
  int mess_y = (ScreenHeight - Message->h + 420) / 2;

  ApplySurface(mess_x,mess_y,Message,Screen,NULL);

  Message = NULL;
}
else {
  ApplySurface(0,421,Sprites,Screen,&TextBox);
}
 
The key here is that the things you're likely to change are easy to change and it's clear what's going on. You can change the constants at the top or the render stuff at the bottom and not have to worry about changing it all 4 times. The organization and alignment of the first set of tests also makes it easier to spot if there's any errors lurking there, such as having PlayerLocationy where there should be a PlayerLocationx, or a - where there should be a +.

Thank you very much.  With some slight modification, my program is now working how I want it to.  Thank you also, everyone else who posted.

Share this post


Link to post
Share on other sites

This topic is 1701 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.

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