0% found this document useful (0 votes)
17 views5 pages

import pygame

This document is a Python script that implements a simple shooter game using Pygame. The game features a player-controlled spaceship that can shoot bullets at falling enemies, with scoring and life mechanics. The game runs in a loop, handling events, updating positions, and rendering graphics until the player loses all lives.
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
17 views5 pages

import pygame

This document is a Python script that implements a simple shooter game using Pygame. The game features a player-controlled spaceship that can shoot bullets at falling enemies, with scoring and life mechanics. The game runs in a loop, handling events, updating positions, and rendering graphics until the player loses all lives.
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 5

import pygame

import random

# Inicializar Pygame

pygame.init()

# Configuración de la pantalla

WIDTH, HEIGHT = 500, 600

screen = pygame.display.set_mode((WIDTH, HEIGHT))

pygame.display.set_caption("Shooter Game")

# Colores

WHITE = (255, 255, 255)

BLUE = (0, 0, 255)

RED = (255, 0, 0)

BLACK = (0, 0, 0)

# Jugador (nave)

player_size = 50

player_x = WIDTH // 2 - player_size // 2

player_y = HEIGHT - player_size - 10

player_speed = 7

# Balas

bullet_width = 5

bullet_height = 10

bullet_speed = 7

bullets = []

# Enemigos

enemy_radius = 25 # Radio de los círculos enemigos


enemy_speed = 5

enemies = []

# Variables del juego

score = 0

lives = 3

# Fuentes

font = pygame.font.SysFont("Arial", 24)

# Crear enemigos

def create_enemy():

x_pos = random.randint(0, WIDTH - enemy_radius * 2)

y_pos = random.randint(-150, -50) # Empieza fuera de la pantalla

enemies.append([x_pos, y_pos])

# Crear balas

def create_bullet():

bullet_x = player_x + player_size // 2 - bullet_width // 2

bullet_y = player_y - bullet_height

bullets.append([bullet_x, bullet_y])

# Bucle del juego

running = True

clock = pygame.time.Clock()

# Crear enemigos iniciales

for _ in range(5):

create_enemy()

while running:
screen.fill(WHITE) # Fondo blanco

pygame.time.delay(30)

# Capturar eventos

for event in pygame.event.get():

if event.type == pygame.QUIT:

running = False

if event.type == pygame.KEYDOWN:

if event.key == pygame.K_SPACE: # Disparar

create_bullet()

# Obtener teclas presionadas

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

player_x += player_speed

# Mover enemigos

for enemy in enemies[:]:

enemy[1] += enemy_speed

if enemy[1] > HEIGHT:

enemies.remove(enemy)

create_enemy()

lives -= 1 # Pierdes una vida si un enemigo llega al fondo

# Comprobar colisión con balas

for bullet in bullets[:]:

if (

bullet[0] > enemy[0] and bullet[0] < enemy[0] + enemy_radius * 2

and bullet[1] > enemy[1] and bullet[1] < enemy[1] + enemy_radius * 2


):

enemies.remove(enemy)

bullets.remove(bullet)

create_enemy() # Crear un nuevo enemigo

score += 1 # Aumentar puntuación

# Mover balas

for bullet in bullets[:]:

bullet[1] -= bullet_speed

if bullet[1] < 0:

bullets.remove(bullet)

# Dibujar la nave (triángulo)

pygame.draw.polygon(screen, BLUE, [(player_x, player_y), (player_x + player_size, player_y),


(player_x + player_size // 2, player_y - 20)])

# Dibujar balas

for bullet in bullets:

pygame.draw.rect(screen, BLACK, (bullet[0], bullet[1], bullet_width, bullet_height))

# Dibujar enemigos (círculos)

for enemy in enemies:

pygame.draw.circle(screen, RED, (enemy[0] + enemy_radius, enemy[1] + enemy_radius),


enemy_radius)

# Mostrar puntuación y vidas

score_text = font.render(f"Puntuación: {score}", True, BLACK)

lives_text = font.render(f"Vidas: {lives}", True, BLACK)

screen.blit(score_text, (10, 10))

screen.blit(lives_text, (WIDTH - 100, 10))

# Fin del juego si las vidas son 0


if lives <= 0:

game_over_text = font.render("¡GAME OVER!", True, RED)

screen.blit(game_over_text, (WIDTH // 2 - 100, HEIGHT // 2))

pygame.display.update()

clock.tick(60)

pygame.quit()

You might also like

pFad - Phonifier reborn

Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.

Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy