不能在两个屏幕中的一个屏幕上包括乒乓球游戏 - kivy

时间:2015-07-06 05:20:10

标签: kivy

这是我的错误:

AttributeError: 'NoneType' object has no attribute 'move'

我一直在寻找,寻找kivy视频并尝试了很多东西,但是当我按下StartScreen中的Play时,我无法通过Pong Game教程来掌握我的GameScreen。 有时我不会收到错误,但只有当游戏不在屏幕上时才会出现错误。该应用程序然后正常工作,并且球(蛇蛇形)确实显示在右侧SnakeWidget小部件上,但即使键入确切位置,我也无法改变其位置。

的.py:

from kivy.app import App
from kivy.uix.screenmanager import ScreenManager, Screen, FadeTransition
from kivy.uix.boxlayout import BoxLayout
from kivy.uix.floatlayout import FloatLayout
from kivy.uix.gridlayout import GridLayout
from kivy.uix.widget import Widget
from kivy.uix.label import Label
from kivy.uix.button import Button
from kivy.core.window import Window
from kivy.core.image import Image
from kivy.graphics import Color, Rectangle
from kivy.uix.popup import Popup
from kivy.uix.bubble import Bubble
from kivy.properties import NumericProperty, ReferenceListProperty
from kivy.properties import ObjectProperty
from kivy.vector import Vector
from kivy.clock import Clock
from kivy.lang import Builder
from random import randint

class InfoWidget(Widget):
    score = NumericProperty(0)


class Snake(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 SnakeWidget(Widget):
    snaky = ObjectProperty(None)

    def __init__(self, *args, **kwargs):
        super(SnakeWidget, self).__init__(*args, **kwargs)
        Clock.schedule_interval(self.update, 1.0 / 60.0)

    def begin(self, vel=(4, 0)):
        self.snaky.center = self.center
        self.snaky.velocity = vel

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

class InfoWidget(Widget):
    score = NumericProperty(0)

class StartScreen(Screen): 
    snake_widget = SnakeWidget()
    Clock.schedule_interval(snake_widget.update, 1.0 / 60.0)

class GameScreen(Screen):
    pass

class RootScreen(ScreenManager):
    pass


class Main2App(App):
    def build(self):
        self.load_kv("main2.kv")
        return RootScreen()

if __name__ == "__main__":
    Main2App().run()

.kv文件:

#:import FadeTransition kivy.uix.screenmanager.FadeTransition

<StartScreen>:
    name: "start"
    canvas.before:
        Rectangle:
            pos: self.pos
            size: self.size
            source: "lights.png"
    FloatLayout:
        Button:
            id: "play"
            background_color: 0.012, 0.227, 0.11, 0.1
            text: "Play!"
            size_hint: 0.4, 0.3
            pos_hint: {'center_x':.5, 'center_y':.5}
            font_size: 70
            on_release: root.current = 'game'; snake_widget.begin()
        Button:
            id: "how"
            background_color: 0.012, 0.227, 0.11, 0.1
            text: "How to play"
            size_hint: 0.4, 0.1
            pos_hint: {"center_x":.5, "center_y":.3}
            font_size: 40

<GameScreen>:
    name: "game"
    canvas.before:
        Rectangle:
            pos: self.pos
            size: self.size
            source: "lights.png"
    FloatLayout:
        SnakeWidget:
        InfoWidget: 
        Button:
            id: menu
            size_hint: 0.2, 0.1
            pos_hint: {"x": 0.8,"y":0.9}
            background_color: 0.012, 0.227, 0.11,0.3
            text: "Menu"
            font_size: 17
            on_release: root.manager.current  = "start"
        Button: 
            id: levo
            size_hint: 0.1, 0.1
            pos_hint: {"x": 0.8,"y":0}
            background_color: 0.012, 0.227, 0.11,0.3
            text: "<"
            font_size: 20
        Button: 
            id: desno
            size_hint: 0.1, 0.1
            pos_hint: {"x": 0.9,"y":0}
            background_color: 0.012, 0.227, 0.11,0.3
            text: ">"
            font_size: 20

<Snake>:
    size: 50, 50 
    canvas:
        Color: 
            rgba: 1,1,1,1
        Ellipse:
            pos: self.pos
            size: 40, 40
            source: "snaky.png"

<SnakeWidget>:
    id: snake_widget
    snaky: snake
    size_hint: 0.797, 1
    pos_hint: {"x": 0,"center_y":0.5}
    canvas: 
        Color: 
            rgba: 1,1,1,0.07
        Rectangle:
            size: self.size
            pos: self.pos
    Snake: 
        id: snake
        center: self.parent.center

<InfoWidget>:
    size_hint: 0.2, 0.799
    pos_hint: {"x": 0.8014,"y":0.101}
    canvas.before: 
        Color:
            rgba: 0.010, 0.227, 0.17, 0.005
        Rectangle:
            size: self.size
            pos: self.pos

    Label: 
        canvas.before: 
            Color:
                rgba: 0.010, 0.227, 0.17, 0.005
            Rectangle:
                size: root.parent.width*0.192, root.parent.height*0.06
                pos: root.parent.width*0.805, root.parent.height*0.837
        id: "result"
        text: "Result:"
        font_size: 17
        pos: root.parent.width*0.79, root.parent.height*0.78

    Label:
        canvas.before: 
            Color:
                rgba: 0.012, 0.227, 0.15, 0.005
            Rectangle:
                size: root.parent.width*0.192, root.parent.height*0.06
                pos: root.parent.width*0.805, root.parent.height*0.77
        id: "record"
        text: "Record:"
        font_size: 17
        pos: root.parent.width*0.79, root.parent.height*0.72



<RootScreen>:
    id: screen_manager
    transition: FadeTransition() 
    StartScreen:
        name: "start"
    GameScreen:
        name: "game"

对不起所有代码,但我真的不知道该怎么办了。

2 个答案:

答案 0 :(得分:0)

第35行,

class SnakeWidget(Widget):
    snaky = ObjectProperty(Snake())

Snaky应该是Snake的实例。

答案 1 :(得分:0)

尝试这样的事情:

.py在这里...

base64Digits[64]

.kv在这里...

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 __init__(self, *args, **kwargs):
        super(PongGame, self).__init__(*args, **kwargs)
        Clock.schedule_interval(self.update, 1.0 / 60.0)

    def serve_ball(self, vel=(4, 0)):
        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)
        self.player2.bounce_ball(self.ball)

    #bounce ball off bottom or top
        if (self.ball.y < self.y) or (self.ball.top > self.top):
        self.ball.velocity_y *= -1

    #went of to a side to score point?
        if self.ball.x < self.x:
            self.player2.score += 1
            self.serve_ball(vel=(4, 0))
        if self.ball.x > self.width:
            self.player1.score += 1
            self.serve_ball(vel=(-4, 0))

    def on_touch_move(self, touch):
        if touch.x < self.width / 3:
            self.player1.center_y = touch.y
        if touch.x > self.width - self.width / 3:
            self.player2.center_y = touch.y

class Manager(ScreenManager):
    pass

class GameApp(App):
    def build(self):
        self.load_kv('t6.kv')
        return Manager(transition=WipeTransition())

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

希望这会帮助...