Python

【Pythonista3】複数のSceneを切り替える方法

2020年4月9日

複数のSceneを切り替えるというのは以下のようなイメージを考えています。

タイトルやボタン名だけ変えているわけではありませんよ!

複数のシーンを切り替える上で重要となるのは以下の関数です。

▼Sceneを閉じます。Sceneを閉じるとシーンがOFFとなりタッチイベントを受け付けなくなります。

Scene.dismiss_modal_scene()

▼Sceneを開きます。シーンが表示されタッチイベントを受け付けます。

Scene.present_modal_scene()

シーンを切り替える際には、present_modal_scene()を用いればOKです。Scene.dismiss_modal_scene()を使うときは、メニューSceneを開いたり閉じたりするときに使うと良いです。

▼Sceneを切り替えるメインプログラム

# coding: utf-8
from scene import *
from panel1 import PanelScene1
from panel2 import PanelScene2
from main_panel import MainScene
import sys

class Game (Scene):
	def setup(self):
		self.back_ground = SpriteNode(color = 'black', size = self.size, position = self.size / 2, parent=self)
		self.main_panel = MainScene('MainMenu')
		self.panel1 = PanelScene1('Panel1')
		self.panel2 = PanelScene2('Panel2')
		self.present_modal_scene(self.main_panel)
		
	def show_main_panel(self):
		self.present_modal_scene(self.main_panel)
	
	def show_panel1(self):
		self.present_modal_scene(self.panel1)
	
	def show_panel2(self):
		self.present_modal_scene(self.panel2)

# Run the game:
if __name__ == '__main__':
	run(Game(), PORTRAIT)

▼MainMenuのシーンプログラム

from scene import *
import ui

class ButtonNode (SpriteNode):
	def __init__(self, title, *args, **kwargs):
		SpriteNode.__init__(self, 'pzl:Button1', *args, **kwargs)
		self.title_label = LabelNode(title, font=('Avenir Next', 20), color='black', position=(0, 1), parent=self)
		self.title = title

class MainScene (Scene):
	def __init__(self, title):
		Scene.__init__(self)
		self.title = title
		
	def setup(self):
		self.background_color = 'white'
		self.set_main_bg()
		self.set_main_title()
		self.set_buttons()		
		
	def set_buttons(self):
		self.button1 = ButtonNode('Panel1', parent=self.main_bg, position = (0, -self.size.h*0.1))
		self.button2 = ButtonNode('Panel2', parent=self.main_bg, position = (0,-self.size.h*0.2))
			
	def set_main_bg(self):
		bg_shape = ui.Path.rounded_rect(0, 0, self.size.w - 20, self.size.h - 20, 15)
		bg_shape.line_width = 5
		shadow = ((0, 0, 0, 0.35), 0, 0, 24)
		self.main_bg = ShapeNode(bg_shape, (1,1,1,0.9), '#15a4ff', shadow=shadow, parent=self)
		self.main_bg.position = self.size / 2
		
	def set_main_title(self):
		self.title_label = LabelNode(self.title, font=('Avenir Next', 36), color='black', position=(0, self.main_bg.size.h * 0.4), parent=self.main_bg)
		self.title_label.anchor_point = (0.5, 1)
	
	def touch_began(self, touch):
		touch_loc = self.main_bg.point_from_scene(touch.location)
		if touch_loc in self.button1.frame:
			self.button1.texture = Texture('pzl:Button2')
		if touch_loc in self.button2.frame:
			self.button2.texture = Texture('pzl:Button2')
	
	def touch_ended(self, touch):
		touch_loc = self.main_bg.point_from_scene(touch.location)
		self.button1.texture = Texture('pzl:Button1')
		self.button2.texture = Texture('pzl:Button1')
		if touch_loc in self.button1.frame:
			self.presenting_scene.show_panel1()
		if touch_loc in self.button2.frame:
			self.presenting_scene.show_panel2()	

[参考元]

よく読まれている記事

1

DeepFaceLab 2.0とは DeepFaceLab 2.0は機械学習を利用して動画の顔を入れ替えるツールです。 以前にDeepFaceLab 1.0を記事としてアップしていましたが、2.0は以 ...

2

自作PCで、多くのパーツをCorsair製品で揃えたので、iCUEでライティング制御していきました。 私のPCでは、表示されている4つのパーツが制御されています。ここで、HX750i電源ユニットは、L ...

3

コンピュータは有限桁の数値しか扱う事はできないので、桁数の多い場合や無限小数の場合は四捨五入され切り捨てられます。なので実際の数値とは多少の誤差が生じますが、これを丸め誤差といいます。 なので、コンピ ...

-Python