使用类定义的对象不继承颜色属性Kivy

时间:2014-10-25 01:16:47

标签: python kivy

我试图学习如何使用Kivy并遵循他们的乒乓教程。现在,我试图通过某种方式改变代码来实现自己的目标。目前,我正试图让乒乓球拍改变颜色。

我已经让其中一个人这样做了,但另一个,我打电话给同一个班级,却没有。

class PongPaddle(Widget):
    score = NumericProperty(0)

    def bounce_ball(self, ball):
            if self.collide_widget(ball):
                    vx, vy = ball.velocity
                    offset = (ball.center_y - self.center_y) / (self.height /2)
                    bounced = Vector(-1 * vx , vy)
                    vel = bounced * 1.1
                    ball.velocity = vel.x, vel.y + offset

    def on_touch_down(self, touch):
            color = (random(), random(), random())
            with self.canvas:
                    Color(*color)

class PongBall(Widget):

    #velocity of the ball on the X and Y Axis
    velocity_x = NumericProperty(0)
    velocity_y = NumericProperty(0)  #Shorthand for referencelist
    velocity = ReferenceListProperty(velocity_x, velocity_y)

    #Used for ball movement.
    def move(self):
            self.pos = Vector(*self.velocity) + self.pos

class PongGame(Widget):
    ball = ObjectProperty(None)
    player1 = ObjectProperty(None)
    player2 = ObjectProperty(None)

    def __init__(self, **kwargs):
            super(PongGame, self).__init__(**kwargs)

self._keyboard = Window.request_keyboard(self._keyboard_closed, self)
            self._keyboard.bind(on_key_down=self._on_keyboard_down)

    def _keyboard_closed(self):
            self.keyboard.unbind(on_key_down=self._on_keyboard_down)
            self._keyboard = None

    def _on_keyboard_down(self, keyboard, keycode, text, modifiers):
            if keycode[1] == 'w':
                    self.player1.center_y += 10
            elif keycode[1] == 's':
                    self.player1.center_y -= 10
            elif keycode[1] == 'k':
                    self.player2.center_y += 10
            elif keycode[1] == 'l':
                    self.player2.center_y -= 10
            return True

    def serve_ball(self, vel=(4, 0)):
            self.ball.center = self.center
            self.ball.velocity = vel

    def update(self, dt):
            self.ball.move()

            self.player1.bounce_ball(self.ball)
            self.player2.bounce_ball(self.ball)

            #Bounce ball
            if (self.ball.y < 0) or (self.ball.top > self.height):
                    self.ball.velocity_y *= -1

            #Scoring
            if self.ball.x < self.x:
                    self.player2.score += 1
                    self.serve_ball(vel=(4, 0))
            if self.ball.x > self.width:
                    self.player1.score += 1
                    self.serve_ball(vel=(-4, 0))

    def on_touch_move(self, touch):
            if touch.x < self.width / 3:
                    self.player1.center_y = touch.y
            if touch.x > self.width - self.width / 3:
                    self.player2.center_y = touch.y
class PongApp(App):
    def build(self):
            game = PongGame()
            game.serve_ball()
            Clock.schedule_interval(game.update, 1.0/60.0)
            return game

if __name__ == '__main__':
    PongApp().run()

随附的KV语言文件。

#:kivy 1.8.0

<PongBall>:
    size: 50, 50
    canvas:
            Ellipse:
                    pos: self.pos
                    size: self.size
<PongPaddle>:
    size: 25, 200
    canvas:
            Rectangle:
                    pos: self.pos
                    size: self.size

<PongGame>:
    ball: pong_ball
    player1: player_left
    player2: player_right
    canvas:
            Rectangle:
                    pos: self.center_x - 5, 0
                    size: 10, self.height
    Label:
            font_size: 70
            center_x: root.width / 4
            top: root.top - 50
            text: str(root.player1.score)
    Label:
            font_size: 70
            center_x: root.width * 3 / 4       
            text: str(root.player2.score)
    PongBall:
            id: pong_ball
            center: self.parent.center
    PongPaddle:
            id: player_left
            x: root.x
            center_y: root.center_y
    PongPaddle:
            id: player_right
            x: root.width-self.width
            center_y: root.center_y

知道为什么左边的桨没有改变颜色?

1 个答案:

答案 0 :(得分:1)

如果在画布上绘图之前未声明Color,则颜色指令默认为(1,1,1,1)为白色。 所以当你使用:

with self.canvas:
    Color(*color)

您只是在声明新颜色后更改绘制的对象的颜色。所以通过查看你的kv文件:

Label:
    font_size: 70
    center_x: root.width / 4
    top: root.top - 50
    text: str(root.player1.score)
Label:
    font_size: 70
    center_x: root.width * 3 / 4       
    text: str(root.player2.score)
PongBall:
    id: pong_ball
    center: self.parent.center
PongPaddle:
    id: player_left
    x: root.x
    center_y: root.center_y
PongPaddle:
    id: player_right
    x: root.width-self.width
    center_y: root.center_y

绘制player_left后添加到画布的颜色指令将在声明颜色(player_right)后绘制的小部件中使用。将Pong Paddle的on_touch_down函数更改为以下内容,以查看Color如何与canvas一起使用:

    def on_touch_down(self, touch):
        color = (random(), random(), random())
        with self.canvas:
            Color(*color)
            Rectangle(size=(self.width-5,self.height-5),pos=self.pos)

您会注意到第一个矩形(左侧球拍顶部)和右侧球拍颜色相同。然后使用您的第二种颜色在右侧桨的顶部绘制第二个矩形。如果要在执行主要指令之前添加指令,可以使用:

with self.canvas.before:
    Color(*color)