4.24 雷霆战坤小游戏

player代码部分

import java.awt.event.KeyAdapter;
import java.awt.event.KeyEvent;

public class Player extends KeyAdapter {
    GameFrame frame;
    public Player(GameFrame frame){
        this.frame=frame;
    }
    @Override
    public void keyPressed(KeyEvent e){//按下
        int keyCode=e.getKeyCode();
        switch (keyCode){
            case 38:frame.heroPlane.up=true;break;
            case 40:frame.heroPlane.down=true;break;
            case 37:frame.heroPlane.left=true;break;
            case 39:frame.heroPlane.right=true;break;
            case 66: addBullut();break;
        }
    }
    @Override
    public void keyReleased(KeyEvent e){//松开
        int keyCode=e.getKeyCode();
        switch (keyCode){
            case 38:frame.heroPlane.up=false;break;
            case 40:frame.heroPlane.down=false;break;
            case 37:frame.heroPlane.left=false;break;
            case 39:frame.heroPlane.right=false;break;
        }

    }
    public void addBullut(){
        frame.bullets.add(new Bullet(frame.heroPlane.x+5,frame.heroPlane.y-20));

    }
}

自己的飞机部分

import javax.swing.*;
import java.awt.*;

public class HeroPlane extends Thread{
    int x=200,y=550;
    int width=50,heigth=50;
    int speed=10;
    Image img=new ImageIcon("img/飞机.png").getImage();
    boolean up,down,left,right;

    public HeroPlane() {
    }

    public HeroPlane(int x, int y, int width, int heigth ) {
        this.x = x;
        this.y = y;
        this.width = width;
        this.heigth = heigth;
    }
    @Override
    public void run(){
        while(true){
            if(up) y-=speed;
            if(down) y+=speed;
            if(left)  x-=speed;
            if(right) x+=speed;
            try {
                Thread.sleep(10);
            }catch (InterruptedException e){
                e.printStackTrace();
            }
        }
    }
}

子弹部分


import javax.swing.*;
import java.awt.*;
public class Bullet {
    int x,y;
    int width=20,heigth=20;
    int speed=10;
    Image image=new ImageIcon("img/子弹.png").getImage();

    public Bullet(int x, int y) {
        this.x = x+10;
        this.y = y;
    }

    public Bullet(int x, int y, int width, int heigth ) {
        this.x = x;
        this.y = y;
        this.width = width;
        this.heigth = heigth;
    }}

敌机代码

import javax.swing.*;
import java.awt.*;

public class EnemyPlane extends Thread{
    public GameFrame gf;
    public int x,y;
    public int width=40,heigth=40;
    public int speed=2;
    public Image img=new ImageIcon("img/苹果.png").getImage();

    public EnemyPlane(int x, int y,GameFrame gf ) {
        super();
        this.x = x;
        this.y = y;
        this.gf=gf;
    }
    public EnemyPlane(int x, int y, int width, int heigth,GameFrame gf ) {
        super();
        this.x = x;
        this.y = y;
        this.width = width;
        this.heigth = heigth;
        this.gf=gf;
    }
    public void run(){
        while(true){
            if(hit()){
                System.out.println("hit...");
                this.speed=0;
                this.img=new ImageIcon("img/爆炸.png").getImage();
                try{
                    this.sleep(500);
                }catch (InterruptedException e){
                    e.printStackTrace();
                }
                gf.enemys.remove(this);
                break;
            }
            if(this.y>648){
                break;
            }
            try{
                this.sleep(10);
            }catch (InterruptedException e){
                e.printStackTrace();
            }
        }
    }


    public boolean hit(){
        Rectangle myrect=new Rectangle(this.x,this.y,this.width,this.heigth);
        Rectangle rect=null;
        for (int i = 0; i <gf.bullets.size() ; i++) {
            Bullet bullet=gf.bullets.get(i);
            System.out.println("test hit");
            rect=new Rectangle(bullet.x,bullet.y-1,bullet.width,bullet.heigth);
            if(myrect.intersects(rect)){
                return true;
            }
        }
        return false;
    }

}

游戏主函数部分

import javax.swing.*;
import java.awt.*;
import java.awt.image.BufferedImage;
import java.util.Random;
import java.util.Vector;

public class GameFrame extends JFrame {
    HeroPlane heroPlane;
    Vector<Bullet> bullets=new Vector<>();
    Vector<EnemyPlane> enemys=new Vector<>();
    GameFrame frame;
    public GameFrame(){
        frame=this;
        heroPlane=new HeroPlane();
        heroPlane.start();
        this.setSize(438,649);
        this.setTitle("雷霆战坤666");
        this.setResizable(false);//false 为固定窗口大小
        this.setDefaultCloseOperation(WindowConstants.EXIT_ON_CLOSE);
        this.setLocationRelativeTo(null);//窗口放中央
        this.setVisible(true);
        new Thread(new Runnable() {//线程
            @Override
            public void run() {
                while(true){
                    repaint();
                    try{
                        Thread.sleep(10);
                    }catch (InterruptedException e){
                        e.printStackTrace();
                    }
                }}
        }).start();
        new Thread(new Runnable() {
            Random r=new Random();
            @Override
            public void run() {
                while(true){
                    EnemyPlane enemyPlane=new EnemyPlane(r.nextInt(430),0,frame);
                    enemyPlane.start();
                    enemys.add(enemyPlane);
                    try {
                        Thread.sleep(1000);
                    }catch (InterruptedException e){
                        e.printStackTrace();
                    }
                }
            }
        }).start();

    }
    public void paint(Graphics g){

        //背景
        BufferedImage image=(BufferedImage)  this.createImage(this.getSize().width,this.getSize().height);
        //高效缓存的画笔
        Graphics bi=image.getGraphics();
        bi.drawImage(new ImageIcon("img/img.png").getImage(),0,0,null);
        bi.drawImage(heroPlane.img, heroPlane.x,heroPlane.y,heroPlane.width,heroPlane.heigth,null);
        for (int i = 0; i < bullets.size(); i++) {
            Bullet bullet=bullets.get(i);
            if(bullet.y>0)
            bi.drawImage(bullet.image, bullet.x,bullet.y-=bullet.speed,bullet.width,bullet.heigth,null);
            else bullets.remove(bullet);
        }
        for (int i = 0; i < enemys.size(); i++) {
            EnemyPlane ep=enemys.get(i);
            if(ep.y<649)
                bi.drawImage(ep.img, ep.x,ep.y+=ep.speed,ep.width,ep.heigth,null);
            else enemys.remove(ep);
        }


        g.drawImage(image,0,0,null);

    }

    public static void main(String[] args) {
        GameFrame frame=new GameFrame();
        Player player=new Player(frame);
        frame.addKeyListener(player);

    }

}

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值