omz:forum

    • Register
    • Login
    • Search
    • Recent
    • Popular

    Welcome!

    This is the community forum for my apps Pythonista and Editorial.

    For individual support questions, you can also send an email. If you have a very short question or just want to say hello — I'm @olemoritz on Twitter.


    [SPACE ESCAPE]-Game example to help with Game dev

    Pythonista
    game dev
    6
    133
    50257
    Loading More Posts
    • Oldest to Newest
    • Newest to Oldest
    • Most Votes
    Reply
    • Reply as topic
    Log in to reply
    This topic has been deleted. Only users with topic management privileges can see it.
    • Karina
      Karina last edited by

      Now it's like that

      import scene
      import sound
      import random
      import objc_util
      
      sw = scene.get_screen_size()[0]
      sh = scene.get_screen_size()[1]
      
      piano_sounds = ['piano:A3', 'piano:C3', 'piano:C4#', 'piano:D4', 'piano:E4', 'piano:F4', 'piano:G3#']
      guitar_sounds = ['8ve:8ve-beep-warmguitar']
      drum_sounds = ['drums:Drums_01', 'drums:Drums_04', 'drums:Drums_07', 'drums:Drums_10', 'drums:Drums_13', 'drums:Drums_16']
      
      class ButtonNode(scene.ShapeNode):
          def __init__(self,
                          action,
                          icon,
                          text_color,
                          sounds,
                          corner_radius=8,
                          border_size=20,
                          text='',
                          name=f'ButtonNode',
                          bg_color='lightgrey',
                          anchor_point=(0.5, 0.5),
                          borderColor=None,
                          parent=None,
                          position=(0, 0),
                          size=(120, 45),
                          enabled=True,
                          animated_icon=False,
                          icon_animation=None,
                          *args, **kwargs):
      
              # these mainly for call to super()
              self.x, self.y = position
              self.w, self.h = size
              super().__init__(
              	    path=scene.ui.Path.rounded_rect(self.x, self.y, self.w, self.h, corner_radius),
                      fill_color=bg_color,
                      stroke_color=borderColor,
                      shadow=None,
                      parent=parent,
                      *args, **kwargs)
              
              # Normal Properties for Instance()
              self.sounds=sounds
              self.enabled=enabled
              self.button_action=action
              self.name=name
              self.position=position
              self.size=size
              self.anchor_point=anchor_point
              
               # for border
              self.border_size=border_size
              self.borderColor=bg_color
              self.corner_radius=corner_radius
              
              # for icon
              self.icon_animation=icon_animation
              self.animated_icon=animated_icon
              self.icon=self._init_textures(icon)
              
              # for Label
              self.text=text 
              self.text_color=text_color
              
              # Container to hold each component. 
              # is just a dict version of self.children but specific.
              self.components=dict({
                      'icon':None,
                      'label':None})
              
              self._setup(self.icon, self.components)
      
          # Type Check to make sure img is a string or ui.Image
          def _init_textures(self, img):
              if type(img) == str or type(img) == scene.ui.Image:
                  return scene.Texture(img)
              else:
                  return None
      
          # setup our Components
          def _setup(self, i, c):
              if i != None:
                  # button image
                  c['icon']=scene.SpriteNode(
                          texture=i,
                          size=scene.Size(self.size[1]/100*80, self.size[1]/100*80), 
                          position=scene.Point(0, 0),                                
                          parent=self,
                          anchor_point=(0.5, 0.5),
                          z_position=9)
                          
              if self.text:
                  # button text..
                  c['label']=scene.LabelNode(
                          text=self.text,
                          position=scene.Point(0 , 0),
                          anchor_point=(0.5, 0.5),
                          color=self.text_color,
                          parent=self,
                          z_position=10)
          
          # called when you tap the button
          def Button_Tapped(self):
          	if self.components['icon']:
          		if self.animated_icon and self.icon_animation:
          			self.components['icon'].run_action(self.icon_animation())
          	if self.enabled:
          		self.button_action(self)
      
      # custom action
      def my_button_action(sender):
          random_tone=random.choice(piano_sounds)
          play = random.choice(sender.sounds)
          sound.play_effect(play)
          return
      
      
      def Animation_Shake(duration=1.5):
          action_list=[]
          action_list.append(
                  scene.Action.rotate_to(0.25, duration/10/2))
          action_list.append(
                  scene.Action.sequence(
                      scene.Action.rotate_to(-0.5, duration/50),
                      scene.Action.rotate_to(0.5, duration/50)))
          action_list.append(
              scene.Action.group(
                  scene.Action.scale_to(1.0, duration/10/2),
                  scene.Action.rotate_to(0.0, duration/10/2)))
          return scene.Action.sequence(action_list)
      
      
      def Animation_Pulse(duration=1.5):
          action_list=[]
          action_list.append(
          	scene.Action.sequence(
                  scene.Action.scale_to(1.2, duration/10/2),
                  scene.Action.scale_to(0.5, duration/10/2)))
          action_list.append(
                      scene.Action.scale_to(1.2, duration/50))
          action_list.append(
              scene.Action.group(
                  scene.Action.scale_to(1.0, duration/10/2)))
          
          return scene.Action.sequence(action_list)
      
      def Animation_Jump(duration=1.5):
          action_list=[]
          action_list.append(
                  scene.Action.sequence(
                      scene.Action.move_by(0, 20, duration/20),
                      scene.Action.move_to(0, 0, duration/20)))
                  
          return scene.Action.sequence(action_list)
      
      class main(scene.Scene):
          def setup(self):
              self.buttons=list([])
              self.background_color='lightgray'
              
              self.guitar=ButtonNode(size=scene.Size(160, 112), icon='emj:Guitar',
                  text=None, text_color=self.background_color, parent=self, action=my_button_action, sounds=guitar_sounds,
                  position=scene.Point(sw/6*1.5, sh/3), animated_icon=True,
                  icon_animation=Animation_Shake)
              self.buttons.append(self.guitar)
              
              self.my_button_pulse=ButtonNode(size=scene.Size(100, 70), icon='emj:Musical_Notes',
                  sounds=drum_sounds, text=None, text_color=self.background_color, parent=self, 
                  action=my_button_action, position=scene.Point(sw/6*3, sh/2), 
                  animated_icon=True, icon_animation=Animation_Pulse)
              self.buttons.append(self.my_button_pulse)
              
              self.my_button_jump=ButtonNode(size=scene.Size(100, 70), icon='emj:Musical_Keyboard',
                  text=None, text_color=self.background_color, parent=self, action=my_button_action, sounds=piano_sounds,
                  position=scene.Point(sw/6*5, sh/2), animated_icon=True,
                  icon_animation=Animation_Jump)
              self.buttons.append(self.my_button_jump)
              
              self.quiter=ButtonNode(size=scene.Size(32, 32),
                  text='X', text_color='black', parent=self, action=self.quit, sounds=None,
                  position=scene.Point(sw-50, sh-50), borderColor='black', icon=None)
              self.buttons.append(self.quiter)
          
          def quit(self, sender):
              self.view.close()
          
          def touch_began(self, touch):
              for btn in self.buttons:
                  if self.point_from_scene(touch.location) in btn.frame:
                      btn.Button_Tapped()
      
      scene.run(main())
      
      1 Reply Last reply Reply Quote 0
      • stephen
        stephen last edited by

        @Karina

        Very well done!

        1 Reply Last reply Reply Quote 0
        • Bumbo Cactoni
          Bumbo Cactoni last edited by

          I have noticed a slight error in the game. Occasionally, I will be playing the game, and face a solid wall of gray asteroids. How do I avoid the wall? I can’t. Please, fix this?

          stephen 1 Reply Last reply Reply Quote 0
          • stephen
            stephen @Bumbo Cactoni last edited by

            @Bumbo-Cactoni said:

            I have noticed a slight error in the game. Occasionally, I will be playing the game, and face a solid wall of gray asteroids. How do I avoid the wall? I can’t. Please, fix this?

            There should always be a gap. What I think is happening here is the gap is being placed above the View Frame and we just cannot see it. I ran some test after changing the following inside Brush class

            
            def __init__(self, game):
            		self.game=game
            		self.crateMin=200
            		self.crateMax=780
            		self.gaps=list()
            		self.gapSize=4
            		self.gapMin=4
            		self.gapMax=6
            		self.gameObjects=self.game.gameObjects
            		self.startPos=Point(Screen.Width()+128, self.gameObjects.Floor)
            		self.ShieldBoost()
            
            

            i did not see this happen again but there could still be a chance somehow. all you need to do is limit the hieght for spawning the objects. you could also do a last check to insure there is a gap pressent before returning.

            1 Reply Last reply Reply Quote 0
            • Bumbo Cactoni
              Bumbo Cactoni last edited by Bumbo Cactoni

              @stephen
              When I tried to run the code with your updated script, it gave me this error:

              TypeError
              __init__() got an unexpected keyword argument ‘isObsticle’

              1 Reply Last reply Reply Quote 0
              • Karina
                Karina last edited by

                @stephen here’s I did something relying on your code, it’s very similar to the music buttons

                from scene import *
                import sound
                
                
                def sw(): return get_screen_size()[0]
                def sh(): return get_screen_size()[1]
                def bw(): return 100
                def bh(): return 100
                
                icons = {
                	     'iob:arrow_down_b_256' : (sw()/2, 60),
                	     'iob:arrow_up_b_256' : (sw()/2, bh() + 60),
                	     'iob:arrow_left_b_256' : (sw()/2 - bw(), bh()),
                	     'iob:arrow_right_b_256' : (sw()/2 + bw(), bh())
                	      }
                
                
                class Arrow(ShapeNode):
                	def __init__(self,
                				 picture,
                				 path=None,
                	             size=Size(120, 120),
                	             corner_radius=8,
                	             border_size=20,
                	             borderColor='#3f0917',
                	             position=(0,0),
                	             parent=None,
                	             *args, **kwargs):
                	             	
                	             	#for border
                	             	self.picture = picture
                	             	self.corner_radius = corner_radius
                	             	self.border_size = border_size
                	             	self.borderColor = borderColor
                	             	
                	             	self.position = position
                	             	self.size = size
                	             	
                	             	#for super()
                	             	self.x, self.y = position
                	             	self.w, self.h = size
                	             	
                	             	super().__init__(fill_color='white',
                	             					path=ui.Path.rounded_rect(self.x, 
                	             											   self.y,
                	             											   self.w/1.5, 
                	             											   self.h/1.5,
                	             											   self.corner_radius),
                	             					stroke_color=borderColor,
                	             					parent=parent,
                	             					*args, **kwargs)
                	             					
                	             	self._setup(self.picture)
                	             	                 
                	def _setup(self, pict):
                		if self.picture:
                			arrow = SpriteNode(self.picture,
                							   position=Point(0, 0), 
                							   size=(100, 100),
                							   parent=self)
                	             	                 
                
                class Main(Scene):            	                 
                	def setup(self):
                		fill_color = self.background_color
                		self.background_color = 'white'
                		self.arrows = []
                		for i in icons:
                			arw = Arrow(picture=i, position=icons[i], parent=self)
                			self.arrows.append(arw)
                			
                			
                	
                	def touch_began(self, touch):
                		tapped = True
                		for arw in self.arrows:
                			if touch.location in arw.frame:
                				sound.play_effect('rpg:Chop')
                				arw.fill_color = '#b2b2b2'
                				
                			
                	def touch_ended(self, touch):
                		for arw in self.arrows:
                			if arw.fill_color == '#b2b2b2':
                				arw.fill_color = 'white'
                				
                		
                				
                				
                run(Main(), PORTRAIT) 
                

                I think it will be a base for Tetris

                1 Reply Last reply Reply Quote 0
                • ccc
                  ccc last edited by

                  https://docs.python.org/3/library/functions.html#enumerate is your friend.

                          self.arrows = []
                          for i in icons:
                              arw = Arrow(picture=i, position=icons[i], parent=self)
                              self.arrows.append(arw)
                  
                  # -->
                  
                          self.arrows = [Arrow(picture=i, position=icon, parent=self)
                                         for i, icon in enumerate(icons)]
                  
                  1 Reply Last reply Reply Quote 0
                  • Karina
                    Karina last edited by

                    @cvp with enumerate doesn’t work cause with this for i, icon in enumerate(icons) you get keys of icons and nums 0, 1, 2. So i is the number and icon picture
                    I did the same but without enumerate

                    self.arrows = [Arrow(i, position=icons[i], parent=self) for i in icons] 
                    

                    Looks better than it was before👍

                    cvp 1 Reply Last reply Reply Quote 1
                    • cvp
                      cvp @Karina last edited by

                      @Karina I suppose that you wanted to answer to @ccc 😀

                      1 Reply Last reply Reply Quote 0
                      • Karina
                        Karina last edited by

                        @cvp yes you’re right 😅

                        1 Reply Last reply Reply Quote 0
                        • First post
                          Last post
                        Powered by NodeBB Forums | Contributors