实现功能:
1.创建游戏对象
2.游戏对象可以加入到房间
3.同一个房间内的两个游戏对象可以进行游戏
4.每次两个游戏对象随机的出剪刀石头布
5.三局两胜 决出谁胜谁负
6.循环游戏
类:
玩家类
属性:姓名,房间
方法:战斗
房间类
属性:房间号 房间成员
方法:加入玩家 移除玩家
代码:
python
"""
@author:psl
@file:剪刀石头布.py
@time:2024/8/23 14:33
"""
import random
class Player():
def __init__(self,name,room=None):
self.name = name
self.room=room
def fight(self,other):
player_score=0
other_score=0
print("提示:1表示剪刀,2表示石头,3表示布")
while player_score<2 and other_score<2:
player_choice=random.randint(1,3)
other_choice=random.randint(1,3)
print(f"玩家与对手的出拳为:{player_choice}, {other_choice}")
result = player_choice - other_choice
if result == -2 or result == 1:
player_score += 1
# print(f"player:{player_score}")
elif result == -1 or result == 2:
other_score += 1
# print(f"other:{other_score}")
if player_score>other_score:
print(f"玩家{self.name}获胜!玩家与对手比分为{player_score}:{other_score}".center(30,"*"))
else:
print(f"对手{other.name}获胜!玩家与对手比分为{player_score}:{other_score}".center(30,"*"))
class Room():
def __init__(self,id):
self.id=id
self.members=[]
def add_player(self,player):
self.members.append(player)
pass
def remove_player(self,player):
self.members.remove(player)
#无限输入:
#1、创建房间
#2、创建角色
#3、将角色加入房间
#4、选择房间战斗
#5、退出
#1、创建初始房间与初始角色
# 房间"I can win"初始无人,房间"the best"原有成员xfb,房间"come on"原有成员xfb,snh
# 初始角色有psl,xfb,snh
r1=Room("I can win")
r2=Room("the best")
r3=Room("come on")
p1=Player("psl")
p2=Player("xfb")
p3=Player("snh")
r2.members=[p2]
r3.members=[p2,p3]
lst_players={"psl":p1,"xfb":p2,"snh":p3}
lst_rooms={"1":r1,"2":r2,"3":r3}
#设置退出房间指令:look="2"时退出房间
look=""
#设置退出玩家账号指令:ex="q"时退出账户
ex=""
#设置退出程序指令:qu="exit"时退出程序
qu=""
def quit(bye):
global qu
if bye=="exit":
qu=bye
#2、创建角色
while qu!="exit":
#设置退出房间指令:look="2"时退出房间
look = ""
# 设置退出玩家账号指令:ex="q"时退出账户
ex = ""
print("欢迎进入猜拳游戏!(按exit退出游戏)")
u=input("请输入您的玩家名:")
#输入exit就退出程序
quit(u)
if qu=="exit":
print("您已退出游戏")
continue
if u in lst_players:
print(f"欢迎回来,玩家{u}")
#将老玩家设置为出战玩家
for i,j in lst_players.items():
if j.name==u:
player_now=j
else:
#将新玩家加入玩家列表
p4=Player(u)
lst_players[u]=p4
#将新玩家设置为出战玩家player_now
player_now=p4
print(f"您是新用户,已为您创建玩家角色{u}")
#print(f"玩家列表为:{lst_players}")
print(f"当前登录玩家为{player_now.name}".center(30,"*"))
#3、将角色加入房间
while ex!="q":
print("现有房间为:1、I can win 2、the best 3、come on")
print("或者您可以选择:4、创建一个新房间并进入")
print(f"按q退出当前登录账户{player_now.name}选择重新登录")
choice=input("请输入您的选择:")
quit(choice)
if qu == "exit":
print("您已退出游戏")
break
if choice in lst_rooms:
# 设置当前房间名为room_now
room_now = lst_rooms[choice]
room_include_player=room_now
if player_now in room_now.members and len(room_now.members)==1:
print(f"玩家{player_now.name},您已经处于该房间".center(30,"*"))
other_name=input("人数不够,请再选择一位玩家加入:")
quit(other_name)
if qu == "exit":
print("您已退出游戏")
break#退出了房间,回到第一层循环头部再进行qu="exit"判断退出游戏
for i,j in lst_players.items():
if other_name==i:
room_include_player.add_player(j)
room_include_player.members[0].fight(room_now.members[1])
#游戏结束后玩家和自定义对手都退出房间
room_include_player.remove_player(j)
print(f"玩家{player_now.name},您和对手{j.name}退出了房间{room_now.id}".center(30, "*"))
break#退出判断玩家是否在当前房间内的循环,
else:
print("您选择的玩家不存在,请重新选择房间")
#break #这里很奇怪,加了break在输入对手时就不能识别到snh,不加就可以了,但是不加上面这句话又会重复循环三遍而且退回到选择房间那一步
#continue #这个没必要,反正在循环末尾了
continue#退出本次房间循环回到房间选择那一步
else:
room_now.add_player(player_now)
# print(lst_rooms[choice].members)#输出房间成员列表
print(f"您已加入房间 {lst_rooms[choice].id},现在开始你的战斗吧".center(30,"*"))
elif choice =="4":
#创建新房间
room_new=input("设置房间名:")#room_new是新房间id,新房间实例是r4
quit(room_new)
if qu == "exit":
print("您已退出游戏")
break
r4=Room(room_new)
lst_rooms[room_new]=r4
# 设置当前房间名为room_now
room_now = lst_rooms[room_new]
#玩家player_now加入新房间r4
room_now.add_player(player_now)
print(f"玩家{player_now.name}成功创建房间{r4.id}并加入".center(30,"*"))
print(f"房间现有成员为:{room_now.members[0].name}")
#print(len(room_now.members))
elif choice=="q":
ex="q"
break
else:
print("请选择正确的选项".center(30,"*"))
continue
#4、判断房间人数是否足够战斗(play_now是当前玩家,room_now是当前房间)
if len(room_now.members)==2:
for i in room_now.members:
if i!=player_now:
other=i
print(f"对决席为:{player_now.name}和{other.name}开始战斗")
player_now.fight(other)
room_now.remove_player(player_now)
print(f"对决完毕,玩家{player_now.name}自动退出房间{room_now.id}".center(30,"*"))
elif len(room_now.members)==1:
other_name=input("人数不够,请再选择一位玩家加入:")
quit(other_name)
if qu == "exit":
print("您已退出游戏")
break
for i,j in lst_players.items():
if j.name==other_name:
other = j
room_now.add_player(other)
player_now.fight(other)
room_now.remove_player(player_now)
print(f"对决完毕,玩家{player_now.name}自动退出房间{room_now.id}".center(30, "*"))
break
else:
print("您选择的玩家不存在,请重新选择房间")
continue
else:
if player_now in room_now.members:
print("房间对决已有两人,您暂处于观战席")
look = input("1、观战 2、退出当前房间重新选择新房间加入")
quit(look)
if qu == "exit":
print("您已退出游戏")
break
if look == "1":
room_now.members[0].fight(room_now.members[1])
room_now.remove_player(player_now)
print(f"对决完毕,玩家{player_now.name}自动退出房间{room_now.id}".center(30, "*"))
elif look == "2":
room_now.remove_player(player_now)
print(f"玩家{player_now.name},您退出了房间{room_now.id}".center(30, "*"))
else:
room_now.members[0].fight(room_now.members[1])
待实现功能:
1.将是否加减玩家写入类方法中
2.简化代码,有些循环和判断要删除
3.加入功能:查看房间,查看房间内玩家情况
4.设想功能:增加记录玩家胜负功能,实现段位匹配