• 周六. 4 月 25th, 2026

物嫩软件资讯网

软件资讯来物嫩

线程游戏-冒险岛开发总结

admin@wunen

5 月 20, 2025
线程游戏之冒险岛

因为小时候超爱玩小霸王游戏的冒险岛,所以在选择线程游戏开发时就自然而然的选择做冒险岛。那么下面我就带你一起走进我的代码世界吧~

首先必须得有一个游戏界面,这就需要用到Swing组件基本代码如下:

this.setTitle(“Gamev1.1”);

this.setSize(800,600);

this.setLayout(new FlowLayout());//流式布局

this.setLocationRelativeTo(null);//居中

this.setResizable(false);//不可改变大小

this.setDefaultCloseOperation(3);

final JPanel Gamepanel = new JPanel() {

public void paint(Graphics g) {

super.paint(g);

g.drawImage(image.getImage(),player.Speedcount,0, null);

// 重绘角色

g.drawImage(player.image.getImage(),player.x, player.y, null);

//重绘怪物

for (int i= 0; i <monitors.size(); i++) {

MonitorThread monitor =monitors.get(i);

g.drawImage(monitor.image.getImage(),monitor.x, monitor.y, null);

}

}

};

// 获取画布

Gamepanel.setPreferredSize(new Dimension(800,600));

Gamepanel.setBackground(Color.white);

构造了基本的界面之后呢,就需要给人物和怪物设置他们的各种属性以及他们的技能啦。

人物类和怪物类分别用两个线程类来表示。代码如下:

人物类:

//人物角色的线程

public class PlayerThread extends Thread {

// 人物属性

public double HP = 200;// 人物血量

public double MP = 100;// 魔量

public double power = 50;// 力量

public String name;// 姓名

ArrayList<MonitorThread> monitors;// 怪物队列

ArrayList<ImageIcon> location = new ArrayList<ImageIcon>();// 人物的位置图像

ArrayList<ImageIcon> skill = new ArrayList<ImageIcon>();// 人物技能用队列保存

// 人物起始坐标

public int x =15;

public int y =400;

// 任务移动的速度

public int Speedx = 0;

public int upSpeedx = 0;

public int upSpeedy = 0;

public int Speedy = 0;

public int Speedcount = 0;

public boolean isjump = false;// 是否可以跳跃

public boolean isright = false;// 是否可以向右

public boolean isleft = false;// 是否可以向左

public boolean isaddmonitor = false;// 是否加怪

public JPanel Gamepanel;

public ImageIcon image;

public void init() {

// 给人物位置队列加入图片

ImageIcon left = new ImageIcon(“人物图片/left.png”);

ImageIcon right = new ImageIcon(“人物图片/right.png”);

ImageIcon leftrun = new ImageIcon(“人物图片/leftrun.gif”);

ImageIcon rightrun = new ImageIcon(“人物图片/rightrun.gif”);

ImageIcon upjumpleft = new ImageIcon(“人物图片/upjumpleft.png”);

ImageIcon upjumpright = new ImageIcon(“人物图片/upjumpright.png”);

location.add(left);

location.add(right);

location.add(leftrun);

location.add(rightrun);

location.add(upjumpleft);

location.add(upjumpright);

this.image = location.get(1);

// 给人物技能队列加入图片

ImageIcon fightleft = new ImageIcon(“人物图片/fightleft.png”);

ImageIcon fightright = new ImageIcon(“人物图片/fightright.png”);

ImageIcon upfightleft = new ImageIcon(“人物图片/upfightleft.png”);

ImageIcon upfightright = new ImageIcon(“人物图片/upfightright.png”);

skill.add(fightleft);

skill.add(fightright);

skill.add(upfightleft);

skill.add(upfightright);

}

public void getmonitors(ArrayList<MonitorThread> monitors) {

this.monitors = monitors;

}

public void getPanel(JPanel Gamepanel) {

this.Gamepanel = Gamepanel;

}

// 用构造方法来初始化人物数据

public PlayerThread() {

this.name = “风之殇”;

init();// 初始化数据

}

public void run() {// 重写它的run方法

while (true) {

if(isaddmonitor){//先判断是否加怪

for(int i=0;i<5;i++){//加5个怪

MonitorThread monitor=new MonitorThread();

monitor.getplayer(this);

monitors.add(monitor);//加到怪物队列中去

}

isaddmonitor=false;//将状态改为false

}

//判断Speedcount的值

if(Speedcount<=-211&&Speedcount>=-210){

isaddmonitor=true;

}

if (isleft && !isjump) {// 如果向左

this.image = this.location.get(2);// 设置要画的图片为人物向左跑得图片

System.out.println(“x=” + x);

if (x > 0) {

this.x += this.Speedx;

y = 400;

this.upSpeedx = 0;

}

if(this.Speedcount<0){

this.Speedcount -= this.Speedx;

}

} else if (isright && !isjump) {// 如果向右

this.image = this.location.get(3);// 设置要画的图片为人物跑得图片

if (x < 350) {

this.x += this.Speedx;

y = 400;

this.upSpeedx = 0;

}

if(this.Speedcount>-4000){

if(x>=350){

Speedx=2;

}

this.Speedcount -= this.Speedx;

}

}

while (this.isjump) {

for (int i = 0; i < 200; i++) {// 跳上去

this.y += this.Speedy;

this.x += this.upSpeedx;

try {

Thread.sleep(2);

}catch (Exception ef) {

ef.printStackTrace();

}

}

for (int i = 0; i < 200; i++) {// 落下来

this.y -= this.Speedy;

this.x += this.upSpeedx;

try {

Thread.sleep(2);

} catch (Exception ef) {

ef.printStackTrace();

}

}

this.image = this.location.get(1);

this.Speedy = 0;

this.Speedx = 0;

this.upSpeedx = 0;

this.isjump = false;

}

if (monitors.size() > 0) {

for (int i = 0; i < monitors.size(); i++) {// 判断

while (Math.abs(this.x – monitors.get(i).x) < 50

&& (this.image == skill.get(0) || this.image == skill

.get(1))) {

monitors.get(i).HP -= this.power;

System.out.println(“怪物的血:” + monitors.get(i).HP);

if (monitors.get(i).HP <= 0) {

System.out.println(“怪物挂掉了+++”);

monitors.remove(i);// 清除队列对象

break;

}

}

}

}

// System.out.println(“+++++++”+Speedcount);

try {

Thread.sleep(5);

} catch (Exception ef) {

ef.printStackTrace();

}

}

}

public void fight() {// 战斗方法

}

}

