Forum Archive

Player Collision , Hitbox

lance1994

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")

abcabc

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())
chriswilson

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.

lance1994

Chris Wilson Sure bro , I appreciate it.

lance1994

@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

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!!!')
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

@ccc

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

chriswilson

@lance1994

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

lance1994

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

lance1994

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

abcabc

@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)
lance1994

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 ?

lance1994

@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?

ccc

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