Pygame Shooting Game Code
This PDF contains the complete Python code for a simple 2D shooting game using Pygame.
The game allows a player to move left and right, shoot bullets, and destroy falling enemies.
import pygame
import random
[Link]()
WIDTH, HEIGHT = 800, 600
WHITE = (255, 255, 255)
RED = (255, 0, 0)
PLAYER_SPEED = 5
BULLET_SPEED = 7
ENEMY_SPEED = 2
screen = [Link].set_mode((WIDTH, HEIGHT))
[Link].set_caption("Shooting Game")
player_x = WIDTH // 2
player_y = HEIGHT - 80
player_width = 50
bullets = []
enemies = []
enemy_spawn_time = 30
clock = [Link]()
running = True
frame_count = 0
while running:
[Link](30)
[Link](WHITE)
for event in [Link]():
if [Link] == [Link]:
running = False
keys = [Link].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
if keys[pygame.K_SPACE]:
[Link]([player_x + player_width // 2, player_y])
for bullet in bullets:
bullet[1] -= BULLET_SPEED
bullets = [b for b in bullets if b[1] > 0]
if frame_count % enemy_spawn_time == 0:
enemy_x = [Link](0, WIDTH - 50)
[Link]([enemy_x, 0])
for enemy in enemies:
enemy[1] += ENEMY_SPEED
enemies = [e for e in enemies if e[1] < HEIGHT]
for bullet in bullets:
for enemy in enemies:
if enemy[0] < bullet[0] < enemy[0] + 50 and enemy[1] < bullet[1] < enemy[1] + 50:
[Link](enemy)
[Link](bullet)
break
[Link](screen, RED, (player_x, player_y, player_width, 50))
for bullet in bullets:
[Link](screen, RED, (bullet[0], bullet[1], 5, 10))
for enemy in enemies:
[Link](screen, (0, 0, 255), (enemy[0], enemy[1], 50, 50))
[Link]()
frame_count += 1
[Link]()