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

Linda Hamilton
Linda HamiltonOriginal
2024-11-01 11:46:02622browse

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:

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

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.

The above is the detailed content of How to Make a Player Shoot Bullets in a Game While Keeping the Player Visible?. For more information, please follow other related articles on the PHP Chinese website!

Statement:
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