自己寫了個非常簡單的游戲,然而自己還玩不過去,不過簡單改一下就可以通關了。我本來可以寫的更好的,但是懶得再改。現在自己寫了一個游戲,雖然很簡單。但是,對游戲有了更多的了解。其實所有游戲都是一堆數據而已,卻可以讓人那麼著迷。不說了,游戲寫的不好。不過感觸良多!
廢話說的略多,進入主題。寫項目嗎?都是先分析項目需求,今天寫的是游戲。(開發環境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的年輕碼農!