简单角色扮演打怪游戏设计(Java)

需要构建窗体类,工具类,场景类,人物类,敌人类,敌人控制类,以及运行代码的Main类。

窗体类构建,控制绘画图像和键盘控制:

package cn.qvtu.game.frame;

import cn.qvtu.game.model.BackGround;
import cn.qvtu.game.model.Enemy;
import cn.qvtu.game.model.Person;
import cn.qvtu.game.util.StaticValue;

import java.awt.event.KeyEvent;
import java.awt.event.KeyListener;
import java.time.LocalDate;
import javax.swing.JFrame;
import java.awt.*;
import java.awt.image.BufferedImage;
import java.util.Date;
import java.util.List;

public class MyFrame extends JFrame implements Runnable, KeyListener {

    private Date startDate=new Date();      //游戏开始时间
    private Person person;      //添加人物
    private BackGround nowBackGround;       //当前场景

    public MyFrame(){
        this.setTitle("game");//窗体标题
        this.setSize(900,600);//大小

        //设置居中
        int width= Toolkit.getDefaultToolkit().getScreenSize().width;
        int height=Toolkit.getDefaultToolkit().getScreenSize().height;
        this.setLocation((width-900)/2,(height-600)/2);
        //场景初始化
        nowBackGround = new BackGround(1);

        this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);//设置关闭
        this.setVisible(true);//设置窗体可见

        person = new Person(-100, 5,nowBackGround);//初始化人物坐标
        Thread thread=new Thread(this);//创建线程对象,实现接口对象
        thread.start();//启动线程

        this.addKeyListener(this);      //监听键盘
    }

    public void paint(Graphics g){
        //创建图片对象,跟窗体一致
        BufferedImage image=new BufferedImage(900,600,BufferedImage.TYPE_3BYTE_BGR);
        Graphics graphics=image.getGraphics();//获取画笔

        //绘图,添加B01,从(0,0)开始绘画
        graphics.drawImage(nowBackGround.getShowBgImage(),0,0,this);

        long dif=new Date().getTime()-startDate.getTime();//添加时间
        //绘画时间,参数1是文本,参数2、3是坐标。
        graphics.drawString("时间"+(dif/1000/60)+":"+(dif/1000%60),800, 50);

        //画出敌人
        List<Enemy> allEnemy = nowBackGround.getAllEnemy();
        for (Enemy enemy:allEnemy){
            graphics.drawImage(enemy.getShowImage(),enemy.getX(),enemy.getY(),this);
        }
        //画出人物
        graphics.drawImage(person.getShowImage(),person.getX(),person.getY(),this);

        //窗体的画笔绘画image
        g.drawImage(image,0,0,this);//画笔绘画image

        //判定进入下个场景
        if (nowBackGround.isPass(person) && nowBackGround.hasNext()){
            nowBackGround = nowBackGround.next();
            person.setX(0);
            person.setBackground(nowBackGround);        //重设场景
        }
    }

    public void run() {
        while(true){
            try {
                this.repaint();//循环重新绘画窗口
                Thread.sleep(50);//线程休眠:毫秒
            }catch (InterruptedException e){
                e.printStackTrace();
            }
        }
    }

    public void keyTyped(KeyEvent e) {

    }

    public void keyPressed(KeyEvent e) {
        int KeyCode=e.getKeyCode();
        if (KeyCode == 68) {        //按D向右
            this.person.rightRun();
        }
        if (KeyCode == 65){         //按A向左
            this.person.LefttRun();
        }
        if (KeyCode == 87){         //按W向上
            this.person.jump();
        }
        if (KeyCode == 74){         //按K攻击
            this.person.attack();
        }
        if (KeyCode == 32){         //按空格放大
            this.person.daozhao();
        }
    }

    public void keyReleased(KeyEvent e) {
        int KeyCode=e.getKeyCode();
        if (KeyCode ==68 ){
            this.person.stoprightRun();
        }
        if (KeyCode == 65 ){
            this.person.stopLeftRun();
        }
    }
}

工具类,获取资源:

package cn.qvtu.game.util;

import cn.qvtu.game.model.Enemy;

import javax.imageio.ImageIO;
import java.awt.image.BufferedImage;
import java.io.File;
import java.io.IOException;
import java.text.DecimalFormat;
import java.util.ArrayList;
import java.util.List;

public class StaticValue {
    //获取根目录
    public static final String ImagePath = System.getProperty("user.dir")+"/ykm/";
    //设置背景图
    public static BufferedImage B01=null;
    public static BufferedImage B02=null;
    public static BufferedImage B03=null;
    public static BufferedImage B04=null;
        //人物图集
    public static List<BufferedImage> LeftPersonImgs=new ArrayList<>();
    public static List<BufferedImage> rightPersonImgs=new ArrayList<>();
    public static List<BufferedImage> LeftPersonDZImgs=new ArrayList<>();
    public static List<BufferedImage> rightPersonDZImgs=new ArrayList<>();
        //敌人图集
    public static List<BufferedImage> LeftEnemyImgs = new ArrayList<>();
    public static List<BufferedImage> rightEnemyImgs = new ArrayList<>();



    static {
        try{
            //图片获取
         
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值