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?
-
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 sceneSorry for the long post.
-
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
-
@ccc great! I will have a look at it, thank you
-
@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())```
-
Circle-to-circle collision detection is also quite easy to implement, and may be more suitable for spaceships/rocks. Just an idea...
-
@omz can you give me an example?
I understand what you mean but not how to do it -
@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)
-
@omz ok thanks! Gonna try it tonight
-
@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.
-
@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 -
distance = abs(item.position - self.ship.frame.center()) if distance < 20: exit()
-
@ccc thank you that worked really good for me. Love all the help from all of you
-
We just wanna play the game.
-
@ccc I promise that as soon as SpaceGalaxy (Name temporary, might be changed) is finished
You will have it