如何修复我的弹跳球在屏幕底部出现故障?
How can i fix my bouncing ball glitching through the bottom of my screen?
如果你的 运行 它一旦球即将停止,它就会开始上下跳跃并消失在地板上。
所以我一直在学习 pong 游戏的 kivy 教程,中途我想为什么不让球有重力......我有点想通了,虽然我不知道它是否是好的代码但这不是我的问题。我已经将它与其他一些基本相同的代码进行了比较,但没有发现任何差异。有人可以告诉我我做错了什么吗? (抱歉,我不得不将所有代码粘贴到此处,但我不知道问题出在哪里...)
import kivy
kivy.require("1.10.1")
from kivy.app import App
from kivy.uix.widget import Widget
from kivy.properties import NumericProperty, ReferenceListProperty, ObjectProperty
from kivy.vector import Vector
from kivy.clock import Clock
from random import randint
class PongBall(Widget):
velocity_x = NumericProperty(0)
velocity_y = NumericProperty(0)
velocity = ReferenceListProperty(velocity_x, velocity_y)
def update_speed(self):
self.velocity[1] = self.velocity[1] - 15/60
self.pos = Vector(*self.velocity) + self.pos
class PongGame(Widget):
ball = ObjectProperty(None)
def serve_ball(self):
self.ball.center = self.center
self.ball.velocity = Vector(0, 0)
def update(self, dt):
self.ball.update_speed()
if (self.ball.y < 0) or (self.ball.top > self.height):
self.ball.velocity_y *= -1
if (self.ball.x < 0) or (self.ball.right > self.width):
self.serve_ball()
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.10.1
<PongBall>:
size: 50, 50
canvas:
Ellipse:
pos: self.pos
size: self.size
<PongGame>:
ball: pong_ball
canvas:
Rectangle:
pos: self.center_x - 5, 0
size: 10, self.height
Label:
font_size: 70
center_x: root.width / 4
top: root.height * 9 / 10
text: "0"
Label:
font_size: 70
center_x: root.width * 3 / 4
top: root.height * 9 / 10
text: str(pong_ball.velocity[1])
PongBall:
id: pong_ball
center: self.parent.center
我想让球减速,直到它落在地上不动。
这只是浮动的常见问题...
就在你的更新速度函数中做一些类似的事情
if velocity <= 1e-6: # some really small number
velocity = 0.0
在你的移动函数中固定你的 y 位置可能也是个好主意
if (self.ball.y < 0) or (self.ball.top > self.height):
self.ball.y = 0.1
self.ball.velocity_y *= -1
如果你的 运行 它一旦球即将停止,它就会开始上下跳跃并消失在地板上。
所以我一直在学习 pong 游戏的 kivy 教程,中途我想为什么不让球有重力......我有点想通了,虽然我不知道它是否是好的代码但这不是我的问题。我已经将它与其他一些基本相同的代码进行了比较,但没有发现任何差异。有人可以告诉我我做错了什么吗? (抱歉,我不得不将所有代码粘贴到此处,但我不知道问题出在哪里...)
import kivy
kivy.require("1.10.1")
from kivy.app import App
from kivy.uix.widget import Widget
from kivy.properties import NumericProperty, ReferenceListProperty, ObjectProperty
from kivy.vector import Vector
from kivy.clock import Clock
from random import randint
class PongBall(Widget):
velocity_x = NumericProperty(0)
velocity_y = NumericProperty(0)
velocity = ReferenceListProperty(velocity_x, velocity_y)
def update_speed(self):
self.velocity[1] = self.velocity[1] - 15/60
self.pos = Vector(*self.velocity) + self.pos
class PongGame(Widget):
ball = ObjectProperty(None)
def serve_ball(self):
self.ball.center = self.center
self.ball.velocity = Vector(0, 0)
def update(self, dt):
self.ball.update_speed()
if (self.ball.y < 0) or (self.ball.top > self.height):
self.ball.velocity_y *= -1
if (self.ball.x < 0) or (self.ball.right > self.width):
self.serve_ball()
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.10.1
<PongBall>:
size: 50, 50
canvas:
Ellipse:
pos: self.pos
size: self.size
<PongGame>:
ball: pong_ball
canvas:
Rectangle:
pos: self.center_x - 5, 0
size: 10, self.height
Label:
font_size: 70
center_x: root.width / 4
top: root.height * 9 / 10
text: "0"
Label:
font_size: 70
center_x: root.width * 3 / 4
top: root.height * 9 / 10
text: str(pong_ball.velocity[1])
PongBall:
id: pong_ball
center: self.parent.center
我想让球减速,直到它落在地上不动。
这只是浮动的常见问题...
就在你的更新速度函数中做一些类似的事情
if velocity <= 1e-6: # some really small number
velocity = 0.0
在你的移动函数中固定你的 y 位置可能也是个好主意
if (self.ball.y < 0) or (self.ball.top > self.height):
self.ball.y = 0.1
self.ball.velocity_y *= -1