我正在尝试使用kivy创建一个乒乓球游戏。但是,当我尝试在有人得到10分后结束比赛时,我遇到了问题。除了我想使用标签显示Game Over文本之外,一切都运行良好。我创建了一个标签,当前文本设置为空字符串,但是在有人得分足够之后它应该会改变。
我查看了kivy文档并搜索了stackoverflow,但我没有找到答案(或者至少没有找到我理解的答案。)
如果有人能指出我正确的方向,我会非常感激。
这是我的代码:
.py文件:
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
count1 = 0
count2 = 0
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 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):
global count1
global count2
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.player1.score += 1
count2 = count2 + 1
self.serve_ball(vel=(4, 0))
if self.ball.x > self.width:
self.player2.score += 1
self.serve_ball(vel=(-4, 0))
if count1 or count2 >= 10:
#
#
#
#I don't know what needs to be placed here to change the third label's text
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.0.9
<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.player2.score)
Label:
font_size: 70
center_x: root.width * 3/4
top: root.top - 50
text: str(root.player1.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
Label:
id: game_over_label
text: " "
font_size: 100
center_x: root.width / 2
center_y: root.height / 2
答案 0 :(得分:1)
您可以使用相同的方法更改游戏而非用于在分数更改时更改分数标签的标签。
如果您在main.py中添加ObjectProperty
到PongGame
:
end_label = ObjectProperty()
在PongGame
下的kv文件中,写一下:
end_label: game_over_label # linking them up
您可以在main.py中为PongGame
类添加一个函数:
def check_score(self):
scores = [self.player1.score,
self.player2.score]
for s in scores:
if s == 10:
self.end_label = "Game Over"
return
然后在update
函数中,您可以在分数增加时调用此函数,例如:
self.player1.score += 1
self.check_score()
和self.player2.score
答案 1 :(得分:0)
在树中找到ID的一种方法是放置一个&#34;重定向器&#34;在小部件的根目录
在.kv中,在PongGame下:
gameoverlabel: game_over_label
在.py下的.py如果count1或count2&gt; = 10:&#34;添加
self.gameoverlabel.text = "win"
希望这有用