本文实例为大家分享了一个简单的小游戏,分享给大家。
利用随机函数制作石头剪刀布小游戏
程序只运行一次
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
|
import random
a = input ( "请出拳(石头/剪刀/布):" )
b = [ "剪刀" , "石头" , "布" ]
# 定义赢的列表
win_list = [[ "石头" , "剪刀" ], [ "剪刀" , "布" ], [ "布" , "石头" ]]
# 计算机随机选择出拳
mac = random.choice(b)
print ( "你出拳:" , a)
print ( "计算机出拳:" , mac)
if a in b:
if a = = mac:
print ( "平局" )
elif [a, mac] in win_list:
# 如果在赢的列表中,代表你赢了,主要你和计算机的顺序要和赢的列表一样
print ( "恭喜,你赢了" )
else :
print ( "很遗憾,你输了" )
else :
print ( "输入错误" )
|
每次出拳一次要运行一次,很麻烦,要让程序一直运行
在程序中加个while条件就解决啦
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
|
import random
while True :
a = input ( "请出拳(石头/剪刀/布):" )
b = [ "剪刀" , "石头" , "布" ]
# 定义赢的列表
win_list = [[ "石头" , "剪刀" ], [ "剪刀" , "布" ], [ "布" , "石头" ]]
# 计算机随机选择出拳
mac = random.choice(b)
print ( "你出拳:" , a)
print ( "计算机出拳:" , mac)
if a in b:
if a = = mac:
print ( "平局" )
elif [a, mac] in win_list:
# 如果在赢的列表中,代表你赢了,主要你和计算机的顺序要和赢的列表一样
print ( "恭喜,你赢了" )
else :
print ( "很遗憾,你输了" )
else :
print ( "输入错误" )
|
长期玩游戏不利于身心健康,玩游戏要有度
那就默认游戏一开始有三次机会吧,每玩一次减掉一次机会,如果赢了就增加一次机会
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
|
import random
count = 3
while count:
a = input ( "请出拳(石头/剪刀/布):" )
b = [ "剪刀" , "石头" , "布" ]
# 定义赢的列表
win_list = [[ "石头" , "剪刀" ], [ "剪刀" , "布" ], [ "布" , "石头" ]]
# 计算机随机选择出拳
mac = random.choice(b)
print ( "你出拳:" , a)
print ( "计算机出拳:" , mac)
if a in b:
count - = 1
if a = = mac:
print ( "平局" )
elif [a, mac] in win_list:
# 如果在赢的列表中,代表你赢了,主要你和计算机的顺序要和赢的列表一样
print ( "恭喜,你赢了" )
count + = 1
else :
print ( "很遗憾,你输了" )
else :
print ( "输入错误" )
print ( "你还剩余机会" , count)
|
免费玩了这么多次,要给项目创造一点收益啊
当游戏剩余机会0时,用户可选择充值继续玩,1块钱等于三次机会,输入字符串或者输入的不是1,5,10时,退出程序
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
|
import random
count = 3
while count:
a = input ( "请出拳(石头/剪刀/布):" )
b = [ "剪刀" , "石头" , "布" ]
win_list = [[ "石头" , "剪刀" ], [ "剪刀" , "布" ], [ "布" , "石头" ]]
mac = random.choice(b)
print ( "你出拳:" , a)
print ( "计算机出拳:" , mac)
if a in b:
count - = 1
if a = = mac:
print ( "平局" )
elif [a, mac] in win_list:
print ( "恭喜,你赢了" )
count + = 1
else :
print ( "很遗憾,你输了" )
else :
print ( "输入错误" )
print ( "你还剩余机会" , count)
if count = = 0 :
print ( "机会用完啦,充值可以继续玩哦,充值金额分别为1,5,10元,一元可增加三次机会。" )
a = input ( "请选择充值金额:" )
try :
a = int (a)
if a = = 1 or a = = 5 or a = = 10 :
count = a
else :
print ( "请输入1,5,10" )
except :
print ( "请输入正确的金额" )
|
输入字符串或者输入不是1,5,10 可以继续输入或者选择q退出
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
|
import random
count = 3
while count:
a = input ( "请出拳(石头/剪刀/布):" )
b = [ "剪刀" , "石头" , "布" ]
win_list = [[ "石头" , "剪刀" ], [ "剪刀" , "布" ], [ "布" , "石头" ]]
mac = random.choice(b)
print ( "你出拳:" , a)
print ( "计算机出拳:" , mac)
if a in b:
count - = 1
if a = = mac:
print ( "平局" )
elif [a, mac] in win_list:
print ( "恭喜,你赢了" )
count + = 1
else :
print ( "很遗憾,你输了" )
else :
print ( "输入错误" )
print ( "你还剩余机会" , count)
if count = = 0 :
print ( "机会用完啦,充值可以继续玩哦,充值金额分别为1,5,10元,一元可增加三次机会。" )
while True :
a = input ( "请选择充值金额或者输入Q退出:" )
if a = = 'Q' :
exit()
try :
a = int (a)
if a = = 1 or a = = 5 or a = = 10 :
count = a
else :
print ( "请输入1,5,10" )
except :
print ( "请输入正确的金额" )
|
以上就是本文的全部内容,希望对大家的学习有所帮助,也希望大家多多支持服务器之家。
原文链接:https://blog.csdn.net/Jenny_He/article/details/89218328