需要帮助修复Kivy KV文件样式

时间:2016-07-30 23:43:27

标签: python-3.x kivy kivy-language

我正在Kivy做一个小游戏。下面是我目前的样式文件。我想让敌人产生不同的图像,或者至少是他们当前分配的图像的修改版本。我尝试向敌人类添加另一个样式规则,但它只是覆盖了前者。我还尝试将随机模块导入KV文件并使用if语句进行随机选择,但是我不能随意导入KV文件。我不知道还能尝试什么。我无法在Kivy文档中找到任何内容。

#: kivy 1.0.9

<Enemy>:
    size: 50,50
    canvas:
        Ellipse:
            source: "myimage.png"
            size: self.size
            pos: self.pos
    size: 50,50

<Player>:
    size: 50,50
    canvas:
        Ellipse:
            source: "playerimage.png"
            size: self.size
            pos: self.pos


<Game>:
    player1: player_shooter
    size: 800, 800
    canvas:
        Rectangle:
            source: "bg.png"
            size: self.width, self.height

    Player:
        id: player_shooter
        pos: self.pos

    Label:
        font_size: 30
        center_x: root.width * 6.6/8
        top: root.top - 10
        text: "Score-Place-Holder"

    Label:
        font_size: 30
        center_x: root.width / 6
        top: root.top - 10
        text: "Lives-Place-Holder"

1 个答案:

答案 0 :(得分:1)

以下是一个示例(将显示您需要的语法):

#:import random random
<Enemy>:
    size: 50,50
    canvas:
        Ellipse:
            source: random.choice(["myimage.png", "anotherimage.png"])
            size: self.size
            pos: self.pos
    size: 50,50