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

Python 使用类定义的对象未继承颜色属性Kivy,python,kivy,Python,Kivy,我试图学习如何使用Kivy,并遵循他们的pong教程。现在,我正试图通过某种方式修改代码,使之成为我自己的。目前,我正在努力使乒乓球拍改变颜色 我已经让其中一个这样做了,但我称之为使用同一个类的另一个却没有 class PongPaddle(Widget): score = NumericProperty(0) def bounce_ball(self, ball): if self.collide_widget(ball):

我试图学习如何使用Kivy,并遵循他们的pong教程。现在,我正试图通过某种方式修改代码,使之成为我自己的。目前,我正在努力使乒乓球拍改变颜色

我已经让其中一个这样做了,但我称之为使用同一个类的另一个却没有

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()
class-PongPaddle(小部件):
分数=数值属性(0)
def弹跳球(自身,球):
如果self.collide_小部件(球):
vx,vy=球的速度
偏移=(ball.center_y-self.center_y)/(self.height/2)
反弹=矢量(-1*vx,vy)
水平=反弹*1.1
球体速度=x层,y层+偏移
def on_触控向下(自身,触控):
颜色=(随机(),随机(),随机())
使用self.canvas:
颜色(*颜色)
类PongBall(小部件):
#球在X轴和Y轴上的速度
速度x=数值属性(0)
velocity_y=NumericProperty(0)#referencelist的简写
速度=ReferenceListProperty(速度x,速度y)
#用于球的运动。
def移动(自我):
self.pos=向量(*self.velocity)+self.pos
类PongGame(小部件):
ball=ObjectProperty(无)
player1=ObjectProperty(无)
player2=ObjectProperty(无)
定义初始(自我,**kwargs):
超级(PongGame,self)。\uuuuu init\uuuuuuuuuu(**kwargs)
self.\u-keyboard=Window.request\u-keyboard(self.\u-keyboard\u-closed,self)
self.\u keyboard.bind(按键盘上下=按键盘上下)
def_键盘_关闭(自身):
self.keyboard.unbind(on_key\u down=self.\u on\u keyboard\u down)
self.\u键盘=无
键盘上下定义(self、键盘、键码、文本、修改器):
如果键码[1]=“w”:
self.player1.center_y+=10
elif keycode[1]=“s”:
self.player1.center_y-=10
elif密钥码[1]=“k”:
self.player2.center_y+=10
elif密钥码[1]=“l”:
self.player2.center_y-=10
返回真值
def发球(自我,水平=(4,0)):
self.ball.center=self.center
self.ball.velocity=vel
def更新(自我,dt):
self.ball.move()
self.player1.弹起球(self.ball)
self.player2.弹起球(self.ball)
#弹球
如果(self.ball.y<0)或(self.ball.top>self.height):
self.ball.velocity_y*=-1
#得分
如果self.ball.xself.width:
self.player1.score+=1
自我发球(水平=(-4,0))
def on_touch_move(自我,触摸):
如果触摸.xself.width-self.width/3:
self.player2.center_y=touch.y
PongApp课程(应用程序):
def生成(自):
game=PongGame()
比赛发球
时钟时间间隔(game.update,1.0/60.0)
回击赛
如果uuuu name uuuuuu='\uuuuuuu main\uuuuuuu':
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
#:kivy 1.8.0
:
尺码:50,50
画布:
椭圆:
pos:self.pos
大小:self.size
:
尺码:25200
画布:
矩形:
pos:self.pos
大小:self.size
:
球:乒乓球
玩家1:玩家左
玩家2:玩家右
画布:
矩形:
位置:自中心x-5,0
尺码:10,身高
标签:
字体大小:70
中心x:root.width/4
顶部:root.top-50
文本:str(root.player1.score)
标签:
字体大小:70
中心x:root.width*3/4
文本:str(root.player2.score)
庞贝尔:
id:pong_ball
中心:self.parent.center
PongPaddle:
id:player_左
x:root.x
中心y:根。中心y
PongPaddle:
id:玩家(右)
x:root.width-self.width
中心y:根。中心y

你知道为什么左边的桨没有变色吗

如果在画布上绘制之前未声明颜色,则颜色说明默认为白色的(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_right)后绘制的小部件将使用在绘制player_left后添加到画布的颜色说明。将乒乓球拍的on_touch_down功能更改为以下内容,以了解颜色如何与画布一起使用:

    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)

非常感谢。我真的很感谢你的帮助。多亏了你的帮助,我会玩得很开心。:-)