• Sprite

    Sprites, differently from animations, has methods that can make an image moves across the screen.

    The Sprite class extends the Animation class, this means that all that we've done on the animation part can be applied to sprites as well.

    1 - Creating an Sprite

    The Sprite class has two constructors:

    					//Pass the name of the image that will be shown, the frame rate will be 1.
    					public Sprite(String fileName);
    					
    					//Pass the name of the image that will be shown, and the frame rate that it has. 
    					public Sprite(String fileName, int numFrames);
    				

    Example: Creating a Sprite

    					/*
    					 * To change this template, choose Tools | Templates
    					 * and open the template in the editor.
    					 */
    
    					package Sprite001;
    
    					import JPlay.GameImage;
    					import JPlay.Sprite;
    					import JPlay.Window;
    
    					/**
    					 * @author Gefersom Cardoso Lima
    					 * Federal Fluminense University
    					 * Computer Science
    					 */
    
    					//Creates a sprite.
    					public class Sprite001
    					{
    						/**
    						 * @param args the command line arguments
    						 */
    						public static void main(String[] args)
    						{
    								Window janela = new Window(800,600);
    								GameImage fundo = new GameImage("fundo.png");
    								Sprite inuyasha = new Sprite("inuyasha.png", 13);
    
    								inuyasha.y = 250;
    								inuyasha.x = 200;
    
    								inuyasha.setSequence(0, 2);
    								inuyasha.setTotalDuration(1200);
    								while(true)
    								{
    									fundo.draw();
    									inuyasha.draw();
    									janela.display();
    
    									inuyasha.
    									inuyasha.update();
    								}
    						}
    
    					}
    
    				


    2 - Mooving a Sprite - Just on the X axis

    Will start to learn how to make a movement in a sprite using the coordinates (x,y) and the directional keys.

    To make a sprite moves along the X axis we just have to modify the value of the variable x."

    Example:
    the position of a sprite on the X axis is 100 (sprite.x = 100)
    to make a sprite moves to the 99 value, we just decrement the value of x: sprite.x--;
    or, to make a sprite moves to the 101 value we must increment the value of x: sprite.x++;

    The meaning of -- and ++ is to move the sprite along the X axis:

    '--' : makes the sprite moves 1 pixel to the left.
    '++' : makes the sprite moves 1 pixel to the right.

    The fact of a sprite moves to the left or to the right links to the way java manipulates the coordinates of the screen.

    Let's take a 798 pixel width screen, on Java the configuration of the screen will be:

    the left corner coordinate will start with 0 (x = 0).
    The right corner has the value 800 (x = 800).

    Desenho da coordenada X

    Example : Moving a sprite along the X axis

    					package Sprite002;
    
    					import JPlay.Keyboard;
    					import JPlay.Sprite;
    					import JPlay.Window;
    					import java.awt.Color;
    					import java.awt.Event;
    					import java.awt.event.KeyEvent;
    
    					/**
    					 * @author Gefersom Cardoso Lima
    					 * Federal Fluminense University
    					 * Computer Science
    					 */
    
    					public class Main
    					{
    						//Move the sprite by the x-axis
    						public static void main(String[] args)
    						{
    							Window janela = new Window(800,600);
    							Keyboard teclado = janela.getKeyboard();
    							teclado.addKey(KeyEvent.VK_N, Keyboard.DETECT_EVERY_PRESS);
    
    							Sprite sprite = new Sprite("boia.png");
    							sprite.y = 250;
    							sprite.x = 350;
    
    							boolean executando = true;
    							while(executando)
    							{
    									janela.clear(Color.black);
    									sprite.draw();
    									janela.display();
    
    									if(teclado.keyDown(Keyboard.LEFT_KEY))
    										sprite.x -= 3;
    									else
    										if( teclado.keyDown(Keyboard.RIGHT_KEY))
    											sprite.x += 3;
    
    									janela.delay(10);
    
    									if (teclado.keyDown(KeyEvent.VK_N) == true)
    										executando = false;
    
    							}
    							janela.exit();
    						}
    					}
    
    				


    3 - Moving a sprite - Along the Y axis

    To make a sprite move along the Y axis we have to modify the value of the y variable..

    Barra do eixo Y

    (y = 0)

    Example:
    The position of a sprite on the Y axis is 347 (sprite.y = 347).
    To make the sprite move to the value 340 we should decrement the value of y:
    sprite.y -= 7 .

    Or, to make the sprite move to the position 154, we shoul increment the value of y:
    sprite.y += 7;

    The meaning of '-7' and '+7' to the movement on the Y axis:

    '-7' : makes the sprite moves 7 pixels to the top.
    '+7' : makes the sprite moves 7 pixels to below.

    Imagine a 325 pixel width screen, on Java the configuration screen will be:

    The left corner coordinate starts with 0 (y = 0).
    The right corner coordinate starts with 355 (y = 355).

    (y = 355)


    Exemplo : Moving an sprite along the Y axis.

    					package Sprite004;
    
    					import JPlay.GameImage;
    					import JPlay.Keyboard;
    					import JPlay.Sprite;
    					import JPlay.Window;
    
    					/**
    					 * @author Gefersom Cardoso Lima
    					 * Universidade Federal Fluminense - UFF - Brasil - 2010
    					 * Ciência da Computação
    					 */
    					public class Sprite004
    					{
    						//Moving an sprite along the Y axis
    						public static void main(String[] args)
    						{
    							Window janela = new Window(800,600);
    							Keyboard keyboard = janela.getKeyboard();
    
    							GameImage backGround = new GameImage("mar.png");
    
    							Sprite sprite = new Sprite("boia.png");
    							sprite.y = 450;
    							sprite.x = 300;
    
    							while(true)
    							{
    									backGround.draw();
    									sprite.draw();
    									janela.display();
    
    									if( keyboard.keyDown(Keyboard.UP_KEY) )
    										sprite.y -= 7;
    									else
    										if( keyboard.keyDown(Keyboard.DOWN_KEY) )
    											sprite.y += 7;
    
    									janela.delay(50);
    							}
    						}
    					}				
    				


    4 - Moving a sprite along the screen inside a limited area..

    					package Sprite006;
    
    
    					import JPlay.GameImage;
    					import JPlay.Keyboard;
    					import JPlay.Sprite;
    					import JPlay.Window;
    
    					/**
    					 * @author Gefersom Cardoso Lima
    					 * Universidade Federal Fluminense - UFF - Brasil - 2010
    					 * Ciência da Computação
    					 */
    					public class Sprite006
    					{
    						//Delimitando a área de movimento do sprite no eixo X e no eixo Y
    						public static void main(String[] args)
    						{
    							Window janela = new Window(800,600);
    							Keyboard keyboard = janela.getKeyboard();
    
    							GameImage backGround = new GameImage("mar.png");
    
    							Sprite sprite = new Sprite("boia.png");
    							sprite.y = 400;
    							sprite.x = 300;
    
    							while(true)
    							{
    									backGround.draw();
    									sprite.draw();
    									janela.display();
    
    									//Move no eixo Y
    									if(keyboard.keyDown(Keyboard.UP_KEY) && sprite.y > 0)
    										sprite.y--;
    									else
    										if( keyboard.keyDown(Keyboard.DOWN_KEY) 
    										&& sprite.y  + sprite.height < janela.getHeight())
    										{
    											sprite.y++;
    										}
    
    									//Move no eixo X
    									if(keyboard.keyDown(Keyboard.LEFT_KEY) && sprite.x > 100)
    										sprite.x--;
    									else
    										if( keyboard.keyDown(Keyboard.RIGHT_KEY) && sprite.x  + sprite.width < 650)
    											sprite.x++;
    							}
    						}
    					}					
    				

    5 - Moving a sprite using methods of Sprite class

    To move a sprite, the following methods could be used:

     
    					//Move the sprite on the screen only on X axis, you must pass the velocity of the sprite's movement
    					public void moveX(double  velocity);
    
    					//Move the sprite on the screen only on Y axis, you must pass the velocity of the sprite's movement
    					public void moveY(double velocity);
    				

    Example: Moving a sprite on the X axis with the velocity 10, and on Y axis with the velocity of 6

     
    					package Sprite007;
    
    					import JPlay.GameImage;
    					import JPlay.Sprite;
    					import JPlay.Window;
    
    					/**
    					 * @author Gefersom Cardoso Lima
    					 * Universidade Federal Fluminense - UFF - Brasil - 2010
    					 * Ciência da Computação
    					 */
    					public class Sprite007
    					{
    						//Movendo um sprite usando os método moveY(double) e moveX(double)
    						public static void main(String[] args)
    						{
    							Window janela = new Window(800,600);
    
    							GameImage backGround = new GameImage("mar.png");
    
    							Sprite sprite = new Sprite("boia.png");
    							sprite.y = 400;
    							sprite.x = 300;
    
    							while(true)
    							{
    									backGround.draw();
    									sprite.draw();
    									janela.display();
    
    									//Move no eixo Y
    									sprite.moveY(6);//velocidade = 6
    
    									//Move no eixo X
    									sprite.moveX(10);//velocidade = 10
    							}
    						}
    					}
    				

    Ps.: the default keys to make a sprite move are de directional keys.

    To make a sprite move with other keys, use the following methods:

     
    					//Pass the key codes to make a sprite move to the left or to the right
    					public void moveX(int leftKey, int rightKey);
    					
    					//Pass the key codes to make a sprite move to the top or to the bottom
    					public void moveY(int upKey, int downKey);
    				

    Example: Move the sprite along the Y axis with the velocity 5 and using the arrows to the right and to the left.

     
    					package Sprite008;
    
    					import JPlay.GameImage;
    					import JPlay.Keyboard;
    					import JPlay.Sprite;
    					import JPlay.Window;
    
    					/**
    					 * @author Gefersom Cardoso Lima
    					 * Federal Fluminense University
    					 * Computer Science
    					 */
    					public class Sprite008
    					{  
    						//Moves the sprite by y-axis using direction buttons left and right.
    						public static void main(String[] args)
    						{
    							Window janela = new Window(800,600);
    							GameImage backGround = new GameImage("mar.png");
    
    							Sprite sprite = new Sprite("boia.png");
    							sprite.y = 400;
    							sprite.x = 300;
    
    							while(true)
    							{
    									backGround.draw();
    									sprite.draw();
    									janela.display();
    
    									//Keyboard.LEFT_KEY moves up
    									//Keyboard.RIGHT_KEY moves down
    									sprite.moveY(Keyboard.LEFT_KEY, Keyboard.RIGHT_KEY, 1);//velocity = 1
    							}
    						}
    					}
    			
    				

    Obs.: All the methods presented do not allow that the sprite moves out of the window.


    4 - Making a sprite jump

    Before making a sprite jump, lets inform the value of Y axis that will act as a floor, to do that use the method bellow:

    					public void setFloor(int floor);
    				

    To make a sprite jump, use the following method:

    					//Esse método usa a tecla SPACE para fazer o sprite pular.
    					public void jump();
    				

    Exemple:
    sprite.setFloor(500);
    sprite.jump(); the sprite will jump and when it fell, it will no cross the coordinate y = 500;


    To control the jump's velocity and high use:

    					//Pass the initial velocity of the sprite's jump.
    					public void setJumpVelocity(double velocity);
    				

    To know if a sprite is still on the jump, use:

    					//'true' is the sprite is jumping, 'false' otherwise
    					public boolean isJumping();
    				

    Example: Makes a sprite jumps and sets its floor.

    					package Sprite009;
    
    					import JPlay.GameImage;
    					import JPlay.Keyboard;
    					import JPlay.Sprite;
    					import JPlay.Window;
    					import java.awt.event.KeyEvent;
    
    					/**
    					 * @author Gefersom Cardoso Lima
    					 * Federal Fluminense University
    					 * Computer Science
    					 */
    					 
    					public class Sprite009
    					{
    						//Makes the sprite to jump.
    						public static void main(String[] args)
    						{
    							Window janela = new Window(800,600);
    							GameImage backGround = new GameImage("cena.png");
    
    							Sprite sprite = new Sprite("mario.png");
    							sprite.y = 440 - sprite.height;
    							sprite.x = 250;
    							sprite.setFloor(440);
    
    							while(true)
    							{
    									backGround.draw();
    									sprite.draw();
    									janela.display();
    
    									sprite.jump();
    									janela.delay(10);
    							}
    						}
    					}
    
    				


    5 - Changing the key used to jump

    By default, the key used to make the sprite jump is the space bar key, if you want to change this key use:

    				
    					//Pass the key code of the key it will be used for the jump.
    					public void jump(int keyCode);
    				

    Example: Make a sprite jump with de top arrow e determinate its floor

    				package Sprite010;
    
    
    				import JPlay.GameImage;
    				import JPlay.Keyboard;
    				import JPlay.Sprite;
    				import JPlay.Window;
    
    				/**
    				 * @author Gefersom Cardoso Lima
    				 * Federal Fluminense University
    				 * Computer Science
    				 */
    				 
    				public class Sprite010
    				{
    					//Make the sprite jump with the directional arrow up and set up its floor.
    					public static void main(String[] args)
    					{
    						Window janela = new Window(800,600);
    						GameImage backGround = new GameImage("cena.png");
    
    						Sprite sprite = new Sprite("mario.png");
    						sprite.y = 440 - sprite.height;
    						sprite.x = 250;
    						sprite.setFloor(440);
    
    						while(true)
    						{
    								backGround.draw();
    								sprite.draw();
    								janela.display();
    
    								sprite.jump(Keyboard.UP_KEY);
    								janela.delay(10);
    						}
    					}
    				}
    
    				


    9.6 - Simulating gravity

    To simulate a gravity efect use the method:

    					public void fall();
    				

    To know if a sprite touch the ground use the following method:

    					//Returns 'true' if the sprite has touched the ground, returns 'false' otherwise.
    					public boolean isOnFloor();
    				

    Like the jump, the value of the Y coordinate will serve as ground and must me passed before using the fall() method

    Para mudar o valor da gravidade use o método:

    					public void setGravity(double gravity).
    				

    Exemplo: Simulating the gravity efect.

    										/*
    					 * To change this template, choose Tools | Templates
    					 * and open the template in the editor.
    					 */
    
    					package Sprite011;
    
    
    					import JPlay.GameImage;
    					import JPlay.Sprite;
    					import JPlay.Window;
    
    					/**
    					 * @author Gefersom Cardoso Lima
    					 * Federal Fluminense University
    					 * Computer Science
    					 */
    					 
    					public class Sprite011
    					{
    						//Simulates the effect of gravity.
    						public static void main(String[] args)
    						{
    							Window janela = new Window(800,600);
    							GameImage backGround = new GameImage("cena.png");
    
    							Sprite sprite = new Sprite("mario.png");
    							sprite.y = 0;
    							sprite.x = 250;
    							sprite.setGravity(1);
    							sprite.setFloor(568);
    
    							while(true)
    							{
    									backGround.draw();
    									sprite.draw();
    									janela.display();
    
    									sprite.fall();
    									janela.delay(10);
    							}
    						}
    					}
    
    				

    9.7 - Moving the sprite from one point to another.

    To move a sprite from one point to another point on the screen without the user interaction, use:

    					//The values passed are the coordinates that you desire the sprite to move
    					public void moveTo(double x, double y);
    				


    Example: Moving a prite from one point to another point.

    					package Sprite012;
    
    					import JPlay.GameImage;
    					import JPlay.Sprite;
    					import JPlay.Window;
    
    					/**
    					 * @author Gefersom Cardoso Lima
    					 * Federal Fluminense University
    					 * Computer Science
    					 */
    					public class Sprite012
    					{
    						//Moves the sprite from one point to another.
    						public static void main(String[] args)
    						{
    							Window janela = new Window(800,565);
    							GameImage backGround = new GameImage("estrada.jpg");
    
    							Sprite sprite = new Sprite("bola.png");
    							sprite.y = 280;
    							sprite.x = 100;
    
    							backGround.draw();
    							sprite.draw();
    							janela.display();
    							janela.delay(2000);
    
    							while(true)
    							{
    									backGround.draw();
    									sprite.draw();
    									janela.display();
    									sprite.moveTo(490, sprite.y , 2);
    
    									if (sprite.x == 490)
    										sprite.moveTo(sprite.x, 35, 1);
    
    									janela.delay(4);
    							}
    						}
    					}
    				

    UFF - Universidade Federal Fluminense - Institudo de Computação - Ciência da Computação