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.


    How to use scene.Rect?

    Pythonista
    rect scene
    3
    14
    9239
    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.
    • Bjucha
      Bjucha last edited by ccc

      Hello. I have just started using scene and followed the tutorial at: http://omz-software.com/pythonista/docs/ios/scene.html#introduction.

      I have created two SpriteNodes one is a spaceship and the other is a rock. And Im able to move the ship using the touch_began method.

      So now I can move the ship over the screen but I would like something to happen to the ship if it touches the rock. I have looked at the game tutorial in pythonista and from the forth tutorial part there is something called "scene.Rect" used for that. I have tried using it in my scene and it only works if the ship is on the lower part of the screen. So my question is:
      What should I use for my "ship" that moves all over the scene

      Sorry for the long post.

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

        I would like something to happen to the ship if it touches the rock.

        This is called collision detection... https://forum.omz-software.com/topic/3471/player-collision-hitbox/5

        Bjucha 2 Replies Last reply Reply Quote 0
        • Bjucha
          Bjucha @ccc last edited by

          @ccc great! I will have a look at it, thank you

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

            @ccc really greatful for your help, it's working now, but the game ends even when the rock is not really touching the ship (Guessing it has something to do with the triangular shape of the ship)If im guessing correct this is where I should use the Rect method

            The code is like this (borrowed much from the game tutorial)

            from scene import *
            import random
            A = Action
            class Rock (SpriteNode):
            	def __init__(self, **kwargs):
            		
            		SpriteNode.__init__(self, 'spc:MeteorBrownSmall1', **kwargs)
            	
            
            class Gamefield (Scene):
            	def setup (self):
            		self.items = []
            		self.background_color = '#0c1037'
            		score_font = ('Futura', 30)
            		self.score_label = LabelNode('0', score_font, parent=self)
            		self.score_label.position = (20,1000)
            		self.score_label.z_position = 1
            		self.score = 0
            		
            		
            		self.ship = SpriteNode('spc:PlayerShip3Red')
            		self.ship.position = (100,40)
            		self.add_child(self.ship) 
            
            	def touch_began(self, touch):
            		x, y = touch.location
            		move_action = Action.move_to(x, y , 0.9, TIMING_SINODIAL)
            		self.ship.run_action(move_action)
            	
            
            	
            	
            	def update(self):
            	
            		self.check_item_collisions()
            		
            		if random.random() < 0.006:
            			self.spawn_item()
            			
            	def spawn_item(self):
            	
            		rock = Rock(parent=self)
            		rock.position = (random.uniform(10, self.size.w-10), self.size.h + 30) 
            		
            		d = random.uniform(2.0, 19.0) 
            		
            		actions = [A.move_by(0, -(self.size.h + 60), d), A.remove()]
            		rock.run_action(A.sequence(actions))
            		self.items.append(rock)
            		self.score += 1
            		self.score_label.text = str(self.score)
            		
            
            	def check_item_collisions(self):
            	
            		
            		for item in list(self.items):
            			if item.frame.intersects(self.ship.frame): 
            				exit()
            					
            
            	
            
            run(Gamefield())```
            1 Reply Last reply Reply Quote 0
            • omz
              omz last edited by

              Circle-to-circle collision detection is also quite easy to implement, and may be more suitable for spaceships/rocks. Just an idea...

              Bjucha 1 Reply Last reply Reply Quote 0
              • Bjucha
                Bjucha @omz last edited by

                @omz can you give me an example?
                I understand what you mean but not how to do it

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

                  @Bjucha Imagine a circle drawn around the center of the space ship and the rock. When the circles intersect, there is a collision. To check whether there is a collision, you simply have to calculate the distance between the center (position) of the space ship and the rock. If that distance is smaller than the radius of both circles combined, you have a collision.

                  Calculating the distance between two points is quite easy btw: distance = abs(pos1 - pos2)

                  Bjucha 1 Reply Last reply Reply Quote 1
                  • Bjucha
                    Bjucha @omz last edited by

                    @omz ok thanks! Gonna try it tonight

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

                      @Bjucha Good luck! Btw, it can make sense to use circles that are a bit smaller than the actual object. This way, your collision detection won't feel "unfair" in some cases.

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

                        @omz really really greatful for your help, however Im lacking skill and knowledge to make this work for now...

                        But I looked at your game tutorial and found this

                        if item.position in self.ship.frame: 
                        				exit()
                        

                        That is working fine, not perfect but better then before
                        Thank you for your game tutorial it has taught me many things

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

                          distance = abs(item.position - self.ship.frame.center())
                          if distance < 20:
                              exit()
                          
                          Bjucha 1 Reply Last reply Reply Quote 0
                          • Bjucha
                            Bjucha @ccc last edited by

                            @ccc thank you that worked really good for me. Love all the help from all of you

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

                              We just wanna play the game.

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

                                @ccc I promise that as soon as SpaceGalaxy (Name temporary, might be changed) is finished
                                You will have it

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