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.


    Random x and y coordinates

    Pythonista
    coordinates scene
    5
    21
    5452
    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.
    • ccc
      ccc last edited by ccc

      Please change ui.scene.Point to just Point (because the line from scene import * pollutes the global namespace with all members of the scene package which runs contrary to the advice of PEP8).

      The -> is using Python type hints to declare what data type this function returns. https://docs.python.org/3/library/typing.html

      The * says treat one object as several objects in an iterator. This is usually referred to as *args and **kwargs.

      resserone13 1 Reply Last reply Reply Quote 0
      • JonB
        JonB @resserone13 last edited by

        @resserone13 just move self.bubble, not self, inside touch_began. And don't remove the bubble.

        Or if there is some reason to actually spawn a new one, you would create it there, and assign it to self.bubble

        resserone13 1 Reply Last reply Reply Quote 0
        • resserone13
          resserone13 @JonB last edited by resserone13

          @JonB we did it! Thanks. I was unaware that I wasn’t moving this sprite node. Now the bubble moves around and re spawns in random locations. I’m trying to create a game where bubbles randomly pop up on the screen and you have to tap them before the screen becomes completely full.

          
          	def touch_began(self, touch):
          		if touch.location in self.bubble.frame:
          			self.bubble.remove_from_parent()
          			self.bubble.position = random.randrange(410), random.randrange(890)
          			self.add_child(self.bubble)
          
          
          1 Reply Last reply Reply Quote 0
          • JonB
            JonB last edited by

            In that case you will want a list of bubbles, and will need to cycle through the list to see which one was tapped.

            resserone13 1 Reply Last reply Reply Quote 0
            • resserone13
              resserone13 @JonB last edited by

              @JonB I added a counter to the center of the screen to show how many tap have been made.

              I have seen people use List To keep track of lasers in a game where a spaceship shoots lasers. I’m not sure how it works.

              
              from scene import *
              import sound
              import random
              import math
              A = Action
              
              #screen_size = (414.00, 896.00)
              
              class MyScene (Scene):
              	def setup(self):
              		
              		#backgound color
              		self.background_color = 'white'
              		
              		#tap count
              		self.tap_count = 0
              		
              		#bubble sprite
              		self.bubble = SpriteNode('emj:Blue_Circle')
              		
              		#bubble position
              		self.bubble.position = random.randrange(440), random.randrange(880)
              
              		#adds bubble to screen
              		self.add_child(self.bubble)
              		
              		#amout of taps
              		self.tap_amount_lable = LabelNode(f'{self.tap_count}', font = ('MarkerFelt-Wide', 150), position = self.size/2, color = 'black')
              		
              		#fades tap amount into background
              		self.tap_amount_lable.alpha = .5
              		
              		#adds tap amount to screen
              		self.add_child(self.tap_amount_lable)
              			
              	def did_change_size(self):
              		pass
              	
              	def update(self):
              		self.tap_amount_lable.text = f'{self.tap_count}'
              
              	
              	def touch_began(self, touch):
              		if touch.location in self.bubble.frame:
              			self.bubble.remove_from_parent()
              			self.bubble.position = random.randrange(410), random.randrange(890)
              			self.add_child(self.bubble)
              			
              			#keep track of taps
              			self.tap_count += 1
              	
              
              
              	
              	def touch_moved(self, touch):
              		pass
              	
              	def touch_ended(self, touch):
              		pass
              
              
              
              
              
              		
              
              
              if __name__ == '__main__':
              	run(MyScene(), show_fps=False)
              
              
              mikael 1 Reply Last reply Reply Quote 0
              • resserone13
                resserone13 @ccc last edited by resserone13

                @ccc ended up using.

                
                self.bubble.position = random.randrange(410), random.randrange(890)
                
                
                1 Reply Last reply Reply Quote 0
                • ccc
                  ccc last edited by ccc

                  So what happens when you move your scene to a different device? iPad, iPad Mini, iPhone small screen, iPhone XL screen? Also, when you rotate the screen.

                  resserone13 2 Replies Last reply Reply Quote 0
                  • mikael
                    mikael @resserone13 last edited by

                    @resserone13, removing and then adding the bubble to the parent is unnecessary and makes no visual difference. Just change the position.

                    If you want a small delay between the bubble vanishing and popping up again, use Actions.

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

                      @ccc I’ve tried a few things but it keeps saying range expected a sequence.

                      Maybe this...

                      
                      			self.bubble.position = ((*(random.randrange(x) for x in self.size)),
                      			(*(random.randrange(x) for x in self.size)))
                      
                      
                      mikael 1 Reply Last reply Reply Quote 0
                      • resserone13
                        resserone13 @mikael last edited by

                        @mikael I had trouble with action. I have a card game I’m working on and I can’t really figure out action or sequence. I was trying to have the cards shuffle around. With . Move_to(). I can get to to move once but no more and I can’t figure out sequence

                        1 Reply Last reply Reply Quote 0
                        • mikael
                          mikael @resserone13 last edited by mikael

                          @resserone13, a little repeated code is not bad, if it makes things more understandable:

                          Try:

                          self.bubble.position = random.randrange(self.size.w), random.randrange(self.size.h)
                          
                          resserone13 1 Reply Last reply Reply Quote 0
                          • resserone13
                            resserone13 @mikael last edited by

                            @mikael thanks. That works and is simple. Can you show me an example of how to use actions and how to use sequence? Maybe make the bubble move twice each time it’s touched?

                            mikael 1 Reply Last reply Reply Quote 0
                            • mikael
                              mikael @resserone13 last edited by

                              @resserone13, here’s an example, quickly adapted from the docs. To make progress on this, be sure to read at least all the intro bits of the scene module, and keep hitting the Help on the individual methods.

                              from random import randrange
                              
                              from scene import *
                              
                              
                              class MyScene (Scene):
                                  
                                  bubble_radius = 40
                                  bubble_diameter = bubble_radius * 2
                                  
                                  def setup(self):
                                      self.background_color = 'midnightblue'
                                      self.bubble = SpriteNode('plf:HudCoin')
                                      self.bubble.position = self.size / 2
                                      self.bubble.size = (self.bubble_diameter,) * 2
                                      self.add_child(self.bubble)
                              
                                  def touch_began(self, touch):
                                      # Hit detect
                                      if abs(self.bubble.position - touch.location) <= self.bubble_radius:
                                          new_position = randrange(self.size.w), randrange(self.size.h)
                                          self.bubble.run_action(
                                              Action.sequence(
                                                  Action.fade_to(0, 0.3),
                                                  Action.move_to(*new_position),
                                                  Action.wait(0.1),
                                                  Action.fade_to(1, 0.3),
                                              )
                                          )
                              
                              
                              run(MyScene())
                              
                              1 Reply Last reply Reply Quote 0
                              • enceladus
                                enceladus last edited by

                                Look at the following scene action examples in
                                https://github.com/encela95dus/ios_pythonista_examples

                                From readme
                                scene_action1,2,3.py
                                textanimation.py
                                textanimation_wayy.py

                                resserone13 1 Reply Last reply Reply Quote 0
                                • resserone13
                                  resserone13 @enceladus last edited by

                                  @enceladus thanks for the info. I have looked through some of this. Mainly the card game. A lot of good stuff. My issue Sometimes is understanding what I’m reading. I believe it’s good to read code and I try to read as much as I can and watch videos and figure out everything I can buy myself and then after that I’ll post here.

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

                                    @ccc would something like this be a good idea?

                                    
                                    position = (self.size.w/2, self.size.h * 1.60)
                                    
                                    

                                    If not would be so kind to so me the proper way.

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