0% found this document useful (0 votes)
13 views

Shooting Game - Py

Uploaded by

aslamswat83
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as TXT, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
13 views

Shooting Game - Py

Uploaded by

aslamswat83
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as TXT, PDF, TXT or read online on Scribd
You are on page 1/ 2

import pygame

import random
import sys

# Initialize Pygame
pygame.init()

# Set up the screen


screen_width = 800
screen_height = 600
screen = pygame.display.set_mode((screen_width, screen_height))
pygame.display.set_caption("Shooting Game")

# Colors
WHITE = (255, 255, 255)
BLACK = (0, 0, 0)
RED = (255, 0, 0)

# Player
player_size = 50
player_x = screen_width // 2 - player_size // 2
player_y = screen_height - player_size - 10
player_speed = 5

# Bullets
bullet_width = 5
bullet_height = 15
bullet_speed = 7
bullets = []

# Targets
target_width = 50
target_height = 50
target_speed = 3
targets = []

def draw_player(x, y):


pygame.draw.rect(screen, WHITE, (x, y, player_size, player_size))

def draw_bullets(bullets):
for bullet in bullets:
pygame.draw.rect(screen, RED, bullet)

def draw_targets(targets):
for target in targets:
pygame.draw.rect(screen, WHITE, target)

def move_bullets(bullets):
for bullet in bullets:
bullet[1] -= bullet_speed

def move_targets(targets):
for target in targets:
target[1] += target_speed

def create_target():
target_x = random.randint(0, screen_width - target_width)
target_y = 0 - target_height
targets.append([target_x, target_y, target_width, target_height])
def collision_check(bullets, targets):
for bullet in bullets:
for target in targets:
if bullet[1] <= target[1] + target[3] and bullet[0] >= target[0] and
bullet[0] <= target[0] + target[2]:
targets.remove(target)
bullets.remove(bullet)

def main():
clock = pygame.time.Clock()
score = 0
while True:
screen.fill(BLACK)
for event in pygame.event.get():
if event.type == pygame.QUIT:
pygame.quit()
sys.exit()

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 < screen_width - player_size:
player_x += player_speed

if keys[pygame.K_SPACE]:
bullets.append([player_x + player_size // 2 - bullet_width // 2,
player_y])

if len(targets) < 5:
create_target()

move_bullets(bullets)
move_targets(targets)
collision_check(bullets, targets)

draw_player(player_x, player_y)
draw_bullets(bullets)
draw_targets(targets)

pygame.display.update()
clock.tick(60)

if __name__ == "__main__":
main()

You might also like