使用Python中的类的骰子生成器

Bet*_*ate 5 python class python-2.7

更新 有人可能会以这种方式放弃我的评论,但评论中没有足够的空间来涵盖这一点,他们明确告诉你不要通过跟进回答你自己的问题,所以这里......

我已经创建了骰子类,就像你们正在谈论的那样.

class dice():
    def __init__(self, sides, number):
        self.sides = sides
        self.number = number

    def roll(self):
        return random.randint(1, self.sides)
Run Code Online (Sandbox Code Playgroud)

这个number论点并没有做任何事情.

def att():
    d = dice(20, 2)
    base = d.roll()
    if base == 1:
        print 'Miss!'
    elif base == 20:
        crit = d.roll()
        if crit < 10:
            print 'Hit!'
        else:
            print 'Critical hit!\n'
            effect = super_crit()               
    else:
        print base
Run Code Online (Sandbox Code Playgroud)

我得到多个骰子的唯一方法是,如果我做这样的事情:

def initiative():
    d = dice(10, 1)
    ini = d.roll(), d.roll()
    print ini
Run Code Online (Sandbox Code Playgroud)

我也尝试过:

def initiative():
    d = dice(10, 2)
    d.sides = 10
    d.number = 2
    ini = d.roll()
    print ini
Run Code Online (Sandbox Code Playgroud)

对我来说这看起来多余,但我得到一个Type Error:没有的论据dice(10, 2).无论我使用哪种方法,我都得到相同的结果 - 一个死.我错过了什么吗?

原帖

我正在学习如何在Python 2.7中使用类,作为练习我正在为基于文本的RPG编写一个战斗模块.它使用旧学校骰子滚动方法来确定结果和效果.滚动确定命中或未命中.如果滚动自然20,则另一个滚动确定它是否是重击.如果临界命中= TRUE,则滚动另一个模具以确定实现哪个身体部位.每个身体部位与字典中的数字1-12配对.根据受影响的部分,有三种可能的输出消息.我的问题是返回整个值列表而不是特定部分.我在这做错了什么?

是的,我知道这是超级书呆子.是的,我知道输出是蹩脚的,但它是所有占位符.

import sys, random

#dice generator
class dice():
    def d4(self):
        number = random.randint(1, 4)
        return number

    def d6(self):
        number = random.randint(1, 6)
        return number

    def d10(self):
        number = random.randint(0, 9)
        return number

    def d12(self):
        number = random.randint(1, 12)
        return number

    def d20(self):
        number = random.randint(1, 20)
        return number

    def d100(self):
        number = random.randint(0, 99)
        return number 

#critical hit effect generator        
class super_crit(dice):
    def __init__(self):
        roll = dice()
        loc = roll.d12()
        hit_loc = {1 : 'Head',
                   2 : 'Left Arm',
                   3 : 'Right Arm',
                   4 : 'Left Leg',
                   5 : 'Right Leg',
                   6 : 'Left Hand',
                   7 : 'Right Hand',
                   8 : 'Left Foot',
                   9 : 'Right Foot',
                   10 : 'Chest',
                   11 : 'Stomach',
                   12 : 'Body'}
        part = hit_loc.values()
        for w in part:
            if loc <= 9:
                print w, "has been severed!"
            elif loc == 10:
                print "You sink your blade into his", w, "and pierce the heart!"
            elif loc == 11:
                print "You slash him across the", w, "and eviscerate him!"
            elif loc == 12:
                print "You shred the enemy's", w, "to ribbons!"

class attackRoll(dice):
        pass

#Attack function        
def att():
    roll = attackRoll()
    base = roll.d20()
    if base == 1:
        print 'Miss!'
    elif base == 20:
        crit = roll.d20()
        if crit < 10:
            print 'Hit!'
        else:
            effect = super_crit()               
    else:
        print base

def main():
    att()


if __name__ == '__main__':

main()
Run Code Online (Sandbox Code Playgroud)

Mar*_*ers 5

你循环遍历dict的所有值:

part = hit_loc.values()
for w in part:
    # loop over each part, so print a damage message for all 12 bodyparts
Run Code Online (Sandbox Code Playgroud)

也许你打算选择受影响的那个?

part = hit_loc[loc]  # assign *one* body part to `part`
if loc <= 9:
    print part, "has been severed!"
elif loc == 10:
    print "You sink your blade into his", part, "and pierce the heart!"
elif loc == 11:
    print "You slash him across the", part, "and eviscerate him!"
elif loc == 12:
    print "You shred the enemy's", part, "to ribbons!"
Run Code Online (Sandbox Code Playgroud)

换句话说,你根本不需要循环.

请注意,每当您发现自己使用一系列连续数字作为字典的键时,您也可以将其改为列表:

hit_loc = [
    'Head', 'Left Arm', 'Right Arm', 'Left Leg', 
    'Right Leg', 'Left Hand', 'Right Hand', 'Left Foot', 'Right Foot',
    'Chest', 'Stomach', 'Body'
]
Run Code Online (Sandbox Code Playgroud)

除了现在索引从0到11运行,所以loc - 1用来找到正确的身体部位:

part = hit_loc[loc - 1]
Run Code Online (Sandbox Code Playgroud)