超级玛丽奥

该项目主要采用面向对象编程(封装、继承、多态)、swing桌面编程、数组、集合、while、if、for、foreach循环语句、多线程等技术。适合中级程序员学习。

1.BackGround 类

package com.sy.mario;

//awt:abstract  window tools
import java.awt.image.BufferedImage;
import java.util.ArrayList;
import java.util.List;

//场景类 关《TankClient:背景 主人公  敌人  障碍物 etc》
public class BackGround {
        //当前的关数
        private int  sort;
        //是不是最后一关的变量
        private boolean  flag;
        //背景图片
        private BufferedImage  showImage;
        //开始下落的变量
        public  boolean 旗帜下落;
        
        //定义存储每一关障碍物的集合
        private List<Obstrcution> allobs=new ArrayList<Obstrcution>();
        //定义存储每一关死亡的障碍物集合
        private List<Obstrcution> removeobs=new ArrayList<Obstrcution>();

        //定义存储每一关敌人集合
        private List<Enemy>  allenemys=new ArrayList<Enemy>();
        //定义存储每一关死亡的敌人集合
        private List<Enemy>  removeenemys=new ArrayList<Enemy>();
        
        public boolean 降旗完毕;
        
        public boolean 通关;

        
        
        public List<Enemy> getAllenemys() {
            return allenemys;
        }

        public List<Enemy> getRemoveenemys() {
            return removeenemys;
        }

        public List<Obstrcution> getRemoveobs() {
            return removeobs;
        }

        //alt+shift+s(Source源码)
        public BackGround(int sort, boolean flag) {
            super();
            this.sort = sort;
            this.flag = flag;
            
            //根据flag确定showImage
            if(flag){//最后一关
                 showImage=StaticValue.endImage;
            }else{   //还在继续
                 showImage=StaticValue.bgImage;
            }
            //根据sort的值确定allobs内容
            if(1 == sort){
                 //地面
                 for(int i=0;i<15;i++){
                     allobs.add(new Obstrcution(60*i,540,9,this));
                 }
                 //第一层
                 allobs.add(new Obstrcution(120,360,4,this));
                 allobs.add(new Obstrcution(300,360,0,this));
                 allobs.add(new Obstrcution(360,360,4,this));
                 allobs.add(new Obstrcution(420,360,0,this));
                 allobs.add(new Obstrcution(480,360,4,this));
                 allobs.add(new Obstrcution(540,360,0,this));
                 //第二层
                 allobs.add(new Obstrcution(420,180,4,this));
                 //花盆
                 allobs.add(new Obstrcution(660,480,8,this));
                 allobs.add(new Obstrcution(720,480,7,this));
                 allobs.add(new Obstrcution(660,540,6,this));
                 allobs.add(new Obstrcution(720,540,5,this));
            
                 //敌人
                 allenemys.add(new Enemy(600,480,1,true,this));
                 allenemys.add(new Enemy(690,520,2,540,360,true,this));
            }
            //处理第二关
            if(sort == 2){
                 //地面
                 for(int i=0;i<15;i++){
                     allobs.add(new Obstrcution(60*i,540,9,this));
                 }
                 //第一个花盆
                 allobs.add(new Obstrcution(160,480,8,this));
                 allobs.add(new Obstrcution(220,480,7,this));
                 allobs.add(new Obstrcution(160,540,6,this));
                 allobs.add(new Obstrcution(220,540,5,this));
                 
                 allenemys.add(new Enemy(190,520,2,540,360,true,this));
                 
                 //从左到右设计
                 allenemys.add(new Enemy(280,480,1,false,this));
                 //从右到左设计
                 allenemys.add(new Enemy(600,480,1,true,this));
                 
                //第二个花盆
                 allobs.add(new Obstrcution(660,420,8,this));
                 allobs.add(new Obstrcution(720,420,7,this));
                 allobs.add(new Obstrcution(660,480,6,this));
                 allobs.add(new Obstrcution(720,480,5,this));
                 allobs.add(new Obstrcution(660,540,6,this));
                 allobs.add(new Obstrcution(720,540,5,this));
                 allenemys.add(new Enemy(690,480,2,540,360,true,this));
                 allenemys.add(new Enemy(690,520,2,540,360,true,this));
            }
            //处理第三关
            if(sort == 3){
                 //地面
                 for(int i=0;i<15;i++){
                     allobs.add(new Obstrcution(60*i,540,9,this));
                 }
                 //画出地面的砖块
                 allobs.add(new Obstrcution(520,480,2,this));
                 //画出旗帜
                 allobs.add(new Obstrcution(550,180,11,this));
            }
            
        }

