目錄
前言
專案設計
工具類
功能操作類
主程式類
總結
前言
他來了他來了,他帶著小游戲的原始碼向大家走來了...

本期主要介紹的是結合檔案IO流撰寫的一款小游戲,將程式運行的執行效果與檔案結合起來,實作動態永久存盤資訊的效果,本專案的代碼內容完善,內部結構簡單,主要是為了鞏固新學的知識,以便能夠達到熟練運用,
專案設計
工具類
概述:將不同的功能封裝為方法,就是可以直接通過呼叫方法使用它的功能,而無需考慮具體的功能實作細節,
簡單來說,工具類是內部封裝方法的一種類,是為了供主程式呼叫而存在的,以此來提高主程式的健壯性,但其實也可以不使用工具類,工具類里面的方法完全也可以寫在主程式類中,為了使主程式類中結構清晰、代碼明了,還是不建議這么做,最好還是創建一個工具類,
public class TSUtility {
//靜態私有(供本類靜態方法使用)
private static Scanner scanner = new Scanner(System.in);
//判斷用戶輸入是否合法
public static char readMenuSelectionPro() {
char c;
for (; ; ) {
String str = readKeyBoard(1, false);
c = str.charAt(0);
if (c != '1' && c != '2' &&
c != '3' && c != '4' && c != '5') {
System.out.print("選擇錯誤,請重新輸入:");
} else break;
}
return c;
}
//判斷是否確認操作
public static char readConfirmSelection() {
char c;
for (; ; ) {
String str = readKeyBoard(1, false).toUpperCase();
c = str.charAt(0);
if (c == 'Y' || c == 'N') {
break;
} else {
System.out.print("選擇錯誤,請重新輸入:");
}
}
return c;
}
//判斷輸入長度是否合法,合法就回傳此字串
public static String readKeyBoard(int limit, boolean blankReturn) {
String line = "";
while (scanner.hasNextLine()) {
line = scanner.nextLine();
if (line.length() == 0) {
if (blankReturn) return line;
else continue;
}
if (line.length() < 1 || line.length() > limit) {
System.out.print("輸入長度(不大于" + limit + ")錯誤,請重新輸入:");
continue;
}
break;
}
return line;
}
//延時加載
public static void loadSpecialEffects() throws InterruptedException {
System.out.println("請稍等:");
for (int i1 = 1; i1 <= 100; i1++) {
System.out.print("加載中:" + i1 + "%");
Thread.sleep(new Random().nextInt(25) + 1);
if (i1 == 100) {
Thread.sleep(50);
}
System.out.print("\r");
}
}
//把全為數字的字串轉換為int型別,并回傳其數值
public static int printInt(String x){
Scanner sc = new Scanner(System.in);
if(isNumberic(x)==true){
int y = Integer.parseInt(x);
return y;
}else{
while(true){
System.out.print("你輸入的值無意義! 請你重新輸入: ");
String num = sc.next();
if(isNumberic(num)==true){
int y1 = Integer.parseInt(num);
return y1;
}
}
}
}
//判斷字串是否全是數字,是回傳true,否則回傳false
public static boolean isNumberic(String str) {
//使用正則運算式進行判斷是否全為數字
Pattern pattern = Pattern.compile("[0-9]*");
return pattern.matcher(str).matches();
}
}
功能操作類
概述:實作一個特定的功能,提供使用,和工具類相似,
本專案此次使用該類用于實作顯示二維碼圖片,想要更改圖片只需修改圖片存放的路徑,在主程式類中寫new PayView(); 就創建好了這個物件,即可展示二維碼圖片,
public class PayView extends JFrame {
MyPanel mp=null;
public PayView(){
mp=new MyPanel();
this.add(mp);
this.setSize(1273, 809);
this.setDefaultCloseOperation(JFrame.DISPOSE_ON_CLOSE);
this.setVisible(true);
}
public static void main(String[] args){
new PayView();
}
}
class MyPanel extends JPanel{
Image image=null;
public void paint(Graphics g){
try {
image= ImageIO.read(new File("E:\\java\\idea_test_8\\src\\game\\1.jpg"));
g.drawImage(image, 0, 0, 1273, 809, null);
} catch (Exception e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
}
主程式類
代碼結構圖:設計各個模塊的方法供主方法呼叫,最終實作專案完整效果,(將余額和游戲次數定義為全域變數,為了避免資料意外重置)

main()方法:實作主界面的顯示、用戶的選擇操作,
public static void main(String[] args) throws IOException, InterruptedException {
while (true) {
System.out.println("**************** TESTGAME ****************");
System.out.println("*** 1.登錄賬號 ———> 開始猜數游戲 ***");
System.out.println("*** 2.修改密碼 ———> 修改后可保存 ***");
System.out.println("*** 3.猜拳游戲 ———> 真好玩到爆炸 ***");
System.out.println("*** 4.贊助作者 ———> 可微信支付寶 ***");
System.out.println("*** 5.退出游戲 ———> 歡迎下次光臨 ***");
System.out.println("**********(內測小程式 功能未完善)**********");
System.out.print("請輸入選擇:");
//呼叫工具類中的判斷輸入方法
char c = TSUtility.readMenuSelectionPro();
switch (c) {
case '1':
loginView();
break;
case '2':
updatePw();
break;
case '3':
moraGame();
break;
case '4':
//創建操作類物件
new PayView();
System.out.println("顯示完成!");
break;
case '5':
System.out.println("確認退出? y/n");
char c1 = TSUtility.readConfirmSelection();
if (c1 == 'Y') {
System.out.println("退出程式成功!");
System.exit(0); //結束程式,JVM退出
} else {
System.out.println("已取消退出!");
}
}
}
}
界面效果:

loginView()方法、guessNumber()方法:
1.用戶登錄,賬戶存在的話驗證檔案中的密碼是否匹配,密碼相同則登錄成功進行猜數游戲;如沒有賬戶則進行注冊,用戶名作為檔案名,檔案用于存盤用戶密碼(Password)、用戶余額(Money)、用戶游戲次數(count),
2.定義用戶初始條件,新注冊的用戶有一次試玩的機會(count=1)余額為零(Money=0),消耗完次數之后必須進行充值才能繼續玩游戲,充值后的規則是,優先消耗用戶的游戲次數,當游戲次數為零時,則五十元余額視為一次游戲次數 (確認玩游戲后在余額充足的情況下自動扣費),當游戲次數為零且余額低于五十時需充值才能繼續玩游戲,
public static void loginView() throws IOException, InterruptedException {
//控制回圈
boolean b = true;
//創建集合
Properties popr = new Properties();
//定義檔案
File file;
File file1;
while (b) {
System.out.println("請輸入賬號和密碼");
System.out.print("賬號:");
String userName = sc.next();
//檔案路徑初始化
file = new File("idea_test_8\\src\\game", userName);
//判斷檔案是否存在
if (!file.exists()) {
System.out.println("賬戶不存在是否注冊賬號y/n");
//使用工具類判斷用戶輸入
char c = TSUtility.readConfirmSelection();
if (c == 'Y') {
System.out.print("賬號為:");
String userName1 = sc.next();
//定義檔案路徑
file1 = new File("idea_test_8\\src\\game", userName1);
file1.createNewFile(); //創建檔案
System.out.print("密碼為:");
String pw = sc.next();
//密碼存到集合、設定其他初始值
popr.setProperty("Password", pw);
popr.setProperty("Money", String.valueOf(money));
popr.setProperty("count", String.valueOf(count));
FileWriter fw = new FileWriter(file1);
//集合中的資料保存到檔案
popr.store(fw, "------UserInfo------");
fw.close(); //釋放資源
//使用工具類,呼叫延時方法
TSUtility.loadSpecialEffects();
System.out.println("注冊成功!");
} else {
System.out.println("已取消注冊賬號!");
b = false; //結束回圈
}
} else {
System.out.print("密碼:");
String pw = sc.next();
//檔案中的內容讀取到集合
FileReader fr = new FileReader(file);
popr.load(fr);
fr.close();
//判斷密碼是否正確
if (popr.get("Password").equals(pw)) {
System.out.println(userName + "歡迎你! " + "您目前余額為:"
+ popr.get("Money") + "元,免費次數為:" + popr.get("count"));
b = false;
//猜數小游戲,開始玩游戲
guessNumber(popr,file);
} else {
System.out.println("密碼錯誤!");
}
}
}
}
public static void guessNumber(Properties popr, File file) throws IOException {
System.out.println("宇宙至尊游戲:猜數字(0-100)");
System.out.println("開始!");
while (true) {
//獲取count和Money并轉為int型別
int count1 = Integer.parseInt(popr.getProperty("count"));
int money1 = Integer.parseInt(popr.getProperty("Money"));
//判斷賬戶是否還有免費的次數玩游戲
if (count1 <= 0 && money1 < 50) {
System.out.println("余額不足,是否充值y/n");
char c1 = TSUtility.readConfirmSelection();
if (c1 == 'Y') {
//充值
addMoney(popr, file);
} else {
System.out.println("已退出充值!");
break;
}
} else {
Random r = new Random();
//生成亂數
int number = r.nextInt(101);
System.out.println("測驗使用:" + number);
while (true) {
Scanner sc = new Scanner(System.in);
System.out.println("請輸入你要猜的數字:");
int guessnumber = sc.nextInt();
if (guessnumber > number) {
System.out.println("你猜的數字" + guessnumber + "大了");
} else if (guessnumber < number) {
System.out.println("你猜的數字" + guessnumber + "小了");
} else {
System.out.println("恭喜你猜中了");
break;
}
}
//判斷是否還有游戲次數
if (count1 != 0) {
count1--; //次數減一
//改變集合中的count值
popr.setProperty("count", String.valueOf(count1));
} else {
//判斷是否有余額
if (money1 != 0) {
//設定使用金額50元玩一次
money1 -= 50;
//改變集合中的Money值
popr.setProperty("Money", String.valueOf(money1));
}
}
//集合中的內容寫入到檔案
FileWriter fw = new FileWriter(file);
popr.store(fw, null);
fw.close();
}
System.out.println("繼續玩? y/n");
char c = TSUtility.readConfirmSelection();
if (c == 'Y') {
//重新獲取一次值,為了保證獲取到的是充值后的資料
int count2 = Integer.parseInt(popr.getProperty("count"));
int money2 = Integer.parseInt(popr.getProperty("Money"));
//判斷是否有玩的條件
if (count2 <= 0 && money2 < 50) {
System.out.println("余額不足,是否充值y/n");
char c1 = TSUtility.readConfirmSelection();
if (c1 == 'Y') {
//充值模塊
addMoney(popr, file);
} else {
System.out.println("已退出充值!");
break;
}
}
} else {
System.out.println("已退出游戲!");
break;
}
}
}
界面效果:


看到這里相信大家都有些疲憊了,其實博主的個人觀點是勞逸結合才會讓學習的效率變得更高,所以接下來讓大家放松一下,換換腦子,欣賞一段最近比較流行的舞蹈,Show Time ~

舞蹈也看了,腦袋也換了,希望各位小伙伴能夠收收心,繼續來看接下來幾個部分的方法設計,
addMoney()方法:實作用戶充值的基本功能,并且設定充值金額只能是100、200、300、500及以上,充值成功后用戶的余額會增加并且贈送的游戲次數也會到賬,也就是修改的內容會保存到用戶資訊檔案中,下次玩游戲時再讀取出來并判斷用戶是否有條件玩游戲,
public static void addMoney(Properties popr, File file) throws IOException {
//設定死回圈
while (true) {
System.out.println("¥¥¥¥ 充值系統 ¥¥¥¥");
System.out.println("¥¥ 充一百送5次 ¥¥");
System.out.println("¥¥ 充兩百送12次 ¥¥");
System.out.println("¥¥ 充三百送18次 ¥¥");
System.out.println("¥ 充五百及以上送31次! ¥");
System.out.println("¥¥¥¥ 充值系統 ¥¥¥¥");
System.out.println("請輸入您的充值金額:");
//使用工具類,讓用戶輸入的字串必須是數字,并轉為int型別
String s = sc.next();
int money = TSUtility.printInt(s);
while (true) {
if (money != 100 && money != 200 && money != 300 && money < 500) {
System.out.println("輸入金額錯誤!請重新輸入:");
String s1 = sc.next();
int money1 = TSUtility.printInt(s1);
money = money1;
} else {
//修改集合中的資料
popr.setProperty("Money", String.valueOf(money));
if (money == 100) {
popr.setProperty("count", String.valueOf(5));
} else if (money == 200) {
popr.setProperty("count", String.valueOf(12));
} else if (money == 300) {
popr.setProperty("count", String.valueOf(18));
} else {
popr.setProperty("count", String.valueOf(31));
}
//集合中的資料寫到檔案
FileWriter fw = new FileWriter(file);
popr.store(fw, null);
fw.close();
System.out.println("充值成功!");
break;
}
}
break;
}
}
界面效果:

updatePw()方法:實作用戶修改密碼的功能,首先用戶輸入用戶名,判斷用戶名是否存在,不存在給出提示資訊直接退出該功能;如果存在直接設定新密碼,然后將新密碼寫到檔案,
public static void updatePw() throws IOException {
//創建集合
Properties popr = new Properties();
System.out.println("請輸入你想要修改密碼的賬戶:");
String userName = sc.next();
File file = new File("idea_test_8\\src\\game", userName);
//判斷賬戶檔案是否存在
if (!file.exists()) {
System.out.println("你輸入的賬戶不存在!");
} else {
//檔案內容讀到集合
FileReader fr = new FileReader(file);
popr.load(fr);
fr.close();
//修改密碼
System.out.print("新密碼(" + popr.getProperty("Password") + "):");
String pw = sc.next();
//修改集合中的資料
popr.setProperty("Password", pw);
//集合中的內容寫到檔案
FileWriter fw = new FileWriter(file);
popr.store(fw, null);
fw.close();
System.out.println("修改密碼成功!");
System.out.println("當前賬戶:" + userName);
System.out.println("當前密碼:" + pw);
}
}
界面展示:

moraGame()方法:實作用戶猜拳與系統隨機出拳比較,然后輸出比較結果,此處用簡單的判斷陳述句實作,該部分與用戶檔案資訊無關,無需進行登錄,可持續玩耍,
public static void moraGame() {
System.out.println("規則如下:");
System.out.println("1代表剪刀、2代表石頭、3代表布,剪刀<石頭<布,石頭<布<剪刀,布<剪刀<石頭\n" +
"首先由用戶輸入一個數字代表給定的出拳,然后再由系統隨機給定一個數字代表給定的" +
"\n出拳,最后判定結果輸贏!");
while (true) {
int n = 0; //定義用戶輸入的拳數數字
while (true) {
System.out.println("請輸入你的出拳數字:");
n = sc.nextInt();
if (n != 1 && n != 2 && n != 3) {
System.out.println("輸入錯誤!");
} else break;
}
if (n == 1) {
System.out.println("你出的拳是:剪刀");
} else if (n == 2) {
System.out.println("你出的拳是:石頭");
} else {
System.out.println("你出的拳是:布");
}
Random r = new Random();
int num = r.nextInt(3) + 1;
if (num == 1) {
System.out.println("系統出的拳是:剪刀");
} else if (num == 2) {
System.out.println("系統出的拳是:石頭");
} else {
System.out.println("系統出的拳是:布");
}
if (n == num) {
System.out.println("出拳一樣,平局!");
} else {
if (n == 1) {
if (num == 2) {
System.out.println("很遺憾,你輸了!");
} else if (num == 3) {
System.out.println("恭喜你,你贏了!");
}
} else if (n == 2) {
if (num == 1) {
System.out.println("恭喜你,你贏了!");
} else if (num == 3) {
System.out.println("很遺憾,你輸了!");
}
} else {
if (num == 1) {
System.out.println("很遺憾,你輸了!");
} else if (num == 2) {
System.out.println("恭喜你,你贏了!");
}
}
}
System.out.println("繼續玩? y/n");
char c = TSUtility.readConfirmSelection();
if (c != 'Y') {
System.out.println("已退出游戲!");
break;
}
}
}
界面效果:

贊助作者功能界面展示:跳出一個彈窗顯示給定的二維碼圖片,

總結
本專案的設計可靈活多變,沒有固定的設計要求,這也是為什么一開始沒有介紹專案需求的原因,可以在博主書寫的基礎上自行設計其他功能,或者在某些方面做的更加完善一些,不知道大家有沒有發現,博主是一個注重細節的人,仔細看過博主代碼的小伙伴應該都會知道,博主的細節都藏在注釋里,希望你們也能夠注重細節,對于撰寫代碼,在每一次運行時,都能夠想起第一次運行顯示出"HelloWorld"那樣的喜悅,

本專案到此就已經介紹完畢了,是不是還是比較有趣呢,感興趣的小伙伴可以寫來玩一玩,該專案的代碼量其實并不是很多,主要就是讓我們能夠熟練使用檔案IO流中的各種方法,程式運行時達到動態永久存盤資訊的效果,
轉載請註明出處,本文鏈接:https://www.uj5u.com/qita/295502.html
標籤:其他
上一篇:女朋友讓我深夜十二點催她睡覺,還好我有Python!不然游戲得掛機!
下一篇:Unity零基礎到入門 ??| 萬字教程 對 Unity 中的 Navigation導航系統基礎 全面決議+實戰演練【收藏不迷路】
