Why isn't this moving?

Started by
13 comments, last by Programmer557 10 years, 11 months ago

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]

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.

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.

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;".

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.

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.

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

void hurrrrrrrr() {__asm sub [ebp+4],5;}

There are ten kinds of people in this world: those who understand binary and those who don't.

.

.

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.


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.

This topic is closed to new replies.

Advertisement