JAVA 弹幕小游戏 1.0版本

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);
    }
}
评论 13
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值