背带马里奥(含源码)总结

文章展示了使用JavaSwing库开发的一款背带马里奥游戏,主要包括游戏背景、地图、台阶、马里奥角色、小怪、分数统计、IO流和多线程的实现。马里奥能左右移动、跳跃,与地图上的障碍物和敌人互动。游戏通过键盘事件控制马里奥的动作,实现了基本的游戏逻辑。
摘要由CSDN通过智能技术生成

思维导图

画板需求:JFrame

主要设计:
1、游戏背景的设计

2、地图的显示

3、台阶的显示(砖块的坐标)

4、背带马里奥的设计,左右移动能力,跳动能力

5、小怪的设计,以及出现的位置和移动

6、游戏分数的统计

7、IO流的及其应用

8、多线程的实现

代码实现:


main方法:

package com.sxt;

import javax.swing.*;
import java.awt.*;
import java.awt.event.KeyEvent;
import java.awt.event.KeyListener;
import java.util.ArrayList;
import java.util.List;

public  class MyFrame extends JFrame implements KeyListener,Runnable {
    //用于存储所有的背景
    private List<BackGround> allBg = new ArrayList<>();
    //用于存储当前的背景
    private BackGround nowBg = new BackGround();
    //用于双缓存
    private Image offScreenImage = null;
    //马里奥对象
    private Mario mario = new Mario();
    //定义一个线程对象,用于实现马里奥的运动
    private Thread thread = new Thread(this);

    public MyFrame() {
        //设置窗口的大小为800 * 600
        this.setSize(800,600);
        //设置窗口居中显示
        this.setLocationRelativeTo(null);
        //设置窗口的可见性
        this.setVisible(true);
        //设置点击窗口上的关闭键,结束程序
        this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        //设置窗口大小不可变
        this.setResizable(false);
        //向窗口对象添加键盘监听器
        this.addKeyListener(this);
        //设置窗口名称
        this.setTitle("背带马里奥");
        //初始化图片
        StaticValue.init();
        //初始化马里奥
        mario = new Mario(10,355);
        //创建全部的场景
        for (int i = 1;i <= 3;i++) {
            allBg.add(new BackGround(i, i == 3 ? true : false));
        }
        //将第一个场景设置为当前场景
        nowBg = allBg.get(0);

        mario.setBackGround(nowBg);
        //绘制图像
        repaint();
        thread.start();
    }

    @Override
    public void paint(Graphics g) {
        if (offScreenImage == null) {
            offScreenImage = createImage(800,600);
        }

        Graphics graphics = offScreenImage.getGraphics();
        graphics.fillRect(0,0,800,600);

        //绘制背景
        graphics.drawImage(nowBg.getBgImage(),0,0,this);

        //绘制敌人
        for (Enemy e : nowBg.getEnemyList()) {
            graphics.drawImage(e.getShow(),e.getX(),e.getY(),this);
        }

        //绘制障碍物
        for (Obstacle ob : nowBg.getObstacleList()) {
            graphics.drawImage(ob.getShow(),ob.getX(),ob.getY(),this);
        }

        //绘制城堡
        graphics.drawImage(nowBg.getTower(),620,270,this);

        //绘制旗杆
        graphics.drawImage(nowBg.getGan(),500,220,this);

        //绘制马里奥
        graphics.drawImage(mario.getShow(),mario.getX(),mario.getY(),this);
        //添加分数
        graphics.setColor(Color.CYAN);
        graphics.setFont(Font.getFont("黑体"));
       graphics.drawString("当前分数为:"+mario.getScore(),300,100);
        //将图像绘制到窗口中
        g.drawImage(offScreenImage,0,0,this);
    }

    public static void main(String[] args) {
        MyFrame myFrame = new MyFrame();
    }

    @Override
    public void keyTyped(KeyEvent e) {

    }
    //当键盘按下按键时调用
    @Override
    public void keyPressed(KeyEvent e) {
        //向右移动
        if (e.getKeyCode() == 39) {
            mario.rightMove();
        }
        //向左移动
        if (e.getKeyCode() == 37) {
            mario.leftMove();
        }
        //跳跃
        if (e.getKeyCode() == 38) {
            mario.jump();
        }
        if(e.getKeyCode()==40){
            mario.falling();
        }
        if(e.getKeyCode()==32){
            repaint();

        }

    }
    //当键盘松开按键时调用
    @Override
    public void keyReleased(KeyEvent e) {
        //想左停止
        if (e.getKeyCode() == 37) {
            mario.leftStop();
        }
        //向右停止
        if (e.getKeyCode() == 39) {
            mario.rightStop();
        }
    }

    @Override
    public void run() {
        while (true) {
            repaint();
            try {
                Thread.sleep(50);

                if (mario.getX() >= 775) {
                    nowBg = allBg.get(nowBg.getSort());
                    mario.setBackGround(nowBg);
                    mario.setX(10);
                    mario.setY(355);
                }

                //判断马里奥是否死亡
                if (mario.isDeath()) {
                    JOptionPane.showMessageDialog(this,"马里奥死亡!!!");
                    System.exit(0);
                }

                //判断游戏是否结束
                if (mario.isOK()) {
                    JOptionPane.showMessageDialog(this,"恭喜你!成功通关");
                    System.exit(0);
                }
            } catch (InterruptedException e) {
                e.printStackTrace();
            }
        }
    }

}

马里奥类:

package com.sxt;

import java.awt.image.BufferedImage;

public class Mario implements Runnable{
    //用于表示横纵坐标
    private int x;
    private int y;
    //用于表示当前的状态
    private String status;
    //用于显示当前状态对应的图像
    private BufferedImage show = null;
    //定义一个BackGround对象,用来获取障碍物的信息
    private BackGround backGround = new BackGround();
    //用来实现马里奥的动作
    private Thread thread = null;
    //马里奥的移动速度
    private int xSpeed;
    //马里奥的跳跃速度
    private int ySpeed;
    //定义一个索引
    private int index;
    //表示马里奥上升的时间
    private int upTime = 0;
    //用于判断马里奥是否走到了城堡的门口
    private boolean isOK;
    //用于判断马里奥是否死亡
    private boolean isDeath = false;
    //定义一个变量,表示得分
    private int score=0;

    public Mario() {
    }

    public Mario (int x,int y) {
        this.x = x;
        this.y = y;
        show = StaticValue.stand_R;
        this.status = "stand--right";
        thread = new Thread(this);
        thread.start();
    }

    //人物的死亡
    public void death() {
        isDeath = true;
    }

    //马里奥向左移动
    public void leftMove() {
        //改变速度
        xSpeed = -5;
        //判断马里奥是否处于空中
        if (status.indexOf("jump") != -1) {//status中存在jump,及人物处于空中
            status = "jump--left";
        }else {
            status = "move--left";
        }
    }

    //马里奥向右移动
    public void rightMove() {
        xSpeed = 5;

        //判断马里奥是否碰到旗子
        if (backGround.isReach()) {
            xSpeed = 0;
        }
        if (status.indexOf("jump") != -1) {
            status = "jump--right";
        }else {
            status = "move--right";
        }
    }

    //马里奥向左停止
    public void leftStop() {
        xSpeed = 0;
        if (status.indexOf("jump") != -1) {
            status = "jump--left";
        }else {
            status = "stop--left";
        }
    }

    //马里奥向右停止
    public void rightStop() {
        xSpeed = 0;
        if (status.indexOf("jump") != -1) {
            status = "jump--right";
        }else {
            status = "stop--right";
        }
    }

    //马里奥跳跃
    public void jump() {
        if (status.indexOf("jump") == -1) {
            if (status.indexOf("left") != -1) {
                status = "jump--left";
            }else {
                status = "jump--right";
            }
            ySpeed = -10;
            upTime = 7;
        }

        //判断马里奥是否碰到旗子
        if (backGround.isReach()) {
            ySpeed = 0;
        }
    }

    //马里奥下落
    public void falling() {
        if (status.indexOf("left") != -1) {
            status = "jump--left";
        }else {
            status = "jump--right";
        }
        ySpeed = 10;
    }


