首先,我知道这不是特定的,但是现在我无法弄清楚原因,因此我将在有原因的情况下编辑问题。抱歉!
我从Kivy开始,我有很多问题,但是在这种情况下,我什至没有错误消息!
这里是代码,这只是Pong Game教程的改编,但只使用了一个.py文件(没有.kv文件):
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
from kivy.graphics import Color, Ellipse, Line, Rectangle
from kivy.core.window import Window
from kivy.uix.label import Label
from kivy.graphics.instructions import InstructionGroup
from kivy.uix.gridlayout import GridLayout
class PongBall(Widget):
def __init__(self):
self.id = "ball"
velocity_x = NumericProperty(0)
velocity_y = NumericProperty(0)
velocity = ReferenceListProperty(velocity_x, velocity_y)
self.canvas = Ellipse(pos=(Window.width*0.5, Window.height*0.5), size=(50,50), Color=(1.0, 1.0, 0.5))
#self.canvas.add(Ellipse(pos=(width*0.5, height*0.5), size=(50,50), Color=(1.0, 1.0, 0.5)))#This way doesn't works
def move(self):
print "move called"
self.pos = Vector(*self.velocity) + self.pos
def serve_ball(self):
self.center = self.center
self.velocity = Vector(4, 0).rotate(randint(0, 360))
print "ball served"
class PongGame(Widget):
def __init__(self):
mainLayout = GridLayout(cols=1)
ball = PongBall()
ball.id = "pong_ball"
ball.center = Window.center
# mainLayout.add_widget(Rectangle(pos=(Window.width*0.5, 0), size=(10, Window.height)))#Unresearched error.
mainLayout.add_widget(Label(id="playerOneScore", font_size=70, center_x = Window.width*0.25, top=Window.height-50, text="0"))
mainLayout.add_widget(Label(id="playerTwoScore", font_size=70, center_x = Window.width*0.75, top=Window.height-50, text="0"))
mainLayout.add_widget(ball)
print "Hello"
ball.serve_ball()
def update(self, dt):
print "updated!"
ball.move()
# bounce off top and bottom
if (self.ball.y < 0) or (self.ball.top > self.heightt):
self.ball.velocity_y *= -1
# bounce off left and right
if (self.ball.x < 0) or (self.ball.right > self.width):
self.ball.velocity_x *= -1
class PongApp(App):
def build(self):
game = PongGame()
print "game created!"
Clock.schedule_interval(game.update, 1.0 / 60.0)#Clock statement neverminds for the error.
return game
if __name__ == '__main__':
PongApp().run()
具有Kivy经验的人能够理解为什么Kivy会因此代码而崩溃?
我正在使用python 2.7.9和Kivy 1.10.1