Java入门项目:渣男粉碎机(游戏)

环境配置:IntelliJ IDEA 2021.1.2 x64

游戏项目包括一下部分

 首先解释GameFrame.java:因为游戏要显示一个窗口,该类就继承了JFrame,下面简单介绍一下JFrame:

JFrame 类是一个容器,允许我们把其他组件添加到它里面,把它们组织起来,并把它们呈现给用户。 JFrame 实际上不仅仅把组件放入其中并呈现给用户。比起它表面上的简单性,它实际上是 Swing 包中最复杂的组件。为了最大程度地简化组件,在独立于操作系统的 Swing 组件与实际运行这些组件的操作系统之间,JFrame 起着桥梁的作用。JFrame 在本机操作系统中是以窗口的形式注册的,这么做之后,就可以得到许多熟悉的操作系统窗口的特性:最小化/最大化、改变大小、移动,常用的几个方法就不一一赘述。

 paint(Graphics g):

java绘图时,最常使用到的就是paint(Graphics g){...内容...}方法获取画笔,然后利用JPanel等容器作为画布,在JFrame内呈现出内容。

在此次项目中,图片放在paint类中呈现,需要主义炸弹和渣男的图片输出,因为这两个类采用了集合的存储方法,图片呈现要用for循环。

另外轰炸机和渣男是两个线程,也在GameFrame中启动,因为轰炸机中要实现炸弹的不断输出,渣男类也类似。

还有一个点就是KeyAdepter的使用,因为要连接到键盘上的键,所以玩家类继承keyAdepter 的类。

 GameFrame.java
package cn.tx;

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

public class GameFrame extends JFrame {
     plane Heroplane;
     //定义子弹的集合
    Vector<bullet> bullets=new Vector<>();//构造炸弹集合
    Vector<EnemyPlane> enemys=new Vector<>();//构造渣男集合
    GameFrame frame;
    public GameFrame(){
        Heroplane=new plane();
        frame=this;
       // bullet=new bullet();
        Heroplane.start();
       // bullet bullet1 = new bullet();
        this.setSize(500,760);
        this.setTitle("渣男粉碎机!");
        this.setResizable(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) {
                    //enemys.add(new EnemyPlane(r.nextInt(1000),0,frame));
                    EnemyPlane enemyPlane=new EnemyPlane(r.nextInt(1000),0,frame);
                    enemyPlane.start();
                    enemys.add(enemyPlane);
                    //repaint();
                    try {
                        Thread.sleep(500);
                    } catch (InterruptedException e)
                    {
                        e.printStackTrace();
                    }
                }
            }
        }).start();
    }
    public void paint(Graphics g){
        //System.out.println("绘制画板");
        BufferedImage  image=(BufferedImage) this.createImage(this.getSize().width,this.getSize().height);
        Graphics bi= image.createGraphics();
        bi.drawImage(new ImageIcon("img/background.png").getImage(),0,0,null);
        bi.drawImage(Heroplane.img,Heroplane.x, Heroplane.y, null);

        //打印子弹
        for (int i = 0; i <bullets.size() ; i++) {
            bullet bulletn=bullets.get(i);
            if(bulletn.y>0)
            bi.drawImage(bulletn.image,bulletn.x=Heroplane.x+220,bulletn.y-= bulletn.speed,bulletn.width,bulletn.width,null);
            else
                bullets.remove(bulletn);
        }
        for (int i = 0; i <enemys.size() ; i++) {
            EnemyPlane ep=enemys.get(i);
            if(ep.y<1260)
                bi.drawImage(ep.img,ep.x,ep.y+=ep.speed,ep.width,ep.hight,null);
            else
                enemys.remove(ep);
        }
        g.drawImage(image,0,0,null);
    }

    public static void main(String[] args) {
        GameFrame frame=new GameFrame();
        //启用player按键控制
        Player player=new Player(frame);
        frame.addKeyListener(player);

    }
}
EnemyPlane.java
package cn.tx;

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

public class EnemyPlane extends Thread {
    public GameFrame gf;
    public int x, y;
    public int width = 50, hight = 50;
    public int speed = 2;
    public Image img = new ImageIcon("img/enemy.jpg").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 hight, GameFrame gf) {
        super();
        this.x = x;
        this.y = y;
        this.width = width;
        this.hight = hight;
        this.gf = gf;
    }

    public void run() {
        while (true) {
            if (hit()) {
                System.out.println("hit...");
                this.speed=0;
                this.img=new ImageIcon("img/bomb.png").getImage();
                try {
                    this.sleep(500);
                }catch (InterruptedException e){
                    e.printStackTrace();
                }
                break;
            }
            if (this.y<-760){
                break;
            }
            try {
                this.sleep(10);
            }catch (InterruptedException e){
                e.printStackTrace();
            }
        }

    }

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

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

public class bullet {
    int x=200,y=400;
    int width=50,hight=50;
    int speed=8;
    Image image=new ImageIcon("img/bullet.jpg").getImage();
    public bullet() {
        this.x = x;
        this.y = y;
        this.width = width;
        this.hight = hight;
    }

    public bullet(int i, int i1) {

    }
}
Player.java
package cn.tx;
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;
        }
    }

    @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;
            case 66:
                addBullet();
                break;
        }
    }

    public void addBullet() {
        frame.bullets.add(new bullet(frame.Heroplane.x+5,frame.Heroplane.y-20));


    }
}
plane.java
package cn.tx;

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

public class plane extends Thread{
    int x=0,y=350;
    int width=5,hight=5;
    int speed=5;
    Image img= new ImageIcon("img/plane.png").getImage();
    //定义方向键
    boolean up,down,left,right;

    public plane() {
        this.x = x;
        this.y = y;
        this.width = 20;
        this.hight = 20;
    }

    @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();
            }
        }
    }
}

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值