• Announcements

    • khawk

      Download the Game Design and Indie Game Marketing Freebook   07/19/17

      GameDev.net and CRC Press have teamed up to bring a free ebook of content curated from top titles published by CRC Press. The freebook, Practices of Game Design & Indie Game Marketing, includes chapters from The Art of Game Design: A Book of Lenses, A Practical Guide to Indie Game Marketing, and An Architectural Approach to Level Design. The GameDev.net FreeBook is relevant to game designers, developers, and those interested in learning more about the challenges in game development. We know game development can be a tough discipline and business, so we picked several chapters from CRC Press titles that we thought would be of interest to you, the GameDev.net audience, in your journey to design, develop, and market your next game. The free ebook is available through CRC Press by clicking here. The Curated Books The Art of Game Design: A Book of Lenses, Second Edition, by Jesse Schell Presents 100+ sets of questions, or different lenses, for viewing a game’s design, encompassing diverse fields such as psychology, architecture, music, film, software engineering, theme park design, mathematics, anthropology, and more. Written by one of the world's top game designers, this book describes the deepest and most fundamental principles of game design, demonstrating how tactics used in board, card, and athletic games also work in video games. It provides practical instruction on creating world-class games that will be played again and again. View it here. A Practical Guide to Indie Game Marketing, by Joel Dreskin Marketing is an essential but too frequently overlooked or minimized component of the release plan for indie games. A Practical Guide to Indie Game Marketing provides you with the tools needed to build visibility and sell your indie games. With special focus on those developers with small budgets and limited staff and resources, this book is packed with tangible recommendations and techniques that you can put to use immediately. As a seasoned professional of the indie game arena, author Joel Dreskin gives you insight into practical, real-world experiences of marketing numerous successful games and also provides stories of the failures. View it here. An Architectural Approach to Level Design This is one of the first books to integrate architectural and spatial design theory with the field of level design. The book presents architectural techniques and theories for level designers to use in their own work. It connects architecture and level design in different ways that address the practical elements of how designers construct space and the experiential elements of how and why humans interact with this space. Throughout the text, readers learn skills for spatial layout, evoking emotion through gamespaces, and creating better levels through architectural theory. View it here. Learn more and download the ebook by clicking here. Did you know? GameDev.net and CRC Press also recently teamed up to bring GDNet+ Members up to a 20% discount on all CRC Press books. Learn more about this and other benefits here.
Sign in to follow this  
Followers 0
Programmer557

Why isn't this moving?

14 posts in this topic

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]

1

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
2

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.

0

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

1

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.

1

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.

0

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

1

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.

0

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.
0

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.

0

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.
0

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
2

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.
0

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.

0

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  
Followers 0