تصميم لعبه فري فاير ![]()
```python
import pygame
import random
# تعريف الألوان
WHITE = (255, 255, 255)
BLACK = (0, 0, 0)
RED = (255, 0, 0)
# تحديد حجم النافذة وتهيئة Pygame
WIDTH, HEIGHT = 800, 600
pygame.init()
screen = pygame.display.set_mode((WIDTH, HEIGHT))
pygame.display.set_caption("لعبة حرب")
# تحميل الصور
player_img = pygame.image.load("player.png")
enemy_img = pygame.image.load("enemy.png")
bullet_img = pygame.image.load("bullet.png")
# تحديد سرعة اللاعب والعدو والرصاص
player_speed = 5
enemy_speed = 3
bullet_speed = 7
# إنشاء اللاعب والعدو والرصاص
player_rect = player_img.get_rect()
player_rect.centerx = WIDTH / 2
player_rect.bottom = HEIGHT - 20
enemy_rect = enemy_img.get_rect()
enemy_rect.centerx = random.randint(50, WIDTH - 50)
enemy_rect.top = 50
bullets = []
# إنشاء المؤقتة
clock = pygame.time.Clock()
# الحلقة الرئيسية للعبة
running = True
while running:
# تحديث الساحة
screen.fill(BLACK)
# رسم اللاعب والعدو والرصاص
screen.blit(player_img, player_rect)
screen.blit(enemy_img, enemy_rect)
for bullet in bullets:
screen.blit(bullet_img, bullet)
# التحقق من الأحداث
for event in pygame.event.get():
if event.type == pygame.QUIT:
running = False
elif event.type == pygame.KEYDOWN:
if event.key == pygame.K_LEFT:
player_rect.x -= player_speed
elif event.key == pygame.K_RIGHT:
player_rect.x += player_speed
elif event.key == pygame.K_SPACE:
bullet_rect = bullet_img.get_rect()
bullet_rect.centerx = player_rect.centerx
bullet_rect.bottom = player_rect.top
bullets.append(bullet_rect)
# تحريك العدو
enemy_rect.y += enemy_speed
# التحقق من اصطدام العدو باللاعب
if player_rect.colliderect(enemy_rect):
pygame.draw.rect(screen, RED, player_rect)
running = False
# تحريك الرصاص
for bullet in bullets:
bullet.y -= bullet_speed
# التحقق من اصطدام الرصاص بالعدو
for bullet in bullets:
if bullet.colliderect(enemy_rect):
bullets.remove(bullet)
enemy_rect.centerx = random.randint(50, WIDTH - 50)
enemy_rect.top = 50
# إعادة تعيين العدو عند وصوله إلى الجزء السفلي من الشاشة
if enemy_rect.bottom > HEIGHT:
enemy_rect.centerx = random.randint(50, WIDTH - 50)
enemy_rect.top = 50
# تحديث الشاشة
pygame.display.flip()
# تحديث المؤقتة
clock.tick(60)
Published:April 18, 2023