首页 > 学院 > 开发设计 > 正文

Super Mario(easy ver.)

2019-11-10 17:57:13
字体:
来源:转载
供稿:网友

超级玛丽

几个重要细节:

1.跳跃单独开个线程,两次循环一次上一次下

2.开启“万有引力”线程,让人物在跳跃结束后或者走到空中时y坐标增加直到到达地面

3.人物与障碍物对象要有预碰撞否则会粘住不动

public class Mario extends Rectangle implements Runnable{	public int x = 20,y = 359;	public int width = 28,height = 34;	public int xspeed = 2,yspeed = 3;	public boolean isUp,isDown,isLeft,isRight;	public boolean isjump = false;	public UI ui;		public Image c_stop = new ImageIcon("images/mari1.png").getImage();	public Image c_left = new ImageIcon("images/mari_left.gif").getImage();	public Image c_right = new ImageIcon("images/mari_Right.gif").getImage();	public Image currentImage = c_stop;		public Mario(UI ui) {		this.ui = ui;		startGravityThread();	}	public void run() {		while(true){			if(isUp){					if(!isjump){					if(!isGravity){						isjump =true;						jump();					}				}			}			 if(isDown){				 			 }			 if(isLeft){				 currentImage = c_left;				 boolean hit = crash(StaticParams.direction_left);				 if(hit){										}				 				 else{					if(x>0){					 x-=xspeed;					} 				 } 			}			 if(isRight){				 currentImage = c_right;				 boolean hit = crash(StaticParams.direction_right);				 if(hit){									}				else {					if(x > 370){						ui.n-=xspeed;						for (int i = 0; i < StaticParams.list.size(); i++) {							Barrier b = StaticParams.list.get(i);							b.x-=xspeed;						}					}else{						x+=xspeed;					}				}							}			 			 if(StaticParams.isShoot)			 {				 				 if(!(StaticParams.bulletlist.size()>StaticParams.bullet_Num))				 {					 //发子弹					 Image image = new ImageIcon("images/coin.png").getImage();					 String dir = "right";					 if(isLeft)						 dir ="left";					 					 Bullet bullet = new Bullet(x, y, 15, 15, image , dir);					 StaticParams.bulletlist.add(bullet);					 					 StaticParams.isShoot = false;				 }				 				 			 }			 			 			 			 Thread_Util.sleep(10);		}				}	public void jump(){				new Thread(){			public void run(){								for (int i = 0; i < 50; i++) {						boolean hit = crash(StaticParams.direction_up);						if(hit){							break;						}						 						else {														y-=yspeed;						}												Thread_Util.sleep(10);					}					for (int i = 0; i < 100; i++) {						boolean hit = crash(StaticParams.direction_down);						if(hit){							break;						} 						else {							if(y >= StaticParams.floor_height){								break;							}							else if(y < StaticParams.floor_height){								y+=yspeed;							}													}						Thread_Util.sleep(10);					}										isjump = false;			}		}.start();	}	public boolean crash(String direction){		Rectangle rect = null;				if("up".equals(direction)){			rect = new Rectangle(x,y-2,width,height);		}				else if("down".equals(direction)){			rect = new Rectangle(x,y+3,width,height);		}		else if("left".equals(direction)){			rect = new Rectangle(x-1,y,width,height);		}		else if("right".equals(direction)){			rect = new Rectangle(x+1,y,width,height);		}		boolean hit = false; 		for (int i = 0; i < StaticParams.list.size(); i++) {			Barrier b = StaticParams.list.get(i);			hit = b.intersects(rect);			if(hit){				if(b instanceof Coin){					StaticParams.list.remove(b);					StaticParams.coin_Num++;				}				if(b instanceof Bank){					Thread_Util.sleep(10);					y+=yspeed;					if(y >= 450){						StaticParams.isPause = !StaticParams.isPause;						Thread_Util.sleep(10);					}				}								return true;			}				}		return hit;	}	boolean isGravity = false;		public void startGravityThread(){		new Thread(){			public void run(){				while(true){					Thread_Util.sleep(10);					boolean hit = crash(StaticParams.direction_down);					if(hit){						isGravity = false;						continue;					} 					if(y >= StaticParams.floor_height){						isGravity = false;						continue;					}					if(isjump){						isGravity = false;						continue;					}					isGravity = true;					y+=yspeed;				}			}		}.start();	}}4.字符流的读,地图信息的读取

public class StaticParams {		public final static int floor_height = 359;	public static int coin_Num = 0;	public static int bullet_Num = 3;	public final static String direction_up = "up";	public final static String direction_down = "down";	public final static String direction_left = "left";	public final static String direction_right = "right";	public static boolean isPause = false;	public static boolean isShoot = false;	public static List<Barrier> list = new ArrayList<Barrier>();	public static List<Bullet> bulletlist = new ArrayList<Bullet>();		public static void genBarrier(){		mapToBarrier();	}	public static List<String> readMap(){		BufferedReader br = null;		List<String> lines = new ArrayList<String>();		try {			br = new BufferedReader(new FileReader("marioMap"));			String l = null;			while((l = br.readLine())!=null){				lines.add(l);			}			return lines;		} catch (Exception e) {						e.PRintStackTrace();		}finally{			try {				if(br != null)					br.close();				} catch (IOException e) {					e.printStackTrace();				}		}		return null;			}	public static void mapToBarrier(){		List<String> lines = readMap();		for (int i = 0; i < lines.size(); i++) {			String s = lines.get(i);							for (int j = 0; j < s.length(); j++) {					char c = s.charAt(j);					if('0' == c){											}					else if('1' == c){						ImageIcon imageIcon1 = new ImageIcon("images/brick.png");						Image image1 = imageIcon1.getImage();						Brick brick = new Brick(j*30, i*30, 30, 30,image1);						list.add(brick);					}else if('2' == c){						ImageIcon imageIcon2 = new ImageIcon("images/coin.png");						Image image2 = imageIcon2.getImage();						Coin coin = new Coin(j*30, i*30, 30, 30,image2);						list.add(coin);					}else if('3' == c){						ImageIcon imageIcon3 = new ImageIcon("images/pipe.png");						Image image3 = imageIcon3.getImage();						Pipe pipe = new Pipe(j*30, i*30, 60, 120,image3);						list.add(pipe);					}else if('5' == c){						Bank bank = new Bank(j*30, i*30+3, 30, 30,null);						list.add(bank);										}else if('4' == c){						ImageIcon imageIcon4 = new ImageIcon("images/coin_brick.png");						Image image4 = imageIcon4.getImage();						CoinBrick coin_brick = new CoinBrick(j*30, i*30, 30, 30,image4);						list.add(coin_brick);												}				}		}	}		}


发表评论 共有条评论
用户名: 密码:
验证码: 匿名发表