您好我正在开发游戏,现在我正在尝试使用用户名跟随角色而我屏幕跟随角色但是当屏幕开始跟随角色时,字符串开始飞走我尝试了一切,包括为它制作个人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;
}
}
答案 0 :(得分:0)
您应该插入相对于角色的用户名。尝试使用g.drawString(currentUser, X, Y);
您可以在之后居中。
注意:最好将此代码放在Player
单独的Animations
扩展walkLeft
中。你可以给它一些方法walkRight
,jump
,username
,...和一些属性,例如hp
,x
,..这会简化你的游戏课。并且更容易理解玩家对象及其y
和{{1}},因为您在同一级别上调整所有对象。