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

Free Fire Clone - Py

The document defines classes for a player, enemies, and bullets to create a basic shooting game in Pygame. The player can move with arrow keys and shoot with spacebar. Enemies spawn randomly and move across the screen. Bullets are removed if they go off screen. The main function initializes the game and runs the main loop to update and draw sprites each frame.

Uploaded by

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

Free Fire Clone - Py

The document defines classes for a player, enemies, and bullets to create a basic shooting game in Pygame. The player can move with arrow keys and shoot with spacebar. Enemies spawn randomly and move across the screen. Bullets are removed if they go off screen. The main function initializes the game and runs the main loop to update and draw sprites each frame.

Uploaded by

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

import pygame

import sys
import random

# Initialize Pygame
pygame.init()

# Set up the game window


WIDTH, HEIGHT = 800, 600
screen = pygame.display.set_mode((WIDTH, HEIGHT))
pygame.display.set_caption("Free Fire Clone")

# Define colors
WHITE = (255, 255, 255)
BLACK = (0, 0, 0)
RED = (255, 0, 0)

# Define player class


class Player(pygame.sprite.Sprite):
def __init__(self):
super().__init__()
self.image = pygame.Surface((50, 50))
self.image.fill(WHITE)
self.rect = self.image.get_rect()
self.rect.center = (WIDTH // 2, HEIGHT // 2)
self.speed = 5

def update(self):
# Basic movement controls
keys = pygame.key.get_pressed()
if keys[pygame.K_LEFT]:
self.rect.x -= self.speed
if keys[pygame.K_RIGHT]:
self.rect.x += self.speed
if keys[pygame.K_UP]:
self.rect.y -= self.speed
if keys[pygame.K_DOWN]:
self.rect.y += self.speed

# Define enemy class


class Enemy(pygame.sprite.Sprite):
def __init__(self):
super().__init__()
self.image = pygame.Surface((30, 30))
self.image.fill(RED)
self.rect = self.image.get_rect()
self.rect.center = (random.randint(0, WIDTH), random.randint(0, HEIGHT))
self.speed = random.randint(1, 3)

def update(self):
# Move the enemy
self.rect.x += self.speed
if self.rect.left > WIDTH:
self.rect.right = 0

# Define bullet class


class Bullet(pygame.sprite.Sprite):
def __init__(self, x, y):
super().__init__()
self.image = pygame.Surface((10, 5))
self.image.fill(WHITE)
self.rect = self.image.get_rect()
self.rect.center = (x, y)
self.speed = 10

def update(self):
# Move the bullet
self.rect.x += self.speed
if self.rect.right > WIDTH:
self.kill() # Remove bullet if it goes off-screen

# Define main function


def main():
# Create player object
player = Player()
all_sprites = pygame.sprite.Group()
all_sprites.add(player)

# Create enemy sprites


enemies = pygame.sprite.Group()
for _ in range(5):
enemy = Enemy()
all_sprites.add(enemy)
enemies.add(enemy)

# Create bullets group


bullets = pygame.sprite.Group()

# Game loop
running = True
while running:
# Event handling
for event in pygame.event.get():
if event.type == pygame.QUIT:
running = False
elif event.type == pygame.KEYDOWN and event.key == pygame.K_SPACE:
# Shoot a bullet when spacebar is pressed
bullet = Bullet(player.rect.right, player.rect.centery)
all_sprites.add(bullet)
bullets.add(bullet)

# Update
all_sprites.update()

# Check for collisions between bullets and enemies


hits = pygame.sprite.groupcollide(enemies, bullets, True, True)
for hit in hits:
# Spawn a new enemy after one is destroyed
enemy = Enemy()
all_sprites.add(enemy)
enemies.add(enemy)

# Draw
screen.fill(BLACK)
all_sprites.draw(screen)

# Refresh display
pygame.display.flip()
# Quit Pygame
pygame.quit()
sys.exit()

if __name__ == "__main__":
main()

You might also like