怪物类:

//怪物线程

public class MonitorThread extends Thread{

//怪物属性

public double HP=0;//怪物血量

public double MP=0;//魔量

public double power=0;//力量

public String name;//姓名

PlayerThread player;//传入角色

ArrayList<ImageIcon>monitorimage=new ArrayList<ImageIcon>();//怪物的图像

//怪物起始坐标

public int x=5;

public int y=410;

//任务移动的速度

public int Speedx=1;

public int Speedy=1;

public ImageIcon image;//怪物当前图片

Random rand=new Random();//创建随机对象

public void getplayer(PlayerThread player){

this.player=player;

}

public void init(){

ImageIcon pig=new ImageIcon(“动态图/pig.gif”);

ImageIcon pig1=new ImageIcon(“动态图/pig1.gif”);

ImageIcon pig2=new ImageIcon(“动态图/pig2.gif”);

ImageIcon red=new ImageIcon(“动态图/red蜗牛.gif”);

ImageIcon blue=new ImageIcon(“动态图/蓝蜗牛.gif”);

ImageIcon mogu=new ImageIcon(“动态图/蘑菇.gif”);

ImageIcon tree=new ImageIcon(“动态图/木妖.gif”);

ImageIcon woniuboss=new ImageIcon(“动态图/蜗牛boss.gif”);

ImageIcon moguboss=new ImageIcon(“动态图/蘑菇boss.gif”);

monitorimage.add(pig);

monitorimage.add(pig1);

monitorimage.add(pig2);

monitorimage.add(red);

monitorimage.add(blue);

monitorimage.add(mogu);

monitorimage.add(tree);

monitorimage.add(woniuboss);

monitorimage.add(moguboss);

//随机下标

int x=rand.nextInt(7);

image=monitorimage.get(x);

}

//用构造方法来初始化怪物数据

public MonitorThread(){

this.HP=80;

this.MP=50;

this.power=5;

this.name=”小怪物”;

this.x=rand.nextInt(500)+10;//随机

init();//初始化数据

}

public void run(){//重写它的run方法

while (true){

if(player!=null){

//判断人物在怪物的哪边

while(Math.abs(this.x-player.x)<100&&this.x<player.x){//在人物的左边,一直前进,直到到达那里

this.Speedx=5;//速度设为1

this.x+=this.Speedx;

}

while(Math.abs(this.x-player.x)<100&&this.x>player.x){//在人物的右边,就像

this.Speedx=-5;

this.x+=this.Speedx;

}

this.Speedx=5;//速度设为1

this.x-=this.Speedx;

// while(this.y>player.y){//在人物的下边,就像上

// this.Speedy=-1;

// this.y+=this.Speedy;

// }

// while(this.y<player.y){//在人物的上边,就像下

// this.Speedy=1;

// this.y+=this.Speedy;

// }

while(Math.abs(this.x-player.x)<10){//当两者坐标相等时,就战斗

try {

Thread.sleep(50);

} catch (Exception ef) {

ef.printStackTrace();

}

player.HP-=this.power;

while(player.HP<=0){//清除那个对象

break;

}

}

}

try {

Thread.sleep(1000);

} catch (Exception ef) {

ef.printStackTrace();

}

}

}

}

