2013-11-04 39 views
-1

此任务旨在确定游戏角色的两个属性(力量和技能)之间的差异。这个过程是:如何循环代码直到创建特定数字?

  • 确定强度属性之间的差异。
  • 将差异除以5并向下舍入以创建“强度修饰符”。
  • 对技能属性执行相同的过程并命名为“技能修改器”。
  • 然后游戏开始:
    • 双方玩家掷出六面骰子。
    • 如果值相同,则不会发生更改。
    • 如果数值不同,那么具有最高价值的玩家将获得先前制定的力量和技能,然后将其添加到总数中。具有最低价值的玩家的力量和技能已经从他们的总数中拿走了。

这必须重复进行,直到力量属性等于或低于0,但是,这部分我不能这样做,我知道如何循环,但其始终通过询问用户是否希望再次去完成。

c1sc=random.randint(1,6) 
c2sc=random.randint(1,6) 
if c1sc > c2sc: 
    c1st=c1st+strengthmodifier 
    c2st=c2st-strengthmodifier 
    c1sk=c1sk+skillmodifier 
    c2sk=c2sk-skillmodifier 
    print('Character 1, your strength attribute is: '+(str(c1st))+' and your skill attribute is: '+(str(c1sk))) 
    print('Character 2, your strength attribute is: '+(str(c2st))+' and your skill attribute is: '+(str(c2sk))) 
elif c2sc > c1sc: 
    c1st=c1st-strengthmodifier 
    c2st=c2st+strengthmodifier 
    c1sk=c1sk-skillmodifier 
    c2sk=c2sk+skillmodifier 
    print('Character 1, your strength attribute is: '+(str(c1st))+' and your skill attribute is: '+(str(c1sk))) 
    print('Character 2, your strength attribute is: '+(str(c2st))+' and your skill attribute is: '+(str(c2sk))) 
else: 
    print('It\'s a draw, no changes were made.') 
if c1sk < 0: 
    c1sk=0 
elif c2sk < 0: 
    c2sk=0 

if c1st < 0: 
    print('Character 1 died. Game over.') 
elif c2st < 0: 
    print('Character 2 died. Game over.') 

请帮忙!

回答

0

只要你得到的图片:(在伪代码)

while (c2sk > 0 && c1sk > 0) 
    //do complete everything up until the if statements 
if c1st <0 
    print "c1st died" 
elif c2st <0 
    print "c2st died" 

不知道这是你在找什么。如果c2sk和c1sk应该被随机重新分配,每次它应该在循环中。

+0

'虽然'可能永远不会是真的 - 如果他们每轮减少2并开始奇数呢? – Basic

+0

@基本我明白你的观点。我不知道整场比赛是应该重播还是只是回合? c1sc应该每轮随机重新分配?因为它应该在循环内 –

+0

你没有错 - 我只是认为他们会永远以相同的速度死亡...... – Basic

0

添加另一个循环来削减...

c1sc=random.randint(1,6) 
c2sc=random.randint(1,6) 
if c1sc > c2sc: 
    while c2st > 0 and c2sk > 0: 
     c1st=c1st+strengthmodifier 
     c2st=c2st-strengthmodifier 
     c1sk=c1sk+skillmodifier 
     c2sk=c2sk-skillmodifier 
     print('Character 1, your strength attribute is: '+(str(c1st))+' and your skill attribute is: '+(str(c1sk))) 
     print('Character 2, your strength attribute is: '+(str(c2st))+' and your skill attribute is: '+(str(c2sk))) 
elif c2sc > c1sc: 
    while c1st > 0 and c1sk > 0: 
     c1st=c1st-strengthmodifier 
     c2st=c2st+strengthmodifier 
     c1sk=c1sk-skillmodifier 
     c2sk=c2sk+skillmodifier 
     print('Character 1, your strength attribute is: '+(str(c1st))+' and your skill attribute is: '+(str(c1sk))) 
     print('Character 2, your strength attribute is: '+(str(c2st))+' and your skill attribute is: '+(str(c2sk))) 
else: 
    print('It\'s a draw, no changes were made.') 
if c1sk < 0: 
    c1sk=0 
elif c2sk < 0: 
    c2sk=0 

if c1st < 0: 
    print('Character 1 died. Game over.') 
elif c2st < 0: 
    print('Character 2 died. Game over.') 

当然,还有更加紧凑写这个的方式。这仍然是不理想,但在减少代码...

c1sc=random.randint(1,6) 
c2sc=random.randint(1,6) 
if c1sc != c2sc: 
    while c1st > 0 and and c2st > 0: 
     c1st += strengthmodifier if c1sc > c2sc else -1 * strengthmodifier 
     c2st += strengthmodifier if c1sc < c2sc else -1 * strengthmodifier 
     c1sk += skillmodifier if c1sc > c2sc else -1 * skillmodifier 
     c2sk += skillmodifier if c1sc < c2sc else -1 * skillmodifier 
     print('Character 1, your strength attribute is: '+(str(c1st))+' and your skill attribute is: '+(str(c1sk))) 
     print('Character 2, your strength attribute is: '+(str(c2st))+' and your skill attribute is: '+(str(c2sk))) 
    c1sk = math.max(c1sk, 0) # Clamp skill to a minimum of 0 
    c2sk = math.max(c2sk, 0) # ... 

    if c1st < c2st: 
     print('Character 1 died. Game over.') 
    else: 
     print('Character 2 died. Game over.') 
else: 
    print('It\'s a draw, no changes were made.') 

最后,假设重新卷每一轮...

while c1st > 0 and and c2st > 0: 
    c1sc=random.randint(1,6) 
    c2sc=random.randint(1,6) 
    if c1sc != c2sc: 
     c1st += strengthmodifier if c1sc > c2sc else -1 * strengthmodifier 
     c2st += strengthmodifier if c1sc < c2sc else -1 * strengthmodifier 
     c1sk += skillmodifier if c1sc > c2sc else -1 * skillmodifier 
     c2sk += skillmodifier if c1sc < c2sc else -1 * skillmodifier 
     print('Character 1, your strength attribute is: '+(str(c1st))+' and your skill attribute is: '+(str(c1sk))) 
     print('Character 2, your strength attribute is: '+(str(c2st))+' and your skill attribute is: '+(str(c2sk))) 
    else: 
     print('It\'s a draw, no changes were made.') 


    c1sk = math.max(c1sk, 0) # Clamp skill to a minimum of 0 
    c2sk = math.max(c2sk, 0) # ... 

if c1st < c2st: 
    print('Character 1 died. Game over.') 
else: 
    print('Character 2 died. Game over.') 
1

让我来向您介绍while循环。如果指定的条件为true或false,则在希望发生某些情况时使用此选项。

我简要地编辑了你的代码。

进口随机

def main(): 
player1=20 
player2=12 
strength=(20-10)/5 
strength_mod=int(strength) 
while player1>0 and player2>0: 
    dice1=random.randint(1,6) 
    dice2=random.randint(1,6) 
    if dice1>dice2: 
    player1+=strength_mod 
    player2-=strength_mod 
    print("Player 1 strength is: ", player1) 
    print("Player 2 strength is: ", player2) 
    elif dice1<dice2: 
    player2+=strength_mod 
    player1-=strength_mod 
    print("Player 1 strength is: ", player1) 
    print("Player 2 strength is: ", player2) 

main() 

我给玩家1和2个的初始值打赌这些都可以改变自己的喜好。为了取整,我做了另一个变量,并将强度改为一个整数。