PrettyPrint

Sunday, 24 February 2013

Creating The Game - 24/02/2013

This week there has been a large amount of work that has been done on the Game but there is still a lot of work to be done on the game. There has also been no meeting between the members of the group as with it being half term some members were unable to attend due to being out of the country, but I have spoken to Stephen during the week and he has given me the completed animation frames for the character allowing me to see for the first time what the character and animation will look like now that it is in the 32x64 dimensions.

This week I myself have concentrated on getting a pseudo gravity system into the game which I severely underestimated on the complexity of the coding that would have to go into this. To start off I figured out how to get a smooth jump from the character instead of just being able to walk up and down whenever the user pleased meaning that they can press the jump key once and the character will the perform the jump, the complex part of this was handling collisions while in the air allowing for the character to come back down if they were to collide with a block above them while jumping, but this was all figured out over the week and has been successfully implemented into the game along with the feature of the character changing its sprite depending on the direction it is facing, this means that if the user presses the left key the character will now face left but there is still no animation within the game, all of these features have been implemented into the game using the code below.

Jumping Gravity + Sprite Change
if ((input.isKeyPressed(Input.KEY_UP) || input.isKeyPressed(Input.KEY_W)) && !jumping){

   if(lastKeyLeft){
    player = jumpLeft;
   } else {
    player = jumpRight;
   }

   gravSpeeds = -12;
   jumping = true;
   }

   if(jumping && gravSpeeds < 12) { 
    gravSpeeds += 0.5; 
   } 

   if (jumping == false){
    gravSpeeds = 12;
    if (lastKeyLeft == true){
     player = moveLeft;
    } else {
     player = moveRight;
    }
   }

   y += gravSpeeds;
   origY = y - gravSpeeds;

   playerPoly.setY(y);
   if (entityCollisionWith()){

    if (jumping && gravSpeeds < ((gravSpeeds * (-1)))){
     gravSpeeds = 0;
     gravSpeeds += 0.5;
     y += gravSpeeds;
    }

    if ( gravSpeeds > 0) {
     gravSpeeds = 0;
     jumping = false;
     y = origY;
     playerPoly.setY(y);
    }  
   } 


   if (input.isKeyDown(Input.KEY_DOWN) || input.isKeyDown(Input.KEY_S))
   {
    y += floatDelta;
    playerPoly.setY(y);
    if (entityCollisionWith()){
     y -= floatDelta;
     playerPoly.setY(y);
    }
   }

   if (input.isKeyDown(Input.KEY_LEFT) || input.isKeyDown(Input.KEY_A))
   {
    lastKeyLeft = true;

    if (jumping){
     player = jumpLeft;
     jumpLeft.update(delta);
    } else {
     player = moveLeft;
     moveLeft.update(delta);
    }
    x -= floatDelta;
    playerPoly.setX(x);
    if (entityCollisionWith()){
     x += floatDelta;
     playerPoly.setX(x);
    }
   } else {
    moveLeft.setCurrentFrame(0);
   }

   if (input.isKeyDown(Input.KEY_RIGHT) || input.isKeyDown(Input.KEY_D))
   {
    lastKeyLeft = false;

    if (jumping){
     player = jumpRight;
     jumpRight.update(delta);
    } else {
     player = moveRight;
     moveRight.update(delta);
    }
    x += floatDelta;
    playerPoly.setX(x);
    if (entityCollisionWith()){
     x -= floatDelta;
     playerPoly.setX(x);
    }
   }
   else {
    moveRight.setCurrentFrame(0);
   }