        public BufferedImage getShowImage() {
            return showImage;
        }

        public List<Obstrcution> getAllobs() {
            return allobs;
        }

        public int getSort() {
            // TODO Auto-generated method stub
            return sort;
        }

        public boolean isFlag() {
            return flag;
        }
        
        //解冻方法
        public  void  resume(){
            for(Enemy  e:allenemys){
                 e.resume();
            }
        }

        public void reset() {
            this.allobs.addAll(removeobs);
            this.allenemys.addAll(removeenemys);
            
            //分别取出敌人和障碍物将对应数据回传给他们 
            for(Obstrcution  obs:allobs){
                obs.reset();
            }
            
            for(Enemy   e:allenemys){
                e.reset();
            }
        }        
}
2.Enemy  类

package com.sy.mario;

import java.awt.image.BufferedImage;

//敌人类
public class Enemy  implements Runnable{
            //坐标
            private  int  x,y,startX,startY;
            //类型
            private  int  type;
            //图片
            private  BufferedImage  showImage;
            //食人花的上下限范围
            private  int  downmax,upmax;
            //默认朝向
            private  boolean isUporLeft;
            
            //持有场景对象
            private  BackGround   bg;
            
            //创建有名对象
            Thread  t=new Thread(this);
            
            //普通敌人
            public Enemy(int x, int y, int type,
                    boolean isUporLeft,BackGround   bg) {
                super();
                this.bg=bg;
                this.startX=this.x = x;
                this.startY=this.y = y;
                this.type = type;
                this.isUporLeft = isUporLeft;
                //根据type来划分是蘑菇还是乌龟还是其他的
                showImage=StaticValue.allTriangleImage.get(0);
            
                t.start();
                t.suspend();
            }

            
            //食人花
            public Enemy(int x, int y, int type, int downmax, int upmax,
                    boolean isUporLeft,BackGround   bg) {
                super();
                this.bg=bg;
                this.startX=this.x = x;
                this.startY=this.y = y;
                this.type = type;
                this.downmax = downmax;
                this.upmax = upmax;
                this.isUporLeft = isUporLeft;
                showImage=StaticValue.allFlowerImage.get(0);
            
                t.start();
                t.suspend();
            }


            public int getX() {
                return x;
            }


            public int getY() {
                return y;
            }


            public BufferedImage getShowImage() {
                return showImage;
            }

            //定义切换步子
            private  int  imagetype=0;
            public void run() {
                 while(true){
                        boolean  canLeft=true;
                        boolean  canRight=true;
                        //【对集合数组同时进行大批量的增加和删除,建议采用基本for】
                        for(int i=0;i<bg.getAllobs().size();i++){
                            Obstrcution  obs=bg.getAllobs().get(i);
                            //不能向右
                            if(x+60 == obs.getX()&& y>=obs.getY()-40
                                        && y<=obs.getY()+40){
                                canRight=false;
                            }
                            //不能向左
                            if(x-60 == obs.getX()&& y>=obs.getY()-40
                                        && y<=obs.getY()+40){
                                canLeft=false;
                            }
                        }
                     
                     //根据定义type找出食人花和普通敌人
                     if(1 == type){
                         //普通敌人
                        
                         
                         if(imagetype == 0){
                             imagetype=1;
                         }else{
                             imagetype=0;
                         }
                         //正在朝左走但是不能朝左走
                         if(isUporLeft&&!canLeft || x<=0){
                             //朝右走
                             isUporLeft=false;
                         }
                         
                         //正在朝右走但是不能朝右走
                         if(!isUporLeft&&!canRight || x>=840){
                             //朝左走
                             isUporLeft=true;
                         }
                         
                         if(isUporLeft)
                             x-=2;//默认朝左
                         else
                             x+=2;
                         
                        showImage=StaticValue.allTriangleImage.get(imagetype);
                     }
                     
                     if(type == 2){
                         //食人花
                        
                         
                         if(imagetype == 0){
                             imagetype=1;
                         }else{
                             imagetype=0;
                         }
                         
                         //正在朝上走但是不能朝上(小于最大值)
                         if(isUporLeft&&y<=upmax){
                             isUporLeft=false;
                         }
                         //正在朝下走但是不能朝下(大于最小值)
                         if(!isUporLeft&&y>=downmax){
                             isUporLeft=true;
                         }
                         
                         if(isUporLeft)
                             y-=2;//默认朝上
                         else
                             y+=2;
                         
                        showImage=StaticValue.allFlowerImage.get(imagetype);
                     }
                     
                     
                     try {
                        Thread.sleep(100);
                    } catch (InterruptedException e) {
                        e.printStackTrace();
                    }
                 }
            }


