📄 sprite.java
字号:
// Sprite.java
// [Imperial Snowman Soft]
package GameLib;
import java.awt.*;
import java.applet.*;
public class Sprite
{
protected Component comp;
protected Image[] image;
protected int frame;
protected int animSpeed, animTimer;
protected boolean animated;
protected boolean isAutoMoving;
protected boolean oneTime, animating;
protected double velocity;
protected Point dir;
protected Polygon polyCollision;
protected Rectangle pos, collision; //The position
protected boolean visible; //Is the sprite visible?
public static final Point DIR_NORTH = new Point(0, -3);
public static final Point DIR_EAST = new Point(3, 0);
public static final Point DIR_SOUTH = new Point(0, 3);
public static final Point DIR_WEST = new Point(-3, 0);
public static final Point DIR_NORTHEAST = new Point(2, -2);
public static final Point DIR_SOUTHEAST = new Point(2, 2);
public static final Point DIR_NORTHWEST = new Point(-2, -2);
public static final Point DIR_SOUTHWEST = new Point(-2, 2);
public Sprite(Component component, Image img, int x, int y, double vel, boolean vis)
//img = The Image, x = x-position, y = y-position
//vel = Velocity, z = z-order, vis = visibility
{
comp = component;
oneTime = false;
animated = false;
animating = false;
image = new Image[1];
image[0] = img;
visible = vis;
frame = 0;
isAutoMoving = false; //the auto-movement default=false
velocity = vel;
pos = new Rectangle(x, y,
x + img.getWidth(comp), y + img.getHeight(comp));
collision = new Rectangle(0, 0, img.getWidth(comp),
img.getHeight(comp));
}
public Sprite(Component component, Image[] img, int x, int y, double vel, int curframe, int animspd, boolean anim, boolean vis)
//img = The Image, x = x-position, y = y-position
//vel = Velocity, z = z-order, vis = visibility
{
comp = component;
oneTime = false;
animating = false;
image = img;
visible = vis;
animated = anim;
frame = curframe;
isAutoMoving = false; //the auto-movement default=false
animSpeed = animspd;
animTimer = animspd;
pos = new Rectangle(x, y,
x + img[curframe].getWidth(comp), y + img[curframe].getHeight(comp));
collision = new Rectangle(0, 0, img[curframe].getWidth(comp),
img[curframe].getHeight(comp));
}
public void draw(Graphics g)
// Draw the current frame
{
updateSprite();
if (visible)
{
g.drawImage(image[frame], pos.x, pos.y, comp);
}
}
public void setAnimOnlyOnce(boolean onlyOnce)
{
oneTime = onlyOnce;
}
public void runAnim(int startFrame)
{
if (!animating)
{
visible = true;
animating = true;
frame = startFrame;
}
}
public void runAnim()
{
if (!animating)
{
visible = true;
animating = true;
frame = 0;
}
}
public Point getDirection()
{
return (dir);
}
public void setDirection(Point dir)
{
this.dir = dir;
}
public boolean rectCollision(Sprite other)
{
return (getCollisionRect().intersects(other.getCollisionRect()));
}
/* increase the animation frame */
public void incAnim()
{
if (--animTimer <= 0)
{
frame++;
animTimer = animSpeed;
}
if (frame >= image.length)
{
frame = 0;
if (oneTime)
{
visible = false;
animating = false;
}
}
}
public void updateSprite()
{
if (animated) incAnim();
if (isAutoMoving)
{
pos.move((int)(pos.x+(dir.x*velocity)), (int)(pos.y+(dir.y*velocity)));
}
}
public void setAutoMoving(boolean moving)
{
isAutoMoving = moving;
}
public void setAnimSpeed(int animSpd)
{
animSpeed = animSpd;
}
public void setVelocity(double vel)
{
velocity = vel;
}
public double getVelocity()
{
return (velocity);
}
public void moveTo(int x, int y)
{
moveTo(new Point(x, y));
}
public void moveTo(Point position)
{
pos.move(position.x, position.y);
}
public void moveTo(Rectangle position)
{
pos = position;
}
public int getHeight()
{
return pos.height;
}
public int getWidth()
{
return pos.width;
}
public void setCollisionRect(Rectangle collRect)
{
collision = collRect;
}
public Rectangle getCollisionRect()
{
Rectangle collRect = new Rectangle(0,0, collision.width, collision.height);
collRect.move(collision.x+pos.x, collision.y+pos.y);
return (collRect);
}
public Rectangle getPosition()
{
return (pos);
}
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -