马里奥游戏 java

这是一个使用Java Swing实现的马里奥游戏。游戏包含多个关卡,每个关卡有不同的背景和障碍物。玩家可以控制马里奥左右移动和跳跃,避开敌人和障碍物。游戏通过线程实现马里奥的动态运动,同时有得分系统和场景切换功能。
摘要由CSDN通过智能技术生成

这是跟着B站教程一起做的,链接在此:点击即可
在这里插入图片描述
在这里插入图片描述
一个类包括属性和方法两种,我们首先定义MyFrame的属性。MyFrame的作用就是绘制最终的页面,所以在这个类里面要定义页面的大小、位置等等,主要目的就是将我们定义的图片绘制到窗口中去。

在游戏中我们有三个关卡,前两个关卡的背景图和第三个关卡的背景图是不一样的,所以设置了一个数组来保存我们所有的图片信息,因为在绘制的时候总要有一个场景吧,所以设置了nowbg对象,这里的background对象就是为了保存每一个场景中的所有要放置的图片,当我们在绘制的时候直接将nowbg里保存的图片画出即可。

游戏的绘制怎么能少得了主人公马里奥呢?所以我们这个时候定义一个Mario对象,并为它设置线程对象。

属性都定义好了以后我们就开始定义构造方法,绘制好游戏窗口,为定义的变量赋值,使用重定义的repaint方法,将图片显示在屏幕上。

重写paint方法的时候,先把图片放在缓冲区,等全部缓冲好以后我们再把它们一起绘制出来。

根据马里奥的坐标判断是否已经到达了场景的终点来进行场景的切换,根据bg里的障碍物坐标进行障碍物坐标的绘制。

对于主人公来说,我们需要控制的有他的位置即x、y坐标和当前的状态,由于要走路和跳跃,我们还要判断一下马里奥是否在空中,代码中的注释也写得很详细了,就是构造一些方法,通过方法去判断状态,重点是要提前判断好各点的坐标和位置关系从而设置限定条件,进行判断。

MyFrame类

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(){
    //窗口大小
    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 ob:nowBg.getEnemyList()){
            graphics.drawImage(ob.getShow(),ob.getX(),ob.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);
        //绘制分数
        Color c=graphics.getColor();
        graphics.setColor(Color.black);
        graphics.setFont(new Font("黑体",Font.BOLD,25));
        graphics.drawString("当前的分数为"+mario.getScore(),300,100);
        graphics.setColor(c);
        //把缓冲区的图片绘制到窗口中
        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();
        }
    }

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

        }
    }
}

Mario类

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 int  score=0;

    //判断马里奥是否死亡
    private boolean isDeath=false;
    //判断马里奥是否走到了城堡的门口
    private boolean isOK;
    public Mario(){
    }
    public void death()
    {
        isDeath=true;
    }
    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 leftMove(){
        xSpeed=-5;
        //判断马里奥1是否碰到了旗子
        if(backGround.isReach())
            xSpeed=0;
        //判断马里奥是否处于空中
        if(status.indexOf("jump")!=-1){
            status="jump--left";
        }else
        {
            status="move--left";
        }
    }

    //马里奥向右移动
    public void rightMove(){
        xSpeed=5;
        //判断马里奥1是否碰到了旗子
        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;
            //判断马里奥1是否碰到了旗子
            if(backGround.isReach())
                ySpeed=0;
        }
    }
    //马里奥下落
    public void fall(){
        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);
                        upTime = 0;
                        score+=1;
                    }
                }
                //判断是否可以往右走
                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();
                        upTime=3;
                        ySpeed=-10;
                        score+=2;
                    }
                    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 fall();
                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 int getY() {
        return y;
    }
    public void setX(int x) {
        this.x = x;
    }

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

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

    public BufferedImage getShow() {
        return show;
    }
    public void setBackGround(BackGround backGround) {
        this.backGround = backGround;
    }
    public boolean isOK() {
        return isOK;
    }
    public boolean isDeath() {
        return isDeath;
    }
}

Enemy类

import java.awt.image.BufferedImage;

public class Enemy implements  Runnable{
    //存储当前坐标
    private int x,y;



