java 之2D过气游戏类的写法
2D游戏中各对象的父类
package cn.littlepage.game;
import java.awt.Graphics;
import java.awt.Image;
import java.awt.Rectangle;
public class GameObject {
/*
* 任何一个2D游戏都必须要有图片,坐标,速度,大小,矩形(碰撞检测)
* 所以,这个可以做成一个2D游戏的父类
*/
public Image img;
public int x,y;
public int speed;
public int width,height;
public void drawSelf(Graphics g) {
g.drawImage(img, x, y, null);
}
public GameObject() {
super();
// TODO Auto-generated constructor stub
}
public GameObject(Image img, int x, int y, int speed, int width, int height) {
super();
this.img = img;
this.x = x;
this.y = y;
this.speed = speed;
this.width = width;
this.height = height;
}
public Rectangle getRect() {
return new Rectangle(x, y, width, height);
}
}