java 弹幕小游戏 最初版本
最近在学习javaSE,根据b站视频老师的讲解,也参考了他的代码,做了一个弹幕小游戏,也增添了一些自己的代码进去,因为只是最简单的游戏体,以后会慢慢做完整,所以如果有错误,或者代码不够整洁的话,可以帮我改正,谢谢啦。
父类
import java.awt.*;
public class GameObject {
//游戏物体的父类
Image img;
double x,y;
int speed = 3;
int width,height;
public void drawSelf(Graphics g){
g.drawImage(img,(int)x,(int)y,null);
}
public GameObject(Image img, double x, double y, int X_speed,int Y_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(Image img, double x, double y) {
this.img = img;
this.x = x;
this.y = y;
}
public GameObject(){ }
//返回物体所在的矩形,便于后续的碰撞检测
public Rectangle getRect(){
return new Rectangle((int)x,(int)y,width,height);
}
}
弹幕类
import com.sun.xml.internal.ws.model.wsdl.WSDLPortProperties;
import java.awt.*;
public class Shell extends GameObject {
double degree;
public Shell() {
x = 200;
y = 200;
width = 10;
height = 10;
speed = 2;
speed = 2;
//弧度
degree = Math.random()*Math.PI*2;
}
public void draw(Graphics g){
Color c = g.getColor();
g.setColor(Color.YELLOW);
g.fillOval((int)x,(int)y,width,height);
//炮弹沿着任意角度去飞
x+=speed*Math.cos(degree);
y+=speed*Math.sin(degree);
if(x<0 || x> Constant.GAME_WIDTH-width){
degree = Math.PI - degree;
}
if(y<40 || y> Constant.GAME_HEIGHT-height){
degree = -degree;
}
g.setColor(c);
}
}