python - 向kivy屏幕添加功能
问题描述
当我第一次离开主屏幕时,屏幕被复制了。当我第一次返回主屏幕时,屏幕全黑。但它工作正常。
我第一次滑动时第二个屏幕的图像。我第一次回来时的主屏幕图像:。但它适用于第二次和其他滑动。
Builder.load_string("""
<MenuScreen>:
BoxLayout:
Button:
text: 'Go to main screen'
on_press: root.manager.current = 'main'
size_hint: None, None
size: self.texture_size
Button:
text: 'Go to second screen'
on_press: root.manager.current = 'second'
size_hint: None, None
size: self.texture_size
<MainScreen>:
# add Game widget
Game:
power: powerbar
lifes: lifesbar
id: game
BoxLayout:
Button:
text: 'Go to the menu screen'
on_press: root.manager.current = 'menu'
size_hint: None, None
size: self.texture_size
FloatLayout:
ShootProgressBar:
id: powerbar
pos_hint: {'x': 0.035, 'y': 0.9}
size_hint: (0.25, 0.1)
max:10
LifesProgressBar:
canvas:
Color:
rgba: 1, 0, 0, 1
id: lifesbar
pos_hint: {'x': 0.7, 'y': 0.92}
size_hint: (0.25, 0.1)
max:3
value:3
Image:
id: ethereum
size_hint: (0.04, 0.04)
pos_hint: {'x': 0.002, 'y': 0.93}
allow_stretch: True
source: "ethereum.png"
Image:
id: lifes_img
size_hint: (0.04, 0.04)
pos_hint: {'x': 0.952, 'y': 0.93}
allow_stretch: True
source: "lifes.png"
<SecondScreen>:
# add Game widget
Game:
power: powerbar
lifes: lifesbar
id: game
BoxLayout:
Button:
text: 'Back to menu'
on_press: root.manager.current = 'menu'
size_hint: None, None
size: self.texture_size
FloatLayout:
ShootProgressBar:
id: powerbar
pos_hint: {'x': 0.035, 'y': 0.9}
size_hint: (0.25, 0.1)
max:10
LifesProgressBar:
canvas:
Color:
rgba: 1, 0, 0, 1
id: lifesbar
pos_hint: {'x': 0.7, 'y': 0.92}
size_hint: (0.25, 0.1)
max:3
value:3
Image:
id: ethereum
size_hint: (0.04, 0.04)
pos_hint: {'x': 0.002, 'y': 0.93}
allow_stretch: True
source: "ethereum.png"
Image:
id: lifes_img
size_hint: (0.04, 0.04)
pos_hint: {'x': 0.952, 'y': 0.93}
allow_stretch: True
source: "lifes.png"
<-ShootProgressBar@ProgressBar>:
canvas:
Color:
rgb: 1, 1, 1
BorderImage:
border: (12, 12, 12, 12)
pos: self.x, self.center_y - 3
size: self.width, 24
source : 'black_bg.png'
BorderImage:
border: [int(min(self.width * (self.value / float(self.max)) if self.max else 0, 12))] * 4
pos: self.x, self.center_y - 12
size: self.width * (self.value / float(self.max)) if self.max else 0, 24
source : 'blue_fg.png'
<-LifesProgressBar@ProgressBar>:
canvas:
Color:
rgb: 1, 1, 1
BorderImage:
border: (12, 12, 12, 12)
pos: self.x, self.center_y - 14
size: self.width, 24
source: 'black_bg.png'
BorderImage:
border: [int(min(self.width * (self.value / float(self.max)) if self.max else 0, 12))] * 4
pos: self.x, self.center_y - 12
size: self.width * (self.value / float(self.max)) if self.max else 0, 24
source: 'green2_fg.png'
<ColourScreen>:
BoxLayout:
orientation: 'vertical'
Label:
text: 'colour {:.2},{:.2},{:.2} screen'.format(*root.colour[:3])
font_size: 30
Widget:
canvas:
Color:
rgba: root.colour
Ellipse:
pos: self.pos
size: self.size
BoxLayout:
Button:
text: 'goto first screen'
font_size: 30
on_release: app.root.current = 'first'
Button:
text: 'get random colour screen'
font_size: 30
on_release: app.root.new_colour_screen()
""")
# Declare both screens
class MenuScreen(Screen):
pass
class MainScreen(Screen):
def on_enter(self, *args):
self.ids.game.initialize()
self.ids.game.power.value = 0
self.ids.game.lifes.value = 3
self.update_event = Clock.schedule_interval(self.ids.game.update_glsl, 1.0 / 60.0)
def on_leave(self, *args):
self.update_event.cancel()
self.ids.game.indices = []
self.ids.game.vertices = []
self.ids.game.particles = []
self.ids.game.canvas.clear()
self.update_event = None
class SecondScreen(Screen):
def on_enter(self, *args):
self.ids.game.initialize()
self.ids.game.power.value = 0
self.ids.game.lifes.value = 3
self.update_event = Clock.schedule_interval(self.ids.game.update_glsl, 1.0 / 60.0)
def on_leave(self, *args):
self.update_event.cancel()
self.ids.game.indices = []
self.ids.game.vertices = []
self.ids.game.particles = []
self.ids.game.canvas.clear()
self.update_event = None
我想要的是使用条件来改变屏幕。例如,if self.parent.power.value == 10: sm.current = 'second'
解决方案
如果我了解您要做什么,您可以将您的Game
小部件添加到MainScreen
中kv
(您还必须调整 的大小Button
,使其不覆盖整个MainScreen
):
<MainScreen>:
# add Game widget
Game:
id: game
BoxLayout:
Button:
text: 'Go to the second screen'
on_press: root.manager.current = 'second'
size_hint: None, None
size: self.texture_size
FloatLayout:
ShootProgressBar:
id: powerbar
pos_hint: {'x': 0.035, 'y': 0.9}
size_hint: (0.25, 0.1)
max:10
LifesProgressBar:
canvas:
Color:
rgba: 1, 0, 0, 1
id: lifesbar
pos_hint: {'x': 0.7, 'y': 0.92}
size_hint: (0.25, 0.1)
max:3
value:3
Image:
id: ethereum
size_hint: (0.04, 0.04)
pos_hint: {'x': 0.002, 'y': 0.93}
allow_stretch: True
source: "ethereum.png"
Image:
id: lifes_img
size_hint: (0.04, 0.04)
pos_hint: {'x': 0.952, 'y': 0.93}
allow_stretch: True
source: "lifes.png"
然后在MainScreen
和SecondSceen
类中,您可以进行初始化并开始更新:
class MainScreen(Screen):
def on_enter(self, *args):
self.ids.game.initialize()
self.ids.game.power.value = 0
self.ids.game.lifes.value = 0
self.update_event = Clock.schedule_interval(self.ids.game.update_glsl, 1.0 / 60.0)
def on_leave(self, *args):
self.update_event.cancel()
self.ids.game.indices = []
self.ids.game.vertices = []
self.ids.game.particles = []
self.ids.game.canvas.clear()
self.update_event = None
class SecondScreen(Screen):
def on_enter(self, *args):
self.ids.game1.initialize()
self.ids.game1.power.value = 0
self.ids.game1.lifes.value = 0
self.update_event = Clock.schedule_interval(self.ids.game1.update_glsl, 1.0 / 60.0)
def on_leave(self, *args):
self.update_event.cancel()
self.ids.game1.indices = []
self.ids.game1.vertices = []
self.ids.game1.particles = []
self.ids.game1.canvas.clear()
self.update_event = None
并在Game.update_glsl()
方法中,用于super
访问PSWidget
更新方法,如下所示:
def update_glsl(self, nap):
if self.use_mouse:
self.player_x, self.player_y = Window.mouse_pos
if self.firing:
self.fire_delay -= nap
self.spawn_delay -= nap
super(Game, self).update_glsl(nap)
还。在您的PSWidget
班级中,更改 , 的定义indices
,vertices
并使particles
它们成为Properties
,如下所示:
class PSWidget(Widget):
indices = ListProperty([])
vertices = ListProperty([])
particles = ListProperty([])
推荐阅读
- reactjs - 这个内联样式三元有什么问题
- spring - Spring MVC - 5.2.9.RELEASE 与 Mongo(spring-data-mongodb - 3.0.4.RELEASE)不工作
- python-3.x - 将文件中的单词或 URL 端点添加到由 URLS 组成的另一个文件
- mysql - 如何找到用户最大的捐赠?没有聚合函数
- java - 如何从 JUnit 中具有 InputStream 的方法中覆盖 IOException
- android - android10:如何设置每显示焦点
- python - 用socket recv的无限循环杀死一个线程
- casting - OpenCL 铸造将 float4 组件转换为 uchars
- javascript - 在满足条件之前可以与 fetch 轮询异步吗?(幸免于难)
- azure - 如何为 azure 容器应用服务设置 fiexd 主机名