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
-
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())
-
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.
-
Chris Wilson Sure bro , I appreciate it.
-
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
-
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!!!')
-
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)
-
Beautiful ccc I think I love you 😂 But for real thank you, you been a big help in my journey.
-
@ccc has been a big help on my journey too! :)
-
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
-
Got everything up and running , it was a indention problem.
-
@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)
-
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 ?
-
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?
-
@lance1994 I am a bit lost in the code snippets. Would it be possible to put a more complete codebase in a GitHub repo?