"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 Make a Player Shoot Bullets in a Game While Keeping the Player Visible?

How to Make a Player Shoot Bullets in a Game While Keeping the Player Visible?

Posted on 2025-02-22
Browse:135

How to Make a Player Shoot Bullets in a Game While Keeping the Player Visible?

Shooting Bullets with the Space Bar

This question involves implementing the ability to shoot bullets when the space bar is pressed, while maintaining a player character on the screen.

Problem and Solution

The asker's code initially had a problem where the player character disappeared when shooting. This is because the shooting and player movement were separated into different loops. To address this, we need to combine them into a single main loop where both behaviors are updated simultaneously.

Another issue was the inability to break the shooting loop when the bullet reached the top of the screen. The original code used a while loop that continued infinitely. To fix this, we need to use a while loop with a condition that checks if the bullet has reached the top.

Code Explanation

Here's a revised version of the code:

import pygame, os

# Boilerplate setup omitted for brevity

class Player:
    def __init__(self, x, y, height, width):
        ...

    def draw(self):
        ...

    def move_left(self):
        ...

    def move_right(self):
        ...


class Bullet:
    def __init__(self, x, y):
        ...

    def update(self):
        ...

    def draw(self):
        ...


# Lists of bullets
bullets = []

# Initialize player
p = Player(600, 500, 50, 30)

# Main game loop
run = True
while run:
    clock.tick(100)
    
    # Handle events
    for event in pygame.event.get():
        if event.type ==  pygame.QUIT:
            run = False
        if event.type ==  pygame.KEYDOWN:
            if event.key == pygame.K_SPACE:
                bullets.append(Bullet(p.x p.width//2, p.y))

    # Update objects
    keys = pygame.key.get_pressed()        
    if keys[pygame.K_LEFT]:
        p.move_left()
    if keys[pygame.K_RIGHT]:
        p.move_right()
    for b in bullets:
        b.update()
        # Update position and remove bullet if it goes off-screen
        if b.y < 0:
            bullets.remove(b)

    # Update frame
    d.fill((98, 98, 98))
    for b in bullets:
        b.draw()
    p.draw()
    win.update()

Key Changes:

  • The shooting and player movement logic are now combined into a single main game loop.
  • The bullet update function now checks if the bullet has gone off-screen and removes it accordingly.
  • The main loop continuously updates and draws the player and bullets, ensuring they are both present on 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