gpt4 book ai didi

python - 我可以从 kiwi 语言中的一个小部件引用另一小部件的属性吗?

转载 作者:行者123 更新时间:2023-12-01 07:41:07 24 4
gpt4 key购买 nike

我从 kivy 开始在教程中玩,我在 Pong 教程的最后,想根据分数添加“获胜者”标签。

为了做到这一点,我设计了一个获胜者标签:

<Winner>:
Label:
font_size: 200
#center_x: self.parent.width * .5
#center_y: self.parent.top - .5
text: "wINNER!"

我想根据 Pong 游戏的尺寸将此标签放在中间,但它最终位于左下角的某个位置。

我也尝试直接在python中添加小部件(参见更新方法),但是我仍然不知道如何动态地进行放置,就像如何将带有分数的其他标签放置在kivy文件中的PongGame中.

from kivy.app import App
from kivy.uix.widget import Widget
from kivy.uix.label import Label
from kivy.properties import (
NumericProperty, ReferenceListProperty, ObjectProperty
)
from kivy.vector import Vector
from kivy.clock import Clock


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


class PongBall(Widget):
velocity_x = NumericProperty(0)
velocity_y = NumericProperty(0)
velocity = ReferenceListProperty(velocity_x, velocity_y)

def move(self):
self.pos = Vector(*self.velocity) + self.pos

class Winner(Widget):
pass



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

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

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

# bounce of paddles
self.player1.bounce_ball(self.ball)
self.player2.bounce_ball(self.ball)

# bounce ball off bottom or top
if (self.ball.y < self.y) or (self.ball.top > self.top):
self.ball.velocity_y *= -1

# went of to a side to score point?
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))
if self.player1.score>0:
Clock.unschedule(self.update)
self.add_widget(Winner())
# self.add_widget(text='bla',center_y=self.parent.top-50)
self.add_widget(Label(text='bla',center_y=self.parent.top-50))
if self.player2.score>0:
Clock.unschedule(self.update)
self.add_widget(Winner())
self.add_widget(Label(text='bla',center_y=self.parent.top-50))
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()

kivy

#:kivy 1.0.9

<PongBall>:
size: 50, 50
canvas:
Ellipse:
pos: self.pos
size: self.size

<Winner>:
Label:
font_size: 200
center_x: self.parent.width * .5
center_y: self.parent.top - .5
text: "wINNER!"

<PongPaddle>:
size: 20, 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
top: root.top - 50
text: str(root.player2.score)

Label:
font_size: 70
center_x: root.width * .5
top: root.top - 50
text: "Winner!"

PongBall:
id: pong_ball
center: self.parent.center

PongPaddle:
id: player_left
x: root.x
y: root.center_y

PongPaddle:
id: player_right
x: root.width-self.width
center_y: root.center_y

如何引用父控件的属性?谢谢!

最佳答案

需要在 kv 文件和 Python 脚本中进行以下增强才能解决该问题。

kv 文件

  • 使用 app.root 引用根小部件。

Kv language » Rule context

There are three keywords specific to the Kv language:

  • app: always refers to the instance of your application.
  • root: refers to the base widget/template in the current rule
  • self: always refer to the current widget

Kivy Language » Value Expressions, on_property Expressions, ids, and Reserved Keywords

self

The keyword self references the “current widget instance”:

Button:
text: 'My state is %s' % self.state

root

This keyword is available only in rule definitions and represents the root widget of the rule (the first instance of the rule):

<MyWidget>:
custom: 'Hello world'
Button:
text: root.custom

app

This keyword always refers to your app instance. It’s equivalent to a call to kivy.app.App.get_running_app() in Python.

Label:
text: app.name

片段 - kv 文件

<Winner>:
font_size: 200
center: app.root.center
text: "WINNER!"

py 文件

  • 将类Winner()的继承从Widget更改为Label,因为我们实际上并不需要两个小部件,这样会减少资源使用情况,例如内存、应用程序大小等。
  • 添加Winner 对象。

代码片段 - py 文件

class Winner(Label):
pass


class PongGame(Widget):
...
def update(self, dt):
self.ball.move()
...
if self.player1.score > 0:
self.add_widget(Winner())
if self.player2.score > 0:
self.add_widget(Winner())

def on_touch_move(self, touch):
...

输出

Result

关于python - 我可以从 kiwi 语言中的一个小部件引用另一小部件的属性吗?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/56711262/

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