"إذا أراد العامل أن يؤدي عمله بشكل جيد، فعليه أولاً أن يشحذ أدواته." - كونفوشيوس، "مختارات كونفوشيوس. لو لينجونج"
الصفحة الأمامية > برمجة > أسبوع بناء الألعاب التفاعلية

أسبوع بناء الألعاب التفاعلية

تم النشر بتاريخ 2024-11-07
تصفح:255

Week Building Interactive Games

الأسبوع الثاني: بناء الألعاب التفاعلية


الفئة 3: فيزياء الألعاب والحركة

3.1 فهم فيزياء الألعاب

تتضمن فيزياء اللعبة محاكاة فيزياء العالم الحقيقي لجعل الألعاب أكثر واقعية وجاذبية. يمكن لمبادئ الفيزياء الأساسية مثل السرعة والتسارع والجاذبية أن تجعل الحركات والتفاعلات في اللعبة تبدو طبيعية.

3.1.1 السرعة والتسارع
  • السرعة هي معدل تغير موضع الجسم.
  • التسارع هو معدل تغير السرعة.

مثال: الحركة الأساسية ذات السرعة المتجهة

import pygame

# Initialize Pygame
pygame.init()

# Screen setup
screen = pygame.display.set_mode((800, 600))
pygame.display.set_caption("Basic Movement with Velocity")

# Colors
white = (255, 255, 255)
black = (0, 0, 0)

# Player setup
player = pygame.Rect(375, 275, 50, 50)
velocity = pygame.Vector2(0, 0)

# Main game loop
running = True
while running:
    for event in pygame.event.get():
        if event.type == pygame.QUIT:
            running = False

    # Keyboard input for movement
    keys = pygame.key.get_pressed()
    if keys[pygame.K_LEFT]:
        velocity.x = -5
    elif keys[pygame.K_RIGHT]:
        velocity.x = 5
    else:
        velocity.x = 0

    if keys[pygame.K_UP]:
        velocity.y = -5
    elif keys[pygame.K_DOWN]:
        velocity.y = 5
    else:
        velocity.y = 0

    # Update player position
    player.move_ip(velocity)

    # Clear screen
    screen.fill(white)

    # Draw player
    pygame.draw.rect(screen, black, player)

    # Update display
    pygame.display.flip()

pygame.quit()
3.1.2 محاكاة الجاذبية

تضيف الجاذبية الواقعية إلى الألعاب عن طريق سحب الأشياء إلى الأسفل، ومحاكاة تأثير الجاذبية على الأرض.

مثال: إضافة الجاذبية إلى جسم ساقط

import pygame

# Initialize Pygame
pygame.init()

# Screen setup
screen = pygame.display.set_mode((800, 600))
pygame.display.set_caption("Gravity Simulation")

# Colors
white = (255, 255, 255)
black = (0, 0, 0)

# Object setup
object = pygame.Rect(375, 50, 50, 50)
gravity = 0.5
velocity_y = 0

# Main game loop
running = True
while running:
    for event in pygame.event.get():
        if event.type == pygame.QUIT:
            running = False

    # Apply gravity
    velocity_y  = gravity
    object.y  = velocity_y

    # Reset object position if it falls off-screen
    if object.y > 600:
        object.y = 50
        velocity_y = 0

    # Clear screen
    screen.fill(white)

    # Draw object
    pygame.draw.rect(screen, black, object)

    # Update display
    pygame.display.flip()

pygame.quit()

3.2 الأجسام المرتدة والمنعكسة

لإنشاء ألعاب ديناميكية، غالبًا ما يكون من الضروري محاكاة الأجسام المرتدة، مثل ارتداد الكرة عن الجدران.

مثال: محاكاة الكرة المرتدة

import pygame

# Initialize Pygame
pygame.init()

# Screen setup
screen = pygame.display.set_mode((800, 600))
pygame.display.set_caption("Bouncing Ball")

# Colors
white = (255, 255, 255)
black = (0, 0, 0)

# Ball setup
ball = pygame.Rect(375, 275, 50, 50)
velocity = pygame.Vector2(4, 4)

# Main game loop
running = True
while running:
    for event in pygame.event.get():
        if event.type == pygame.QUIT:
            running = False

    # Move ball
    ball.move_ip(velocity)

    # Bounce off walls
    if ball.left = 800:
        velocity.x = -velocity.x
    if ball.top = 600:
        velocity.y = -velocity.y

    # Clear screen
    screen.fill(white)

    # Draw ball
    pygame.draw.ellipse(screen, black, ball)

    # Update display
    pygame.display.flip()

pygame.quit()

3.3 مشروع صغير: لعبة الكرة المرتدة

الهدف: أنشئ لعبة ترتد فيها الكرة حول الشاشة، وتغير اتجاهها عندما تصطدم بالجدران.

3.3.1 مثال على الكود

import pygame

# Initialize Pygame
pygame.init()

# Screen setup
screen = pygame.display.set_mode((800, 600))
pygame.display.set_caption("Bouncing Ball Game")

# Colors
white = (255, 255, 255)
black = (0, 0, 0)