Animation Code
  player = new Animation();
  player.setAutoUpdate(true);
  Image [] walkRight = {new Image ("data/Sprites/CharStand.png"), 
                        new Image ("data/Sprites/CharStep1.png"), 
                        new Image ("data/Sprites/CharStep2.png")};
  Image [] walkLeft = {new Image ("data/Sprites/CharStandBACKWARDS.png"), 
                       new Image ("data/Sprites/CharStep1BACKWARDS.png"), 
                       new Image ("data/Sprites/CharStep2BACKWARDS.png")};
  Image [] jumpingLeft = {new Image ("data/Sprites/CharJumpBACKWARDS.png")};
  Image [] jumpingRight = {new Image ("data/Sprites/CharJump.png")};

  moveRight = new Animation(walkRight, 200, false);
  moveLeft = new Animation(walkLeft , 200, false);
  jumpRight = new Animation(jumpingRight, 200, false);
  jumpLeft = new Animation(jumpingLeft, 200, false);

  jumpRight.setPingPong(true);
  jumpLeft.setPingPong(true);
  moveRight.setPingPong(true);
  moveLeft.setPingPong(true);

  player = moveRight;

Sunday, 17 February 2013

Creating The Game - 17/02/2013

This week I have been concentrating on getting the collision within the game working, and this was one of the hardest things that I have done with the project to date. But first I will discuss the what was said at the short group discussion that we had. Stephen has created some more tiles for me to use within the map creation but we have decided that we are not going to use them, because of this he has started creating the animation frames for the character walking and jumping that I will use to animate the character in the game. Sam has completed the jump sound which will be played when the character jumps within the game to his liking and has started on a collection sound for picking up coins. Andy has started to create the real flash animation from one of his designs that he has chosen for this and the whole project is still moving in the right direction.

This week as stated before I have been focusing on getting the collision working within the game. This means that the character will not be able to move where there is a block being rendered on the screen. As I had originally thought, this was extremely hard to get the grasp of but finally after looking through many different resources I understand how this is done and have implemented it successfully into the game. The basics of the collision system are that the TileD map that I created is loaded as a blockmap allowing Slick2D to read each tile and see if anything is being drawn there according to a specific tile ID. If there is a tile meant to be there then this is added to an array of entities which store the size of the block and where it is. Once this is done a polygon box is drawn around the players character and all of the blocks that are in the map, when the user is attempting to move the game checks to see if these polygon boxes ever intersect by looking through each object in the array and making sure the co-ordinates never intersect, if they do the movement is blocked and stops the character from moving through this object. All of the code that was written for this is down below.

BlockMap Class
import java.util.ArrayList;
import org.newdawn.slick.SlickException;
import org.newdawn.slick.tiled.TiledMap;
 
public class BlockMap {
	public static TiledMap tmap;
	public static int mapWidth;
	public static int mapHeight;
	private int square[] = {1,1,31,1,31,31,1,31}; //square shaped tile
	public static ArrayList(objects) entities;
 
	public BlockMap(String ref) throws SlickException {
		entities = new ArrayList(objects)();
		tmap = new TiledMap(ref, "data");
		mapWidth = tmap.getWidth() * tmap.getTileWidth();
		mapHeight = tmap.getHeight() * tmap.getTileHeight();
 
		for (int x = 0; x < tmap.getWidth(); x++) {
			for (int y = 0; y < tmap.getHeight(); y++) {
				int tileID = tmap.getTileId(x, y, 0);
				if (tileID == 1) {
					entities.add(
                                        new Block(x * 32, y * 32, square, "square")
                                        );
				}
			}
		}
	}
}

Block Class
import org.newdawn.slick.Graphics;
import org.newdawn.slick.geom.Polygon;
 
public class Block  {
	public Polygon poly;
	public Block(int x, int y, int test[],String type) {
        poly = new Polygon(new float[]{
				x+test[0], y+test[1],
				x+test[2], y+test[3],
				x+test[4], y+test[5],
				x+test[6], y+test[7],
        });   
	}
 
	public void update(int delta) {
	}
 
	public void draw(Graphics g) {
		g.draw(poly);
	}
}

Collision Check Method
	public boolean entityCollisionWith() throws SlickException {
		for (int i = 0; i < BlockMap.entities.size(); i++) {
			Block entity1 = (Block) BlockMap.entities.get(i);
			if (playerPoly.intersects(entity1.poly)) {
				return true;
			}       
		}       
		return false;

	}

