Я столкнулся с одной проблемой при попытке нарисовать Fizzy с помощью Slick2D.Я думаю, что рисую плохо, потому что я не знаю, в чем еще может быть проблема.
Вот видео с моей работающей программой: http://www.youtube.com/watch?v=Q5THJ54hHBw
А вот код:
import org.jbox2d.common.Vec2;
import org.newdawn.fizzy.DynamicBody;
import org.newdawn.fizzy.Rectangle;
import org.newdawn.fizzy.StaticBody;
import org.newdawn.fizzy.World;
import org.newdawn.slick.GameContainer;
import org.newdawn.slick.Graphics;
import org.newdawn.slick.Image;
import org.newdawn.slick.Input;
import org.newdawn.slick.SlickException;
import org.newdawn.slick.state.BasicGameState;
import org.newdawn.slick.state.StateBasedGame;
public class GameplayState extends BasicGameState {
private World world;
private Rectangle floorShape;
private StaticBody floorBody;
private Rectangle manShape;
private DynamicBody manBody;
private Rectangle o1Shape;
private DynamicBody o1Body;
private Rectangle o2Shape;
private DynamicBody o2Body;
private Image podeaua;
private Image om;
private Image obs;
private int dir = 1;
private int stateID;
SlickDebugDraw sDD;
public GameplayState(int stateID) {
this.stateID = stateID;
}
@Override
public void init(GameContainer container, StateBasedGame game)
throws SlickException {
world = new World(new Vec2(0, 10));
floorShape = new Rectangle(800, 30);
floorBody = new StaticBody(floorShape, 0, 570);
floorBody.setRestitution(0);
manShape = new Rectangle(30,30);
manBody = new DynamicBody(manShape, 400, 530);
manBody.setRestitution(0);
o1Shape = new Rectangle(30,30);
o1Body = new DynamicBody(o1Shape, 350, 530);
o1Body.setRestitution(0);
o2Shape = new Rectangle(30,30);
o2Body = new DynamicBody(o2Shape, 460, 530);
o2Body.setRestitution(0);
podeaua = new Image("data/podeaua.jpg");
om = new Image("data/om.jpg");
obs = new Image("data/obs.jpg");
world.add(floorBody);
world.add(manBody);
world.add(o1Body);
world.add(o2Body);
}
@Override
public void render(GameContainer container, StateBasedGame game, Graphics g)
throws SlickException {
g.drawImage(podeaua,
floorBody.getX(),
floorBody.getY());
g.drawImage(om,
manBody.getX(),
manBody.getY());
g.drawImage(obs,
o1Body.getX(),
o1Body.getY());
g.drawImage(obs,
o2Body.getX(),
o2Body.getY());
}
@Override
public void update(GameContainer container, StateBasedGame game, int delta)
throws SlickException {
Input input = container.getInput();
if(input.isKeyDown(Input.KEY_A)){
manBody.setVelocity(-10, manBody.getYVelocity());
} else if(input.isKeyDown(Input.KEY_D)){
manBody.setVelocity(10, manBody.getYVelocity());
} else {
manBody.setVelocity(0, manBody.getYVelocity());
}
if(input.isKeyDown(Input.KEY_W) && manBody.isTouching(floorBody))
{
manBody.applyImpulse(0, -2000);
}
if(input.isMouseButtonDown(input.MOUSE_LEFT_BUTTON)) {
manBody.setAngularVelocity(0);
manBody.setRotation(0);
}
if(input.isMouseButtonDown(input.MOUSE_RIGHT_BUTTON)) {
manBody.setAngularVelocity(+2f);
}
podeaua.setRotation((float) Math.toDegrees(floorBody.getRotation()));
om.setRotation((float) Math.toDegrees(manBody.getRotation()));
world.update(delta * 0.001f);
}
@Override
public int getID() {
// TODO Auto-generated method stub
return stateID;
}
}