gpt4 book ai didi

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

转载 作者:太空宇宙 更新时间:2023-11-03 11:29:11 26 4
gpt4 key购买 nike

我正在尝试学习如何使用 Kivy 并遵循他们的乒乓教程。现在,我试图通过以某些方式更改代码来使它成为我自己的。目前,我正在尝试让乒乓 Racket 改变颜色。

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

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

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

最佳答案

如果在 Canvas 上绘制之前没有声明颜色,则颜色指令默认为 (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 后添加到 Canvas 的颜色指令将被在声明颜色(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)

关于python - 使用类定义的对象不继承颜色属性 Kivy,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/26558428/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com