Chunky by FelipeFS
Chunky by FelipeFS
GPWiki.org
It is currently Tue Oct 21, 2014 1:30 pm

All times are UTC




Post new topic Reply to topic  [ 12 posts ] 
Author Message
 Post subject: Creating Games
PostPosted: Thu Mar 21, 2013 4:46 pm 
Prolific Poster

Joined: Sat Apr 23, 2011 4:39 pm
Posts: 15
i'm trying do animation in my game. the left key do the left move and the left move have 4 subtimages. how can i change the subimage?


Top
 Profile  
 
 Post subject: Re: Creating Games
PostPosted: Thu Mar 21, 2013 9:01 pm 
Grand Optimizer

Joined: Sun Oct 16, 2011 3:09 pm
Posts: 365
Location: Here (where else?)
The usual way to do animation is to redraw the same area several times a second, and each time draw a different image or at a different position. That is, like like a movie, at 25fps it looks smooth to the eye. Note that you don't actually need 25fps, you can do with much less.

_________________
My project: Messing about in FreeRCT, dev blog, and IRC #freerct at oftc.net


Top
 Profile  
 
 Post subject: Re: Creating Games
PostPosted: Fri Mar 22, 2013 10:39 pm 
BANNED

Joined: Sun Jun 24, 2012 12:49 am
Posts: 504
Le le'ft keye do teh left mooove.


Top
 Profile  
 
 Post subject: Re: Creating Games
PostPosted: Sat Apr 27, 2013 8:44 pm 
Prolific Poster

Joined: Sat Apr 23, 2011 4:39 pm
Posts: 15
i create my class Sprite. and inside i build a timer for the animation and it's greate;)
i can share;)


Top
 Profile  
 
 Post subject: Re: Creating Games
PostPosted: Sun Apr 28, 2013 8:17 am 
Bytewise
User avatar

Joined: Sun Aug 05, 2012 9:32 pm
Posts: 268
If we are adding information to what seems to be a month dead thread i could as well add information
When doing animation with spritesheets I create a Sprite class and make functions nextFrame(), goToFrame(), startAnimation(), animationSpeed(). So I have as much power as possible around the animations. then I make an Update() function inside the class that updates everything that needs to be done as well as calling nextFrame if animated == true. and then In the game loop I call the update function for every sprite with a cycle that goes through all sprite objects visible on screen. :)

_________________
Did you ever wonder, how time works?


Top
 Profile  
 
 Post subject: Re: Creating Games
PostPosted: Sun Apr 28, 2013 10:32 am 
Prolific Poster

Joined: Sat Apr 23, 2011 4:39 pm
Posts: 15
if the thread is dead, i don't know. but i create it;)
your sprite it's great;)
maybe i win some ideias;)
let me ask 1 thing: what you use for control the keyboard?
how can i do a diagonal move? i know that i must change X and Y position, but i need understand how combine the keys;)


Top
 Profile  
 
 Post subject: Re: Creating Games
PostPosted: Sun Apr 28, 2013 5:47 pm 
Bytewise
User avatar

Joined: Sun Aug 05, 2012 9:32 pm
Posts: 268
Cambalinho wrote:
if the thread is dead, i don't know. but i create it;)
your sprite it's great;)
maybe i win some ideias;)
let me ask 1 thing: what you use for control the keyboard?
how can i do a diagonal move? i know that i must change X and Y position, but i need understand how combine the keys;)


I am using SDL library which has controls managed nicely, I just write it like
Code:
if (keystate[SDLK_UP]){
...code...
}


instead of SDLK_UP it can be anything SDLK_RIGHT/LEFT/DOWN or letter keys or function buttons.

The best way to handle keystrokes and combinations is to assign them a boolean value on their self. like
Code:
bool movingLeft = false;
bool movingRight = false;
bool movingUp = false;
bool movingDown = false;


it's best if you can use these as global variables or pointers, so you can access them from everywhere

then in the logic of the gameloop just write something like
Code:
if (movingLeft == true){
Player.x = Player.x - Player.Speed;
}
if (movingRight == true){
Player.x = Player.x + Player.Speed;
}
..Up..
..Down..


You could probably do it without the booleans, I don't remember but i think i had some problems with that tho. But if you use global variables and do all the logic in the logic (or update) section you are going to end up with much better managed code in opposite with just slamming half of it into the controls section.

the important thing is to do multiple IF statements here, if you just use one IF like this:
Code:
if (movingLeft == true){
Player.x = Player.x - Player.Speed;
}else if (movingRight == true){
Player.x = Player.x + Player.Speed;
}else if ...UP...DOWN...

