python開發_自己開發的一個小游戲


先看看游戲的運行效果:

看完游戲的運行情況,你可能對游戲有了一定了了解:

#運行游戲后,玩家首先要進行語音的選擇,1選擇英語,2選擇漢語,其他則默認選擇英語
#根據玩家選擇的語音,進入不同的語音環境
#游戲規則:玩家輸入一個0-9的數字,系統根據玩家輸入的數字,打印出數字的信息
#        如果玩家輸入的數字范圍不在0-9,則會打印出"Error!"
#退出游戲:游戲會隨着打印信息的完成提示退出游戲

代碼部分:

 1 #運行游戲后,玩家首先要進行語音的選擇,1選擇英語,2選擇漢語,其他則默認選擇英語
 2 #根據玩家選擇的語音,進入不同的語音環境
 3 #游戲規則:玩家輸入一個0-9的數字,系統根據玩家輸入的數字,打印出數字的信息
 4 #        如果玩家輸入的數字范圍不在0-9,則會打印出"Error!"
 5 #退出游戲:游戲會隨着打印信息的完成提示退出游戲
 6 language_option = """\
 7     Language: Choose the language for System[OPTION]
 8             -1                    Choose English Language
 9             -2                    Choose Chinese Language
10             """
11 enter_str = 'please enter an integer:'
12 #開始游戲前的說明
13 en_game_start_str = 'You choose English language!,Now,Game Start!'
14 cn_game_start_str = '你選擇的中文模式!現在,開始游戲!'
15 #游戲規則
16 en_game_rule_str = 'you should enter a number that from 0 to 9,then the \nSystem will print the information of the number'
17 cn_game_rule_str = '你輸入一個0-9的數字,系統會打印出該數字的信息'
18 #結束游戲
19 en_game_over_str = 'Game Over!'
20 cn_game_over_str = '游戲結束!'
21 print(language_option)
22 number = int(input(enter_str))
23 
24 def print_info(num):
25     if num == 0:
26         print('0 zero 零')
27     elif num == 1:
28         print('1 one 壹')
29     elif num == 2:
30         print('2 two 貳')
31     elif num == 3:
32         print('3 three 叄')
33     elif num == 4:
34         print('4 four 肆')
35     elif num == 5:
36         print('5 five 伍')
37     elif num == 6:
38         print('6 six 陸')
39     elif num == 7:
40         print('7 seven 柒')
41     elif num == 8:
42         print('8 eight 捌')
43     elif num == 9:
44         print('9 nine 玖')
45     else:
46         print('Error!')
47 
48 def start_game(num):
49     if num == 1:
50         print(en_game_rule_str)
51     elif num == 2:
52         print(cn_game_rule_str)
53     else:
54         print(en_game_rule_str)
55     n = int(input(enter_str))
56     print_info(n)
57           
58 
59 if number == 1:
60     print(en_game_start_str)
61     start_game(1)
62     print(en_game_over_str)
63     exit()
64 elif number == 2:
65     print(cn_game_start_str)
66     start_game(2)
67     print(cn_game_over_str)
68     exit()
69 else:
70     print(en_game_start_str)
71     start_game(number)
72     print(en_game_over_str)
73     exit()

才剛開始接觸python,希望志同道合的朋友一起學習python....

=================================================

Edit by Hongten 2013-07-21

=================================================

下面是對以上程序的改進:

優化了print_info()方法,增加了詢問玩家是繼續玩功能..詳細請參看代碼

  1 #運行游戲后,玩家首先要進行語音的選擇,1選擇英語,2選擇漢語,其他則默認選擇英語
  2 #根據玩家選擇的語音,進入不同的語音環境
  3 #游戲規則:玩家輸入一個0-9的數字,系統根據玩家輸入的數字,打印出數字的信息
  4 #        如果玩家輸入的數字范圍不在0-9,則會打印出"Error!"
  5 #退出游戲:游戲會隨着打印信息的完成提示退出游戲
  6 language_option = """\
  7     Language: Choose the language for System[OPTION]
  8             -1                    Choose English Language
  9             -2                    Choose Chinese Language
 10             """
 11 enter_str = 'please enter an integer:'
 12 
 13 #開始游戲前的說明
 14 en_game_start_str = 'You choose English language!,Now,Game Start!'
 15 cn_game_start_str = '你選擇的中文模式!現在,開始游戲!'
 16 
 17 #游戲規則
 18 en_game_rule_str = 'you should enter a number that from 0 to 9,then the \nSystem will print the information of the number'
 19 cn_game_rule_str = '你輸入一個0-9的數字,系統會打印出該數字的信息'
 20 
 21 #結束游戲
 22 en_game_over_str = 'Game Over!'
 23 cn_game_over_str = '游戲結束!'
 24 print(language_option)
 25 
 26 #定義列表
 27 en_list = ['zero','one','two','three','four','five','six','seven','eight','nine']
 28 cn_list = ['','','','','','','','','','']
 29 
 30 #循環標志
 31 FLAG = True
 32 
 33 #還需要玩嗎?
 34 en_play_again_str = """\
 35     #############################################
 36     Do you want play again?
 37     -1              Play Again
 38     -2              Exit Game
 39              """
 40 cn_play_again_str = """\
 41     #############################################
 42     你還要繼續玩嗎?
 43     -1              繼續玩
 44     -2              退出游戲
 45              """
 46 
 47 number = int(input(enter_str))
 48 
 49 #游戲打印信息
 50 def print_info(num):
 51     if num in range(0,9):
 52         print(num,en_list[num],cn_list[num])
 53     else:
 54         print('Error!')
 55 
 56 #開始游戲
 57 def start_game(num):
 58     if num == 1:
 59         print(en_game_rule_str)
 60     elif num == 2:
 61         print(cn_game_rule_str)
 62     else:
 63         print(en_game_rule_str)
 64     n = int(input(enter_str))
 65     print_info(n)
 66 
 67 #循環玩游戲
 68 def play_again(n):
 69     if n == 1:
 70         print(en_play_again_str)
 71     elif n == 2:
 72         print(cn_play_again_str)
 73     else:
 74         print(en_play_again_str)
 75     again = int(input(enter_str))
 76     if again == 1:
 77         pass
 78     elif again == 2:
 79         #這里使用的是全局變量,注意這里不要寫成:global FLAG = False
 80         global FLAG
 81         FLAG = False
 82         
 83 #游戲的循環體   
 84 while True:
 85     if FLAG:
 86         if number == 1:
 87             print(en_game_start_str)
 88             start_game(1)
 89             play_again(1)
 90         elif number == 2:
 91             print(cn_game_start_str)
 92             start_game(2)
 93             play_again(2)
 94         else:
 95            print(en_game_start_str)
 96            start_game(number)
 97            play_again(number)
 98     else:
 99         print(en_game_over_str)
100         break
101         #exit()

運行效果:


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM