gpt4 book ai didi

python - Kivy Pong教程语法错误: invalid syntax

转载 作者:行者123 更新时间:2023-12-03 08:28:50 25 4
gpt4 key购买 nike

python 2.7.x
基辅1.9.0

我只是从Kivy开始,然后进行Pong教程。事情进展顺利,但是现在我收到了一个看起来很简单的错误。我只是不知道怎么了。

我得到的错误是:

File: "C:\Users\toreilly\mystuff\kivypong.py" \n
def update(self, dt):
^
SyntaxError: invalid syntax

代码在这里:
    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 move(self):
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(4,0).rotate(randint(0,360)

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

#bounce off top and bottom
if (self.ball.y < 0) or (self.ball.top > self.height):
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()
game.serve_ball()
Clock.schedule_interval(game.update, 1.0/60.0)
return game


if __name__ == '__main__':
PongApp().run()

我将不胜感激。谢谢。

最佳答案

您在上一个方法中缺少右括号:

def serve_ball(self):
self.ball.center = self.center
self.ball.velocity = Vector(4,0).rotate(randint(0,360)) # HERE

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

关于python - Kivy Pong教程语法错误: invalid syntax,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/31142092/

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