then you will be only able to use one button at time, which is obviously not ideal.
if you use multiple statements tho and you, lets say press UP and RIGHT the code will go through both of them resulting in
Player.x = Player.x + Player.Speed; AND Player.y = Player.y - Player.Speed, and this will create a diagonal movement of the sprite. After that you just need to update the screen and flip the buffer :)

_________________
Did you ever wonder, how time works?


Top
 Profile  
 
 Post subject: Re: Creating Games
PostPosted: Sun Apr 28, 2013 6:23 pm 
Prolific Poster

Joined: Sat Apr 23, 2011 4:39 pm
Posts: 15
Hazarth wrote:
Cambalinho wrote:
if the thread is dead, i don't know. but i create it;)
your sprite it's great;)
maybe i win some ideias;)
let me ask 1 thing: what you use for control the keyboard?
how can i do a diagonal move? i know that i must change X and Y position, but i need understand how combine the keys;)


I am using SDL library which has controls managed nicely, I just write it like
Code:
if (keystate[SDLK_UP]){
...code...
}


instead of SDLK_UP it can be anything SDLK_RIGHT/LEFT/DOWN or letter keys or function buttons.

The best way to handle keystrokes and combinations is to assign them a boolean value on their self. like
Code:
bool movingLeft = false;
bool movingRight = false;
bool movingUp = false;
bool movingDown = false;


it's best if you can use these as global variables or pointers, so you can access them from everywhere

then in the logic of the gameloop just write something like
Code:
if (movingLeft == true){
Player.x = Player.x - Player.Speed;
}
if (movingRight == true){
Player.x = Player.x + Player.Speed;
}
..Up..
..Down..


You could probably do it without the booleans, I don't remember but i think i had some problems with that tho. But if you use global variables and do all the logic in the logic (or update) section you are going to end up with much better managed code in opposite with just slamming half of it into the controls section.

the important thing is to do multiple IF statements here, if you just use one IF like this:
Code:
if (movingLeft == true){
Player.x = Player.x - Player.Speed;
}else if (movingRight == true){
Player.x = Player.x + Player.Speed;
}else if ...UP...DOWN...

then you will be only able to use one button at time, which is obviously not ideal.
if you use multiple statements tho and you, lets say press UP and RIGHT the code will go through both of them resulting in
Player.x = Player.x + Player.Speed; AND Player.y = Player.y - Player.Speed, and this will create a diagonal movement of the sprite. After that you just need to update the screen and flip the buffer :)

thanks for the tip;)
do you know use GetAsyncKeyState()?
how can i test no key?


Top
 Profile  
 
 Post subject: Re: Creating Games
PostPosted: Sun Apr 28, 2013 8:14 pm 
Bytewise
User avatar

Joined: Sun Aug 05, 2012 9:32 pm
Posts: 268
Cambalinho wrote:
thanks for the tip;)
do you know use GetAsyncKeyState()?
how can i test no key?


I am not using GetAsyncKeyState() all that much but I can tell you what I know.

GetAsyncKeyState() returns a short if a key was pressed. This is mostly used in cycles like this

Code:
bool GameLoop = true;
While (GameLoop){

if (GetAsyncKeyState(VK_UP)){
...DoSomething...
}

}


if the UP key is not pressed the function returns value 0 meaning the IF statement will be skipped
if the UP key IS pressed the function returns value different from 0 and the IF statement will be accepted

The problem is that the loop will go through the code like thousands of times, so the DoSomething would actually be called thousands of time, so you need some kind of a protection against that. What I used was something like this

Code:
bool GameLoop = true;
bool Pressure = false;                //Pressure lock variable

while(GameLoop){
  if (GetAsyncKeyState(VK_UP)){  //On Key press
    if (!Pressure){                       //Do Stuff if UP is not already pressed
    ...DoSomethingOnce...
    }
  Pressure = true;                       //Set the pressure lock
  }

  if (!GetAsyncKeyState(VK_UP)){   //On Key release
  Pressure = false;                       //Release the pressure lock
  }

}


you can then set a variable like movingUp = true; or do something else you need...

The code can be used without the pressure lock for directly moving sprites around by doing

Code:
bool GameLoop = true;

while(GameLoop){
  if (GetAsyncKeyState(VK_UP)){         //On Key press
  Player.y = Player.y - Player.Speed;     //Move player up
  }
}


but again this would probably end up with messy coding...

Also you need to make some kind of FPS (frames per second) regulator so it doesn't run 2000 times on one machine and 50 000 times on other.

Tho I am not sure how to check if all keys are released... you can probably use a cycle like this

Code:
bool noKeys = true;
for(int allKeys = 0; allKeys < 256; allKeys++){
  if (GetAsyncKeyState(allKeys)){
  noKeys = false;
  }
}


after the above cycle noKeys should hold the value TRUE if no keys were pressed or FALSE if there was something pressed at that time.

