omz:forum

    • Register
    • Login
    • Search
    • Recent
    • Popular

    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.

    Pythonista
    7
    66
    17004
    Loading More Posts
    • Oldest to Newest
    • Newest to Oldest
    • Most Votes
    Reply
    • Reply as topic
    Log in to reply
    This topic has been deleted. Only users with topic management privileges can see it.
    • JonB
      JonB last edited by

      for l in self.lasers:
                      l.position += direction*75
      

      That is the code that moves a laser beam, right?

      In words, can you explain under what conditions this code runs?

      Does that code execute EVERY time update is called, or is there some logic that controls when it will run?

      Hint, look above it for any for loops or if statements.

      1 Reply Last reply Reply Quote 0
      • arsartindustry
        arsartindustry last edited by

        This post is deleted!
        1 Reply Last reply Reply Quote 0
        • BurntRice
          BurntRice last edited by

          The code runs whenever the laser button detects that someone has touched it.

          JonB 1 Reply Last reply Reply Quote 0
          • JonB
            JonB @BurntRice last edited by

            @BurntRice but you told me that you want lasers to MOVE even when someone is not currently touching the button.

            So, you must move the MOVE code out from inside the for touch in self.touches loop.

            Your code has two parts:

            • Create a new laser object when person touches the button
            • Move all of the existing lasers

            The first part should only happen when user is touching the button.

            The second part should happen always.

            Erase your for l in self.lasers code. Now, write new movement code inside a new function, which you will call from update. You will need to redefine direction inside your loop -- it is not the same direction variable that is used inside your creation code. for now, hardcod direction=Vector2(0,1) just so you can get something to work. Later you will use the laser rotation to determine direction.

            1 Reply Last reply Reply Quote 0
            • BurntRice
              BurntRice last edited by ccc

              I’m still working on it, but I added this:

              def update_lasers(self):
                      for l in self.lasers:
                          l.direction = Vector2(0,1)
                          l.rotation = self.Player.rotation
                          l.position = l.position + l.direction
              

              I just want to check if I’m getting side tracked or not.

              1 Reply Last reply Reply Quote 0
              • JonB
                JonB last edited by

                When a laser is moving, it should move based on IT's rotation,not the player rotation. You might move after the laser gets fired, but you want the laser to go straight.

                So delete the line updating the rotation. The rotation was set when you spawn the laser.

                This code should work, but of course the laser always goes in one direction. So next, you can redefine direction using the l.rotation, similar to the way you calculated it when you spawn the laser

                1 Reply Last reply Reply Quote 0
                • BurntRice
                  BurntRice last edited by

                  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?

                  1 Reply Last reply Reply Quote 0
                  • BurntRice
                    BurntRice last edited by

                    Also, I would also like to know how to code screen wrapping.

                    1 Reply Last reply Reply Quote 0
                    • ccc
                      ccc last edited by ccc

                      player.location.x %= screen.width
                      player.location.y %= screen.height
                      
                      1 Reply Last reply Reply Quote 0
                      • BurntRice
                        BurntRice last edited by

                        Oh, thanks.

                        1 Reply Last reply Reply Quote 0
                        • BurntRice
                          BurntRice last edited by

                          Wait, how do find the screen width and height? And what about location?

                          cvp 1 Reply Last reply Reply Quote 0
                          • cvp
                            cvp @BurntRice last edited by

                            @BurntRice said:

                            how do find the screen width and height

                            ui.get_screen_size() --> width,height

                            BurntRice 1 Reply Last reply Reply Quote 0
                            • JonB
                              JonB last edited by

                              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.

                              1 Reply Last reply Reply Quote 0
                              • BurntRice
                                BurntRice @cvp last edited by

                                @cvp And what about location?

                                1 Reply Last reply Reply Quote 0
                                • JonB
                                  JonB last edited by ccc

                                  position, not location.

                                  If you want to wrap, you need to fundamentally change your button logic, since you check limits

                                  1 Reply Last reply Reply Quote 0
                                  • BurntRice
                                    BurntRice last edited by BurntRice

                                    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.

                                    1 Reply Last reply Reply Quote 0
                                    • JonB
                                      JonB last edited by

                                      What do you think your if statement does?

                                      1 Reply Last reply Reply Quote 0
                                      • BurntRice
                                        BurntRice last edited by

                                        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.

                                        1 Reply Last reply Reply Quote 0
                                        • JonB
                                          JonB last edited by

                                          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.

                                          1 Reply Last reply Reply Quote 0
                                          • BurntRice
                                            BurntRice last edited by ccc

                                            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.

                                            1 Reply Last reply Reply Quote 0
                                            • First post
                                              Last post
                                            Powered by NodeBB Forums | Contributors