    //存储敌人类型
    private int type;
    //判断敌人运动的方向
    private boolean face_to=true;
    //用于显示敌人的当前图像
    private BufferedImage show;
    //定义一个背景对象
    private BackGround bg;
    //食人花运动的极限范围
    private int max_up=0;
    private int max_down=0;
    //定义线程对象
    private Thread thread=new Thread(this);
    //表示当前的图片状态
    private int image_type=0;
    //蘑菇敌人的构造函数
    public Enemy(int x,int y,boolean face_to,int type,BackGround bg){
        this.x=x;
        this.y=y;
        this.face_to=face_to;
        this.bg=bg;
        this.type=type;
        show=StaticValue.mogu.get(0);
        thread.start();
    }
    //敌人的死亡方法
    public void death(){
        show=StaticValue.mogu.get(2);
        this.bg.getEnemyList().remove(this);
    }
    //蘑菇敌人的构造函数
    public Enemy(int x,int y,boolean face_to,int type,int max_up,int max_down,BackGround bg){
        this.x=x;
        this.y=y;
        this.face_to=face_to;
        this.max_up=max_up;
        this.max_down=max_down;
        this.bg=bg;
        this.type=type;
        show=StaticValue.flower.get(0);
        thread.start();
    }
    public int getX() {
        return x;
    }

    public int getY() {
        return y;
    }

    public BufferedImage getShow() {
        return show;
    }
    public int getType() {
        return type;
    }

    @Override
    public void run() {
      while(true)
      {
          if(type==1) {
              if (face_to)
                  this.x -= 2;
              else
                  this.x += 2;
              image_type = image_type == 1 ? 0 : 1;
              show = StaticValue.mogu.get(image_type);
          }
      //定义两个布尔变量
        boolean canLeft =true;
        boolean canRight =true;
        for (int i = 0; i < bg.getObstacleList().size(); i++) {
            Obstacle ob1 = bg.getObstacleList().get(i);
            //判断是否可以向右走
            if(ob1.getX()==this.x+36&&(ob1.getY()+65>this.y&&ob1.getY()-35<this.y))
            {
                canRight=false;
            }
            //判断是否可以向左走
            if(ob1.getX()==this.x-36&&(ob1.getY()+65>this.y)&&ob1.getY()-35<this.y)
            {
                canLeft=false;
            }
        }
         if(face_to && !canLeft||this.x==0)
             face_to=false;
         else if (!face_to&& !canRight ||this.x==750)
             face_to=true;
         //判断是否是食人花敌人
          if(type==2){
              if(face_to)
                  this.y-=2;
              else
                  this.y+=2;
              image_type=image_type==1?0:1;
              //食人花是否移动到了极限位置
              if(face_to&&(this.y==max_up)){
                  face_to=false;
              }
              else if(!face_to&&(this.y==max_down)){
                  face_to=true;
              }
              show=StaticValue.flower.get(image_type);
          }
          try {
              Thread.sleep(50);
          } catch (InterruptedException e) {
              e.printStackTrace();
          }

      }


    }
}

Obstacle类

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;
        this.show=StaticValue.obstacle.get(type);
        if(type==8)
        {
            thread.start();
        }
    }

    public int getX() {
        return x;
    }

    public int getY() {
        return y;
    }

    public int getType() {
        return type;
    }

    public BufferedImage getShow() {
        return show;
    }

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

StaticValue类

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

