Untitled

mail@pastecode.io avatar
unknown
plain_text
2 months ago
3.3 kB
1
Indexable
Never
import pygame
import random

# Initialize Pygame
pygame.init()

# Set up the screen
WIDTH, HEIGHT = 800, 600
screen = pygame.display.set_mode((WIDTH, HEIGHT))
pygame.display.set_caption("Shooting Game")

# Set up colors
WHITE = (255, 255, 255)
BLACK = (0, 0, 0)
RED = (255, 0, 0)

# Set up player
player_width, player_height = 50, 50
player_x, player_y = WIDTH // 2 - player_width // 2, HEIGHT - player_height - 10
player_speed = 5
player = pygame.Rect(player_x, player_y, player_width, player_height)

# Set up bullets
bullet_width, bullet_height = 10, 20
bullet_speed = 7
bullets = []

# Set up enemies
enemy_width, enemy_height = 50, 50
enemy_speed = 3
enemies = []

# Set up score
score = 0
font = pygame.font.SysFont(None, 36)

# Set up game over
game_over_font = pygame.font.SysFont(None, 72)

# Set up clock
clock = pygame.time.Clock()

# Main game loop
running = True
while running:
    # Handle events
    for event in pygame.event.get():
        if event.type == pygame.QUIT:
            running = False
        elif event.type == pygame.KEYDOWN:
            if event.key == pygame.K_SPACE:
                bullet_x = player.x + player.width // 2 - bullet_width // 2
                bullet_y = player.y - bullet_height
                bullet = pygame.Rect(bullet_x, bullet_y, bullet_width, bullet_height)
                bullets.append(bullet)

    # Move player
    keys = pygame.key.get_pressed()
    if keys[pygame.K_LEFT] and player.x > 0:
        player.x -= player_speed
    if keys[pygame.K_RIGHT] and player.x < WIDTH - player_width:
        player.x += player_speed

    # Move bullets
    for bullet in bullets:
        bullet.y -= bullet_speed

    # Move enemies
    for enemy in enemies:
        enemy.y += enemy_speed

        # Check collision with player
        if player.colliderect(enemy):
            running = False

        # Check collision with bullets
        for bullet in bullets:
            if bullet.colliderect(enemy):
                bullets.remove(bullet)
                enemies.remove(enemy)
                score += 1

    # Spawn enemies
    if random.randint(1, 100) < 3:
        enemy_x = random.randint(0, WIDTH - enemy_width)
        enemy_y = -enemy_height
        enemy = pygame.Rect(enemy_x, enemy_y, enemy_width, enemy_height)
        enemies.append(enemy)

    # Clear the screen
    screen.fill(BLACK)

    # Draw player
    pygame.draw.rect(screen, WHITE, player)

    # Draw bullets
    for bullet in bullets:
        pygame.draw.rect(screen, RED, bullet)

    # Draw enemies
    for enemy in enemies:
        pygame.draw.rect(screen, WHITE, enemy)

    # Draw score
    score_text = font.render(f"Score: {score}", True, WHITE)
    screen.blit(score_text, (10, 10))

    # Update the display
    pygame.display.flip()

    # Cap the frame rate
    clock.tick(60)

# Game over
game_over_text = game_over_font.render("GAME OVER", True, WHITE)
screen.blit(game_over_text, (WIDTH // 2 - game_over_text.get_width() // 2, HEIGHT // 2 - game_over_text.get_height() // 2))
pygame.display.flip()

# Wait for a moment before quitting
pygame.time.wait(2000)

# Quit Pygame
pygame.quit()
Leave a Comment