            public int getType() {
                // TODO Auto-generated method stub
                return type;
            }


            public void dead() {
                bg.getAllenemys().remove(this);
                bg.getRemoveenemys().add(this);
            }


            public void reset() {
                x=startX;
                y=startY;
            }
            public void resume() {
                t.resume();
            }           
}

3. Mario 类

package com.sy.mario;

import java.awt.image.BufferedImage;

//主人公
public class Mario implements Runnable{

        private int  x,y;//坐标
        private BufferedImage  showImage;//图片
        //横向移动的偏移量
        private  int  xmove;
        //纵向移动的偏移量
        private  int  ymove;
        
        //表示图片的字符串变量
        private String status;
        //持有场景
        private BackGround  bg;
        
        //定义Mario存活的变量(假设只有1条命)
        public  boolean  live;
        //定义Mario设置3条命
        public  int   life=3;
        
        public  void  setBg(BackGround  bg){
            this.bg=bg;
        }
        
        public Mario(int  x,int y){
            //默认是活的
            this.live=true;
            
            this.x=x;
            this.y=y;
            //指定第一张右站立的图片1.gif
            showImage=StaticValue.allMarioImage.get(0);
            //指定默认的状态"right-standing"
            status="right-standing";
            
            //开启线程完成mario的横向移动
            new Thread(this).start();
        }

        //定义出Mario横向移动的四个基本方法
        public  void  leftstop(){
            xmove=0;
            //初始化
            status="left-standing";
        }
        public  void  leftmove(){
            xmove=-5;
            status="left-moving";
        }
        public  void  rightstop(){
            xmove=0;
            status="right-standing";
        }
        public  void  rightmove(){
            xmove=5;
            status="right-moving";
        }
        
        //Mario纵向移动的方法
        //上升
        //定义上升的时间
        int  uptime=0;//默认没有上升
        public  void  jump(){
            if(status.indexOf("left")!=-1){
                //Mario打算从左边起跳
                status="left-jumping";
            }else{
                //Mario打算从右边起跳
                status="right-jumping";
            }
            ymove=-5;//ymove认为是速度0.1s(1上升50)
            //求出上升的时间t=s/v(Math.abs())
            //uptime从起始点给定上升的时间
            //但是随着线程的刷新我们程序员人为减少uptime
            //时间,直到uptime=0说明上升结束
            uptime=180/5;
        }
        
        //下降的方法(不需要给定下降的时间)
        public  void   down(){
            if(status.indexOf("left")!=-1){
                //PS:上升和下降对应的图片是一致的
                status="left-jumping";
            }else{
                status="right-jumping";
            }
            ymove=5;
        }
        
        
        
