您好我的滚动游戏有问题。当滚动播放器太快而无法滚动时。我认为问题不在于计算坐标显示的玩家。
以下是代码:
Main.class
import java.applet.Applet;
import java.awt.Graphics;
import java.awt.Image;
import java.awt.event.KeyEvent;
import java.awt.event.KeyListener;
public class Main extends Applet implements Runnable, KeyListener
{
Applet app;
public static final int gameX = 660;
public static final int gameY = 660;
Thread thread;
int map[][] = new int[29][52];
boolean moveU = false;
boolean moveD = false;
boolean moveR = false;
boolean moveL = false;
loadImage loadImg = new loadImage();
Player player;
Graphics bgGrap;
Image bgimage;
public void init(){
app = this;
setSize(gameX, gameY);
requestFocus();
addKeyListener(this);
loadMap();
player = new Player(200,200);
bgimage = createImage(gameX, gameY);
bgGrap = bgimage.getGraphics();
}
public void update(Graphics g)
{
bgGrap.clearRect(0, 0, gameX, gameY);
paint(bgGrap);
g.drawImage(bgimage, 0, 0, app);
}
public void paint(Graphics g){
drawHugeWorld(g);
g.drawImage(loadImg.getImg("tree.PNG"), player.getX(), player.getY(), null); //here I draw the player.
}
public void start() {
if(thread == null)
{
thread = new Thread(this);
thread.start();
}
}
public void stop() {
if(thread != null)
{
thread = null;
}
}
public void destroy(){
thread = null;
}
public void drawHugeWorld(Graphics g){
int cameraX = player.getX() - (gameX/2);
int cameraY = player.getY() - (gameY/2);
int tileX = cameraX/32;
int tileY = cameraY/32;
if(tileX % 32 != 0) {
tileX--;
}
if(tileY % 32 != 0) {
tileY--;
}
for (int y = tileY; y < cameraY + gameY; y+=32)
{
for (int x = tileX; x < cameraX + gameX; x+=32)
{
switch(map[y/32][x/32])
{
case 0:
g.drawImage(loadImg.getImg("grass.png"), x - cameraX, y - cameraY, null);
break;
case 1:
g.drawImage(loadImg.getImg("tree.png"), x - cameraX, y - cameraY, null);
break;
}
}
}
}
public void loadMap(){
int map1[][] =
{
{1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1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){
player.addY(-1);
repaint();
}
if(moveD){
player.addY(1);
repaint();
}
if(moveR){
player.addX(1);
repaint();
}
if(moveL){
player.addX(-1);
repaint();
}
try {
thread.sleep(4);
} catch (InterruptedException e) {
e.printStackTrace();
}
}
}
public void keyPressed(KeyEvent e)
{
if(e.getKeyCode() == KeyEvent.VK_W || e.getKeyCode() == KeyEvent.VK_UP){
moveU = true;
}
if(e.getKeyCode() == KeyEvent.VK_S || e.getKeyCode() == KeyEvent.VK_DOWN){
moveD = true;
}
if(e.getKeyCode() == KeyEvent.VK_D || e.getKeyCode() == KeyEvent.VK_RIGHT){
moveR = true;
}
if(e.getKeyCode() == KeyEvent.VK_A || e.getKeyCode() == KeyEvent.VK_LEFT){
moveL = true;
}
}
public void keyReleased(KeyEvent e)
{
if(e.getKeyCode() == KeyEvent.VK_W || e.getKeyCode() == KeyEvent.VK_UP){
moveU = false;
}
if(e.getKeyCode() == KeyEvent.VK_S || e.getKeyCode() == KeyEvent.VK_DOWN){
moveD = false;
}
if(e.getKeyCode() == KeyEvent.VK_D || e.getKeyCode() == KeyEvent.VK_RIGHT){
moveR = false;
}
if(e.getKeyCode() == KeyEvent.VK_A || e.getKeyCode() == KeyEvent.VK_LEFT){
moveL = false;
}
}
public void keyTyped(KeyEvent e)
{
}
}
和Player.class
public class Player {
int x;
int y;
public Player(int x, int y){
this.x = x;
this.y = y;
}
public void addX(int value){
x += value;
}
public void addY(int value){
y += value;
}
public int getX(){
return x;
}
public int getY(){
return y;
}
}
答案 0 :(得分:0)
我想我知道你应该改变什么,它只是来找我。
g.drawImage(loadImg.getImg("tree.PNG"), player.getX(), player.getY(), null); //here I draw the player.
如果要更换相机,则不应尝试更改播放器的x,y位置。例如,我尝试硬编码200,200而不是player.getX()和player.getY(),我得到了合理的滚动。也许这就是你所追求的效果?