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.


    Player Collision , Hitbox

    Pythonista
    development game
    4
    15
    10826
    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.
    • lance1994
      lance1994 last edited by

      Is there a way I can make the Rect around my player a color for debugging purposes I'm fairly new , and I'm sure this is fairly simple but I tried fill_color, set_color nothing worked. Here is the code

      player_hitbox = Rect(self.player.position.x - 20, 32, 40, 65,fill_color = "Blur")

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

        May be you can make a new image with suitable background using ui.ImageContext See the example below.

        import scene, ui
        
        class MyScene(scene.Scene):
            def setup(self): 
                img = ui.Image.named('plf:AlienGreen_front')       
                w, h = img.size
                with ui.ImageContext(w, h) as ctx:
                    ui.set_color('gray')
                    ui.Path.rect(0, 0, w, h).fill()
                    img.draw(0,0,w,h)
                    img1 = ctx.get_image()
                self.sprite_node = scene.SpriteNode(scene.Texture(img1),
                    position=self.size/2, parent=self)
        
        scene.run(MyScene())
        
        lance1994 1 Reply Last reply Reply Quote 0
        • chriswilson
          chriswilson last edited by

          The hitbox you created is just a Rect() object, which is simply coordinates and not actually something rendered on the screen. You could make a Shapenode() or SpriteNode() of the same size which would be visible.

          I'm not at my computer just now, but can show some code later if you like.

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

            Chris Wilson Sure bro , I appreciate it.

            1 Reply Last reply Reply Quote 0
            • lance1994
              lance1994 @abcabc last edited by

              @abcabc

              I dont understand how ai would implement this into my game , but the concept is what I'm looking for being able to see my players/enemy hit box. but I want to implement this with out going thru ui , there has to be a easier way. 🤔 I'm currently trying many different options

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

                Collision: If Player is a SpriteNode and Bullet is a SpriteNode then in Player.update() do something like:

                    update(self):
                        if self.frame.intersects(bullet.frame):
                            exit('Player has died of rapid onset lead poisoning!!!')
                
                1 Reply Last reply Reply Quote 0
                • ccc
                  ccc last edited by ccc

                  A SpriteNode contained in a ShapeNode...

                  from scene import *
                  import ui
                  
                  class MyScene (Scene):
                      def setup(self):
                          sprite = SpriteNode('plf:HudPlayer_yellow')
                          self.player = ShapeNode(parent=self, path=ui.Path.rect(*sprite.frame),
                                                  position=self.size/2, fill_color='clear', stroke_color='red')
                          self.player.add_child(sprite)
                  
                  if __name__ == '__main__':
                      run(MyScene(), show_fps=False)
                  
                  lance1994 1 Reply Last reply Reply Quote 1
                  • lance1994
                    lance1994 @ccc last edited by

                    @ccc

                    Beautiful ccc I think I love you 😂 But for real thank you, you been a big help in my journey.

                    chriswilson 1 Reply Last reply Reply Quote 0
                    • chriswilson
                      chriswilson @lance1994 last edited by

                      @lance1994

                      @ccc has been a big help on my journey too! :)

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

                        Okay guys so everything works but now my touch movement for my Sprite isn't responding here's the original code before I implemented your code ,

                        		x, _= touch.location
                        		_, y = self.player.position
                        		move_action = Action.move_to(x,y,2,TIMING_SINODIAL)
                        		self.player.run_action(move_action)
                        		if (x,_ > 0.05):
                        			self.player.x_scale = cmp(touch.location.x -
                        	     self.player.position.x, 0)
                        			x = self.player.position.x
                        

                        Do I switch it to self.sprite.position

                        Hmm

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

                          Got everything up and running , it was a indention problem.

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

                            @lance1994 it is good that your problem got solved. Here are some notes about imagecontext and I hope it is useful.

                            ui.Path, ui.Image and ui.mageContext are general purpose routines (not like ui.View) and they are also heavily used in scene programs. ShapeNode has node only few types of shapes and if you need shapes like polygon (triangle, pentagon etc.) you may need to use ImageContext. Here is @ccc 's example, coded using ImageContext.

                            import scene
                            import ui
                            
                            class MyScene (scene.Scene):
                                def setup(self):
                                    img = ui.Image.named('plf:HudPlayer_yellow')       
                                    w, h = img.size
                                    with ui.ImageContext(w, h) as ctx:
                                        img.draw(0,0,w,h)
                                        path = ui.Path.rect(0, 0, w,h)
                                        ui.set_color('red')
                                        path.stroke()
                                        img1 = ctx.get_image()
                                    self.sprite_node = scene.SpriteNode(scene.Texture(img1), position=self.size/2, parent=self)
                                    # use img for regular game and img1 for debugging purpose
                                    #self.sprite_node = scene.SpriteNode(scene.Texture(img), position=self.size/2, parent=self)
                            
                            if __name__ == '__main__':
                                scene.run(MyScene(), show_fps=False)
                            
                            1 Reply Last reply Reply Quote 0
                            • lance1994
                              lance1994 last edited by omz

                              Okay Guys Something New Here Is The Code

                              class Rock(SpriteNode):
                               def __init__(self, **kwargs):
                              		SpriteNode.__init__(self, 'IMG_1726.GIF', **kwargs)
                              
                              def update (self): 
                               
                               self.spawn_rock 
                              
                              
                              
                              class Rock(SpriteNode):
                               def __init__(self, **kwargs):
                              		SpriteNode.__init__(self, 'IMG_1726.GIF', **kwargs)
                              

                              It does what it's suppose to do spawn the rocks, but I want it to spawn 3 rocks at a time. After each rock is destroyed it is randomly generated somewhere else. Not immediately but like a 10 second duration. I tried if and else statements in the update but nothing seems to work , going to keep poking around any input ?

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

                                @ccc

                                For the collision I still had to set a player_hitbox Rect() for it to intersect with the rock , if you look above you'll see my rock code . I tried the

                                self.frame.intersects(item.frame)

                                But that doesn't work any idea guys?

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

                                  @lance1994 I am a bit lost in the code snippets. Would it be possible to put a more complete codebase in a GitHub repo?

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