Rumah  >  Artikel  >  pembangunan bahagian belakang  >  Permainan Interaktif Membina Minggu

Permainan Interaktif Membina Minggu

WBOY
WBOYasal
2024-09-03 11:30:32534semak imbas

Week Building Interactive Games

Minggu 2: Membina Permainan Interaktif


Kelas 3: Fizik dan Pergerakan Permainan

3.1 Memahami Fizik Permainan

Fizik permainan melibatkan simulasi fizik dunia sebenar untuk menjadikan permainan lebih realistik dan menarik. Prinsip fizik asas seperti halaju, pecutan dan graviti boleh membuat pergerakan dan interaksi dalam permainan berasa semula jadi.

3.1.1 Halaju dan Pecutan
  • Halaju ialah kadar perubahan kedudukan objek.
  • Pecutan ialah kadar perubahan halaju.

Contoh: Pergerakan Asas dengan Halaju

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 Simulasi Graviti

Graviti menambah realisme pada permainan dengan menarik objek ke bawah, meniru kesan graviti di Bumi.

Contoh: Menambah Graviti pada Objek Jatuh

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 Melantun dan Memantulkan Objek

Untuk mencipta permainan dinamik, selalunya anda perlu mensimulasikan objek yang melantun, seperti bola yang melantun dari dinding.

Contoh: Simulasi Bola Melantun

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 <= 0 or ball.right >= 800:
        velocity.x = -velocity.x
    if ball.top <= 0 or ball.bottom >= 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()

Projek Mini 3.3: Permainan Bola Melantun

Matlamat: Cipta permainan di mana bola melantun di sekeliling skrin, menukar arah apabila ia mengenai dinding.

3.3.1 Contoh Kod

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 <= 0 or ball.right >= 800:
        velocity.x = -velocity.x
    if ball.top <= 0 or ball.bottom >= 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 Senaman

  1. Tambah Halangan:
    • Perkenalkan halangan pegun yang bola boleh melantun.
  2. Tukar Warna Bola:
    • Jadikan bola bertukar warna setiap kali ia melantun dari dinding.

Kelas 4: Bekerja dengan Bunyi dan Muzik

4.1 Menambah Kesan Bunyi dan Muzik

Kesan bunyi dan muzik adalah penting untuk mencipta pengalaman permainan yang mengasyikkan. Pygame membolehkan anda menambahkan bunyi pada permainan anda dengan mudah.

4.1.1 Memuatkan dan Memainkan Bunyi
  • Untuk menggunakan bunyi dalam Pygame, anda mesti memuatkan fail bunyi dahulu dan kemudian memainkannya.

Contoh: Menambah Kesan Bunyi

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 <= 0 or ball.right >= 800:
        velocity.x = -velocity.x
        bounce_sound.play()  # Play sound on bounce
    if ball.top <= 0 or ball.bottom >= 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 Muzik Latar Belakang
  • Anda juga boleh menambah muzik latar yang dimainkan secara berterusan semasa permainan.

Contoh: Menambah Muzik Latar Belakang

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 Mencetuskan Bunyi Berdasarkan Peristiwa

Kesan bunyi boleh dicetuskan berdasarkan acara permainan tertentu, seperti perlanggaran atau tindakan pemain.

Contoh: Permainan Memori Bunyi

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:

Atas ialah kandungan terperinci Permainan Interaktif Membina Minggu. Untuk maklumat lanjut, sila ikut artikel berkaitan lain di laman web China PHP!

Kenyataan:
Kandungan artikel ini disumbangkan secara sukarela oleh netizen, dan hak cipta adalah milik pengarang asal. Laman web ini tidak memikul tanggungjawab undang-undang yang sepadan. Jika anda menemui sebarang kandungan yang disyaki plagiarisme atau pelanggaran, sila hubungi admin@php.cn
Artikel sebelumnya:Contoh Penomboran KursorArtikel seterusnya:Contoh Penomboran Kursor