        //独立线程Thread-A
        //定义:朝一个方向走的步子(0-1逐个逐个走)
        private  int  moving=0;
        public void run() {
            //不断监听玩家操作键盘-->xmove
            while(true){
                //处理Mario游戏通过过程
                if(x>520 && bg.isFlag()){
                    //游戏正在通关
                    //1.改变状态
                    status="right-jumping";
                    //2.mario下落到砖块上面
                    if(y<420){
                        y+=5;
                    }
                    if(y==420){
                        y=420;
                        status="right-standing";
                    //3.通知旗帜开始下落
                        bg.旗帜下落=true;
                    }
                    
                    //4.当降旗完毕后,再来通关
                    if(bg.降旗完毕){
                        if(x<580){
                            x+=5;
                            status="right-moving";
                        }
                        if(x == 580){
                            if(y<480){
                                y+=5;
                                status="right-jumping";
                            }
                            if(y == 480){
                                //落到地面
                                status="right-standing";
                            }
                        }
                        
                        if(y == 480){
                            //朝右走
                            if(x<800){
                                x+=10;
                                status="right-moving";
                            }
                            if(x == 750){
                                //游戏通关结束
                                bg.通关=true;
                            }
                        }
                    }
                    
                }else{
                    //游戏还在继续
                //处理Mario遇到敌人
                for(int i=0;i<bg.getAllenemys().size();i++){
                     Enemy  e=bg.getAllenemys().get(i);
                     /*
                      * 1.通过类型在区分横向和纵向
                      * 2.通过横向和纵向再来类型
                      */
                     //横向
                     if(x>e.getX()-60&&x<e.getX()+60
                         && y>e.getY()-60&& y<e.getY()+60){
                         //Mario死亡
                         this.dead();
                     }
                     //纵向
                     if(y+60==e.getY() && x<=e.getX()+60
                                && x>=e.getX()-60){
                         //根据敌人类型做出死亡判断
                         if(e.getType() == 1){
                             //普通敌人
                             e.dead();
                         }
                         if(e.getType() == 2){
                             //食人花
                             this.dead();
                         }
                     }
                     
                }
                
                
                
                
                
                //分析:Mario纵向行为
                //1.找出是否在障碍物上面
                boolean  οnlοad=false;
                //2.找出Mario上升的高度 180
                //3.整理上升和下降的方法
                
                //分析:Mario横向遇到障碍物逻辑
                boolean  canLeft=true;
                boolean  canRight=true;
                //【对集合数组同时进行大批量的增加和删除,建议采用基本for】
                for(int i=0;i<bg.getAllobs().size();i++){
                    Obstrcution  obs=bg.getAllobs().get(i);
                    //不能向右
                    if(x+60 == obs.getX()&& y>=obs.getY()-40
                                && y<=obs.getY()+40){
                        canRight=false;
                    }
                    //不能向左
                    if(x-60 == obs.getX()&& y>=obs.getY()-40
                                && y<=obs.getY()+40){
                        canLeft=false;
                    }
                    //在障碍物上面
                    if(y+60==obs.getY() && x<=obs.getX()+60
                                && x>=obs.getX()-60){
                        οnlοad=true;
                    }
                    //顶到障碍物
                    if(y-60==obs.getY() && x<=obs.getX()+60
                            && x>=obs.getX()-60){
                        //根据障碍物的类型处理对应的结果
                        //普通的砖块
                        if(obs.getType() == 0){
                            //干掉障碍物
                            bg.getAllobs().remove(obs);
                            //为游戏的重置做准备
                            bg.getRemoveobs().add(obs);
                        }
                        //问号砖块
                        if(obs.getType() == 4){
                            //直接置换类型
                            obs.setType(2);
                            //uptime=5;
                        }
                        uptime=0;
                  }
                }
                
                //通过onload操作ymove进一步控制Y
                if(onload && uptime == 0){
                     //我在障碍物上面(1.定在障碍物上面 2.打算去跳)
                     //分析:standing下来|moving下来的
                     if(status.indexOf("left")!=-1){
                         if(status.indexOf("moving")!=-1){
                              status="left-moving";
                         }else{
                              status="left-standing";
                         }
                     }else{
                         if(status.indexOf("moving")!=-1){
                              status="right-moving";
                         }else{
                              status="right-standing";
                         }
                     }
                    
                    
                     MyFrame.跳跃=true;
                }else{
                    //我不在障碍物上面(空中:上升或者下降)
                    if(uptime!=0){
                        //说明还在上升
                        uptime--;
                    }else{
                        //说明上升最高点,只能是下降
                        this.down();
                    }
                    
                    y+=ymove;
                    
                    MyFrame.跳跃=false;
                }
                
                //根据canLeft和canRight进行x的控制
                if(xmove<0&&canLeft || xmove>0&&canRight){
                    x+=xmove;
                    //处理最左边
                    if(x<=0) x=0;
                }
        }//else结束符号
                //分析:Mario横向移动图片逻辑
                //1:朝一个方向走图片切换最多是3个
                //2.朝左=朝右+5
                
                //定义:allMarioImges集合图片的索引
                //右: 0  1  2  3
                //左: 5  6  7  8
                int index=0;//默认是第0张
                if(status.indexOf("left")!=-1){
                    index+=5;
                }
                //走和停止
                //standing
                //1.standing--->standing(ERROR)
                //2.standing--->moving(移动)
                //3.moving--->moving(移动)
                //4.moving--->standing(移动|停止)
                if(status.indexOf("moving")!=-1){
                    index+=moving;//2
                    moving++;
                 //切换完毕之后的判断结果
                    if(moving == 4){
                        moving=0;
                    }
                }
                
                //指定图片的切换利用index
                if(status.indexOf("left-jumping")!=-1){
                    showImage=StaticValue.allMarioImage.get(9);
                }else if(status.indexOf("right-jumping")!=-1){
                    showImage=StaticValue.allMarioImage.get(4);
                }else{
                    showImage=StaticValue.allMarioImage.get(index);
                }
                
                try {
                    Thread.sleep(20);//参数 long(毫秒)
                } catch (InterruptedException e) {
                    e.printStackTrace();
                }
            }
        }
        
