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.
Random x and y coordinates
-
I’m trying to generate random x and y coordinates for a spritenode with scene.
from scene import * import sound import random import math A = Action #screen_size = (414.00, 896.00) class MyScene (Scene): def setup(self): self.bubble = SpriteNode('emj:Blue_Circle') self.x = random.randrange(410) self.y = random.randrange(890) self.bubble.position = (self.x, self.y) self.background_color = 'white' self.add_child(self.bubble) def next_location(): for x in range(9999): self.x = random.randrange(410) self.y = random.randrange(890) yield x, y #self.new_position = self.next_location() def did_change_size(self): pass def update(self): pass def touch_began(self, touch): if touch.location in self.bubble.frame: self.bubble.remove_from_parent() self.x = random.randrange(410) self.y = random.randrange(890) def touch_moved(self, touch): pass def touch_ended(self, touch): self.add_child(self.bubble) if __name__ == '__main__': run(MyScene(), show_fps=False)
-
Untested...
def random_location(self) -> ui.scene.Point: return ui.scene.Point(*(random.randrange(x) for x in self.size))
-
@ccc I’ll see how it works. Thanks.
-
@resserone13, note that in your code:
- self.x, self.y move the scene around, not your SpriteNode
- next_position is missing self
- if all you are returning is random numbers, function like @ccc’s is sufficient, the generator with yield seems to add no value
-
@mikael how would I be able to change the location of the sprite. I was trying to have a bubble pop up on the screen and once it’s tapped it pops up on another random place of the screen and can be tapped again and then pop up at another random place
Also. What does the —> and * do?
def random_location(self) -> ui.scene.Point: return ui.scene.Point(*(random.randrange(x) for x in self.size))
It was saying ui has no attribute scene when I tried it. I’m using 3.6
-
Please change
ui.scene.Point
to justPoint
(because the linefrom scene import *
pollutes the global namespace with all members of thescene
package which runs contrary to the advice of PEP8).The
->
is using Python type hints to declare what data type this function returns. https://docs.python.org/3/library/typing.htmlThe
*
says treat one object as several objects in an iterator. This is usually referred to as *args and **kwargs. -
@resserone13 just move self.bubble, not self, inside touch_began. And don't remove the bubble.
Or if there is some reason to actually spawn a new one, you would create it there, and assign it to self.bubble
-
@JonB we did it! Thanks. I was unaware that I wasn’t moving this sprite node. Now the bubble moves around and re spawns in random locations. I’m trying to create a game where bubbles randomly pop up on the screen and you have to tap them before the screen becomes completely full.
def touch_began(self, touch): if touch.location in self.bubble.frame: self.bubble.remove_from_parent() self.bubble.position = random.randrange(410), random.randrange(890) self.add_child(self.bubble)
-
In that case you will want a list of bubbles, and will need to cycle through the list to see which one was tapped.
-
@JonB I added a counter to the center of the screen to show how many tap have been made.
I have seen people use List To keep track of lasers in a game where a spaceship shoots lasers. I’m not sure how it works.
from scene import * import sound import random import math A = Action #screen_size = (414.00, 896.00) class MyScene (Scene): def setup(self): #backgound color self.background_color = 'white' #tap count self.tap_count = 0 #bubble sprite self.bubble = SpriteNode('emj:Blue_Circle') #bubble position self.bubble.position = random.randrange(440), random.randrange(880) #adds bubble to screen self.add_child(self.bubble) #amout of taps self.tap_amount_lable = LabelNode(f'{self.tap_count}', font = ('MarkerFelt-Wide', 150), position = self.size/2, color = 'black') #fades tap amount into background self.tap_amount_lable.alpha = .5 #adds tap amount to screen self.add_child(self.tap_amount_lable) def did_change_size(self): pass def update(self): self.tap_amount_lable.text = f'{self.tap_count}' def touch_began(self, touch): if touch.location in self.bubble.frame: self.bubble.remove_from_parent() self.bubble.position = random.randrange(410), random.randrange(890) self.add_child(self.bubble) #keep track of taps self.tap_count += 1 def touch_moved(self, touch): pass def touch_ended(self, touch): pass if __name__ == '__main__': run(MyScene(), show_fps=False)
-
@ccc ended up using.
self.bubble.position = random.randrange(410), random.randrange(890)
-
So what happens when you move your scene to a different device? iPad, iPad Mini, iPhone small screen, iPhone XL screen? Also, when you rotate the screen.
-
@resserone13, removing and then adding the bubble to the parent is unnecessary and makes no visual difference. Just change the position.
If you want a small delay between the bubble vanishing and popping up again, use Actions.
-
@ccc I’ve tried a few things but it keeps saying range expected a sequence.
Maybe this...
self.bubble.position = ((*(random.randrange(x) for x in self.size)), (*(random.randrange(x) for x in self.size)))
-
@mikael I had trouble with action. I have a card game I’m working on and I can’t really figure out action or sequence. I was trying to have the cards shuffle around. With . Move_to(). I can get to to move once but no more and I can’t figure out sequence
-
@resserone13, a little repeated code is not bad, if it makes things more understandable:
Try:
self.bubble.position = random.randrange(self.size.w), random.randrange(self.size.h)
-
@mikael thanks. That works and is simple. Can you show me an example of how to use actions and how to use sequence? Maybe make the bubble move twice each time it’s touched?
-
@resserone13, here’s an example, quickly adapted from the docs. To make progress on this, be sure to read at least all the intro bits of the scene module, and keep hitting the Help on the individual methods.
from random import randrange from scene import * class MyScene (Scene): bubble_radius = 40 bubble_diameter = bubble_radius * 2 def setup(self): self.background_color = 'midnightblue' self.bubble = SpriteNode('plf:HudCoin') self.bubble.position = self.size / 2 self.bubble.size = (self.bubble_diameter,) * 2 self.add_child(self.bubble) def touch_began(self, touch): # Hit detect if abs(self.bubble.position - touch.location) <= self.bubble_radius: new_position = randrange(self.size.w), randrange(self.size.h) self.bubble.run_action( Action.sequence( Action.fade_to(0, 0.3), Action.move_to(*new_position), Action.wait(0.1), Action.fade_to(1, 0.3), ) ) run(MyScene())
-
Look at the following scene action examples in
https://github.com/encela95dus/ios_pythonista_examplesFrom readme
scene_action1,2,3.py
textanimation.py
textanimation_wayy.py -
@enceladus thanks for the info. I have looked through some of this. Mainly the card game. A lot of good stuff. My issue Sometimes is understanding what I’m reading. I believe it’s good to read code and I try to read as much as I can and watch videos and figure out everything I can buy myself and then after that I’ll post here.