Untitled
unknown
plain_text
9 months ago
2.0 kB
4
Indexable
import pygame
import random
# Initialize Pygame
pygame.init()
# Game Constants
WIDTH, HEIGHT = 800, 600
WHITE = (255, 255, 255)
RED = (255, 0, 0)
BLUE = (0, 0, 255)
BLACK = (0, 0, 0)
PLAYER_SPEED = 5
BULLET_SPEED = 7
ENEMY_SPEED = 3
# Create screen
screen = pygame.display.set_mode((WIDTH, HEIGHT))
pygame.display.set_caption("Mini Free Fire Shooting Game")
# Load player
player = pygame.Rect(WIDTH // 2, HEIGHT - 60, 50, 50)
# Bullet and enemy lists
bullets = []
enemies = []
# Game loop variables
running = True
clock = pygame.time.Clock()
# Main game loop
while running:
screen.fill(WHITE)
# Draw player
pygame.draw.rect(screen, BLUE, player)
# Move bullets
for bullet in bullets:
bullet.y -= BULLET_SPEED
pygame.draw.rect(screen, RED, bullet)
# Move enemies
for enemy in enemies:
enemy.y += ENEMY_SPEED
pygame.draw.rect(screen, BLACK, enemy)
# Check collision with bullets
for bullet in bullets:
if enemy.colliderect(bullet):
bullets.remove(bullet)
enemies.remove(enemy)
# End game if enemy reaches player
if enemy.y > HEIGHT:
print("Game Over!")
running = False
# Handle events
for event in pygame.event.get():
if event.type == pygame.QUIT:
running = False
elif event.type == pygame.KEYDOWN:
if event.key == pygame.K_SPACE:
bullets.append(pygame.Rect(player.x + 20, player.y, 10, 20)) # Fire bullet
# Move player
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 - 50:
player.x += PLAYER_SPEED
# Spawn enemies randomly
if random.randint(1, 50) == 1:
enemies.append(pygame.Rect(random.randint(0, WIDTH - 50), 0, 50, 50))
# Refresh screen
pygame.display.flip()
clock.tick(30)
pygame.quit()
Editor is loading...
Leave a Comment