Home > Backend Development > Python Tutorial > 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?

DDD
Release: 2024-10-29 19:17:30
Original
655 people have browsed it

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.

<code class="python">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))</code>
Copy after login

Game Class Functions

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

<code class="python">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</code>
Copy after login

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.

<code class="python">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()</code>
Copy after login

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.

The above is the detailed content of How to shoot a bullet in the direction of the mouse in Pygame?. For more information, please follow other related articles on the PHP Chinese website!

source:php.cn
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template