[英]Kivy ScreenManager does not switch screens
i am a newbie to both python and kivy. 我是python和kivy的新手。 I have been trying to develop a simple Pong game with Python.
我一直在尝试用Python开发一个简单的Pong游戏。 Everything was going well until i tried to put together a Menu.
一切都进行得很好,直到我尝试整理一个菜单。 I used ScreenManager to try to navigate through the two screens.
我使用ScreenManager尝试浏览两个屏幕。 But when i press the button, nothing happens.
但是当我按下按钮时,什么也没发生。 Every answer or idea is well appreciated!
每个答案或想法都非常感谢!
Here is my .py code: 这是我的.py代码:
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 pygame
from kivy.uix.button import Button
from kivy.uix.screenmanager import ScreenManager, Screen
from kivy.uix.gridlayout import GridLayout
from kivy.lang import Builder
class Menu(GridLayout,Screen):
def __init__(self, **kwargs):
super(Menu, self).__init__(**kwargs)
pass
class PongSpadi(Widget):
stig = NumericProperty(0)
def skoppa_bolti(self, bolti):
if self.collide_widget(bolti):
vx, vy = bolti.velocity
offset = (bolti.center_y - self.center_y) / (self.height / 2)
skoppadur = Vector(-1 * vx, vy)
vel = skoppadur
bolti.velocity = vel.x, vel.y + offset
class PongBolti(Widget):
velocity_x = NumericProperty(0)
velocity_y = NumericProperty(100)
velocity = ReferenceListProperty(velocity_x, velocity_y)
def move(self):
self.pos = Vector(*self.velocity) + self.pos
class PongGame(Screen, Widget):
bolti = ObjectProperty(None)
spilari1 = ObjectProperty(None)
spilari2 = ObjectProperty(None)
h1 = NumericProperty(310)
h2 = NumericProperty(310)
def serve_bolti(self, vel=(12, 6)):
self.bolti.center = self.center #Staðsetur boltann í miðjuna
self.bolti.velocity = vel
def update(self, dt):
self.bolti.move()
self.spilari1.skoppa_bolti(self.bolti) #skoppar bolta af spöðum
self.spilari2.skoppa_bolti(self.bolti)
if (self.bolti.y < 0) or (self.bolti.top > self.height):
self.bolti.velocity_y *= -1 #Lætur boltan skoppa ef hann snertir vegginn uppi/niðri
if self.bolti.x <self.x:
self.spilari2.stig += 1 #skráir stig ef bolti fer í hliðarnar
self.serve_bolti(vel= (12,6)) #byrjar nýtt uppkast
if self.bolti.x > self.width:
self.spilari1.stig += 1
self.serve_bolti(vel = (-12,6))
keys = pygame.key.get_pressed() #Takka stjórnun fyrir leikmann 1
if self.h1 < 567: #Bannar spaða 1 að fara of hátt
if self.h1 > 25: #Bannar spaða 1 að fara of látt
if keys[pygame.K_z]: #býr til takka fyrir spaða 1
self.h1 -= 10
elif keys[pygame.K_a]:
self.h1 += 10
if self.h1 >= 567: #ýtir honum til baka ef farið er of hátt
self.h1 -= 10
if self.h1 <= 25: #ýtir honum til baka ef farið er of látt
self.h1 += 10
keys = pygame.key.get_pressed() #takka stjórnun fyrir leikmann 2
if self.h2 < 567:
if self.h2 > 25:
if keys[pygame.K_m]:
self.h2 -= 10
elif keys[pygame.K_k]:
self.h2 += 10
if self.h2 >= 567: #ýtir honum til baka ef farið er of hátt
self.h2 -= 10
if self.h2 <= 25: #ýtir honum til baka ef farið er of látt
self.h2 += 10
pass
sm = ScreenManager()
sm.add_widget(Menu(name='menu'))
sm.add_widget(PongGame(name='ponggame'))
class PongApp(App): #ræsir forritið
title = "Menu"
def build(self): #kallar á eiginleika leiksins
sm = Menu()
return sm
sm = PongGame()
sm.serve_bolti() #kallar á uppkast boltans
Clock.schedule_interval(sm.update, 1.0 / 60) #Lætur leikinn uppfærast 60 sinnum á sekúndu.
return sm
if __name__ == '__main__':
PongApp().run()
And here is my .kv code: 这是我的.kv代码:
#: kivy 1.9.1
ScreenManager:
Menu:
PongGame:
<Menu>
name: 'menu'
Button:
on_press: app.root.current = 'ponggame'
on_press: print("virkar")
text: "Manneskja á móti manneskju"
font_size: 20
center_x: root.width / 2
top: root.top / 1.5
size: 275, 50
Label: #nafn á leik
font_size: 100
center_x: root.width / 2
top: root.top - 50
text: 'Pong og Gervigreind'
Label:
font_size: 20
center_x: root.width / 2
top: root.top -120
text: 'Lokaverkefni eftir Hróbjart Höskuldsson vor 2018'
<PongBolti>:
size: 20, 20
canvas:
Rectangle:
pos: self.pos
size: self.size
<PongSpadi>:
size: 23, 100
canvas:
Rectangle:
pos:self.pos
size:self.size
<PongGame>:
name: 'ponggame'
bolti: pong_bolti
spilari1: spilari_vinstri
spilari2: spilari_haegri
canvas:
Rectangle:
pos: self.center_x - 5, 0
size: 3, self.height
Label: # staða leikmanns 1
font_size: 70
center_x: root.width / 4
top: root.top - 50
text: str(root.spilari1.stig)
Label: # staða leikmanns 2
font_size: 70
center_x: root.width * 3 / 4
top: root.top - 50
text: str(root.spilari2.stig)
PongBolti:
id: pong_bolti
center: self.parent.center
PongSpadi:
id: spilari_vinstri
x: root.x + 30
center_y: root.h1
PongSpadi:
id: spilari_haegri
x: root.width-self.width - 30
center_y: root.h2
Button:
on_release: app.root.current = 'menu'
on_press: print("virkar")
text: "Manneskja á móti manneskju"
font_size: 20
center_x: root.width / 2
top: root.top / 1.5
size: 275, 50
This is not a complete answer, but some ideas to get you moving: 这不是一个完整的答案,但是有一些想法可以帮助您:
Your build()
method should return a ScreenManager
. 您的
build()
方法应返回ScreenManager
。 I think it should look something like this: 我认为应该看起来像这样:
def build(self): #kallar á eiginleika leiksins sm = ScreenManager() sm.add_widget(Menu()) sm.add_widget(PongGame()) return sm
Your PongGame
does not need to extend Widget
( Screen
is a Widget) 您的
PongGame
不需要扩展Widget
( Screen
是Widget)
Your Menu
class does not need to extend both Screen
and GridLayout
. 您的
Menu
类不需要同时扩展Screen
和GridLayout
。 Just use a GridLayout
in your Menu
like this: 只需在
Menu
使用GridLayout
,如下所示:
<Menu> name: 'menu' GridLayout: rows: 3 Button: on_press: app.root.current = 'ponggame' on_press: print("virkar") text: "Manneskja á móti manneskju" font_size: 20 center_x: root.width / 2 top: root.top / 1.5 size: 275, 50 Label: #nafn á leik font_size: 100 center_x: root.width / 2 top: root.top - 50 text: 'Pong og Gervigreind' Label: font_size: 20 center_x: root.width / 2 top: root.top -120 text: 'Lokaverkefni eftir Hróbjart Höskuldsson vor 2018'
You have code in your build()
method that looks like it might be starting the game. 您的
build()
方法中有代码看起来像是在开始游戏。 I think that might be better in the on_enter()
method of PongGame
. 我认为这可能是更好的
on_enter()
的方法PongGame
。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.