    @Override
    public void run() {
        while (true) {
            //判断是否处于障碍物上
            boolean onObstacle = false;
            //判断是否可以往右走
            boolean canRight = true;
            //判断是否可以往左走
            boolean canLeft = true;
            //判断马里奥是否到达旗杆位置
            if (backGround.isFlag() && this.x >= 500) {
                this.backGround.setReach(true);

                //判断旗子是否下落完成
                if (this.backGround.isBase()) {
                    status = "move--right";
                    if (x < 690) {
                        x += 5;
                    }else {
                        isOK = true;
                    }
                }else {
                    if (y < 395) {
                        xSpeed = 0;
                        this.y += 5;
                        status = "jump--right";
                    }

                    if (y > 395) {
                        this.y = 395;
                        status = "stop--right";
                    }
                }

            }else {
                //遍历当前场景里所有的障碍物
                for (int i = 0; i < backGround.getObstacleList().size(); i++) {
                    Obstacle ob = backGround.getObstacleList().get(i);
                    //判断马里奥是否位于障碍物上
                    if (ob.getY() == this.y + 25 && (ob.getX() > this.x - 30 && ob.getX() < this.x + 25)) {
                        onObstacle = true;
                    }

                    //判断是否跳起来顶到砖块
                    if ((ob.getY() >= this.y - 30 && ob.getY() <= this.y - 20) && (ob.getX() > this.x - 30 && ob.getX() < this.x + 25)) {
                        if (ob.getType() == 0) {
                            backGround.getObstacleList().remove(ob);
                            score++;
                        }
                        upTime = 0;
                    }

                    //判断是否可以往右走
                    if (ob.getX() == this.x + 25 && (ob.getY() > this.y - 30 && ob.getY() < this.y + 25)) {
                        canRight = false;
                    }

                    //判断是否可以往左走
                    if (ob.getX() == this.x - 30 && (ob.getY() > this.y - 30 && ob.getY() < this.y + 25)) {
                        canLeft = false;
                    }

                }

                //判断马里奥是否碰到敌人死亡或者踩死蘑菇敌人
                for (int i = 0;i < backGround.getEnemyList().size();i++) {
                    Enemy e = backGround.getEnemyList().get(i);

                    if (e.getY() == this.y + 20 && (e.getX() - 25 <= this.x && e.getX() + 35 >= this.x)) {
                        if (e.getType() == 1) {
                            e.death();
                            score=score+2;
                            upTime = 3;
                            ySpeed = -10;
                        }else if (e.getType() == 2) {
                            //马里奥死亡
                            death();
                        }
                    }

                    if ((e.getX() + 35 > this.x && e.getX() - 25 < this.x) && (e.getY() + 35 > this.y && e.getY() - 20 < this.y)) {
                        //马里奥死亡
                        death();
                    }
                }

                //进行马里奥跳跃的操作
                if (onObstacle && upTime == 0) {
                    if (status.indexOf("left") != -1) {
                        if (xSpeed != 0) {
                            status = "move--left";
                        } else {
                            status = "stop--left";
                        }
                    } else {
                        if (xSpeed != 0) {
                            status = "move--right";
                        } else {
                            status = "stop--right";
                        }
                    }
                } else {
                    if (upTime != 0) {
                        upTime--;
                    } else {
                        falling();
                    }
                    y += ySpeed;
                }
            }

            if ((canLeft && xSpeed < 0) || (canRight && xSpeed > 0)) {//判断人物是否在最左端,还是在最右端
                x += xSpeed;
                //判断马里奥是否到了最左边
                if (x < 0) {
                    x = 0;
                }
            }
            //判断当前是否是移动状态
            if (status.contains("move")) {
                index = index == 0 ? 1 : 0;
            }
            //判断是否向左移动
            if ("move--left".equals(status)) {
                show = StaticValue.run_L.get(index);
            }
            //判断是否向右移动
            if ("move--right".equals(status)) {
                show = StaticValue.run_R.get(index);
            }
            //判断是否向左停止
            if ("stop--left".equals(status)) {
                show = StaticValue.stand_L;
            }
            //判断是否向右停止
            if ("stop--right".equals(status)) {
                show = StaticValue.stand_R;
            }
            //判断是否向左跳跃
            if ("jump--left".equals(status)) {
                show = StaticValue.jump_L;
            }
            //判断是否向右跳跃
            if ("jump--right".equals(status)) {
                show = StaticValue.jump_R;
            }

            try {
                Thread.sleep(50);
            } catch (InterruptedException e) {
                e.printStackTrace();
            }
        }
    }

    public int getX() {
        return x;
    }

    public void setX(int x) {
        this.x = x;
    }

    public int getY() {
        return y;
    }

    public void setY(int y) {
        this.y = y;
    }

    public BufferedImage getShow() {
        return show;
    }

    public void setShow(BufferedImage show) {
        this.show = show;
    }

    public void setBackGround(BackGround backGround) {
        this.backGround = backGround;
    }

    public boolean isOK() {
        return isOK;
    }

    public boolean isDeath() {
        return isDeath;
    }

