JAVA全系列 教程
3762个小节阅读:7089.2k
目录
C语言快速入门
JAVA全系列 教程
面向对象的程序设计语言
Python全系列 教程
Python3.x版本,未来主流的版本
人工智能 教程
顺势而为,AI创新未来
大厂算法 教程
算法,程序员自我提升必经之路
C++ 教程
一门通用计算机编程语言
微服务 教程
目前业界流行的框架组合
web前端全系列 教程
通向WEB技术世界的钥匙
大数据全系列 教程
站在云端操控万千数据
AIGC全能工具班
A A
White Night
·GameObject类的定义
我们发现,窗口中所有的对象(飞机、炮弹等等)都有很多共性:“图片对象、坐标位置、运行速度、宽度和高度”。为了方便程序开发,我们需要设计一个GameObject类,它可以作为所有游戏物体的父类,方便我们编程。
【示例13-11】GameObject类
xxxxxxxxxx
package com.bjsxt.plane;
import java.awt.Graphics;
import java.awt.Image;
import java.awt.Rectangle;
public class GameObject {
Image img; //该物体对应的图片对象
double x,y; //该物体的坐标
int speed; //该物体的运行速度
int width,height; //该物体所在矩形区域的宽度和高度
/**
* 怎么样绘制本对象
* @param g
*/
public void drawMySelf(Graphics g){
g.drawImage(img, (int)x, (int)y, null);
}
public GameObject(Image img, double x, double y) {
this.img = img;
this.x = x;
this.y = y;
if(img!=null){
this.width = img.getWidth(null);
this.height = img.getHeight(null);
}
}
public GameObject(Image img, double x, double y, int speed, int width,
int height) {
this.img = img;
this.x = x;
this.y = y;
this.speed = speed;
this.width = width;
this.height = height;
}
public GameObject() {
}
/**
* 返回物体对应矩形区域,便于后续在碰撞检测中使用
* @return
*/
public Rectangle getRect(){
return new Rectangle((int)x,(int) y, width, height);
}
}