Jump to content
  • Advertisement
Sign in to follow this  
Programmer557

Why isn't this moving?

This topic is 1855 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
Advertisement

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
Sign in to follow this  

  • Advertisement
×

Important Information

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

Participate in the game development conversation and more when you create an account on GameDev.net!

Sign me up!