        //三目童子
        private void dead() {
            life--;
            if(life == 0){
                //Mario所有的命都没有了
                live=false;
            }else{
                //让Mario和敌人以及障碍物全部重置
                this.reset();
                bg.reset();
            }
        }

        public  void  reset(){
            x=0;
            y=480;
        }
        
        public int getX() {
            return x;
        }

        public int getY() {
            return y;
        }

        public BufferedImage getShowImage() {
            return showImage;
        }

        public void setX(int i) {
            // TODO Auto-generated method stub
            this.x=i;
        }

}
4.MyFrame类  主程序类

package com.sy.mario;

import java.awt.Color;
import java.awt.Graphics;
import java.awt.event.KeyAdapter;
import java.awt.event.KeyEvent;
import java.awt.event.KeyListener;
import java.awt.image.BufferedImage;
import java.util.ArrayList;
import java.util.Iterator;
import java.util.List;

import javax.swing.JFrame;
import javax.swing.JOptionPane;

//1.画出游戏的窗口 2.画出窗口默认的背景  3.画出障碍物
//4.画出主人公Mario  5.处理Mario的横向移动  6.切换图片
//7.处理Mario横向遇到障碍物
//8.完成Mario的基本跳跃  9.处理障碍物死亡问题  10.画出敌人
//11.处理敌人移动    12.完成Mario遇到敌人的情况
//13.处理切换场景    14.完成Mario多条生命
//15.处理开机画面       16.处理线程挂载(冻结起来)
public class MyFrame extends JFrame{
    
    //定义所有的场景的集合
    List<BackGround>  allbg=new ArrayList<BackGround>();
    //定义当前的场景
    BackGround  nowbg;
    //定义当前的主人公
    Mario  m;
    //操作Jump的boolean变量 $¥
    public static  boolean  跳跃=true;
    //定义游戏是否开始
    public static  boolean 开始=false;
    
    public  MyFrame(){
        //初始化图片
        StaticValue.init();
        //初始化场景
        for(int i=1;i<=3;i++){
            allbg.add(new BackGround(i,i==3?true:false));
        }
        //初始化当前的场景【默认关数=1】
        nowbg=allbg.get(0);
        //初始化Mario
        m=new Mario(0,480);
        //告诉mario在哪一个场景中
        m.setBg(nowbg);
        
        //窗口基本的设置
        //标题
        this.setTitle("超级玛丽");
        //大小
        this.setSize(900,600);
        //固定大小
        this.setResizable(false);
        //关闭
        this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        //显示
        this.setVisible(true);
        
        //完成键盘的监听
        this.addKeyListener(new MyListener());
        //实现界面的刷新
        new Thread(new MyThread()).start();
    }
    
