這篇“Java怎么實(shí)現(xiàn)QuickHit游戲”文章的知識(shí)點(diǎn)大部分人都不太理解,所以小編給大家總結(jié)了以下內(nèi)容,內(nèi)容詳細(xì),步驟清晰,具有一定的借鑒價(jià)值,希望大家閱讀完這篇文章能有所收獲,下面我們一起來(lái)看看這篇“Java怎么實(shí)現(xiàn)QuickHit游戲”文章吧。
10年的梨樹(shù)網(wǎng)站建設(shè)經(jīng)驗(yàn),針對(duì)設(shè)計(jì)、前端、開(kāi)發(fā)、售后、文案、推廣等六對(duì)一服務(wù),響應(yīng)快,48小時(shí)及時(shí)工作處理。營(yíng)銷(xiāo)型網(wǎng)站的優(yōu)勢(shì)是能夠根據(jù)用戶(hù)設(shè)備顯示端的尺寸不同,自動(dòng)調(diào)整梨樹(shù)建站的顯示方式,使網(wǎng)站能夠適用不同顯示終端,在瀏覽器中調(diào)整網(wǎng)站的寬度,無(wú)論在任何一種瀏覽器上瀏覽網(wǎng)站,都能展現(xiàn)優(yōu)雅布局與設(shè)計(jì),從而大程度地提升瀏覽體驗(yàn)。創(chuàng)新互聯(lián)從事“梨樹(shù)網(wǎng)站設(shè)計(jì)”,“梨樹(shù)網(wǎng)站推廣”以來(lái),每個(gè)客戶(hù)項(xiàng)目都認(rèn)真落實(shí)執(zhí)行。
(1)QuickHit游戲考驗(yàn)學(xué)員鍵盤(pán)輸入內(nèi)容的速度和準(zhǔn)確性。
(2)根據(jù)輸入速率和正確性將玩家分為不同級(jí)別,級(jí)別越高,一次顯示的字符數(shù)越多,玩家正確輸入一次的得分也越高。
(3)如果玩家在規(guī)定的時(shí)間內(nèi)完成規(guī)定次數(shù)的輸入,正確率達(dá)到規(guī)定要求,則玩家升級(jí)(為了簡(jiǎn)單起見(jiàn),規(guī)定用戶(hù)只要輸入錯(cuò)誤一次,則游戲結(jié)束)
(4)玩家最高級(jí)別為6級(jí),初始級(jí)別一律為一級(jí)。
完成“QuickHit”,對(duì)于開(kāi)發(fā)環(huán)境的要求如下:
(1)開(kāi)發(fā)工具:MyEclipse 10
(2)JDK1.7
(1)面向?qū)ο蟪绦蛟O(shè)計(jì)的思想
(2)使用類(lèi)圖理解類(lèi)的關(guān)系
(3)類(lèi)的封裝
(4)構(gòu)造方法的使用
(5)this和static關(guān)鍵字的使用
1、先編寫(xiě)級(jí)別類(lèi)Level:
(1)定義屬性:
private int levelNo; // 級(jí)別號(hào)
private int strLength; // 各級(jí)別一次輸出字符串的長(zhǎng)度
private int strTimes; // 各級(jí)別輸出字符串的次數(shù)
private int timeLimit; // 各級(jí)別闖關(guān)的時(shí)間限制
private int perScore; // 各級(jí)別成功輸入一次字符串后增加的分值
(2)添加包含所有參數(shù)的有參構(gòu)造方法(無(wú)參構(gòu)造方法后面不需要使用,可以不添加)
(3)針對(duì)封裝好的屬性,添加getXxx()方法,不需要添加setXxx()方法(針對(duì)級(jí)別的屬性是系統(tǒng)規(guī)定好的,不需要用戶(hù)設(shè)置)
2、編寫(xiě)級(jí)別參數(shù)類(lèi)LevelParam:
(1)此項(xiàng)目會(huì)涉及到6個(gè)級(jí)別(每個(gè)級(jí)別是一個(gè)對(duì)象),所以先創(chuàng)建一個(gè)長(zhǎng)度為6,類(lèi)型為L(zhǎng)evel的數(shù)組,這個(gè)數(shù)組可以聲明為靜態(tài)常量值
(2)使用有參構(gòu)造方法創(chuàng)建6個(gè)Level類(lèi)對(duì)象,并將這6個(gè)對(duì)象存儲(chǔ)到第一步創(chuàng)建的數(shù)組中,可以將這個(gè)操作生命在靜態(tài)代碼塊中
3、創(chuàng)建Play玩家類(lèi):
(1)定義屬性:
private int levelNo; // 級(jí)別號(hào)
private int curScore; // 當(dāng)前積分
private long startTime = 0; // 各級(jí)別開(kāi)始時(shí)間
private int elapsedTime; // 各級(jí)別已用時(shí)間
(2)添加無(wú)參構(gòu)造方法、有參構(gòu)造方法、getXxx()/setXxx()方法
(3)定義玩家玩游戲的方法play():
功能:用戶(hù)由默認(rèn)0級(jí)升級(jí)到1級(jí),開(kāi)始玩游戲(調(diào)用Game類(lèi)里的printStr()方法和printResult())4、創(chuàng)建游戲類(lèi)Game:
(1)定義屬性:
private Player player;// 玩家
(2)添加無(wú)參構(gòu)造方法、有參構(gòu)造方法
(3)定義方法printStr()
功能:根據(jù)用戶(hù)級(jí)別輸出指定長(zhǎng)度的字符串(字符串的內(nèi)容是隨機(jī)產(chǎn)生的)
(4)定義方法printResult()
功能:判斷用戶(hù)輸入的字符串與系統(tǒng)隨機(jī)產(chǎn)生的字符串內(nèi)容是否相同
1)如果用戶(hù)輸入的字符串與系統(tǒng)產(chǎn)生的字符串相同:
--》用戶(hù)輸入字符串的時(shí)間>對(duì)應(yīng)級(jí)別需要的時(shí)間:超時(shí),退出游戲系統(tǒng)
--》用戶(hù)輸入字符串的時(shí)間<對(duì)應(yīng)級(jí)別需要的時(shí)間:
計(jì)算用戶(hù)完游戲的時(shí)間
用戶(hù)玩游戲積累的積分
輸出當(dāng)前用戶(hù)所處的級(jí)別,玩游戲時(shí)間、積累的積分
計(jì)算該游戲最后一個(gè)級(jí)別的總分并判斷用戶(hù)是否到達(dá)第六級(jí)別以及積累的積分是否等于最高級(jí)別的積分,如果滿足,輸出闖關(guān)成功
2)如果用戶(hù)輸入的字符串與系統(tǒng)產(chǎn)生的字符串相同:
直接退出游戲系統(tǒng)
5、定義測(cè)試類(lèi):
在main()方法中創(chuàng)建Play類(lèi)對(duì)象,調(diào)用play()方法,開(kāi)始游戲
1、初始屬性:Level類(lèi)(全部代碼)
package cn.bdqn.demo09; public class Level { //定義屬性 private int levelNo; // 級(jí)別號(hào) private int strLength; // 各級(jí)別一次輸出字符串的長(zhǎng)度 private int strTimes; // 各級(jí)別輸出字符串的次數(shù) private int timeLimit; // 各級(jí)別闖關(guān)的時(shí)間限制 private int perScore; // 各級(jí)別成功輸入一次字符串后增加的分值 //添加有參構(gòu)造 public Level(int levelNo, int strLength, int strTimes, int timeLimit, int perScore) { super(); this.levelNo = levelNo; this.strLength = strLength; this.strTimes = strTimes; this.timeLimit = timeLimit; this.perScore = perScore; } //添加get方法 public int getLevelNo() { return levelNo; } public int getStrLength() { return strLength; } public int getStrTimes() { return strTimes; } public int getTimeLimit() { return timeLimit; } public int getPerScore() { return perScore; } }
2、初始類(lèi)Level對(duì)象:LevelParam類(lèi)(全部代碼)
package cn.bdqn.demo09; public class LevelParam { // 定義靜態(tài)常量:Level數(shù)組 public static final Level[] levels = new Level[6]; // 用靜態(tài)代碼塊初始化Level對(duì)象 static { levels[0] = new Level(1, 2, 10, 30, 1); levels[1] = new Level(2, 3, 9, 27, 2); levels[2] = new Level(3, 4, 8, 24, 3); levels[3] = new Level(4, 5, 7, 21, 4); levels[4] = new Level(5, 6, 6, 16, 5); levels[5] = new Level(6, 7, 5, 12, 6); } }
3、系統(tǒng)類(lèi):Game(全部代碼)
package cn.bdqn.demo09; import java.util.Random; import java.util.Scanner; public class Game { Scanner sc=new Scanner(System.in); // 定義屬性 private Player player; String str=""; public Game(Player player) { super(); this.player = player; } // 定義系統(tǒng)輸入printStr方法 public String printStr() { StringBuffer buffer = new StringBuffer(); int strLength = LevelParam.levels[player.getLevelNo()-1].getStrLength(); Random random = new Random(); // System.out.println(strLength); // 通過(guò)循環(huán)生成要輸出的字符串 for (int i = 0; i < strLength; i++) { int rand = random.nextInt(strLength); // 產(chǎn)生隨機(jī)數(shù) // 根據(jù)隨機(jī)數(shù)拼接字符串 switch (rand) { case 0: buffer.append(">"); break; case 1: buffer.append("<"); break; case 2: buffer.append("*"); break; case 3: buffer.append("#"); break; case 4: buffer.append("@"); break; case 5: buffer.append("$"); break; default: break; } } str=buffer.toString(); System.out.println(str); return str; } // 定義printResult()方法 public void printResult(String out,String in) { if(out.equals(in)){ int currentTime=(int)System.currentTimeMillis()/1000; player.setElapsedTime(currentTime); //用戶(hù)輸入字符串的時(shí)間<對(duì)應(yīng)級(jí)別需要的時(shí)間: if(player.getElapsedTime()-player.getStartTime() < LevelParam.levels[player.getLevelNo()-1].getTimeLimit()){ //計(jì)算當(dāng)前積分 player.setCurScore(player.getCurScore()+ LevelParam.levels[player.getLevelNo()-1].getPerScore()); //計(jì)算已用時(shí)間 player.setElapsedTime(currentTime-(int)player.getStartTime()); //輸出當(dāng)前積分、當(dāng)前級(jí)別、已用時(shí)間 System.out.println("輸入正確,您的級(jí)別為"+player.getLevelNo()+"級(jí),您的積分為"+player.getCurScore() +"分,總用時(shí)為:"+player.getElapsedTime()+"秒。"); //判斷用戶(hù)是否已經(jīng)闖過(guò)最后一關(guān) if(player.getLevelNo()==6){ int score=LevelParam.levels[player.getLevelNo()-1].getPerScore()* LevelParam.levels[player.getLevelNo()-1].getStrTimes(); if(player.getCurScore()==score){ System.out.println("您已闖關(guān)成功,成為絕世高手,恭喜你?。?!"); System.exit(0); } } }else{ System.out.println("你輸入太慢了,已經(jīng)超時(shí),退出!"); System.exit(1); } }else{ System.out.println("輸入錯(cuò)誤,退出!"); System.exit(1); } } }
4、玩家類(lèi):Player類(lèi)(全部代碼)
package cn.bdqn.demo09; import java.util.Scanner; public class Player { //定義屬性 private int levelNo; // 級(jí)別號(hào) private int curScore; // 當(dāng)前積分 private long startTime = 0; // 各級(jí)別開(kāi)始時(shí)間 private int elapsedTime; // 各級(jí)別已用時(shí)間 //添加get/set方法 public int getLevelNo() { return levelNo; } public void setLevelNo(int levelNo) { this.levelNo = levelNo; } public int getCurScore() { return curScore; } public void setCurScore(int curScore) { this.curScore = curScore; } public long getStartTime() { return startTime; } public void setStartTime(long startTime) { this.startTime = startTime; } public int getElapsedTime() { return elapsedTime; } public void setElapsedTime(int elapsedTime) { this.elapsedTime = elapsedTime; } //定義玩家玩游戲的方法 public void play(){ Game game=new Game(this); Scanner sc=new Scanner(System.in); for (int i = 0; i < LevelParam.levels.length; i++) { //1、晉級(jí) levelNo+=1; //2、晉級(jí)后計(jì)時(shí)清零,積分清零 startTime=(int)System.currentTimeMillis()/1000; curScore=0; //3、內(nèi)層循環(huán)一次,完成一次字符串輸出、輸入、比較 for (int j = 0; j < LevelParam.levels[levelNo-1].getStrTimes(); j++) { //系統(tǒng)輸出 String outStr=game.printStr(); //用戶(hù)輸入 String inStr=sc.next(); //判斷用戶(hù)輸入是否正確,并顯示相應(yīng)信息 game.printResult(outStr, inStr); } } sc.close(); } }
5、測(cè)試類(lèi):GameTest類(lèi)(全部代碼)
package cn.bdqn.demo09; public class GameTest { public static void main(String[] args) { Player player=new Player(); player.play(); } }
以上就是關(guān)于“Java怎么實(shí)現(xiàn)QuickHit游戲”這篇文章的內(nèi)容,相信大家都有了一定的了解,希望小編分享的內(nèi)容對(duì)大家有幫助,若想了解更多相關(guān)的知識(shí)內(nèi)容,請(qǐng)關(guān)注創(chuàng)新互聯(lián)行業(yè)資訊頻道。