- android - 多次调用 OnPrimaryClipChangedListener
- android - 无法更新 RecyclerView 中的 TextView 字段
- android.database.CursorIndexOutOfBoundsException : Index 0 requested, 光标大小为 0
- android - 使用 AppCompat 时,我们是否需要明确指定其 UI 组件(Spinner、EditText)颜色
我不能让我的 self.points 改变我的 points 变量。每次玩家再次轮到他/她时,它只会重新启动用户点数回到 0。
该程序最少支持 2 名玩家,最多支持 4 名玩家。
您有 2 个随机掷出的六面骰子。如果玩家获得 double ,则将两个骰子加在一起,然后乘以 2。答案将加到分数中。如果第一个骰子是 1,则将两个骰子相加,然后从玩家点数中减去,结束回合。
如果玩家没有得到双倍或第一个骰子没有得到 1,则将两个骰子相加,然后添加到玩家的分数中。这一直持续到玩家达到 100 分,结束游戏。
import random, sys
class game():
def __init__(self, name):
self.name = name
self.stop = False
def mainloop(self):
print("Would you like to roll the dice {}?: ".format(self.name))
return input().lower().startswith('y')
def playing(self, points, score):
self.points = points
self.score = score
while True:
if self.mainloop() == True:
print("it's {} go at the game with {} points".format(self.name, self.points))
self.rule()
if self.points >= 100:
print("the winners is {} with {} many points".format(self.name, self.points))
sys.exit()
print("{} your points is: {}".format(self.name, self.points))
if self.stop == True:
print("###############################################################")
print("##################### NEXT PLAYERS TURN #####################")
print("###############################################################")
self.score.append(self.points)
return (self.score)
else:
self.score.append(self.points)
return (self.score)
def rule(self):
question = 0
answer = 0
dice_1 = random.randint(1, 6)
dice_2 = random.randint(1, 6)
print("dice 1 is: {}".format(dice_1))
print("dice 2 is: {}".format(dice_2))
if dice_1 == dice_2:
if dice_1 != 1:
question = "({} + {}) * 2".format(dice_1, dice_2)
answer = eval(question)
print("add {} POINTS!!!".format(answer))
question = ("{} + {}".format(self.points, answer))
self.points = eval(question)
print(self.points)
elif dice_1 == 1:
print("add 25 POINTS!!!")
question = ("{} + 25".format(self.points))
self.points = eval(question)
print(self.points)
elif dice_1 == 1 or dice_2 == 1:
print("unluckly! minus the points")
question = "{} + {}".format(dice_1, dice_2)
answer = eval(question)
print("{} - {}".format(self.points, answer))
question = ("{} - {}".format(self.points, answer))
self.points = eval(question)
print(self.points)
self.stop = True
elif dice_1 != 1 or dice_2 != 1:
print("adding points!!!")
question = "{} + {}".format(dice_1, dice_2)
answer = eval(question)
print("{} + {}".format(self.points, answer))
question = "{} + {}".format(self.points, answer)
self.points = eval(question)
print(self.points)
return True
def check_letter(question):
if question.isalpha():
return False
else:
print("please input a letter")
return True
def main():
number = [2, 3, 4]
players = 0
while players not in number:
players = int(input("how many players are there? please input a number between 2 and 4: "))
score = []
username = []
for x in range(int(players)):
name = input("what is your name?: ")
while check_letter(name) == True:
name = input("what is your name? please input a letter: ")
username == username.append(name)
for z in range(int(players)):
score.append(0)
while True:
for user in username:
for points in score:
while True:
gamer = game(user)
gamer.playing(points, score)
break
break
main()
最佳答案
可能需要进行此调整:
while True:
for i, user in enumerate(username):
for points in score:
while True: # I'm not sure why you have this, if you break in the first iteration in any case
gamer = game(user, points)
new_points = gamer.playing()
if new_points is not None: # I was not sure if you return always the new points
score[i] = new_points
break
break
另一件事,你应该改变:
def __init__(self, name, points):
self.name = name
self.points = points
self.stop_turn = False #instead of own method self.false
(删除方法 self.false
并重命名变量;))
一次完整的代码(还有很多需要重构):
import random, sys
class game():
def __init__(self, name, points, score):
self.name = name
self.stop = False
self.points = points
self.score = score
def mainloop(self):
print("Would you like to roll the dice {}?: ".format(self.name))
return input().lower().startswith('y')
def playing(self):
while True:
if self.mainloop():
print("it's {} go at the game with {} points".format(self.name, self.points))
self.rule()
if self.points >= 100:
print("the winners is {} with {} many points".format(self.name, self.points))
sys.exit()
print("{} your points is: {}".format(self.name, self.points))
if self.stop:
print("###############################################################")
print("##################### NEXT PLAYERS TURN #####################")
print("###############################################################")
# self.score.append(self.points)
return self.points
else:
# self.score.append(self.points)
return self.points
def rule(self):
question = 0
answer = 0
dice_1 = random.randint(1, 6)
dice_2 = random.randint(1, 6)
print("dice 1 is: {}".format(dice_1))
print("dice 2 is: {}".format(dice_2))
if dice_1 == dice_2:
if dice_1 != 1:
question = "({} + {}) * 2".format(dice_1, dice_2)
answer = dice_1 + dice_2
print("add {} POINTS!!!".format(answer))
question = ("{} + {}".format(self.points, answer))
self.points += answer
print(self.points)
elif dice_1 == 1:
print("add 25 POINTS!!!")
question = ("{} + 25".format(self.points))
self.points += 25
print(self.points)
elif dice_1 == 1 or dice_2 == 1:
print("unluckly! minus the points")
question = "{} + {}".format(dice_1, dice_2)
answer = dice_1 + dice_2
print("{} - {}".format(self.points, answer))
question = ("{} - {}".format(self.points, answer))
self.points -= answer
print(self.points)
self.stop = True
elif dice_1 != 1 or dice_2 != 1:
print("adding points!!!")
question = "{} + {}".format(dice_1, dice_2)
answer = dice_1 + dice_2
print("{} + {}".format(self.points, answer))
question = "{} + {}".format(self.points, answer)
self.points += answer
print(self.points)
return True
def check_letter(question):
if question.isalpha():
return False
else:
print("please input a letter")
return True
def main():
number = [2, 3, 4]
players = 0
while players not in number:
players = int(input("how many players are there? please input a number between 2 and 4: "))
score = []
username = []
for x in range(int(players)):
name = input("what is your name?: ")
while check_letter(name) == True:
name = input("what is your name? please input a letter: ")
username.append(name)
for z in range(int(players)):
score.append(0)
while True:
for i, user in enumerate(username):
gamer = game(user, score[i], score)
print("??", score, i)
score[i] = gamer.playing()
main()
关于python - 玩家轮流后我如何保持积分更新?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/56167746/
这个问题在这里已经有了答案: What does the question mark character ('?') mean in C++? (8 个答案) 关闭 7 年前。 这一行我看不懂为什么
在构建模式下甚至可以有两个玩家吗?查看 Roblox 开发者杂志文章“What did you sleigh?”,它在玩家列表中显示了两个“玩家”。 最佳答案 打开 Roblox Studio 转到任
在构建模式下甚至可以有两个玩家吗?查看 Roblox 开发者杂志文章“What did you sleigh?”,它在玩家列表中显示了两个“玩家”。 最佳答案 打开 Roblox Studio 转到任
关闭。这个问题需要更多focused .它目前不接受答案。 想改进这个问题吗? 更新问题,使其只关注一个问题 editing this post . 关闭 5 年前。 Improve this qu
“Clash of Clans”使用 Game Center 对玩家进行身份验证并将其与现有的远程存储游戏状态相关联。 据我所知,游戏仅在客户端提供玩家标识符。是否有支持的技术来安全地验证用户而不是仅
我正在开发多人游戏,但我无法找出如何将其他客户端连接到创建的游戏。我的意思是客户端 A 创建到服务器的套接字连接,其他客户端(A,B ...)如何连接到客户端 A?有人可以帮我吗? 附注我是网络编程新
我正在尝试使用浏览器控制台一步一步地制作井字游戏,并最终改进我的功能。然而,我被困在玩家2回合(ttt_player2_turn()),我必须检查箱子是否为空。看来我在这个例子中的论证有问题。感谢您的
如果我向上移动玩家 1 和玩家 2,假设我按下玩家 1 的向下键,我的玩家将停止向上移动。我找不到问题所在。有人可以帮助我并解释我做错了什么吗? package game; import java.a
我正在创建一个自上而下的 2D 游戏,并且使用 Box2D 来模拟物理,我的问题是: 如何使玩家保持与我的宇宙飞船的相对速度,并且仍然能够在飞船也在移动的情况下围绕我的玩家移动? 我在下面放了一个插图
我是 Java 新手,我正在尝试制作一个简单的游戏来娱乐。我首先尝试将 repaint 放入 PaintComponent() 中,它一直有效,直到我尝试添加一些背景。有谁知道如何让我的玩家在有或没有
//我正在尝试对玩家 2 的代码进行一些编辑,因此我删除了涉及玩家 1 的所有内容。但出于某种原因,如果没有玩家 1 的代码,玩家 2 根本不会执行任何操作。(假设使用 i、j、k 和 l 键 mov
我接到了一项任务,要编写一个由人类玩家和 AI 玩家组成的 NIM 游戏。游戏是“Misere”(最后一个必须拿起一根棍子的人输了)。 AI 应该使用 Minimax 算法,但它正在采取使其输得更快的
我是一名优秀的程序员,十分优秀!