    //内部类(成员、静态、局部、匿名)
    private class MyListener extends KeyAdapter
        implements  KeyListener{//F4
        
        //压下
        public void keyPressed(KeyEvent e) {
            int  key=e.getKeyCode();//ASCII
    if(开始){    
            if(key == KeyEvent.VK_LEFT){
                //调用mario朝左移动方法
                m.leftmove();
            }
            if(key == KeyEvent.VK_RIGHT){
                //调用mario朝右移动方法
                m.rightmove();
            }
            if(key == KeyEvent.VK_SPACE){
                //调用mario上升的方法
             if(跳跃)
                m.jump();
            }
    }else{
            if(key == KeyEvent.VK_SPACE){
                //说明游戏开始
                开始=true;
                //解冻
                nowbg.resume();
            }
        
    }
        }
         
        //松开
        public void keyReleased(KeyEvent e) {
            int  key=e.getKeyCode();
    if(开始){        
            if(key == KeyEvent.VK_LEFT){
                //调用mario朝左停止方法
                m.leftstop();
            }
            if(key == KeyEvent.VK_RIGHT){
                //调用mario朝右停止方法
                m.rightstop();
            }
    }
        }
    }
    
    private class MyThread implements Runnable{
        public void run() {
            while(true){
                //处理游戏通关
                if(nowbg.通关){
                    //显示对话框
                    JOptionPane.showMessageDialog(null, "恭喜你,大内低手");
                    //关闭JVM 
                    System.exit(-1);
                }
                
                //切换场景
                if(m.getX()>=840){
                    //1.获取当前的场景
                    int  n=nowbg.getSort();
                    //2.设置游戏下一个场景
                    nowbg=allbg.get(n+1-1);
                    //3.重置m的横坐标
                    m.setX(0);
                    //4.告诉Mario你也需要切换场景
                    m.setBg(nowbg);
                    
                    //解冻
                    nowbg.resume();
                }
                
                //根据Mario的live判断游戏是否结束
                if(!m.live){
                    //显示对话框
                    JOptionPane.showMessageDialog(null, "mario死亡,游戏结束");
                    //关闭JVM 
                    System.exit(-1);
                }    
                
                try {
                    Thread.sleep(100);
                } catch (InterruptedException e) {
                    e.printStackTrace();
                }
                //repaint/update
                repaint();//重绘reset+paint
            }
        }
    }
    
    //绘制方法(main函数系统会刷新调用)
    public void paint(Graphics g) {
        // super.paint(g);
        //定义临时变量(没有内存的 和方法)帮助我们解决闪烁问题(桌布)
        BufferedImage image=new BufferedImage(900,600,BufferedImage.TYPE_3BYTE_BGR);
        Graphics g2 = image.getGraphics();
    if(开始){    
        //绘制背景(参数:图片 坐标  当前对象)
        g2.drawImage(nowbg.getShowImage(),0, 0, this);
        //绘制障碍物
        Iterator<Obstrcution> its = nowbg.getAllobs().iterator();
        while(its.hasNext()){
            Obstrcution  obs=its.next();
            g2.drawImage(obs.getShowImage(),obs.getX(),obs.getY(),this);
        }
        //绘制Mario
        g2.drawImage(m.getShowImage(),m.getX(),m.getY(),this);
    
        //绘制敌人    擦式类型
        Iterator<Enemy> it2 = nowbg.getAllenemys().iterator();
        while(it2.hasNext()){
             Enemy e=it2.next();//返回值是Object
             g2.drawImage(e.getShowImage(), e.getX(), e.getY(), this);
        }
    }else{
        //绘制开机的画面
        g2.drawImage(StaticValue.startImage,0,0,this);
    }
        
        //将image(桌布)整体和g发生关系
        g.drawImage(image,0,0,this);
    }
    
    public static void main(String[] args) {
        new MyFrame();
    }
}
5.Obstrcution类

package com.sy.mario;

import java.awt.image.BufferedImage;

//障碍物
public class Obstrcution implements Runnable{
        
        private  int  x,y;//坐标
        private  int  type;//类型
        private  BufferedImage  showImage;//图片
        
        //定义保存原来障碍物类型的变量
        private  int startType;
        
        
        //相互持有
        BackGround  bg;
        
        public  int  getType(){
            return type;
        }
        
        public Obstrcution(int x, int y, int type,BackGround  bg) {
            super();
            this.x = x;
            this.y = y;
            this.startType=this.type = type;
            this.bg=bg;
            //根据type确定showImage
            setType(type);
            //如果是旗帜
            if(11 == type)
                new Thread(this).start();
        }
        
        
        public int getX() {
            return x;
        }
        public int getY() {
            return y;
        }
        public BufferedImage getShowImage() {
            return showImage;
        }

