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
    16997
    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.
    • BurntRice
      BurntRice last edited by ccc

      from scene import *
      import math
      A = Action()
      
      
      #Setting up background colour for the entire scene
      class Game(Scene):
          
          def setup(self):
              self.bg = SpriteNode('spc:BackgroundBlack')
              self.lasers = []
              
              #Creating the player
              self.Player = SpriteNode('shp:RoundRect')
              self.Player.color = ("cyan")
              self.Player.anchor_point = (0.5, 0.5)
              self.Player.position = (512, 400)
              self.Player.rotation = 0.0
              self.add_child(self.Player)
              
              self.UpCrtl = SpriteNode('iow:arrow_up_b_32')
              self.UpCrtl.x_scale = 3.5/1.0
              self.UpCrtl.y_scale = 3.5/1.0
              self.UpCrtl.alpha = 0.5
              self.UpCrtl.position = (175, 295)
              self.add_child(self.UpCrtl)
              
              self.DownCrtl = SpriteNode('iow:arrow_down_b_32')
              self.DownCrtl.x_scale = 3.5/1.0
              self.DownCrtl.y_scale = 3.5/1.0
              self.DownCrtl.alpha = 0.5
              self.DownCrtl.position = (175, 130)
              self.add_child(self.DownCrtl)
              
              self.RightCrtl = SpriteNode('iow:arrow_right_b_32')
              self.RightCrtl.x_scale = 3.5/1.0
              self.RightCrtl.y_scale = 3.5/1.0
              self.RightCrtl.alpha = 0.5
              self.RightCrtl.position = (250, 212.5)
              self.add_child(self.RightCrtl)
              
              self.LeftCrtl = SpriteNode('iow:arrow_left_b_32')
              self.LeftCrtl.x_scale = 3.5/1.0
              self.LeftCrtl.y_scale = 3.5/1.0
              self.LeftCrtl.alpha = 0.5
              self.LeftCrtl.position = (100, 212.5)
              self.add_child(self.LeftCrtl)
              
              #The button for shooting
              self.laserButton = SpriteNode('shp:Circle')
              self.laserButton.color = ('gray')
              self.laserButton.x_scale = 3/1
              self.laserButton.y_scale = 3/1
              self.add_child(self.laserButton)
              self.laserButton.position = (1000, 212.5)
              
              #The score label
              self.score_label = LabelNode(text='Score: 0')
              self.score_label.anchor_point = (0, 0)
              self.score_label.position = (10, 790)
              self.score_label.font = ('Arial Rounded MT Bold', 30)
              self.add_child(self.score_label)
      
          #Movement code.
          def update(self):
              for touch in self.touches.values():
                  if touch.location in self.LeftCrtl.bbox:
                      new_x = self.Player.position.x - 5
                      self.Player.rotation = math.pi*3/2
                      if new_x >= 0 and new_x <= 1100:
                          self.Player.position = (new_x, self.Player.position.y)
                          
                  if touch.location in self.RightCrtl.bbox: 
                      new_x = self.Player.position.x + 5
                      self.Player.rotation = math.pi/2
                      if new_x >= 0 and new_x <= 1100:
                          self.Player.position = (new_x, self.Player.position.y)
                  
                  if touch.location in self.UpCrtl.bbox:
                      new_y = self.Player.position.y + 5
                      self.Player.rotation = 0.0
                      if new_y >= 0 and new_y <= 800:
                          self.Player.position = (self.Player.position.x, new_y)
                      
                  if touch.location in self.DownCrtl.bbox: 
                      new_y = self.Player.position.y - 5
                      self.Player.rotation = math.pi
                      if new_y >= 0 and new_y <= 800:
                          self.Player.position = (self.Player.position.x, new_y)
                          
                  if touch.location in self.laserButton.bbox:
                      new_laser = SpriteNode('spc:LaserBlue6')
                      new_laser.rotation = self.Player.rotation
                      direction = Vector2(-math.sin(new_laser.rotation), math.cos(new_laser.rotation))
                      self.add_child(new_laser)
                      self.lasers.append(new_laser)
                                  
                  for l in self.lasers:
                      l.position += direction*75
      
      
      if __name__ == '__main__':
          run(Game(), LANDSCAPE, show_fps=True)
      
      JonB 1 Reply Last reply Reply Quote 0
      • ccc
        ccc last edited by ccc

        Can

                    if touch.location in self.DownCrtl.bbox: 
                        new_y = self.Player.position.y - 5
                        self.Player.rotation = math.pi
                        if new_y >= 0 and new_y <= 800:
                            self.Player.position = (self.Player.position.x, new_y)
        

        be simplified to

                    if touch.location in self.DownCrtl.bbox:
                        self.Player.rotation = math.pi
                        if 0 <= new_y <= 800:
                            self.Player.position.y -= 5
        
        1 Reply Last reply Reply Quote 0
        • JonB
          JonB @BurntRice last edited by

          @BurntRice okay, so

          l.position += direction*75
          

          Let's think about this for a minute. Where have you defined direction? Remember, the goal here in the movement code is to figure out the direction for EACH laser that you are moving. So, you need to figure out the direction Vector2 based on each laser's rotation.

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

            Perhaps

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

            needs to be indented under the if touch.location in self.laserButton.bbox: block where direction is defined.

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

              No. Trying the Socratic method to get him to think about his own code...

              This would be simpler if the touch code and laser update code were their own functions, as they are completely independent. The fact that he shares a direction variable name is what is confusing him.

              The touch code creates a laser. The laser created is spawned, and the code that sets the rotation and initial offset.

              The code that moves the lasers is completely separate (one needn't have touch code at all to move laser objects). Lasers move even when buttons are not touched. So, how can you figure out what direction a given laser should travel?

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

                I’ve made it so my laser shoots in the direction that I want it to, but, they are not moving. Once one laser pops up, the next one goes 75 pixels ahead, for ad infinitum. I don’t know if this is because of where I indented my direction value, but I don’t think so as that is mostly for rotation; nothing about moving forward etc.

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

                  So you have code which is moving your laser, but it is only running once?

                  Go back and look at all of your code to see if your indentation is correct. If you want laser movement code to run every update cycle, and it isn't, there must be some control code (if, for, with, while, etc ) that is preventing it from running.

                  What will help you in this case is to break your update function into functions that you call, so that you can better follow the program flow. Once an indented section of code gets to be longer than a screenfull, that's a good indication that you might need a function.

                  For instance, if each button had it's own method. Or, if all of your touch handling had it's own method. Then your update method would be easier for you to follow:

                  def update (self):
                     handleTouches(self)
                     moveLasers(self)
                     checkCollisions(self)
                     spawnEnemies(self)
                  
                  1 Reply Last reply Reply Quote 0
                  • BurntRice
                    BurntRice last edited by ccc

                    I have been looking for quite a bit, and I’m really blind, but I can’t find any indentation errors.

                    from scene import *
                    import math
                    A = Action()
                    
                    
                    #Setting up background colour for the entire scene
                    class Game(Scene):
                    
                        def setup(self):
                            self.bg = SpriteNode('spc:BackgroundBlack')
                            self.lasers = []
                    
                            #Creating the player
                            self.Player = SpriteNode('spc:Fire1')
                            self.Player.color = ("cyan")
                            self.Player.anchor_point = (0.5, 0.5)
                            self.Player.x_scale = 2/1.0
                            self.Player.y_scale = 1.5/1.0
                            self.Player.position = (512, 400)
                            self.Player.rotation = 0.0
                            self.add_child(self.Player)
                    
                            self.UpCrtl = SpriteNode('iow:arrow_up_b_32')
                            self.UpCrtl.x_scale = 3.5/1.0
                            self.UpCrtl.y_scale = 3.5/1.0
                            self.UpCrtl.alpha = 0.5
                            self.UpCrtl.position = (175, 295)
                            self.add_child(self.UpCrtl)
                    
                            self.DownCrtl = SpriteNode('iow:arrow_down_b_32')
                            self.DownCrtl.x_scale = 3.5/1.0
                            self.DownCrtl.y_scale = 3.5/1.0
                            self.DownCrtl.alpha = 0.5
                            self.DownCrtl.position = (175, 130)
                            self.add_child(self.DownCrtl)
                    
                            self.RightCrtl = SpriteNode('iow:arrow_right_b_32')
                            self.RightCrtl.x_scale = 3.5/1.0
                            self.RightCrtl.y_scale = 3.5/1.0
                            self.RightCrtl.alpha = 0.5
                            self.RightCrtl.position = (250, 212.5)
                            self.add_child(self.RightCrtl)
                    
                            self.LeftCrtl = SpriteNode('iow:arrow_left_b_32')
                            self.LeftCrtl.x_scale = 3.5/1.0
                            self.LeftCrtl.y_scale = 3.5/1.0
                            self.LeftCrtl.alpha = 0.5
                            self.LeftCrtl.position = (100, 212.5)
                            self.add_child(self.LeftCrtl)
                    
                            #The button for shooting
                            self.laserButton = SpriteNode('shp:Circle')
                            self.laserButton.color = ('gray')
                            self.laserButton.x_scale = 3/1
                            self.laserButton.y_scale = 3/1
                            self.add_child(self.laserButton)
                            self.laserButton.position = (1000, 212.5)
                    
                            #The score label
                            self.score_label = LabelNode(text='Score: 0')
                            self.score_label.anchor_point = (0, 0)
                            self.score_label.position = (10, 790)
                            self.score_label.font = ('Arial Rounded MT Bold', 30)
                            self.add_child(self.score_label)
                    
                        #Movement code.
                        def update(self):
                            for touch in self.touches.values():
                                if touch.location in self.LeftCrtl.bbox:
                                    new_x = self.Player.position.x - 5
                                    self.Player.rotation = math.pi/2
                                    if new_x >= 0 and new_x <= 1100:
                                        self.Player.position = (new_x, self.Player.position.y)
                    
                                if touch.location in self.RightCrtl.bbox:
                                    new_x = self.Player.position.x + 5
                                    self.Player.rotation = math.pi*3/2
                                    if new_x >= 0 and new_x <= 1100:
                                        self.Player.position = (new_x, self.Player.position.y)
                    
                                if touch.location in self.UpCrtl.bbox:
                                    new_y = self.Player.position.y + 5
                                    self.Player.rotation = 0.0
                                    if new_y >= 0 and new_y <= 800:
                                        self.Player.position = (self.Player.position.x, new_y)
                    
                                if touch.location in self.DownCrtl.bbox:
                                    new_y = self.Player.position.y - 5
                                    self.Player.rotation = math.pi
                                    if new_y >= 0 and new_y <= 800:
                                        self.Player.position = (self.Player.position.x, new_y)
                    
                                if touch.location in self.laserButton.bbox:
                                    new_laser = SpriteNode('shp:Circle')
                                    new_laser.x_scale = 0.25/1.0
                                    new_laser.y_scale = 0.25/1.0
                                    new_laser.position = self.Player.position
                                    new_laser.rotation = self.Player.rotation
                                    direction = Vector2(-math.sin(new_laser.rotation), math.cos(new_laser.rotation))
                                    self.add_child(new_laser)
                                    self.lasers.append(new_laser)
                                    for l in self.lasers:
                                        l.position += direction*75
                    
                    
                    if __name__ == '__main__':
                        run(Game(), LANDSCAPE, show_fps=True)
                    
                    1 Reply Last reply Reply Quote 0
                    • JonB
                      JonB last edited by JonB

                      Think about what should happen when you lifr your finger. Remember, update gets called 60 times per second.

                      Do you want the existing lasers to keep moving? Or move only when your finger is down, and freeze when your finger is up.

                      I would suggest rewriting your update function as follows, to make it easier for you to follow:

                         def update(self):
                            self.handle_button_presses()
                            self.move_lasers()
                      

                      Your touch handling code then moves to one method, and other game logic moves to their own functions, and get called from update. For example, eventually you might be moving enemies or asteroids or the map. You might be spawning enemies at certain intervals, which would also be it's own method. Thus, you would create a spawn_enemies, and move_enemies methods, maybe a scroll_map if this is scroller type game, perhaps a check_collisions which checks if the player hit any obstacles or enemies, etc. You should be able to write your entire update method using dummy methods that sort of lay out what you need to work in, and can implement the methods one at a time, since they should all be mostly independent of each other, just depending on a common set of attributes or positions.

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

                        I want my existing lasers to keep on moving. I have also implemented your idea of rewriting my update function.
                        I really don’t know how to though. If I use a touch.began method, it runs once, but stays in it’s place.
                        Am I forgetting to add something?

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

                          Okay, let's try this again.

                          If you want your laser to move, the movement code must be called EVERY time update is called. There should be no if statements around the for loop that moves the lasers.

                          In your old code the laser loop was inside the if statement that checked for the laser button touch. That's why it doesn't move when your finger is up. You will need to update your loop because the direction variable that you use for moving the lasers must be define inside the loop, because it can be different for every laser. You'll need to grab the laser rotation for each laser to figure that out.

                          Post your new updated 5 line update method. And post your update_laser_postions function or whatever you called it.

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

                            def update(self):
                            self.handle_button_presses()
                            self.move_lasers()
                            if random.random() < 0.01:
                            self.spawn_item()

                            def handle_button_presses(self):
                                for touch in self.touches.values():
                                    if touch.location in self.LeftCrtl.bbox:
                                        new_x = self.Player.position.x - 5
                                        self.Player.rotation = math.pi/2
                                        if new_x >= 0 and new_x <= 1100:
                                            self.Player.position = (new_x, self.Player.position.y)
                            
                                    if touch.location in self.RightCrtl.bbox:
                                        new_x = self.Player.position.x + 5
                                        self.Player.rotation = math.pi*3/2
                                        if new_x >= 0 and new_x <= 1100:
                                            self.Player.position = (new_x, self.Player.position.y)
                            
                                    if touch.location in self.UpCrtl.bbox:
                                        new_y = self.Player.position.y + 5
                                        self.Player.rotation = 0.0
                                        if new_y >= 0 and new_y <= 800:
                                            self.Player.position = (self.Player.position.x, new_y)
                            
                                    if touch.location in self.DownCrtl.bbox:
                                        new_y = self.Player.position.y - 5
                                        self.Player.rotation = math.pi
                                        if new_y >= 0 and new_y <= 800:
                                            self.Player.position = (self.Player.position.x, new_y)
                            
                            def move_lasers(self):
                                for touch in self.touches.values():
                                    if touch.location in self.laserButton.bbox:
                                        new_laser = SpriteNode('shp:Circle')
                                        new_laser.x_scale = 0.25/1.0
                                        new_laser.y_scale = 0.25/1.0
                                        new_laser.position = self.Player.position
                                        new_laser.rotation = self.Player.rotation
                                        direction = Vector2(-math.sin(new_laser.rotation), math.cos(new_laser.rotation))
                                        self.add_child(new_laser)
                                        self.lasers.append(new_laser)
                                        for l in self.lasers:
                                            l.position += direction*75
                            
                            1 Reply Last reply Reply Quote 0
                            • dereq
                              dereq last edited by

                              This post is deleted!
                              1 Reply Last reply Reply Quote 0
                              • 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
                                            • First post
                                              Last post
                                            Powered by NodeBB Forums | Contributors