# Ball setup
ball = pygame.Rect(375, 275, 50, 50)
velocity = pygame.Vector2(3, 3)

# Main game loop
running = True
while running:
    for event in pygame.event.get():
        if event.type == pygame.QUIT:
            running = False

    # Move ball
    ball.move_ip(velocity)

    # Bounce off walls
    if ball.left = 800:
        velocity.x = -velocity.x
    if ball.top = 600:
        velocity.y = -velocity.y

    # Clear screen
    screen.fill(white)

    # Draw ball
    pygame.draw.ellipse(screen, black, ball)

    # Update display
    pygame.display.flip()

pygame.quit()

تمارين 3.4

  1. إضافة عوائق:
    • أدخل عوائق ثابتة يمكن للكرة أن ترتد عنها.
  2. تغيير لون الكرة:
    • اجعل الكرة تغير لونها في كل مرة ترتد فيها عن الحائط.

الفئة 4: العمل مع الأصوات والموسيقى

4.1 إضافة المؤثرات الصوتية والموسيقى

تعد المؤثرات الصوتية والموسيقى أمرًا ضروريًا لإنشاء تجربة لعب غامرة. يتيح لك Pygame إضافة الصوت إلى ألعابك بسهولة.

4.1.1 تحميل الأصوات وتشغيلها
  • لاستخدام الصوت في Pygame، يجب عليك أولاً تحميل ملف الصوت ثم تشغيله.

مثال: إضافة تأثير صوتي

import pygame

# Initialize Pygame and Mixer
pygame.init()
pygame.mixer.init()

# Load sound effect
bounce_sound = pygame.mixer.Sound("bounce.wav")

# Screen setup
screen = pygame.display.set_mode((800, 600))
pygame.display.set_caption("Sound Effects")

# Colors
white = (255, 255, 255)
black = (0, 0, 0)

# Ball setup
ball = pygame.Rect(375, 275, 50, 50)
velocity = pygame.Vector2(3, 3)

# Main game loop
running = True
while running:
    for event in pygame.event.get():
        if event.type == pygame.QUIT:
            running = False

    # Move ball
    ball.move_ip(velocity)

    # Bounce off walls and play sound
    if ball.left = 800:
        velocity.x = -velocity.x
        bounce_sound.play()  # Play sound on bounce
    if ball.top = 600:
        velocity.y = -velocity.y
        bounce_sound.play()

    # Clear screen
    screen.fill(white)

    # Draw ball
    pygame.draw.ellipse(screen, black, ball)

    # Update display
    pygame.display.flip()

pygame.quit()
4.1.2 موسيقى الخلفية
  • يمكنك أيضًا إضافة موسيقى خلفية يتم تشغيلها بشكل مستمر أثناء اللعبة.

مثال: إضافة موسيقى خلفية

import pygame

# Initialize Pygame and Mixer
pygame.init()
pygame.mixer.init()

# Load and play background music
pygame.mixer.music.load("background_music.mp3")
pygame.mixer.music.play(-1)  # Loop indefinitely

# Screen setup
screen = pygame.display.set_mode((800, 600))
pygame.display.set_caption("Background Music")

# Colors
white = (255, 255, 255)
black = (0, 0, 0)

# Main game loop
running = True
while running:
    for event in pygame.event.get():
        if event.type == pygame.QUIT:
            running = False

    # Clear screen
    screen.fill(white)

    # Update display
    pygame.display.flip()

pygame.quit()

4.2 إطلاق الأصوات بناءً على الأحداث

يمكن تشغيل المؤثرات الصوتية بناءً على أحداث معينة في اللعبة، مثل الاصطدامات أو تصرفات اللاعب.

مثال: لعبة الذاكرة الصوتية

python
import pygame
import random

# Initialize Pygame and Mixer
pygame.init()
pygame.mixer.init()

# Load sounds
sounds = [pygame.mixer.Sound(f"sound{i}.wav") for i in range(4)]

# Screen setup
screen = pygame.display.set_mode((800, 600))
pygame.display.set_caption("Sound Memory Game")

# Colors
white = (255, 255, 255)
black = (0, 0, 0)

# Generate random sequence of sounds
sequence = [random.choice(sounds) for _ in range(5)]
current_step = 0

# Main game loop
running = True
while running:

بيان الافراج يتم استنساخ هذه المقالة على: https://dev.to/igbojionu/week-2-building--edter-games-41ok؟1 إذا كان هناك أي انتهاك ، فيرجى الاتصال بـ [email protected] لحذفه.
أحدث البرنامج التعليمي أكثر>

تنصل: جميع الموارد المقدمة هي جزئيًا من الإنترنت. إذا كان هناك أي انتهاك لحقوق الطبع والنشر الخاصة بك أو الحقوق والمصالح الأخرى، فيرجى توضيح الأسباب التفصيلية وتقديم دليل على حقوق الطبع والنشر أو الحقوق والمصالح ثم إرسالها إلى البريد الإلكتروني: [email protected]. سوف نتعامل مع الأمر لك في أقرب وقت ممكن.

Copyright© 2022 湘ICP备2022001581号-3