@@ -64,6 +64,15 @@
# How many pixels to move before we change the texture in the walking animation
DISTANCE_TO_CHANGE_TEXTURE = 20
+# How much force to put on the bullet
+BULLET_MOVE_FORCE = 4500
+# Mass of the bullet
+BULLET_MASS = 0.1
+# Make bullet less affected by gravity
+BULLET_GRAVITY = 300
@@ -280,6 +289,60 @@
self.left_pressed = False
elif key == arcade.key.RIGHT:
self.right_pressed = False
+ def on_mouse_press(self, x, y, button, modifiers):
+ """ Called whenever the mouse button is clicked. """
+ bullet = arcade.SpriteSolidColor(20, 5, arcade.color.DARK_YELLOW)
+ # Position the bullet at the player's current location
+ start_x = self.player_sprite.center_x
+ start_y = self.player_sprite.center_y
+ bullet.position = self.player_sprite.position
+ # Get from the mouse the destination location for the bullet
+ # IMPORTANT! If you have a scrolling screen, you will also need
+ # to add in self.view_bottom and self.view_left.
+ dest_x = x
+ dest_y = y
+ # Do math to calculate how to get the bullet to the destination.
+ # Calculation the angle in radians between the start points
+ # and end points. This is the angle the bullet will travel.
+ x_diff = dest_x - start_x
+ y_diff = dest_y - start_y
+ angle = math.atan2(y_diff, x_diff)
+ # What is the 1/2 size of this sprite, so we can figure out how far
+ # away to spawn the bullet
+ size = max(self.player_sprite.width, self.player_sprite.height) / 2
+ # Use angle to to spawn bullet away from player in proper direction
+ bullet.center_x += size * math.cos(angle)
+ bullet.center_y += size * math.sin(angle)
+ # Set angle of bullet
+ bullet.angle = math.degrees(angle)
+ # Gravity to use for the bullet
+ # If we don't use custom gravity, bullet drops too fast, or we have
+ # to make it go too fast.
+ # Force is in relation to bullet's angle.
+ bullet_gravity = (0, -BULLET_GRAVITY)
+ # Add the sprite. This needs to be done AFTER setting the fields above.
+ # Add force to bullet
+ force = (BULLET_MOVE_FORCE, 0)
+ self.physics_engine.apply_force(bullet, force)
def on_update(self, delta_time):
""" Movement and game logic """