2015-06-19 209 views
0

我想用啓動菜單製作一個kivy應用程序,但是我無法在第二個屏幕上顯示我的乒乓遊戲。我應該如何參考遊戲使其可見?我試過並搜索過但找不到任何東西。我100%確定PongGame正常工作,我無法顯示它。如果有人能告訴我如何正確地做到這一點,那將是非常棒的。如何在屏幕上顯示遊戲?

Main.py:

from kivy.app import App 
from kivy.uix.screenmanager import ScreenManager, Screen, WipeTransition 
from kivy.properties import ObjectProperty 
from kivy.uix.widget import Widget 
from kivy.properties import NumericProperty, ReferenceListProperty,\ 
    ObjectProperty 
from kivy.vector import Vector 
from kivy.clock import Clock 
from kivy.uix.popup import Popup 
from kivy.uix.label import Label 

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=(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)) 
      if self.player1.score == 10: 
       popup = Popup(title='Test popup', content=Label(text='Hello world'), auto_dismiss=False) 
       return popup 
    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 ScreenThree(Screen): 
    pass 

class ScreenTwo(Screen): 
    pass 

class ScreenOne(Screen): 
    pass 

class Manager(ScreenManager): 

    screen_one = ObjectProperty(None) 
    screen_two = ObjectProperty(None) 
    screen_three = ObjectProperty(None) 

class ScreensApp(App): 

    def build(self): 
     m = Manager(transition=WipeTransition()) 
     return m 

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

screen.kv:

<PongBall>: 
    size: 50, 50 
    canvas: 
     Ellipse: 
      pos: self.pos 
      size: self.size 

<PongPaddle>: 
    size: 25, 200 
    canvas: 
     Rectangle: 
      pos:self.pos 
      size:self.size 

<PongGame>: 
    ball: pong_ball 
    player1: player_left 
    player2: player_right 

    canvas: 
     Rectangle: 
      pos: self.center_x-5, 0 
      size: 10, self.height 

    Label: 
     font_size: 70 
     center_x: root.width/4 
     top: root.top - 50 
     text: str(root.player1.score) 

    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_left 
     x: root.x 
     center_y: root.center_y 

    PongPaddle: 
     id: player_right 
     x: root.width-self.width 
     center_y: root.center_y 

<ScreenOne>: 
    Button: 
     text: "Screen 1 >> Screen 2" 
     on_press: root.manager.current = 'screen2' 
<ScreenTwo>: 
     def build(self): 
      game = PongGame() 
      game.serve_ball() 
      Clock.schedule_interval(game.update, 1.0/60.0) 
      return game 

<ScreenThree>: 
    Button: 
     text: "Screen 3 >> Screen 1" 
     on_press: root.manager.current = 'screen1' 


<Manager>: 
    id: screen_manager 

    screen_one: screen_one 
    screen_two: screen_two 
    screen_three: screen_three 

    ScreenOne: 
     id: screen_one 
     name: 'screen1' 
     manager: screen_manager 

    ScreenTwo: 
     id: screen_two 
     name: 'screen2' 
     manager: screen_manager 

    ScreenThree: 
     id: screen_three 
     name: 'screen3' 
     manager: screen_manager 

回答

0

嘛!你的程序中有很多錯誤,我不得不做出改進。 (我理解你是初學者)

首先,請仔細閱讀完整的kivy language documentation,因爲我可以清楚地看到你直接從編碼開始而不掌握基本知識。 您可以製作幾款優秀的遊戲,但從長遠來看,您將面臨如果沒有明確概念就無法解決的問題。 不幸的是,你將無法發現kivy的真實權力。 :)

您可能還想修改您的python概念。

一些改進不值得一提,但很重要,當你閱讀代碼時你會明白。

改進1:

如果您在建立()返回一個小部件的應用程序可以建立,或者如果您設置self.root(但你不能再ñ再次提出申請) 你在這裏做的:

<ScreenTwo>: 
    def build(self): 
     game = PongGame() 
     game.serve_ball() 
     Clock.schedule_interval(game.update, 1.0/60.0) 
     return game 

改進2:

當你點擊按鈕打乒乓球這是屏幕遊戲。你的遊戲始於球的服務。

on_release: root.current = 'game';game.serve_ball() 

(知識)

如果你仍然得到你可能要檢查kivy文件的名稱黑屏,爲您既可以去kivy文檔或this link

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 ScreensApp(App): 
    def build(self): 
     self.load_kv('t6.kv') 
     return Manager(transition=WipeTransition()) 

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

這是kv文件。

<PongBall>: 
    size: 50, 50 
    canvas: 
     Ellipse: 
      pos: self.pos 
      size: self.size 

<PongPaddle>: 
    size: 25, 200 
    canvas: 
     Rectangle: 
      pos:self.pos 
      size:self.size 

<PongGame>: 
    ball: pong_ball 
    player1: player_left 
    player2: player_right 

    canvas: 
     Rectangle: 
      pos: self.center_x-5, 0 
      size: 10, self.height 

    Label: 
     font_size: 70 
     center_x: root.width/4 
     top: root.top - 50 
     text: str(root.player1.score) 

    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_left 
     x: root.x 
     center_y: root.center_y 

    PongPaddle: 
     id: player_right 
     x: root.width-self.width 
     center_y: root.center_y 

<Manager>: 
    id: screen_manager 


    Screen: 
     name: 'home' 
     Button: 
      text: 'Play Ping Pong' 
      halign: 'center' 
      valign: 'middle' 
      font_size: 100 
      text_size: self.size 
      on_release: root.current = 'game';game.serve_ball() 

    Screen: 
     name: 'game' 
     PongGame: 
      id: game 
+0

非常感謝!我認爲我比初學者更多,你的建議非常有幫助。現在一切正常,很容易理解爲什麼。再次非常感謝你! :) – RIP

+0

我很高興幫助:) – kiok46