public class StaticValue {
    //背景
    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 String path = System.getProperty("user.dir")+"/src/images/";
    //初始化方法
    public static void init(){
        try {
            //加载背景图
            bg= ImageIO.read(new File(path+"bg.png"));
            bg2= ImageIO.read(new File(path+"bg2.png"));
            //加载马里奥向左站立
            stand_L=ImageIO.read(new File(path+"s_mario_stand_L.png"));
            //加载马里奥向右站立
            stand_R=ImageIO.read(new File(path+"s_mario_stand_R.png"));
            //加载城堡
            tower=ImageIO.read(new File(path+"tower.png"));
            //加载旗杆
            gan=ImageIO.read(new File(path+"gan.png"));
            //加载马里奥向左跳跃
            jump_L=ImageIO.read(new File(path+"s_mario_jump1_L.png"));
            //加载马里奥向右跳跃
            jump_R=ImageIO.read(new File(path+"s_mario_jump1_R.png"));

        } catch (IOException e) {
            e.printStackTrace();
        }
        for (int i = 1; i <=2 ; i++) {
            try {
                run_L.add(ImageIO.read(new File(path+"s_mario_run"+i+"_L.png")));
            } catch (IOException e) {
                e.printStackTrace();
            }
        }
        for (int i = 1; i <=2 ; i++) {
            try {
                run_R.add(ImageIO.read(new File(path+"s_mario_run"+i+"_R.png")));
            } catch (IOException e) {
                e.printStackTrace();
            }
        }
        //加载障碍物
        try {
            obstacle.add(ImageIO.read(new File(path+"brick.png")));
            obstacle.add(ImageIO.read(new File(path+"soil_up.png")));
            obstacle.add(ImageIO.read(new File(path+"soil_base.png")));
        } catch (IOException e) {
            e.printStackTrace();
        }
        //加载水管
        for (int i = 1; i <=4 ; i++) {
            try {
                obstacle.add(ImageIO.read(new File(path+"pipe"+i+".png")));
            } catch (IOException e) {
                e.printStackTrace();
            }
        }
        //加载不可破坏的砖块和旗帜
        try {
            obstacle.add(ImageIO.read(new File(path+"brick2.png")));
            obstacle.add(ImageIO.read(new File(path+"flag.png")));
        } catch (IOException e) {
            e.printStackTrace();
        }
        //加载蘑菇
        for (int i = 1; i <=3 ; i++) {
            try {
                mogu.add(ImageIO.read(new File(path+"fungus"+i+".png")));
            } catch (IOException e) {
                e.printStackTrace();
            }
        }
        //加载食人花敌人
        for (int i = 1; i <=2 ; i++) {
            try {
                flower.add(ImageIO.read(new File(path+"flower1."+i+".png")));
            } catch (IOException e) {
                e.printStackTrace();
            }
        }

    }
}

BackGround类

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 boolean isReach=false;
    //判断旗子是否落地
    private boolean isBase=false;
    //显示城堡
    private BufferedImage tower = null;

    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) {
            //绘制第一关的地面
            for (int i = 0; i < 27; i++) {
                obstacleList.add(new Obstacle(i * 30, 420, 1, this));
            }
            for (int i = 0; i <= 120; i += 30) {
                for (int j = 0; j < 27; j++) {
                    obstacleList.add(new Obstacle(j * 30, 570 - i, 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,418,this));

        }
        //判断是否是第二关
        if(sort==2)
        {
            //绘制地面
            for (int i = 0; i < 27; i++) {
                obstacleList.add(new Obstacle(i * 30, 420, 1, this));
            }
            for (int i = 0; i <= 120; i += 30) {
                for (int j = 0; j < 27; j++) {
                    obstacleList.add(new Obstacle(j * 30, 570 - i, 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 = 270; 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(200,385,true,1,this));
            enemyList.add(new Enemy(500,385,true,1,this));
            //绘制第二关的食人花敌人
            enemyList.add(new Enemy(75,420,true,2,328,418,this));
            enemyList.add(new Enemy(635,420,true,2,328,418,this));
        }
        if(sort==3)
        {
            //绘制地面
            for (int i = 0; i < 27; i++) {
                obstacleList.add(new Obstacle(i * 30, 420, 1, this));
            }
            for (int i = 0; i <= 120; i += 30) {
                for (int j = 0; j < 27; j++) {
                    obstacleList.add(new Obstacle(j * 30, 570 - i, 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 getTower() {
        return tower;
    }
    public BufferedImage getGan() {
        return gan;
    }

    public boolean isBase() {
        return isBase;
    }

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

    public void setReach(boolean reach) {
        isReach = reach;
    }
    public List<Enemy> getEnemyList() {
        return enemyList;
    }

}



  • 12
    点赞
  • 40
    收藏
    觉得还不错? 一键收藏
  • 6
    评论
评论 6
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值