python - 在 Python 中使用类的骰子生成器

标签 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)

虽然 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

我获得不止一个骰子的唯一方法是如果我做这样的事情:

def initiative():
    d = dice(10, 1)
    ini = d.roll(), d.roll()
    print ini

我也试过:

def initiative():
    d = dice(10, 2)
    d.sides = 10
    d.number = 2
    ini = d.roll()
    print ini

这对我来说看起来是多余的,但我得到了一个 Type Error: 因为没有 dice(10, 2) 参数太少。无论我使用哪种方法,我都会得到相同的结果——一死。我错过了什么吗?

原帖

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

是的,我知道这是 super Nerd 。是的,我知道输出很差劲,但都是占位符。

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()

最佳答案

你遍历字典的所有值:

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

也许您打算改为选择受影响的那个?

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!"

换句话说,这里根本不需要循环。

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

hit_loc = [
    'Head', 'Left Arm', 'Right Arm', 'Left Leg', 
    'Right Leg', 'Left Hand', 'Right Hand', 'Left Foot', 'Right Foot',
    'Chest', 'Stomach', 'Body'
]

除了现在索引从 0 到 11,所以使用 loc - 1 找到正确的 body 部位:

part = hit_loc[loc - 1]

关于python - 在 Python 中使用类的骰子生成器,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/14409661/

相关文章:

python - 在 Django Admin 中添加一个非模型表单

javascript - javascript 切换显示/隐藏按钮适用于小规模,但不适用于我的较大 HTML 代码

objective-c - 为什么是 KindOfClass :[NSHTTPURLResponse self] instead of isKindOfClass:[NSHTTPURLResponse class]?

java - 循环获取动态数据

python - 错误 "virtualenv : command not found"但安装位置在 PYTHONPATH 中

python - OrderedDict 不按顺序排列

python - 将 optparse 输入插入函数调用

python - 双重递归背后的概念

python - 用于关联字典列表和列表列表的 pythonian 单行代码

python - Psycopg2 不仅接受 None (Nonetype) 作为单元格条目,在使用 %s 但不使用 f-strings 时可以为 Null