cheng029 发表于 2015-4-20 09:43:34

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
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
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,cn_list)
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()
  运行效果:

页: [1]
查看完整版本: python开发_自己开发的一个小游戏