0
我想在遊戲中製作一個運動系統,玩家總是按照某個方向前進,通過按左右鍵可以改變他們。到目前爲止,我有這樣的代碼:如何在Java中爲遊戲旋轉圖像?
public class Player
{
private float x, y;
private int health;
private double direction = 0;
private BufferedImage playerTexture;
private Game game;
public Player(Game game, float x, float y, BufferedImage playerTexture)
{
this.x = x;
this.y = y;
this.playerTexture = playerTexture;
this.game = game;
health = 1;
}
public void tick()
{
if(game.getKeyManager().left)
{
direction++;
}
if(game.getKeyManager().right)
{
direction--;
}
x += Math.sin(Math.toRadians(direction));
y += Math.cos(Math.toRadians(direction));
}
public void render(Graphics g)
{
g.drawImage(playerTexture, (int)x, (int)y, null);
}
}
此代碼工作正常的運動,但圖像不旋轉,以反映方向的變化,我想它。我怎樣才能使圖像旋轉,以便通常頂部始終指向「方向」(這是一個以度爲單位的角度)?
你可以保持它定義了播放器所指向的方向,從您可以簡單地accordingnly旋轉'Graphics'背景 – MadProgrammer
它的標誌取決於ru如何調用渲染函數。如果你可以放熱片段你打電話的功能將是有幫助的 –
渲染方法被稱爲每秒多次繪製播放器圖像,如果它正在移動。 – John