【问题标题】:New value for a string for each loop每个循环的字符串的新值
【发布时间】:2012-09-26 08:41:05
【问题描述】:

我正在制作类似 rpg 的游戏。玩家和敌人轮流战斗的地方。 敌人和玩家都有一个伤害范围 [ex (4,10)],当他们攻击时,它会从这个范围内随机取一个数字。

我有这个函数循环,直到其中一个字符达到 0 HP。 唯一的问题是第一个循环只需要一个随机数,然后在其余循环中使用相同的数字。如何为每个新循环获取一个新的随机数?

   import time
import random
global myhp
myhp = 20
global mydmg
mydmg = random.randint(2,5)
mygold = 0
mystr = 2



def start():
    print "Hello there."
    myname = raw_input("What is your name? ")
    print "Welcome %s, this is..." %myname
    uname = myname.upper()
    print "\t\t\tTHE ADVENTURES OF %s" %uname
    choice0 = ''
    allowed = ["y", "n"]
    while choice0.lower() not in allowed:
        choice0 = raw_input("\nWould you like to play the game? Y/N ")
        choice0 = choice0.lower()
    if choice0 == "y":
        game1()
    if choice0 == "n":
        print "Alright, bye!"

def fightmode(name, hp, dmg, gold):
    global myhp
    print '\n\n\nYou are in a fight with %s' %name
    print '%s has %sHP' %(name, hp)
    while myhp > 0 and hp > 0:
        print '\n\t1. Attack \n\t2. Guard \n\t3. Run away.'
        opt1= ''
        allowed = ["1", "2", "3"]
        while opt1 not in allowed:
            opt1 = raw_input("\nWhat will you do? ")
            if opt1 == "1":
                hp = hp - mydmg
                myhp = myhp - dmg
                print "You have inflicted %d damage on %s. %s's HP is %s" %(mydmg, name, name, hp)
                print "%s attacked you and did %d damage. Your HP fell down to %s" %(name, dmg, myhp)
            if opt1 == "2":
                myhp = myhp + 5
                print "You are now guarding yourself. Your HP is now %d" %myhp
                myhp = myhp - dmg
                print "%s attacked you and did %d damage. Your HP fell down to %s" %(name, dmg, myhp)

    if myhp > 0 :
        print"myhp"
    if hp > 0 :
        print"theirhp"





    def fightmode0():
        print """\n\nThis is your first fight. You have 3 seconds each turn
    if you fail to make a move in 3 seconds, you will lose your turn.

    By "Attacking", you inflict damage on the enemy\'s HP, get it down to 0 to defeat him.
    If your HP reaches 0, you will be defeated. 

    By "Guarding", you will regain 10HP back, but that counts as your turn.
    By defeating enemies, you gain gold Use gold to purchase upgrades 
    when you come across a shop.

    You can choose to "Run Away", but you will only have a 1/10 chance of it being sucessful
    """
        raw_input("\nPress any key to continue to battle")
        fightmode("Scrawny Thug", 15, random.randint(1,5), 4)




    def game1():
        print "You wake up and find yourself locked in a room..."
        print "You think you're kidnapped."
        print "Yea, you're probably kidnapped."
        print "You hear footsteps approaching the door..."
        print "\n\t1. Remain in fetal position \n\t2. Attempt a sneak attack" 
        choice1 = ''
        allowed = ["1", "2"]
        while choice1 not in allowed:
            choice1 = raw_input("\nWhat will you do? ")
        print "The doorknob rattles..."
        print "..."
        print "..."
        if choice1 == "1":
            print '"Hey!"'
            print '"Get up maggot!"'
            print 'You see that the thug is small and scrawny'
            print 'He grabs you by your hair and pulls you up'
            print "\n\t1. Punch him in the face. \n\t2. Do nothing" 
            choice1_1 = ''
            allowed = ["1", "2",]
            while choice1_1 not in allowed:
                choice1_1= raw_input("\nWhat will you do?? ")
            if choice1_1 == "1":
                print '\nYou punch the scrawny thug and he lets you go'
                print '"You\'re going to pay for that."'
                print '\n\t\t>>>>>>>>>>ENTERING FIGHT MODE'
                fightmode0()



    start()

【问题讨论】:

  • mydmg 来自哪里? fightmode 被调用了多少次 - 是否总是传递相同的 dmg 值?
  • mydmg 是我设置为 random.randint(2,6) 的全局值。战斗模式被调用,直到玩家或敌人的生命值达到 0。但我想要一个新的随机数,范围为 (2,6) 每回合。因为在我的代码中,只有第一个循环是随机的,然后循环的其余部分使用该值
  • 你在说什么random number??我什么都看不到..我只能看到user input..你指的是random number吗??
  • 请发布您的完整代码..

标签: python loops


【解决方案1】:

dmg = random.randint(dmg_min, dmg_max)放入函数fightmodewhile循环内,这样每个循环都会重新计算,并将函数重新定义为:

def fightmode(name, hp, dmg_min, dmg_max, gold):

【讨论】:

    【解决方案2】:

    只需在需要时生成一个新的随机数:

    代替:

    fightmode("Scrawny Thug", 15, random.randint(1,5), 4)
    

    将其更改为:

    fightmode("Scrawny Thug", 15, (1, 5), 4)
    

    并稍微改变一下功能:

    def fightmode(name, hp, dmg_range, gold): # takes range as randint parameters
        global myhp    
        print '\n\n\nYou are in a fight with %s' %name    
        print '%s has %sHP' %(name, hp)    
        while myhp > 0 and hp > 0:   
            dmg = random.randint(*dmg_range) # generate new each time
            print '\n\t1. Attack \n\t2. Guard \n\t3. Run away.'
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2019-04-30
      • 1970-01-01
      • 2013-02-06
      • 1970-01-01
      • 1970-01-01
      • 2012-01-05
      • 2019-05-31
      • 1970-01-01
      相关资源
      最近更新 更多