"If a worker wants to do his job well, he must first sharpen his tools." - Confucius, "The Analects of Confucius. Lu Linggong"
Front page > Programming > How to shoot a bullet in the direction of the mouse in Pygame?

How to shoot a bullet in the direction of the mouse in Pygame?

Published on 2024-11-07
Browse:343

How to shoot a bullet in the direction of the mouse in Pygame?

How to shoot a bullet in the direction of the mouse in Pygame

In Pygame, one can create a bullet that is fired in the direction of the mouse. To do this, one would need to create a class that represents the bullet and set its initial position and direction based on the mouse position.

Class for the Bullet

First, create a class for the bullet. This class should include attributes for the bullet's position, size, and surface. The surface is what will be rendered on the screen.

import pygame

class Bullet:
    def __init__(self, x, y):
        self.x = x
        self.y = y
        self.height = 7
        self.width = 2
        self.bullet = pygame.Surface((self.width, self.height))
        self.bullet.fill((255, 255, 255))

Game Class Functions

Next, create a class for the game. This class will contain functions for shooting and generating bullets.

class Game:
    def __init__(self):
        self.bullets = []
    
    def shoot_bullet(self):
        mouse_x, mouse_y = pygame.mouse.get_pos() # Get the mouse position
        for bullet in self.bullets:
            rise = mouse_y - bullet.y # Calculate the difference between mouse and bullet y position
            run = mouse_x - bullet.x # Calculate the difference between mouse and bullet x position
            angle = math.atan2(rise, run) # Calculate the angle between mouse and bullet

            bullet.x  = math.cos(angle) * 10 # Update bullet x position
            bullet.y  = math.sin(angle) * 10 # Update bullet y position

            # Rotate and draw the bullet
            rotated_bullet = pygame.transform.rotate(bullet.bullet, -math.degrees(angle))
            screen.blit(rotated_bullet, (bullet.x, bullet.y))

    def generate_bullet(self):
        mouse_buttons = pygame.mouse.get_pressed() # Check if mouse is clicked
        if mouse_buttons[0]: # If left mouse button is clicked
            self.bullets.append(Bullet(player.x, player.y)) # Create a new bullet

Using the Bullet Class

In the main game loop, create an instance of the Game class and call the shoot_bullet and generate_bullet functions.

game = Game()

while running:
    # Event handling

    # Update
    game.shoot_bullet()
    game.generate_bullet()
    
    # Draw
    screen.fill((0, 0, 0))
    for bullet in game.bullets:
        screen.blit(bullet.bullet, (bullet.x, bullet.y))
    pygame.display.update()

This code will create a bullet that is shot in the direction of the mouse. The bullet will move until it goes off the screen.

Latest tutorial More>

Disclaimer: All resources provided are partly from the Internet. If there is any infringement of your copyright or other rights and interests, please explain the detailed reasons and provide proof of copyright or rights and interests and then send it to the email: [email protected] We will handle it for you as soon as possible.

Copyright© 2022 湘ICP备2022001581号-3