Untitled

mail@pastecode.io avatar
unknown
plain_text
5 months ago
3.1 kB
1
Indexable
mport pygame
import random

# Initialize the game
pygame.init()

# Set the screen dimensions
screen_width = 800
screen_height = 600
screen = pygame.display.set_mode((screen_width, screen_height))

# Set the title of the game window
pygame.display.set_caption("Shooting Game")

# Set the background color
background_color = (0, 0, 0)

# Set the player attributes
player_width = 50
player_height = 50
player_x = screen_width // 2 - player_width // 2
player_y = screen_height - player_height - 10
player_speed = 5

# Set the bullet attributes
bullet_width = 10
bullet_height = 20
bullet_x = 0
bullet_y = screen_height - player_height - 10
bullet_speed = 10
bullet_state = "ready"

# Set the target attributes
target_width = 50
target_height = 50
target_x = random.randint(0, screen_width - target_width)
target_y = 0
target_speed = 5

# Set the score
score = 0

# Game loop
running = True
while running:
    # Fill the screen with the background color
    screen.fill(background_color)

    # Handle events
    for event in pygame.event.get():
        if event.type == pygame.QUIT:
            running = False

        # Move the player
        if event.type == pygame.KEYDOWN:
            if event.key == pygame.K_LEFT:
                player_x -= player_speed
            if event.key == pygame.K_RIGHT:
                player_x += player_speed

        # Shoot bullets
        if event.type == pygame.KEYDOWN:
            if event.key == pygame.K_SPACE:
                if bullet_state == "ready":
                    bullet_x = player_x + player_width // 2 - bullet_width // 2
                    bullet_state = "fire"

    # Update the player position
    if player_x < 0:
        player_x = 0
    elif player_x > screen_width - player_width:
        player_x = screen_width - player_width

    # Update the bullet position
    if bullet_state == "fire":
        bullet_y -= bullet_speed
        pygame.draw.rect(screen, (255, 255, 255), (bullet_x, bullet_y, bullet_width, bullet_height))

    if bullet_y <= 0:
        bullet_y = screen_height - player_height - 10
        bullet_state = "ready"

    # Update the target position
    target_y += target_speed
    pygame.draw.rect(screen, (255, 0, 0), (target_x, target_y, target_width, target_height))

    if target_y > screen_height:
        target_x = random.randint(0, screen_width - target_width)
        target_y = 0
        score += 1

    # Check for collision
    if bullet_x >= target_x and bullet_x <= target_x + target_width and bullet_y <= target_y + target_height:
        bullet_y = screen_height - player_height - 10
        bullet_state = "ready"
        target_x = random.randint(0, screen_width - target_width)
        target_y = 0
        score += 10

    # Draw the player
    pygame.draw.rect(screen, (0, 255, 0), (player_x, player_y, player_width, player_height))

    # Display the score
    font = pygame.font.Font(None, 36)
    score_text = font.render("Score: " + str(score), True, (255, 255, 255))
    screen.blit(score_text, (10, 10))

    # Update the display
    pygame.display.update()

# Quit the game
pygame.quit()
Leave a Comment