Location via proxy:   [ UP ]  
[Report a bug]   [Manage cookies]                
0% found this document useful (0 votes)
3 views

Free game coding

This document provides the complete Python code for a simple 2D shooting game developed using Pygame. The game features player movement, bullet shooting, and enemy destruction mechanics. Key parameters such as player speed, bullet speed, and enemy spawn time are defined, along with the main game loop for rendering and updating game elements.

Uploaded by

shaw79282
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
3 views

Free game coding

This document provides the complete Python code for a simple 2D shooting game developed using Pygame. The game features player movement, bullet shooting, and enemy destruction mechanics. Key parameters such as player speed, bullet speed, and enemy spawn time are defined, along with the main game loop for rendering and updating game elements.

Uploaded by

shaw79282
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 2

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

pygame.init()

WIDTH, HEIGHT = 800, 600


WHITE = (255, 255, 255)
RED = (255, 0, 0)
PLAYER_SPEED = 5
BULLET_SPEED = 7
ENEMY_SPEED = 2

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


pygame.display.set_caption("Shooting Game")

player_x = WIDTH // 2
player_y = HEIGHT - 80
player_width = 50

bullets = []
enemies = []
enemy_spawn_time = 30

clock = pygame.time.Clock()
running = True
frame_count = 0

while running:
clock.tick(30)
screen.fill(WHITE)

for event in pygame.event.get():


if event.type == pygame.QUIT:
running = False

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
if keys[pygame.K_SPACE]:
bullets.append([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 = random.randint(0, WIDTH - 50)
enemies.append([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:
enemies.remove(enemy)
bullets.remove(bullet)
break

pygame.draw.rect(screen, RED, (player_x, player_y, player_width, 50))


for bullet in bullets:
pygame.draw.rect(screen, RED, (bullet[0], bullet[1], 5, 10))
for enemy in enemies:
pygame.draw.rect(screen, (0, 0, 255), (enemy[0], enemy[1], 50, 50))

pygame.display.update()
frame_count += 1

pygame.quit()

You might also like