        public void setType(int i) {
            this.type=i;
            showImage=StaticValue.allObstructionImage.get(type);
        }

        public void run() {
            // TODO Auto-generated method stub
            while(true){
                
                //如果场景通知旗帜下落
                if(bg.旗帜下落){
                    if(y<420){
                        y+=5;
                    }
                    if(y == 420){
                        y=420;
                        //降旗完毕
                        bg.降旗完毕=true;
                    }
                }
                
                try {
                    Thread.sleep(100);
                } catch (InterruptedException e) {
                    e.printStackTrace();
                }
            }
        }

        public void reset() {
            //setType(startType);
            this.type=startType;
            setType(type);
        }
        
        
}
6.StaticValue类

package com.sy.mario;

import java.util.ArrayList;
import java.util.Collection;
import java.util.List;
import java.awt.image.BufferedImage;
import java.io.File;
import java.io.IOException;

import javax.imageio.ImageIO;


//将所有的图片加载到集合或者变量属性中,帮助开发
public class StaticValue {
    //程序运行的时候,数据是存放在变量里面,临时存放在内存中 //想做java高级开放,至少配16G的内存
    //8种基本数据类型,其他的就是引用类型,重点要掌握java中的集合 Collection 和 map
    //list集合存放元素的特点:有序(下标从0开始),可重复
    //以下所有变量就是用来存放游戏的图片资源
    public static List<BufferedImage> allMarioImage=new ArrayList<BufferedImage>();
    public static BufferedImage startImage = null;
    public static BufferedImage endImage = null;
    public static BufferedImage bgImage = null;
    
    public static List<BufferedImage> allFlowerImage=new ArrayList<BufferedImage>();
    public static List<BufferedImage> allTriangleImage=new ArrayList<BufferedImage>();
    public static List<BufferedImage> allTurtleImage=new ArrayList<BufferedImage>();
    public static List<BufferedImage> allObstructionImage=new ArrayList<BufferedImage>();
    public static BufferedImage marioDeadImage = null;

    public static String imagePath=System.getProperty("user.dir")+"/bin/";
    // C:\Users\Administrator\
    // workspace\ssh\j1910_MarioV1.0/bin/

    //将全部图片初始化 注解模式(单元测试:public void test())
    
    public static void init(){
        //System.out.println(imagePath);//此方法用来测试是否能够加载到所有图片
        //玛丽的10张图片
        for(int i=1;i<=10;i++){        
            try {//read or  write  mkdirs  createNewFile()
                //    allMarioImage.add(ImageIO.read(new File(System.getProperty("user.dir")+"/bin"+i+".jpg")));
                //list用add方法向集合中添加元素,文件必须用IO去读取,此刻用ImageIO去读取图片
                //imagePath+i+".gif" 是拼接的图片的路径
                allMarioImage.add(ImageIO.read(new File(imagePath+i+".gif")));
            } catch (IOException e) {
                // TODO Auto-generated catch block
                e.printStackTrace();
            }            
        }
        try {
            startImage=ImageIO.read(new File(imagePath+"start.gif"));
            endImage=ImageIO.read(new File(imagePath+"firststageend.gif"));
            bgImage=ImageIO.read(new File(imagePath+"firststage.gif"));
            marioDeadImage=ImageIO.read(new File(imagePath+"over.gif"));
        } catch (IOException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }
        for(int i=1;i<=5;i++){
            try {
                if(i<=2){
                    allFlowerImage.add(ImageIO.read(new File(imagePath+"flower"+i+".gif")));
                }
                
                if(i<=3){
                    allTriangleImage.add(ImageIO.read(new File(imagePath+"triangle"+i+".gif")));
                }
                
                allTurtleImage.add(ImageIO.read(new File(imagePath+"Turtle"+i+".gif")));

            } catch (IOException e) {
                // TODO Auto-generated catch block
                e.printStackTrace();
            }
        }
        for(int i=1;i<=12;i++){
            try {
                allObstructionImage.add(ImageIO.read(new File(imagePath+"ob"+i+".gif")));
            } catch (IOException e) {
                // TODO Auto-generated catch block
                e.printStackTrace();
            }
        }        
    }
}

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

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值