    public int getScore() {
        return score;
    }

    public void setScore(int score) {
        this.score = score;
    }
}

常量类:

package com.sxt;
import javax.imageio.ImageIO;
import java.awt.*;
import java.awt.image.BufferedImage;
import java.io.File;
import java.io.IOException;
import java.util.ArrayList;
import java.util.List;

public class StaticValue {
    //背景图1,2
    public static BufferedImage bg = null;
    public static BufferedImage bg2 = null;
    //马里奥向左跳跃
    public static BufferedImage jump_L = null;
    //马里奥向右跳跃
    public static BufferedImage jump_R = null;
    //马里奥向左站立
    public static BufferedImage stand_L = null;
    //马里奥向右站立
    public static BufferedImage stand_R = null;
    //城堡
    public static BufferedImage tower = null;
    //旗杆
    public static BufferedImage gan = null;
    //障碍物
    public static List<BufferedImage> obstacle = new ArrayList<>();
    //马里奥向左跑
    public static List<BufferedImage> run_L = new ArrayList<>();
    //马里奥向右跑
    public static List<BufferedImage> run_R = new ArrayList<>();
    //蘑菇敌人(两种形态的蘑菇)
    public static List<BufferedImage> mogu = new ArrayList<>();
    //食人花敌人(两种状态,张开以及闭合)
    public static List<BufferedImage> flower = new ArrayList<>();
    //初始化方法
    public static void init() {
        try {
            bg = ImageIO.read(new File("D:\\素材\\images\\04超级玛丽的图片\\bg.png"));
            bg2= ImageIO.read(new File("D:\\素材\\images\\04超级玛丽的图片\\bg2.png"));

        } catch (IOException e) {
            e.printStackTrace();
        }
        //导入mario的图片(加载马里奥向左站立)
        try {
            stand_L = ImageIO.read(new File("D:\\素材\\images\\04超级玛丽的图片\\s_mario_stand_L.png"));
        } catch (IOException e) {
            e.printStackTrace();
        }
        //导入mario的图片(加载马里奥向右站立)
        try {
            stand_R = ImageIO.read(new File("D:\\素材\\images\\04超级玛丽的图片\\s_mario_stand_R.png"));
        } catch (IOException e) {
            e.printStackTrace();
        }
        //加载城堡
        try{
            tower=ImageIO.read(new File("D:\\素材\\images\\04超级玛丽的图片\\tower.png"));
        }catch(IOException e) {
            e.printStackTrace();
        }
        //加载旗杆
        try{
            gan=ImageIO.read(new File("D:\\素材\\images\\04超级玛丽的图片\\gan.png"));
        }catch(IOException e) {
            e.printStackTrace();
        }
        //导入mario的图片(加载马里奥向左跳跃)
        try{
            jump_L=ImageIO.read(new File("D:\\素材\\images\\04超级玛丽的图片\\s_mario_jump1_L.png"));
        }catch(IOException e) {
            e.printStackTrace();
        }
        //导入mario的图片(加载马里奥向右跳跃)
        try{
            jump_R=ImageIO.read(new File("D:\\素材\\images\\04超级玛丽的图片\\s_mario_jump1_R.png"));
        }catch(IOException e) {
            e.printStackTrace();
        }
        //导入mario的图片(加载马里奥向左跑2个图)
        try{
            run_L.add(ImageIO.read(new File("D:\\素材\\images\\04超级玛丽的图片\\s_mario_run1_L.png")));
            run_L.add(ImageIO.read(new File("D:\\素材\\images\\04超级玛丽的图片\\s_mario_run1_R.png")));
        }catch (IOException e) {
            e.printStackTrace();
        }

        //导入mario的图片(加载马里奥向右跑2个图)
        try{
            run_R.add(ImageIO.read(new File("D:\\素材\\images\\04超级玛丽的图片\\s_mario_run1_L.png")));
            run_R.add(ImageIO.read(new File("D:\\素材\\images\\04超级玛丽的图片\\s_mario_run2_R.png")));
        }catch (IOException e) {
            e.printStackTrace();
        }
        //加载障碍物
        try{
            obstacle.add(ImageIO.read(new File("D:\\素材\\images\\04超级玛丽的图片\\brick.png")));//砖块
            obstacle.add(ImageIO.read(new File("D:\\素材\\images\\04超级玛丽的图片\\soil_up.png")));//上地面
            obstacle.add(ImageIO.read(new File("D:\\素材\\images\\04超级玛丽的图片\\soil_base.png")));//下地面
            //加载水管
            obstacle.add(ImageIO.read(new File("D:\\素材\\images\\04超级玛丽的图片\\pipe1.png")));//水管1
            obstacle.add(ImageIO.read(new File("D:\\素材\\images\\04超级玛丽的图片\\pipe2.png")));//水管2
            obstacle.add(ImageIO.read(new File("D:\\素材\\images\\04超级玛丽的图片\\pipe3.png")));//水管3
            obstacle.add(ImageIO.read(new File("D:\\素材\\images\\04超级玛丽的图片\\pipe4.png")));//水管4
            // 加载不可破坏的砖块和旗子
            obstacle.add(ImageIO.read(new File("D:\\素材\\images\\04超级玛丽的图片\\brick2.png")));//
            //加载旗子
            obstacle.add(ImageIO.read(new File("D:\\素材\\images\\04超级玛丽的图片\\flag.png")));//
        }catch (IOException e) {
            e.printStackTrace();
        }
        //加载蘑菇敌人的图片
        try {
            mogu.add(ImageIO.read(new File("D:\\素材\\images\\04超级玛丽的图片\\fungus1.png")));
            mogu.add(ImageIO.read(new File("D:\\素材\\images\\04超级玛丽的图片\\fungus2.png")));
            mogu.add(ImageIO.read(new File("D:\\素材\\images\\04超级玛丽的图片\\fungus3.png")));
        } catch (IOException ex) {
            throw new RuntimeException(ex);
        }
        //加载食人敌人的图片
        try {
            flower.add(ImageIO.read(new File("D:\\素材\\images\\04超级玛丽的图片\\flower1.1.png")));
            flower.add(ImageIO.read(new File("D:\\素材\\images\\04超级玛丽的图片\\flower1.2.png")));
        }catch (IOException e){
            e.printStackTrace();
        }

    }
    }

