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.


    Frame for tetris

    Pythonista
    6
    60
    18106
    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

      @cvp @mikael sorry, I have another problem. I still can’t writing smth by myself🙂

      I tried here to add a block on the board, but it doesn’t show it. But prints the position

      from scene import *
      import random
      import arrows
      
      sw, sh = get_screen_size()
      rect_w = sw/3  #343.33
      rect_h = sh-100  #668
      side = int(rect_w/10)
      colors = ['red']
      
      class Board(ShapeNode):
      	def __init__(self, stroke_color='lightgrey', line_width=1, parent=None, *args, **kwargs):
      			path = ui.Path.rect(0, 0, rect_w, rect_h)
      			path.line_width = line_width
      			
      			if stroke_color == 'lightgrey':
      				d = int(rect_w/10)
      				for l in range(int(rect_w/d)):
      					x = l*d
      					path.move_to(x, 0)
      					path.line_to(x, rect_h)
      			
      			super().__init__(path,
      							 fill_color='white',
      							 stroke_color=stroke_color,
      							 parent=parent,
      							 *args, **kwargs)
      		
      
      
      class Game(Scene):
      	def setup(self):
      		self.background_color = 'white'
      		grey_rect = Board(line_width=2, parent=self, position=(sw/3, sh/2), z_position=0)
      		self.board = Board(stroke_color='purple', line_width=15, parent=self, position=(sw/3, sh/2), 	z_position=-1)
      		self.add_buttons()
      		self.add_figure()
      		
      	def add_buttons(self):
      		ars = arrows.Main()
      		self.present_modal_scene(ars)
      		
      	def add_figure(self):
      		x = random.choice(range(10)) * side
      		y = rect_h
      		block = SpriteNode('pzl:Yellow7', (x, y), z_position=1, size=Size(side, side), parent=self.board)
      		print(block.position)
      
      
      run(Game())  ```
      cvp 2 Replies Last reply Reply Quote 0
      • cvp
        cvp @Karina last edited by cvp

        @Karina try the gray_rect as parent for yours blocks because it is above the board

                self.grey_rect = Board(line_width=2, parent=self, position=(sw/3, sh/2), z_position=0) 
        

        and don't forget child position is versus the center of the parent node

            def add_figure(self):
                r = random.choice(range(10)) 
                x = r * side - rect_w/2 + side/2
                y = rect_h/2 - side/2
                block = SpriteNode('pzl:Yellow7', position=(x, y), size=Size(side, side), parent=self.grey_rect)
                
                #print(block.position)
        

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

          @Karina Here, if you let self.board as parent and use r = 10, you can see that your block is partially hidden by the grid

                  r = 10
                  x = r * side - rect_w/2 + side/2
                  y = rect_h/2 - side/2
                  block = SpriteNode('pzl:Yellow7', position=(x, y), size=Size(side, side), parent=self.board)#self.grey_rect)
          

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

            @cvp so the center is (0, 0)?

            cvp 2 Replies Last reply Reply Quote 0
            • cvp
              cvp @Karina last edited by

              @Karina said:

              so the center is (0, 0)?

              Yes

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

                @Karina read also the doc about anchor_point

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

                  @cvp did so that the block moves down, but for ⬅️➡️ my touch_began doesn’t work. I did this

                  def touch_began(self, touch):
                  		print('touch_began') 
                  

                  And nothing in the console

                  cvp 2 Replies Last reply Reply Quote 0
                  • cvp
                    cvp @Karina last edited by cvp

                    @Karina where did you insert your touch_began? I put it in the game class and print is ok

                    class Game(Scene):
                        def touch_began(self, touch):
                            print('touch_began') 
                        def setup(self): 
                    .
                    .
                    .```
                    1 Reply Last reply Reply Quote 0
                    • cvp
                      cvp @Karina last edited by

                      @Karina I want to add that, sincerely, I don't have any experience in scene module. I always have to try/test before that I can answer something. By example, for position, I've tried some values before to be able to answer something intelligent 😇

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

                        @cvp it’s in the game class that inherited scene. And update in the class he sees

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

                          from scene import *
                          import random
                          import arrows
                          
                          sw, sh = get_screen_size()
                          rect_w = sw/3  #343.33
                          rect_h = 612 #668
                          side = int(rect_w/10)
                          colors = ['red']
                          
                          
                          class Board(ShapeNode):
                          	def __init__(self, stroke_color='lightgrey', line_width=1, parent=None, *args, **kwargs):
                          			path = ui.Path.rect(0, 0, rect_w, rect_h)
                          			path.line_width = line_width
                          			
                          			if stroke_color == 'lightgrey':
                          				d = int(rect_w/10)
                          				for l in range(int(rect_w/d)):
                          					x = l*d
                          					path.move_to(x, 0)
                          					path.line_to(x, rect_h)
                          			
                          			super().__init__(path,
                          							 fill_color='white',
                          							 stroke_color=stroke_color,
                          							 parent=parent,
                          							 *args, **kwargs)
                          		
                          
                          
                          class Game(Scene):
                          	def setup(self):
                          		self.background_color = 'white'
                          		self.grey_rect = Board(line_width=2, parent=self, position=(sw/3, sh/2), z_position=0)
                          		self.board = Board(stroke_color='purple', line_width=15, parent=self, position=(sw/3, sh/2), 	z_position=-1)
                          		
                          		self.seconds = 0
                          		self.figures = {}
                          		self.add_buttons()
                          		self.add_figure()
                          		
                          	
                          	def update(self):
                          		self.seconds += self.dt
                          		if self.seconds > 0.5:
                          			for f in self.figures:
                          				self.figures[f][1] -= side/2
                          				f.position = self.figures[f]
                          				self.seconds = 0
                          				if f.position.y == - rect_h/2 + side/2:
                          					self.add_figure()
                          					self.figures.pop(f)
                          					
                          					
                          	def touch_began(self, touch):
                          		print('touch_began')
                          
                          
                          	def add_buttons(self):
                          		ars = arrows.Main()
                          		self.present_modal_scene(ars)
                          
                          				
                          	def add_figure(self):
                          		x = random.choice(range(10)) * side - rect_w/2 + side/2
                          		y = rect_h/2 - side/2
                          		block = SpriteNode('pzl:Yellow7', (x, y), z_position=1, size=Size(side, side), parent=self.grey_rect)
                          		self.figures[block] = [x, y]
                          
                          
                          run(Game())  
                          

                          Maybe you’ll check if it works?

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

                            You’re talking about that position in the beginnig, when I couldn’t move the purple rect from the corner?)

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

                              @Karina said:

                              Maybe you’ll check if it works?

                              I don't have the arrows module thus no button, but if I touch anywhere in game, it prints touch_began

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

                                And if with arrows? Maybe it looks on that touch_began

                                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
                                
                                right = left = down = up = None
                                
                                icons = {
                                	     'iob:arrow_down_b_256' : (sw()/4*3, 60),
                                	     'iob:arrow_up_b_256' : (sw()/4*3, bh() + 60),
                                	     'iob:arrow_left_b_256' : (sw()/4*3 - 95, bh()),
                                	     'iob:arrow_right_b_256' : (sw()/4*3 + 95, 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 = [Arrow(i, position=icons[i], parent=self) for i in icons] 
                                			
                                			
                                	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 = '#969696'
                                				if 'right' in arw.picture:
                                					global right
                                					right = True
                                				
                                			
                                	def touch_ended(self, touch):
                                		for arw in self.arrows:
                                			if arw.fill_color == '#969696':
                                				arw.fill_color = 'white'
                                				right = left = down = up = None
                                				
                                		
                                				
                                if __name__ == '__main__':				
                                	run(Main(), PORTRAIT) 
                                
                                cvp 2 Replies Last reply Reply Quote 0
                                • cvp
                                  cvp @Karina last edited by

                                  @Karina sorry, not at home, I’ll try later

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

                                    @Karina doc says for your

                                            ars = arrows.Main()
                                            self.present_modal_scene(ars) ```
                                    

                                    Doc says "Scene.present_modal_scene(other_scene)
                                    Present another scene on top of this one. This can be useful for overlay menus etc.
                                    While the scene is being presented, it receives all touch events."

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

                                      @Karina please try this

                                      from scene import *
                                      import random
                                      from  arrows import *
                                      
                                      sw, sh = get_screen_size()
                                      rect_w = sw/3  #343.33
                                      rect_h = 612 #668
                                      side = int(rect_w/10)
                                      colors = ['red']
                                      
                                      bw = 100
                                      bh = 100
                                      
                                      right = left = down = up = None
                                      
                                      icons = {
                                               'iob:arrow_down_b_256' : (sw/4*3, 60),
                                               'iob:arrow_up_b_256' : (sw/4*3, bh + 60),
                                               'iob:arrow_left_b_256' : (sw/4*3 - 95, bh),
                                               'iob:arrow_right_b_256' : (sw/4*3 + 95, bh)
                                                }
                                      
                                      
                                      class Board(ShapeNode):
                                          def __init__(self, stroke_color='lightgrey', line_width=1, parent=None, *args, **kwargs):
                                                  path = ui.Path.rect(0, 0, rect_w, rect_h)
                                                  path.line_width = line_width
                                                  
                                                  if stroke_color == 'lightgrey':
                                                      d = int(rect_w/10)
                                                      for l in range(int(rect_w/d)):
                                                          x = l*d
                                                          path.move_to(x, 0)
                                                          path.line_to(x, rect_h)
                                                  
                                                  super().__init__(path,
                                                                   fill_color='white',
                                                                   stroke_color=stroke_color,
                                                                   parent=parent,
                                                                   *args, **kwargs)
                                              
                                      
                                      
                                      class Game(Scene):
                                          def setup(self):
                                              self.background_color = 'white'
                                              self.grey_rect = Board(line_width=2, parent=self, position=(sw/3, sh/2), z_position=0)
                                              self.board = Board(stroke_color='purple', line_width=15, parent=self, position=(sw/3, sh/2),    z_position=-1)
                                              
                                              self.seconds = 0
                                              self.figures = {}
                                              self.add_buttons()
                                              self.add_figure()
                                              
                                          
                                          def update(self):
                                              self.seconds += self.dt
                                              if self.seconds > 0.5:
                                                  for f in self.figures:
                                                      self.figures[f][1] -= side/2
                                                      f.position = self.figures[f]
                                                      self.seconds = 0
                                                      if f.position.y == - rect_h/2 + side/2:
                                                          self.add_figure()
                                                          self.figures.pop(f)
                                                          
                                                          
                                              
                                          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 = '#969696'
                                                      if 'right' in arw.picture:
                                                          global right
                                                          right = True
                                                      
                                                  
                                          def touch_ended(self, touch):
                                              for arw in self.arrows:
                                                  if arw.fill_color == '#969696':
                                                      arw.fill_color = 'white'
                                                      right = left = down = up = None
                                                      
                                      
                                          def add_buttons(self):
                                              #ars = arrows.Main()
                                              #self.present_modal_scene(ars)
                                              self.arrows = [Arrow(i, position=icons[i], parent=self) for i in icons] 
                                      
                                                      
                                          def add_figure(self):
                                              x = random.choice(range(10)) * side - rect_w/2 + side/2
                                              y = rect_h/2 - side/2
                                              block = SpriteNode('pzl:Yellow7', (x, y), z_position=1, size=Size(side, side), parent=self.grey_rect)
                                              self.figures[block] = [x, y]
                                      
                                      
                                      run(Game())   
                                      
                                      1 Reply Last reply Reply Quote 0
                                      • Karina
                                        Karina last edited by

                                        @cvp So the Tetris didn’t receive touches?
                                        I thought to put the moving ➡️⬅️ into arrows, but it would be much more confusing

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

                                          @Karina said:

                                          I thought to put the moving ➡️⬅️ into arrows, but it would be much more confusing

                                          Sorry, I don't understand.

                                          The touch_began in Game is called. Then, you could check which arrow (like you do) and process the move of the block (left, right,down)

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

                                            @cvp I thought to write how to move blocks right and left in the touch_began of arrows. But no matter, you did it easier👍

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