How to move shapes with keyboard in slick 2d

Im trying to move a shape, an octagon, in my game but I dont know how. Here is what I have so far

import java.awt.Container;

import org.newdawn.slick.AppGameContainer;
import org.newdawn.slick.BasicGame;
import org.newdawn.slick.Color;
import org.newdawn.slick.GameContainer;
import org.newdawn.slick.Graphics;
import org.newdawn.slick.Input;
import org.newdawn.slick.SlickException;
import org.newdawn.slick.geom.Circle;
import org.newdawn.slick.geom.Polygon;
import org.newdawn.slick.geom.Rectangle;
import org.newdawn.slick.geom.Shape;


public class ShapesAndCollisionTest extends BasicGame{
	
	private Shape circle;
	private boolean collides = false;
	private float x = 300;
	private float y = 100;
	float speed = .25f;
	private Shape octagon;
	
	public ShapesAndCollisionTest(String title) {
		
		super(title);
	}
	
	public void init(GameContainer gc) throws SlickException {
		
		circle =  new Circle(100, 100, 50);// x, y, radius
		octagon = new Circle(x, y, 30, 8);//x, y, radius, number of segments
		
	}
	
	public void render(GameContainer gc, Graphics g) throws SlickException {
		
		//set the color
		g.setColor(new Color(0, 255, 255));//inside color
		g.fill(circle);
		g.setColor(new Color(255, 0, 0));//red, green, blue OUTLINE of circle
		g.draw(circle);
				
		g.setColor(Color.magenta);
		g.draw(octagon);
		
		//just show whether the collision is changing or not
		g.drawString("Collides " + collides, 350, 30);
	}
	
	public void update(GameContainer gc, int delta) throws SlickException {
		
		Input input = gc.getInput();
		//control octogon
		//up
		if(input.isKeyDown(Input.KEY_UP)){
			y = y - speed * delta;//delta makes the speed run the same on ANY computer
		}
		//down
		if(input.isKeyDown(Input.KEY_DOWN)){
			y = y + speed * delta;
		}
		//left
		if(input.isKeyDown(Input.KEY_LEFT)){
			x = x - speed * delta;
		}
		//right
		if(input.isKeyDown(Input.KEY_RIGHT)){
			x = x + speed * delta;
		}
		
		//check if it collides
		collides = circle.intersects(octagon);
		
	}

Well… First of all you need to call update() for anything to happen. :slight_smile: Second, since the delta is time between frames per second it shouldn´t be an int. Let´s say your game is running at 60 fps, every frame is 1/60 seconds or 0,016 seconds so that int is most likely going to be zero. The only way that delta value can be correct if is your game is running at 1 fps and nobody want´s that. :slight_smile: Change it to float and then I suggest this post about game loop: http://www.java-gaming.org/index.php?topic=24220.0

Do you have any specific problem you need help with?

But doesnt everything automatically work when i call start()? I mean the game already runs and updates itself right now…or is there more to the update method than just moving stuff around?
And the question I have is how can I make a shape object move around? I know how to make pictures and “built in” shapes move around like this:

	public void render(GameContainer gc, Graphics g) throws SlickException {
		
	
		g.drawOval(x, y, 40, 60, 50);//move the octagon around
		
		//just show whether the collision is changing or not
		g.drawString("Collides " + collides, 350, 30);
	}
	
	public void update(GameContainer gc, int delta) throws SlickException {
		
		Input input = gc.getInput();
		//control octogon
		//up
		if(input.isKeyDown(Input.KEY_UP)){
			y = y - speed * delta;//delta makes the speed run the same on ANY computer
		}
		//down
		if(input.isKeyDown(Input.KEY_DOWN)){
			y = y + speed * delta;
		}
		//left
		if(input.isKeyDown(Input.KEY_LEFT)){
			x = x - speed * delta;
		}
		//right
		if(input.isKeyDown(Input.KEY_RIGHT)){
			x = x + speed * delta;
		}
		

	}

But, if I want to do something like the following in my update method

		//check if it collides
		collides = circle.intersects(oval);

for a non object shape, like in line 4, then I have no idea how to pass in the value(oval) into the parameter. Right now Im just getting my feet wet with the whole collision thing because I have made another project that is essentially a kids maze game, but its only fault as of now is that the player can go through the lines(the walls in the game). Soooooo if I figure this out I will be able to figure that out too, I think. BUT, I’m a noob and need help still.

You don’t need to pass in anything else. Slick handles the collision detection internally, and gets the coordinates of the two objects without you manually telling you where they are.

Like someone mentioned above, delta should not be an integer. Slick also handles updating internally, contrary to what Axeman said, so don’t worry.

But when I change delta to a float I get an error stating that it needs to be an int. Also, is there a way that I can make slick detect a certain color in the background and not allow the user to pass it, like a maze? Or would I just have to outline the areas with shapes and make it go through the collision detection thing?

Well it seems Slick uses an integer for delta time, which is interesting. As for your collision detection question, no I don’t believe Slick2D will handle color picking for you. If you really want to, you can always do it yourself, but I wouldn’t. Use AABBs (Axis Aligned Bounding Boxes). Basically, yes you should outline the area with shapes and detect if you collide with them.

I always hesitated when I made my own delta system in float value. I still wonder to this day why slick is using an integer, and if it is the ‘correct’ way. You would go pretty speedy at just 2px/sec.

Whoops! Sorry for doing nothing but adding confusion. :slight_smile: