Python 在游戏中放置kivy button代码的位置

Python 在游戏中放置kivy button代码的位置,python,button,kivy,Python,Button,Kivy,您好,我的问题是我不知道如何在游戏中放置按钮而不使游戏屏幕消失。 每当我尝试在代码中添加return btn1时,它只会显示按钮。但不是游戏。我确信这是一个初学者的问题,但查找它对我不起作用 顺便说一句,这是我私人使用的教程中的代码 我的代码是: from kivy.app import App from kivy.uix.widget import Widget from kivy.properties import NumericProperty, ReferenceListProperty

您好,我的问题是我不知道如何在游戏中放置按钮而不使游戏屏幕消失。 每当我尝试在代码中添加return btn1时,它只会显示按钮。但不是游戏。我确信这是一个初学者的问题,但查找它对我不起作用

顺便说一句,这是我私人使用的教程中的代码

我的代码是:

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
import kivy.uix.button
from kivy.app import App
from kivy.uix.image import Image
from kivy.uix.behaviors import ButtonBehavior
from kivy.uix.button import Button

def callback(instance):
 print('The button <%s> is being pressed' % instance.text)

btn1 = Button(text='Hello world 1')
btn1.bind(on_press=callback)
btn2 = Button(text='Hello world 2')
btn2.bind(on_press=callback)




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))

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 PongApp(App):
 def build(self):
    game = PongGame()
    game.serve_ball()
    Clock.schedule_interval(game.update, 1.0 / 60.0)





if __name__ == '__main__':
 PongApp().run() 
从kivy.app导入应用
从kivy.uix.widget导入widget
从kivy.properties导入NumericProperty、ReferenceListProperty、\
对象属性
从kivy.vector导入向量
从kivy.clock导入时钟
导入kivy.uix.button
从kivy.app导入应用程序
从kivy.uix.image导入图像
从kivy.uix.behaviors导入按钮行为
从kivy.uix.button导入按钮
def回调(实例):
打印('正在按下按钮''%instance.text〕)
btn1=按钮(text='Hello world 1')
btn1.bind(按=回调时)
btn2=按钮(text='Hello world 2')
绑定(按=回调时)
类PongPaddle(小部件):
分数=数值属性(0)
def弹跳球(自身,球):
如果self.collide_小部件(球):
vx,vy=球的速度
偏移=(ball.center_y-self.center_y)/(self.height/2)
反弹=矢量(-1*vx,vy)
水平=反弹*1.1
球体速度=x层,y层+偏移
类PongBall(小部件):
速度x=数值属性(0)
速度_y=数值属性(0)
速度=ReferenceListProperty(速度x,速度y)
def移动(自我):
self.pos=向量(*self.velocity)+self.pos
类PongGame(小部件):
ball=ObjectProperty(无)
player1=ObjectProperty(无)
player2=ObjectProperty(无)
def发球(自我,水平=(4,0)):
self.ball.center=self.center
self.ball.velocity=vel
def更新(自我,dt):
self.ball.move()
#桨的弹跳
self.player1.弹起球(self.ball)
self.player2.弹起球(self.ball)
#将球从底部或顶部反弹
如果(self.ball.yself.top):
self.ball.velocity_y*=-1
#跑到一边去得分?
如果self.ball.xself.width:
self.player1.score+=1
自我发球(水平=(-4,0))
def on_touch_move(自我,触摸):
如果触摸.xself.width-self.width/3:
self.player2.center_y=touch.y
PongApp课程(应用程序):
def生成(自):
game=PongGame()
比赛发球
时钟时间间隔(game.update,1.0/60.0)
如果uuuu name uuuuuu='\uuuuuuu main\uuuuuuu':
PongApp().run()

如果你想让一个小部件成为另一个小部件的一部分,这个小部件必须是另一个小部件的子部件,或者它的祖先是该小部件的子部件,当你在
build()中返回btn1时,你表示它是根部件,也就是说,窗口,它不显示游戏,要使一个小部件的子部件成为一个选项,可以使用
add\u widget()

class PongApp(应用程序):
def生成(自):
game=PongGame()
game.add_小部件(btn1)#
class PongApp(App):
    def build(self):
        game = PongGame()
        game.add_widget(btn1) # <--
        btn2.pos = 100, 100
        game.add_widget(btn2) # <--
        game.serve_ball()
        Clock.schedule_interval(game.update, 1.0 / 60.0)
        return game