你的位置:首页 > Java教程

[Java教程]小游戏,快速击键


  自己写了个非常简单的游戏,然而自己还玩不过去,不过简单改一下就可以通关了。我本来可以写的更好的,但是懒得再改。现在自己写了一个游戏,虽然很简单。但是,对游戏有了更多的了解。其实所有游戏都是一堆数据而已,却可以让人那么着迷。不说了,游戏写的不好。不过感触良多!

  废话说的略多,进入主题。写项目吗?都是先分析项目需求,今天写的是游戏。(开发环境MyEclipse2014,有时间我会写一个从配置环境变量到破解的博文!)

代码双手奉上

这是需要的几个类

 

没有注释(注释是给别人看的,不过这么简单的代码就不写了,主要是因为我懒)!

 1 package cn.curry.day05; 2  3 public class Player { 4   private int levelNo; 5   private int curScore; 6   private long starTime; 7   private int elapsedTime; 8   public int getLevelNo() { 9     return levelNo;10   }11   public void setLevelNo(int levelNo) {12     this.levelNo = levelNo;13   }14   public int getCurScore() {15     return curScore;16   }17   public void setCurScore(int curScore) {18     this.curScore = curScore;19   }20   public long getStarTime() {21     return starTime;22   }23   public void setStarTime(long starTime) {24     this.starTime = starTime;25   }26   public int getElapsedTime() {27     return elapsedTime;28   }29   public void setElapsedTime(int elapsedTime) {30     this.elapsedTime = elapsedTime;31   }32   public void play() {33     Game game=new Game();34     game.printResult();35   }36 37 }

package cn.curry.day05;import java.util.Random;import java.util.Scanner;public class Game {	private Player player = new Player();	public Player getPlayer() {		return player;	}	public void setPlayer(Player player) {		this.player = player;	}	Scanner inputScanner = new Scanner(System.in);	public String printStr(int levelNo) {		StringBuffer sBuffer = new StringBuffer();		int strLength = LevelParam.level[levelNo - 1].getStrLength();		Random random = new Random();		for (int j = 0; j < strLength; j++) {			int rand = random.nextInt(strLength);			switch (rand) {			case 0:				sBuffer.append(">");				break;			case 1:				sBuffer.append("<");				break;			case 2:				sBuffer.append("*");				break;			case 3:				sBuffer.append("&");				break;			case 4:				sBuffer.append("%");			case 5:				sBuffer.append("#");				break;			}		}		return sBuffer.toString();	}	public void printResult() {		long starTime = System.currentTimeMillis();		player.setStarTime(starTime);		for (int i = 1; i <= LevelParam.level.length; i++) {						starTime = System.currentTimeMillis();						int count = 0;			int allScore = 0;			for (int j = 0; j <= count; j++) {				String liveChar = printStr(LevelParam.level[i - 1].getLevalNo());				System.out.println(liveChar);				String startChar = inputScanner.next();				int score = LevelParam.level[i - 1].getPerScore();				long nextTime = System.currentTimeMillis();				long useTime = (nextTime - starTime) / 1000;				if ((nextTime - player.getStarTime()) / 1000 > LevelParam.level[i - 1]						.getTimeLinit()) {					System.out.println("您输入太慢了,已超时,游戏结束");					System.exit(1);				}				if (liveChar.equals(startChar)) {					System.out.println("输入正确");					allScore += score;					count++;					System.out.println("输入正确,您的级别"							+ LevelParam.level[i - 1].getLevalNo() + ",您的积分"							+ allScore + "已用时" + useTime);				} else {					System.out.println("输入错误,游戏结束!");					System.exit(1);				}				if (count == LevelParam.level[i - 1].getStrTime()) {					long thenTime = System.currentTimeMillis();					player.setStarTime(thenTime);					break;				} else {					continue;				}			}		}		System.out.println("恭喜您游戏通关!");	}}

  

package cn.curry.day05;public class Level {	private int levalNo;	private int strLength;	private int strTime;	private int timeLinit;	private int perScore;	public Level(int levalNo, int strLength, int strTime, int timeLinit,			int perScore) {				this.levalNo = levalNo;		this.strLength = strLength;		this.strTime = strTime;		this.timeLinit = timeLinit;		this.perScore = perScore;	}	public int getLevalNo() {		return levalNo;	}	public void setLevalNo(int levalNo) {		this.levalNo = levalNo;	}	public int getStrLength() {		return strLength;	}	public void setStrLength(int strLength) {		this.strLength = strLength;	}	public int getStrTime() {		return strTime;	}	public void setStrTime(int strTime) {		this.strTime = strTime;	}	public int getTimeLinit() {		return timeLinit;	}	public void setTimeLinit(int timeLinit) {		this.timeLinit = timeLinit;	}	public int getPerScore() {		return perScore;	}	public void setPerScore(int perScore) {		this.perScore = perScore;	}}

  

package cn.curry.day05;public class LevelParam {	public final static Level level[]=new Level[6];	static{		level[0]=new Level(1,2,10,30,1);		level[1]=new Level(2,3,9,26,2);		level[2]=new Level(3,4,8,22,5);		level[3]=new Level(4,5,7,18,8);		level[4]=new Level(5,6,6,15,10);		level[5]=new Level(6,7,5,12,15);	}}

  

package cn.curry.day05;public class Test {  public static void main(String[] args) {    Player player=new Player();    player.play();  }}

其中缺点太多,好多BUG没调。总之很烂的代码。但是不会写的借鉴下看看还是可以的!

互相交流学习而已,写的再烂这是我自己写的,这是我思想的体现。我会进步的,学习其实没有那么难。一个没学过几天JAVA的年轻码农!