接下来呢,就需要设置键盘监听器,用于人物按下指定的键来施放技能了。。

代码:

//键盘监听器,用来控制人物的动作,与技能

public class KeyListener extends KeyAdapter{

PlayerThread player;

JPanel Gamepanel;

public KeyListener(PlayerThread player){

this.player=player;

}

public void keyPressed(KeyEvent e) {//压向键盘时的指令

//先判断队列是否为空

int code=e.getKeyCode();//得到敲击得到的键盘指令

if(player!=null){//不为空

//如果为左右运动,再判断次数

if(code==37){//左边

player.Speedx=-1;//速度加快1

player.upSpeedx=-1;

player.isleft=true;//向左的状态为true

}

if(code==39){//右边

player.Speedx=1;//速度加快1

player.upSpeedx=1;

player.isright=true;//向右的状态为true

}

// if(code==38){//如果为上

// player.Speedy-=1;

// }

// if(code==40){//如果为下,就用当前图片

// player.Speedy+=1;

// }

if(code==67){//如果为跳跃键c就用跳跃键

if(player.image==player.location.get(0)||player.image==player.location.get(2))

{

player.image=player.location.get(4);

player.Speedy=-1;//速度改为1

player.isjump=true;//将状态改为true

}

if(player.image==player.location.get(1)||player.image==player.location.get(3))

{

player.image=player.location.get(5);

player.Speedy=-1;

player.isjump=true;//将状态改为true

}

}

if(code==88){//如果为攻击键x,就调用攻击的图片

//先判断当前图片

if(player.image==player.location.get(0)||player.image==player.location.get(2)){

player.image=player.skill.get(0);//攻击图片

player.y=350;

System.out.println(“调用了打的方法”);

}

if(player.image==player.location.get(1)||player.image==player.location.get(3)){//如果为向优走的图片,则调用向右攻击

player.image=player.skill.get(1);//攻击图片

player.y=350;

System.out.println(“调用了打的方法”);

}

if(player.image==player.location.get(4)){//如果为跳跃的图片,就调用空中攻击的图片

player.image=player.skill.get(2);

System.out.println(“调用了打的方法”);

}

if(player.image==player.location.get(5)){//如果为跳跃的图片,就调用空中攻击的图片

player.image=player.skill.get(3);

System.out.println(“调用了打的方法”);

}

}

}

}

public void keyReleased(KeyEvent e) {//释放时

int code=e.getKeyCode();//得到敲击得到的键盘指令

if(player!=null){//不为空

if(player.image==player.location.get(2)){

player.Speedx=0;

player.Speedy=0;

player.image=player.location.get(0);

player.isleft=false;

}

if(player.image==player.location.get(3)){

player.Speedx=0;

player.Speedy=0;

player.image=player.location.get(1);

player.isright=false;

}

if(player.image==player.skill.get(0)){//向左攻击

player.image=player.image=player.location.get(0);

player.y = 325;

player.Speedx=0;

player.upSpeedx=0;

player.Speedy=0;

}

if(player.image==player.skill.get(1)){//向右攻击

player.image=player.image=player.location.get(1);

player.y = 325;

player.Speedx=0;

player.upSpeedx=0;

player.Speedy=0;

}

}

}

}

基本设置弄好之后,就需要将怪物与人物显示在游戏界面中了,这是最关键的一步

代码如下:

//初始化人物对象

player = new PlayerThread();

player.getmonitors(monitors);

player.start();

MonitorThread monitor= new MonitorThread();

monitor.getplayer(player);

monitors.add(monitor);// 将怪物对象装入队列

这样每一步都有具体的方法来实现,一个简单的线程游戏就完成了。。

发表回复

您的邮箱地址不会被公开。 必填项已用 * 标注