Updated Game Code - Movement
		if (input.isKeyPressed(Input.KEY_UP)){
			y -= floatDelta;
			playerPoly.setY(y);
			if (entityCollisionWith()){
				y += floatDelta;
				playerPoly.setY(y);
			}
		}

		if (input.isKeyDown(Input.KEY_DOWN))
		{
			y += floatDelta;
			playerPoly.setY(y);
			if (entityCollisionWith()){
				y -= floatDelta;
				playerPoly.setY(y);
			}
		}

		if (input.isKeyDown(Input.KEY_LEFT))
		{
			x -= floatDelta;
			playerPoly.setX(x);
			if (entityCollisionWith()){
				x += floatDelta;
				playerPoly.setX(x);
			}
		}

		if (input.isKeyDown(Input.KEY_RIGHT))
		{
			x += floatDelta;
			playerPoly.setX(x);
			if (entityCollisionWith()){
				x -= floatDelta;
				playerPoly.setX(x);
			}
		}

	}

Sunday, 10 February 2013

Creating The Game - 10/02/2013

This week there was no meeting due to time restraints from members of the group, but I have been speaking with Stephen as he has now completed the character sprite using the 32x64 dimensions that we discussed last week. I have not spoken with the other two members of the group about their EPQ work but we are still progressing nicely with the project and I am happy overall with the work.

This week I have been concentrating on making the character that I have drawn to the screen moving in certain directions decided by which buttons on the keyboard have been pressed. This was a much harder task than I first thought but after reading through the available documentation for Slick2D and looking once again at the sample code that is available to me I have created a working movement system for the game. The code I have written allows the user to press either WASD or The arrow keys on the keyboard to control the movement of the character, seeing if the keys are being pressed is helpfully implemented with the input class of Slick2D allowing me to easily check if a key is either been pressed or held down which is very handy indeed. If the button is pressed down the variables of Y and X which are the current co-ordinates are adjusted accordingly and then the image is redrawn at these co-ordinates once updated. The code I have written is below:


 public void update(GameContainer gc, int delta) throws SlickException {

  Input input = gc.getInput();
  float floatDelta = delta*0.1f;

  
  if (input.isKeyDown(Input.KEY_UP)){
   y -= floatDelta;
  }

  if (input.isKeyDown(Input.KEY_DOWN))
  {
   y += floatDelta;
  }

  if (input.isKeyDown(Input.KEY_LEFT))
  {
   x -= floatDelta;
  }

  if (input.isKeyDown(Input.KEY_RIGHT))
  {
   x += floatDelta;
  }

 }

Sunday, 3 February 2013

Creating The Game - 03/02/2013

This week I have been successful in rendering a map to the screen using some of the art assets that Stephen has given to me. But first as with every week I will be discussing what has been said within the group meeting on Saturday. This week me and Stephen decided that having the character a 32x32 which is the same size as the blocks used within the map would not look right, because of this we decided that a 32x64 sprite would be better used for the character and this is now what Stephen has set about designing. Sam has completed the first complete draft of the level music which now sounds a lot better after the discussion we had last week, there a a few improvements that he decided that he would like to make but after this he will start working on the sound effects that will be used within the game such as for jumping and collecting coins. Andy has finished his initial designs and has started creating some very early flash animations and posters from these designs to see what these will look like and decide from this which one he will be using.

This week I have as described up above managed to render an image of what the map will be like within the game. First of all to do this I created the map image in a program called TileD. This is a very highly praised program as it allows for the creation of 2D maps very quickly and easily using a painting style interface with the tiles that you import. Once the map has been created the program allows you to export the map as a .TMX file which can then be read instantly by Slick2D and then rendered onto the screen. This is only the image of the map first of all though as such there is no collision or any other features within the game. I have also managed to get a representation of the played rendered on the screen, for which I have just used a 32x64 coloured box until such time that I have the actual character sprite from Stephen. This is some major progress as I now know draw images onto the screen with great ease and how to draw these images at certain points. By next week I hope to have movement working with the sprite that Stephen will be sending me during the week.