sprite.java
来自「这是本人写的JAVA游戏编程中的一些基本游戏功能的可复用类」· Java 代码 · 共 127 行
JAVA
127 行
import java.awt.Image;
public class Sprite
{
private Animation anim;
// position (pixels)
private float x;
private float y;
// velocity (pixels per millisecond)
private float dx;
private float dy;
/**
Creates a new Sprite object with the specified Animation.
*/
public Sprite(Animation anim)
{
this.anim = anim;
}
/**
Updates this Sprite's Animation and its position based
on the velocity.
*/
public void update(long elapsedTime)
{
x += dx * elapsedTime;
y += dy * elapsedTime;
anim.update(elapsedTime);
}
/**
Gets this Sprite's current x position.
*/
public float getX()
{
return x;
}
/**
Gets this Sprite's current y position.
*/
public float getY()
{
return y;
}
/**
Sets this Sprite's current x position.
*/
public void setX(float x)
{
this.x = x;
}
/**
Sets this Sprite's current y position.
*/
public void setY(float y)
{
this.y = y;
}
/**
Gets this Sprite's width, based on the size of the
current image.
*/
public int getWidth()
{
return anim.getImage().getWidth(null);
}
/**
Gets this Sprite's height, based on the size of the
current image.
*/
public int getHeight()
{
return anim.getImage().getHeight(null);
}
/**
Gets the horizontal velocity of this Sprite in pixels
per millisecond.
*/
public float getVelocityX()
{
return dx;
}
/**
Gets the vertical velocity of this Sprite in pixels
per millisecond.
*/
public float getVelocityY()
{
return dy;
}
/**
Sets the horizontal velocity of this Sprite in pixels
per millisecond.
*/
public void setVelocityX(float dx)
{
this.dx = dx;
}
/**
Sets the vertical velocity of this Sprite in pixels
per millisecond.
*/
public void setVelocityY(float dy)
{
this.dy = dy;
}
/**
Gets this Sprite's current image.
*/
public Image getImage()
{
return anim.getImage();
}
}
⌨️ 快捷键说明
复制代码Ctrl + C
搜索代码Ctrl + F
全屏模式F11
增大字号Ctrl + =
减小字号Ctrl + -
显示快捷键?