设为首页 收藏本站
查看: 1157|回复: 0

[经验分享] python开发_自己开发的一个小游戏

[复制链接]

尚未签到

发表于 2015-4-20 09:43:34 | 显示全部楼层 |阅读模式
  先看看游戏的运行效果:
DSC0000.png
DSC0001.png
DSC0002.png
DSC0003.png
  看完游戏的运行情况,你可能对游戏有了一定了了解:



#运行游戏后,玩家首先要进行语音的选择,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()
DSC0004.gif
  才刚开始接触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()
  运行效果:
DSC0005.png
DSC0006.png

运维网声明 1、欢迎大家加入本站运维交流群:群②:261659950 群⑤:202807635 群⑦870801961 群⑧679858003
2、本站所有主题由该帖子作者发表,该帖子作者与运维网享有帖子相关版权
3、所有作品的著作权均归原作者享有,请您和我们一样尊重他人的著作权等合法权益。如果您对作品感到满意,请购买正版
4、禁止制作、复制、发布和传播具有反动、淫秽、色情、暴力、凶杀等内容的信息,一经发现立即删除。若您因此触犯法律,一切后果自负,我们对此不承担任何责任
5、所有资源均系网友上传或者通过网络收集,我们仅提供一个展示、介绍、观摩学习的平台,我们不对其内容的准确性、可靠性、正当性、安全性、合法性等负责,亦不承担任何法律责任
6、所有作品仅供您个人学习、研究或欣赏,不得用于商业或者其他用途,否则,一切后果均由您自己承担,我们对此不承担任何法律责任
7、如涉及侵犯版权等问题,请您及时通知我们,我们将立即采取措施予以解决
8、联系人Email:admin@iyunv.com 网址:www.yunweiku.com

所有资源均系网友上传或者通过网络收集,我们仅提供一个展示、介绍、观摩学习的平台,我们不对其承担任何法律责任,如涉及侵犯版权等问题,请您及时通知我们,我们将立即处理,联系人Email:kefu@iyunv.com,QQ:1061981298 本贴地址:https://www.yunweiku.com/thread-58675-1-1.html 上篇帖子: python开发_json_一种轻量级的数据交换格式 下篇帖子: 用Python和Django实现多用户博客系统(二)——UUBlog
您需要登录后才可以回帖 登录 | 立即注册

本版积分规则

扫码加入运维网微信交流群X

扫码加入运维网微信交流群

扫描二维码加入运维网微信交流群,最新一手资源尽在官方微信交流群!快快加入我们吧...

扫描微信二维码查看详情

客服E-mail:kefu@iyunv.com 客服QQ:1061981298


QQ群⑦:运维网交流群⑦ QQ群⑧:运维网交流群⑧ k8s群:运维网kubernetes交流群


提醒:禁止发布任何违反国家法律、法规的言论与图片等内容;本站内容均来自个人观点与网络等信息,非本站认同之观点.


本站大部分资源是网友从网上搜集分享而来,其版权均归原作者及其网站所有,我们尊重他人的合法权益,如有内容侵犯您的合法权益,请及时与我们联系进行核实删除!



合作伙伴: 青云cloud

快速回复 返回顶部 返回列表