障碍物类: 

package com.sxt;

import java.awt.image.BufferedImage;

public class Obstacle implements Runnable{
    //用于表示坐标
    private int x;
    private int y;
    //用于记录障碍物类型
    private int type;
    //用于显示图像
    private BufferedImage show = null;
    //定义当前的场景对象
    private BackGround bg = null;
    //定义一个线程对象
    private Thread thread = new Thread(this);
    //空参构造
    public Obstacle(int x,int y,int type,BackGround bg) {
        this.x = x;
        this.y = y;
        this.type = type;
        this.bg = bg;
        show = StaticValue.obstacle.get(type);
        //如果是旗子的话,启动线程
        if (type == 8) {
            thread.start();
        }
    }

    public int getX() {
        return x;
    }

    public void setX(int x) {
        this.x = x;
    }

    public int getY() {
        return y;
    }

    public void setY(int y) {
        this.y = y;
    }

    public int getType() {
        return type;
    }

    public void setType(int type) {
        this.type = type;
    }

    public BufferedImage getShow() {
        return show;
    }

    public void setShow(BufferedImage show) {
        this.show = show;
    }

    public BackGround getBg() {
        return bg;
    }

    public void setBg(BackGround bg) {
        this.bg = bg;
    }

    public Thread getThread() {
        return thread;
    }

    public void setThread(Thread thread) {
        this.thread = thread;
    }

    @Override
    public void run() {
        while (true) {
            if (this.bg.isReach()) {
                if (this.y < 374) {//马里奥在旗子后面时,让旗子自行下落。
                    this.y += 5;
                }else {
                    this.bg.setBase(true);
                }
            }

            try {
                Thread.sleep(50);
            } catch (InterruptedException e) {
                e.printStackTrace();
            }
        }
    }
}

敌人类: 

package com.sxt;

import java.awt.image.BufferedImage;

public class Obstacle implements Runnable{
    //用于表示坐标
    private int x;
    private int y;
    //用于记录障碍物类型
    private int type;
    //用于显示图像
    private BufferedImage show = null;
    //定义当前的场景对象
    private BackGround bg = null;
    //定义一个线程对象
    private Thread thread = new Thread(this);
    //空参构造
    public Obstacle(int x,int y,int type,BackGround bg) {
        this.x = x;
        this.y = y;
        this.type = type;
        this.bg = bg;
        show = StaticValue.obstacle.get(type);
        //如果是旗子的话,启动线程
        if (type == 8) {
            thread.start();
        }
    }

    public int getX() {
        return x;
    }

    public void setX(int x) {
        this.x = x;
    }

    public int getY() {
        return y;
    }

    public void setY(int y) {
        this.y = y;
    }

    public int getType() {
        return type;
    }

    public void setType(int type) {
        this.type = type;
    }

    public BufferedImage getShow() {
        return show;
    }

    public void setShow(BufferedImage show) {
        this.show = show;
    }

    public BackGround getBg() {
        return bg;
    }

    public void setBg(BackGround bg) {
        this.bg = bg;
    }

    public Thread getThread() {
        return thread;
    }

    public void setThread(Thread thread) {
        this.thread = thread;
    }

    @Override
    public void run() {
        while (true) {
            if (this.bg.isReach()) {
                if (this.y < 374) {//马里奥在旗子后面时,让旗子自行下落。
                    this.y += 5;
                }else {
                    this.bg.setBase(true);
                }
            }

            try {
                Thread.sleep(50);
            } catch (InterruptedException e) {
                e.printStackTrace();
            }
        }
    }
}

 

背景类:

 

package com.sxt;

import java.awt.image.BufferedImage;
import java.util.ArrayList;
import java.util.List;

public class BackGround {
    //当前场景要显示的图像
    private BufferedImage bgImage = null;
    //记录当前是第几个场景
    private int sort;
    //判断是否是最后一个场景
    private boolean flag;
    //用于存放我们的所有障碍物
    private List<Obstacle> obstacleList = new ArrayList<>();
    //用于存放我们的所有敌人
    private List<Enemy> enemyList = new ArrayList<>();
    //用于显示旗杆
    private BufferedImage gan = null;
    //用于显示城堡
    private BufferedImage tower = null;
    //判断马里奥是否到达旗杆位置
    private boolean isReach = false;
    //判断旗子是否落地
    private boolean isBase = false;

    public BackGround() {

    }