_________________
Did you ever wonder, how time works?


Top
 Profile  
 
 Post subject: Re: Creating Games
PostPosted: Sun Apr 28, 2013 9:02 pm 
Prolific Poster

Joined: Sat Apr 23, 2011 4:39 pm
Posts: 15
Hazarth wrote:
Cambalinho wrote:
thanks for the tip;)
do you know use GetAsyncKeyState()?
how can i test no key?


I am not using GetAsyncKeyState() all that much but I can tell you what I know.

GetAsyncKeyState() returns a short if a key was pressed. This is mostly used in cycles like this

Code:
bool GameLoop = true;
While (GameLoop){

if (GetAsyncKeyState(VK_UP)){
...DoSomething...
}

}


if the UP key is not pressed the function returns value 0 meaning the IF statement will be skipped
if the UP key IS pressed the function returns value different from 0 and the IF statement will be accepted

The problem is that the loop will go through the code like thousands of times, so the DoSomething would actually be called thousands of time, so you need some kind of a protection against that. What I used was something like this

Code:
bool GameLoop = true;
bool Pressure = false;                //Pressure lock variable

while(GameLoop){
  if (GetAsyncKeyState(VK_UP)){  //On Key press
    if (!Pressure){                       //Do Stuff if UP is not already pressed
    ...DoSomethingOnce...
    }
  Pressure = true;                       //Set the pressure lock
  }

  if (!GetAsyncKeyState(VK_UP)){   //On Key release
  Pressure = false;                       //Release the pressure lock
  }

}


you can then set a variable like movingUp = true; or do something else you need...

The code can be used without the pressure lock for directly moving sprites around by doing

Code:
bool GameLoop = true;

while(GameLoop){
  if (GetAsyncKeyState(VK_UP)){         //On Key press
  Player.y = Player.y - Player.Speed;     //Move player up
  }
}


but again this would probably end up with messy coding...

Also you need to make some kind of FPS (frames per second) regulator so it doesn't run 2000 times on one machine and 50 000 times on other.

Tho I am not sure how to check if all keys are released... you can probably use a cycle like this

Code:
bool noKeys = true;
for(int allKeys = 0; allKeys < 256; allKeys++){
  if (GetAsyncKeyState(allKeys)){
  noKeys = false;
  }
}


after the above cycle noKeys should hold the value TRUE if no keys were pressed or FALSE if there was something pressed at that time.

now i understand why my combination key wans't working;)
- if i test up(for exemple) before up + left(for exemple), the up+left isn't executed;)
thanks for all;)


Top
 Profile  
 
 Post subject: Re: Creating Games
PostPosted: Mon Apr 29, 2013 6:54 am 
Grand Optimizer

Joined: Sun Oct 16, 2011 3:09 pm
Posts: 365
Location: Here (where else?)
An alternative solution is to communicate movement direction instead of keys. Globally something like
Code:
int dx, dy;
while (true) { // Do forever game loop
    dx = 0; dy = 0; // Set dx and dy based on what keys are pressed.
    if (LeftKeyPressed()) dx--;
    if (RightKeyPressed()) dx++;
    if (UpKeyPressed()) dy++;
    if (DownKeyPressed()) dy--;

    // ....  Other stuff in the game loop

    // Move player
    player.x += dx;
    player.y += dy;

    // ... More stuff in the game loop
}
The key detection code sets horizontal and vertical change of position, which you can simply add to the current player position.

_________________
My project: Messing about in FreeRCT, dev blog, and IRC #freerct at oftc.net


Top
 Profile  
 
 Post subject: Re: Creating Games
PostPosted: Mon Apr 29, 2013 10:45 am 
Prolific Poster

Joined: Sat Apr 23, 2011 4:39 pm
Posts: 15
Alberth wrote:
An alternative solution is to communicate movement direction instead of keys. Globally something like
Code:
int dx, dy;
while (true) { // Do forever game loop
    dx = 0; dy = 0; // Set dx and dy based on what keys are pressed.
    if (LeftKeyPressed()) dx--;
    if (RightKeyPressed()) dx++;
    if (UpKeyPressed()) dy++;
    if (DownKeyPressed()) dy--;

    // ....  Other stuff in the game loop

    // Move player
    player.x += dx;
    player.y += dy;

    // ... More stuff in the game loop
}
The key detection code sets horizontal and vertical change of position, which you can simply add to the current player position.

thanks for all


Top
 Profile  
 
Display posts from previous:  Sort by  
Post new topic Reply to topic  [ 12 posts ] 

All times are UTC


Who is online

Users browsing this forum: No registered users and 1 guest


You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum

Search for:
Jump to:  

Powered by phpBB® Forum Software © phpBB Group