使用Python设计一款简单小游戏
时间: 2024-06-05 08:04:12 浏览: 18
使用 Python 设计简单的小游戏可以很有趣,下面是一个示例游戏的介绍:
这个游戏叫“石头、剪刀、布”,它是一个基于命令行的游戏,玩家将与计算机进行一场比赛。在这个游戏中,玩家可以输入“石头”、“剪刀”或“布”作为选择,计算机也会随机选择其中之一。根据规则,石头会赢剪刀,剪刀会赢布,布会赢石头。游戏将持续进行到一方获得3分为止,获得3分的一方将获胜。
下面是这个游戏的实现步骤:
1.导入随机模块和时间模块
```
import random
import time
```
2.定义游戏规则函数
```
def game(player,computer):
if player==computer:
return 0
elif player=='石头' and computer=='剪刀':
return 1
elif player=='剪刀' and computer=='布':
return 1
elif player=='布' and computer=='石头':
return 1
else:
return -1
```
3.主程序部分
```
print('欢迎来到石头、剪刀、布游戏!')
player_score=0
computer_score=0
while player_score<3 and computer_score<3:
print('请出拳(石头、剪刀、布):')
player=input()
while player not in ['石头','剪刀','布']:
print('请重新出拳(石头、剪刀、布):')
player=input()
print('玩家选择:',player)
time.sleep(1)
computer=random.choice(['石头','剪刀','布'])
print('计算机选择:',computer)
time.sleep(1)
result=game(player,computer)
if result==1:
player_score+=1
print('玩家得分!')
elif result==-1:
computer_score+=1
print('计算机得分!')
else:
print('平局!')
print('当前比分:玩家',player_score,'-',computer_score,'计算机')
if player_score>computer_score:
print('恭喜你,你赢了!')
else:
print('很遗憾,你输了!')
```
相关推荐
![pdf](https://img-home.csdnimg.cn/images/20210720083512.png)
![docx](https://img-home.csdnimg.cn/images/20210720083331.png)
![-](https://csdnimg.cn/download_wenku/file_type_column_c1.png)
![-](https://csdnimg.cn/download_wenku/file_type_column_c1.png)
![-](https://csdnimg.cn/download_wenku/file_type_column_c1.png)
![-](https://csdnimg.cn/download_wenku/file_type_column_c1.png)
![-](https://csdnimg.cn/download_wenku/file_type_column_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)