    public BackGround(int sort,boolean flag) {
        this.sort = sort;
        this.flag = flag;

        if (flag) {
            bgImage = StaticValue.bg2;
        }else {
            bgImage = StaticValue.bg;
        }

        //判断是否是第一关
        if (sort == 1) {
            //绘制第一关的地面,上地面type=1,下地面type=2
            for (int i = 0;i < 27;i++) {
                obstacleList.add(new Obstacle(i*30,420,1,this));
            }

            for (int j = 0;j <= 120;j += 30) {
                for (int i = 0;i < 27;i++) {
                    obstacleList.add(new Obstacle(i*30,570-j,2,this));
                }
            }


            //绘制砖块A
            for (int i = 120;i <= 150;i += 30) {
                obstacleList.add(new Obstacle(i,300,7,this));
            }


            //绘制砖块B-F
            for (int i = 300;i <= 570;i += 30) {
                if (i == 360 || i == 390 || i == 480 || i == 510 || i == 540) {
                    obstacleList.add(new Obstacle(i,300,7,this));
                } else {
                    obstacleList.add(new Obstacle(i,300,0,this));
                }
            }


            //绘制砖块G
            for (int i = 420;i <= 450;i += 30) {
                obstacleList.add(new Obstacle(i,240,7,this));
            }


            //绘制水管
            for (int i = 360;i <= 600;i += 25) {
                if (i == 360) {
                    obstacleList.add(new Obstacle(620,i,3,this));
                    obstacleList.add(new Obstacle(645,i,4,this));
                }else {
                    obstacleList.add(new Obstacle(620,i,5,this));
                    obstacleList.add(new Obstacle(645,i,6,this));
                }
            }

            //绘制第一关的蘑菇敌人
            enemyList.add(new Enemy(580,385,true,1,this));
            //绘制第一关的食人花敌人
            enemyList.add(new Enemy(635,420,true,2,328,428,this));
        }

        //判断是否是第二关
        if (sort == 2) {
            //绘制第二关的地面,上地面type=1,下地面type=2
            for (int i = 0;i < 27;i++) {
                obstacleList.add(new Obstacle(i*30,420,1,this));
            }

            for (int j = 0;j <= 120;j += 30) {
                for (int i = 0;i < 27;i++) {
                    obstacleList.add(new Obstacle(i*30,570-j,2,this));
                }
            }

            //绘制第一个水管
            for (int i = 360;i <= 600;i += 25) {
                if (i == 360) {
                    obstacleList.add(new Obstacle(60,i,3,this));
                    obstacleList.add(new Obstacle(85,i,4,this));
                }else {
                    obstacleList.add(new Obstacle(60,i,5,this));
                    obstacleList.add(new Obstacle(85,i,6,this));
                }
            }

            //绘制第二个水管
            for (int i = 330;i <= 600;i += 25) {
                if (i == 330) {
                    obstacleList.add(new Obstacle(620,i,3,this));
                    obstacleList.add(new Obstacle(645,i,4,this));
                }else {
                    obstacleList.add(new Obstacle(620,i,5,this));
                    obstacleList.add(new Obstacle(645,i,6,this));
                }
            }

            //绘制砖块C
            obstacleList.add(new Obstacle(300,330,0,this));

            //绘制砖块B,E,G
            for (int i = 270;i <= 330;i += 30) {
                if (i == 270 || i == 330) {
                    obstacleList.add(new Obstacle(i,360,0,this));
                }else {
                    obstacleList.add(new Obstacle(i,360,7,this));
                }
            }

            //绘制砖块A,D,F,H,I
            for (int i = 240;i <= 360;i += 30) {
                if (i == 240 || i == 360) {
                    obstacleList.add(new Obstacle(i,390,0,this));
                }else {
                    obstacleList.add(new Obstacle(i,390,7,this));
                }
            }

            //绘制妨碍1砖块
            obstacleList.add(new Obstacle(240,300,0,this));

            //绘制空1-4砖块
            for (int i = 360;i <= 540;i += 60) {
                obstacleList.add(new Obstacle(i,270,7,this));
            }

            //绘制第二关的第一个食人花敌人
            enemyList.add(new Enemy(75,420,true,2,328,418,this));
            //绘制第二关的第二个食人花敌人
            enemyList.add(new Enemy(635,420,true,2,298,388,this));
            //绘制第二关的第一个蘑菇敌人
            enemyList.add(new Enemy(200,385,true,1,this));
            //绘制第二关的第二个蘑菇敌人
            enemyList.add(new Enemy(500,385,true,1,this));
        }

        //判断是否是第三关
        if (sort == 3) {
            //绘制第三关的地面,上地面type=1,下地面type=2
            for (int i = 0;i < 27;i++) {
                obstacleList.add(new Obstacle(i*30,420,1,this));
            }

            for (int j = 0;j <= 120;j += 30) {
                for (int i = 0;i < 27;i++) {
                    obstacleList.add(new Obstacle(i*30,570-j,2,this));
                }
            }

            //绘制第三个背景的A-O砖块
            int temp = 290;
            for (int i = 390;i >= 270;i -= 30) {
                for (int j = temp;j <= 410;j += 30) {
                    obstacleList.add(new Obstacle(j,i,7,this));
                }
                temp += 30;
            }

            //绘制第三个背景的P-R砖块
            temp = 60;
            for (int i = 390;i >= 360;i -= 30) {
                for (int j = temp;j <= 90;j += 30) {
                    obstacleList.add(new Obstacle(j,i,7,this));
                }
                temp += 30;
            }

            //绘制旗杆
            gan = StaticValue.gan;

            //绘制城堡
            tower = StaticValue.tower;

            //添加旗子到旗杆上
            obstacleList.add(new Obstacle(515,220,8,this));

            //绘制第三关的蘑菇敌人
            enemyList.add(new Enemy(150,385,true,1,this));
        }
    }

    public BufferedImage getBgImage() {
        return bgImage;
    }

    public int getSort() {
        return sort;
    }

    public boolean isFlag() {
        return flag;
    }

    public List<Obstacle> getObstacleList() {
        return obstacleList;
    }

    public BufferedImage getGan() {
        return gan;
    }

    public BufferedImage getTower() {
        return tower;
    }

    public boolean isReach() {
        return isReach;
    }

    public void setReach(boolean reach) {
        isReach = reach;
    }

    public boolean isBase() {
        return isBase;
    }

    public void setBase(boolean base) {
        isBase = base;
    }

    public List<Enemy> getEnemyList() {
        return enemyList;
    }
}

总结:
马里奥游戏的核心是马里奥的移动和跳跃,根据马里奥的y轴坐标和下落的时间进行改变,从而达到马里奥的移动 ,当然也包括蘑菇和食人花的移动。其中背景、马里奥人物,蘑菇,食人花等的的建立,都是游戏的前提条件。都是根据集合放在一起,最重要的是人物与敌人之间的接触。

评论 2
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值