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.
Help me find a way to make the lasers fire in a certain direction.
-
I think I’ve finally done it! It works, as it shoots in the way I want it, and keeps on travelling even when I haven’t touched it, thanks.
All I have to do is add a action so I can’t spam or hold down on the button. What is the best way to add this? -
Also, I would also like to know how to code screen wrapping.
-
player.location.x %= screen.width player.location.y %= screen.height
-
Oh, thanks.
-
Wait, how do find the screen width and height? And what about location?
-
-
Re spamming, one approach is, in your button handling code, to keep track of self.t, and self.lastfire_time. When current time minus last fire time is > some value, reset the last fire time to current time, and fire a laser. Otherwise, pass
BTW, in your laser loop, you will need to check if the laser is off screen (not in self.bounds), then delete the object from the scene. Otherwise your scene will eventually bog down or crash because it is moving millions of laser objects.
This could also be done in your check_collisions method where you see if your lasers hit your enemies.
-
@cvp And what about location?
-
position
, not location.If you want to wrap, you need to fundamentally change your button logic, since you check limits
-
I have added this:
width, height = ui.get_screen_size()
And then added this later:
if self.Player.position.x <= 0 or self.Player.position.x >= 1112:
self.Player.position.x %= width
if self.Player.position.y <= 0 or self.Player.position.y >= 834:
self.Player.position.y %= height
This didn’t work. I checked if I had gotten the width and height variables in a seperate script, and I did. I am now stumped. -
What do you think your if statement does?
-
Whenever the Players position.x is equal to or smaller than 0 or whenever the position.x is equal to or greater than 1112, you do the remainder of the division of the screen height.
-
Post your button handling code again... You used to have checks that the new position was in the screen.
You actually don't need the if statements --
You could say player.position.x%=self.width, which will always cause it to wrap around. -
Here you go:
def handle_button_presses(self): rotationAdd = math.pi/180 self.Player.location = self.Player.position for touch in self.touches.values(): if touch.location in self.LeftCrtl.bbox: actions = [A.rotate_by(math.pi/180)] self.Player.rotation = self.Player.rotation + rotationAdd self.Player.run_action(A.sequence(actions)) if touch.location in self.RightCrtl.bbox: actions = [A.rotate_by(math.pi/-180)] self.Player.rotation = self.Player.rotation - rotationAdd self.Player.run_action(A.sequence(actions)) if touch.location in self.UpCrtl.bbox: direction = Vector2(-math.sin(self.Player.rotation), math.cos(self.Player.rotation)) self.Player.position += direction*3 if self.Player.position.x <= 0 or self.Player.position.x >= 1112: self.Player.position.x %= width if self.Player.position.y <= 0 or self.Player.position.y >= 834: self.Player.position.y %= height
I did change the movement code a little.
-
@BurntRice said:
if self.Player.position.x <= 0 or self.Player.position.x >= 1112: self.Player.position.x %= width if self.Player.position.y <= 0 or self.Player.position.y >= 834: self.Player.position.y %= height
@JonB said:
You actually don't need the if statements
self.Player.position.x %= width self.Player.position.y %= height
-
Is width an height actually defined someplace?
What isn't working in your code? Is your player rotating and moving as expected? Is only the wrapping broken?
-
Only the wrapping.
-
width, height = ui.get_screen_size()
I think this is right. I did a seperate script where I print it out inside the console, and it worked. -
I tried placing it self.update, but then it wouldn’t work.
-
Can we please get the code full listing? Please use the
</>
icon above the post edit box to paste your code so it is formatted for this forum.
```
Insert Code Here
```