Announcement

Collapse
No announcement yet.

Python Kivy

Collapse
X
  • Filter
  • Time
  • Show
Clear All
new posts

  • Python Kivy

    Hallo zusammen
    Ich habe neu mit porgrammieren angefangen und wollte als Einstieg ein Pong mit Kivy machen (natürlich mit Tutorial).
    Nun habe ich aber einen Fehler den ich nicht verstehe. Ich habe natürlich auch schon im Internet gesucht aber da war das Niveau zu hoch für mich.
    Vielleicht könnte mir ja jemand helfen.
    Hier sind die Code's und der Fehler:

    Pythoncode:

    Code:
    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
    
    
    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=(0, -4)):
            self.ball.center = self.center
            self.ball.velocity = vel
    
        def update(self, dt):
            self.ball.move()
    
            #bounce of paddles
            self.player1.bounce_ball(self.ball)
    
            #bounce ball off bottom or top
            if (self.ball.x < self.x) or (self.ball.top > self.top):
                self.ball.velocity_y *= -1
            if (self.ball.x < self.x) or (self.ball.x > self.width):
                self.ball.velocity_x *= -1
    
            #went of to a side to score point?
            if self.ball.y < 10:
                self.player2.score += 1
                self.serve_ball(vel=(0, -4))
    
        def on_touch_move(self, touch):
            if touch.y < self.height / 3:
                self.player1.center_x = touch.x
    
    
    class Pong4OneApp(App):
        def build(self):
            game = PongGame()
            game.serve_ball()
            Clock.schedule_interval(game.update, 1.0 / 60.0)
            return game
    
    
    if __name__ == '__main__':
        Pong4OneApp().run()
    Kivycode:

    Code:
    <PongBall>:
        size: 50, 50 
        canvas:
            Ellipse:
                pos: self.pos
                size: self.size          
    
    <PongPaddle>:
        size: 200, 25
        canvas:
            Rectangle:
                pos:self.pos
                size:self.size
    
    <PongGame>:
        ball: pong_ball
        player1: player
            
        Label:
            font_size: 70  
            center_x: root.width * 3 / 4
            top: root.top - 50
            text: str(root.player2.score)
        
        PongBall:
            id: pong_ball
            center: self.parent.center
            
        PongPaddle:
            id: player
            y: root.y
            center_x: root.center_x
    Fehler:

    Code:
    Traceback (most recent call last):
       File "pong4one.py", line 70, in <module>
         Pong4OneApp().run()
       File "/usr/lib/python2.7/dist-packages/kivy/app.py", line 766, in run
         root = self.build()
       File "pong4one.py", line 63, in build
         game = PongGame()
       File "/usr/lib/python2.7/dist-packages/kivy/uix/widget.py", line 173, in __init__
         Builder.apply(self)
       File "/usr/lib/python2.7/dist-packages/kivy/lang.py", line 1566, in apply
         self._apply_rule(widget, rule, rule)
       File "/usr/lib/python2.7/dist-packages/kivy/lang.py", line 1704, in _apply_rule
         e))
     kivy.lang.BuilderException: Parser: File "./pong4one.kv", line 23:
     ...
          21:        center_x: root.width * 3 / 4
          22:        top: root.top - 50
     >>   23:        text: str(root.player2.score)
          24:    
          25:    PongBall:
     ...
     BuilderException: Parser: File "./pong4one.kv", line 23:
     ...
          21:        center_x: root.width * 3 / 4
          22:        top: root.top - 50
     >>   23:        text: str(root.player2.score)
          24:    
          25:    PongBall:
     ...
     AttributeError: 'NoneType' object has no attribute 'score'
    Zuletzt editiert von Maalur; 10.12.2014, 13:00.

  • #2
    probiere von
    text: str(root.player2.score)
    nach
    text: str(root.player.score)
    Christian

    Comment


    • #3
      Vielen Dank für die Antwort aber leider funktioniert es nicht.
      Fehler:

      Code:
      AttributeError: 'PongGame' object has no attribute 'player'
      Gruss Maalur

      Comment


      • #4
        Ich brauche ja den Score von Player2 da falls der Ball den unteren Rand berührt, es dem spieler 2 einen Punkt giebt. Ursprünglich war das Spiel für zwei Spieler (wesswegen da auch Player2 steht und es teilweise ziemlich verwirrend ist) aber da man schlecht mit einer Maus zu zweit spielen kann, dachte ich mach ich es nur für einen.

        Comment

        Working...
        X