Kivy 崩溃了。不知道为什么

问题描述 投票:0回答:1

首先我知道这并不具体,但现在我无法弄清楚为什么,所以当我有原因时我会编辑问题。对不起!.

我从 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

kivy
1个回答
0
投票

不要在以下位置添加斜线:

from kivy.properties import NumericProperty, ReferenceListProperty,\ ObjectProperty
您不需要为其换行,也不要在对象属性附近按 Tab 键。

© www.soinside.com 2019 - 2024. All rights reserved.