你好,我正在开发一个游戏,现在我正在尝试让用户名跟随角色,我让屏幕跟随角色,但是当屏幕开始跟随角色时,字符串开始飞走我已经尝试了一切,包括为它制作单独的 X 和 Y。这是我的代码,请帮助:
import java.awt.image.BufferedImage;
import java.awt.Toolkit;
import org.newdawn.slick.*;
import org.newdawn.slick.state.*;
import org.newdawn.slick.GameContainer;
import org.newdawn.slick.Graphics;
import org.newdawn.slick.Input;
import org.newdawn.slick.SlickException;
import org.newdawn.slick.state.BasicGameState;
import org.newdawn.slick.state.StateBasedGame;
public class Play extends BasicGameState {
private BufferedImage screen = new BufferedImage(Core.HEIGHT, Core.WIDTH, BufferedImage.TYPE_INT_RGB);
Animation now, moveUp, moveDown, moveRight, moveLeft;
/*
Image Up1;
Image Up2;
Image Down1;
Image Down2;
Image Right1;
Image Right2;
Image Left1;
Image Left2;
*/
Image map;
Image collision;
boolean exitMenu = false;
int[] duration = {200, 200};
int escapeInt = 1;
int Y = 150;
int X = 150;
public String currentUser = "Evan";
int usernameY;
int usernameX;
int camY;
int camX;
int WORLD_SIZE_X = 5000;
int WORLD_SIZE_Y = 5000;
int VIEWPORT_SIZE_X = 1366;
int VIEWPORT_SIZE_Y = 768;
int offsetMaxX = WORLD_SIZE_X - VIEWPORT_SIZE_X;
int offsetMaxY = WORLD_SIZE_Y - VIEWPORT_SIZE_Y;
int offsetMinX = 0;
int offsetMinY = 0;
public Play(int state) {
}
public void init(GameContainer gc, StateBasedGame sbg) throws SlickException {
/*
Up1 = new Image("res/Up1.png");
Up2 = new Image("res/Up2.png");
Down1 = new Image("res/Down1.png");
Down2 = new Image("res/Down2.png");
Right1 = new Image("res/Right1.png");
Right2 = new Image("res/Right2.png");
Left1 = new Image("res/Left1.png");
Left2 = new Image("res/Left2.png");
*/
Image[] walkUp = {new Image("res/Up1.png"), new Image("res/Up2.png")};
Image[] walkDown = {new Image("res/Down1.png"), new Image("res/Down2.png")};
Image[] walkRight = {new Image("res/Right1.png"), new Image("res/Right2.png")};
Image[] walkLeft = {new Image("res/Left1.png"), new Image("res/Left2.png")};
moveUp = new Animation(walkUp, duration, false);
moveDown = new Animation(walkDown, duration, false);
moveLeft = new Animation(walkLeft, duration, false);
moveRight = new Animation(walkRight, duration, false);
now = moveDown;
map = new Image("res/Map.jpg");
collision = new Image("res/Collision.png");
}
public void update(GameContainer gc, StateBasedGame sbg, int delta) throws SlickException {
Input input = gc.getInput();
if(input.isKeyDown(input.KEY_W)) {
Y -= 1;
usernameY = Y;
now = moveUp;
}
if(input.isKeyDown(input.KEY_S)) {
Y += 1;
usernameY = Y;
now = moveDown;
}
if(input.isKeyDown(input.KEY_A)) {
X -= 1;
usernameX = X;
now = moveLeft;
}
if(input.isKeyDown(input.KEY_D)) {
X += 1;
usernameX = X;
now = moveRight;
}
if(input.isKeyDown(input.KEY_ESCAPE)) {
System.exit(3);
}
camX = X - VIEWPORT_SIZE_X / 2;
camY = Y - VIEWPORT_SIZE_Y / 2;
if (camX > offsetMaxX){ camX = offsetMaxX; }
if (camX < offsetMinX){ camX = offsetMinX;}
if (camY > offsetMaxY){ camY = offsetMaxY; }
if (camY < offsetMinY){ camY = offsetMinY;}
}
public void render(GameContainer gc, StateBasedGame sbg, Graphics g) throws SlickException {
g.translate( -camX, -camY);
map.draw(0,0);
collision.draw(0, 0);
now.draw(X, Y);
g.drawString("X: "+X+" " + "Y: "+ Y, 20 + camX, 20 + camY);
g.drawString(currentUser, usernameX + camX, usernameY + camY);
}
public int getID() {
return 3;
}
}
您应该插入相对于字符的用户名。尝试使用g.drawString(currentUser, X, Y);
之后你可以把它居中。
注意:最好将此代码放在单独的类中Player
扩展Animations
。你可以给它一些方法walkLeft
、walkRight
、jump
,..和一些属性,例如username
、hp
、.。这将简化您的游戏类。并且更容易理解玩家对象及其x
和y
,因为您正在在同一级别上调整所有内容。