週建構互動遊戲

WBOY
發布: 2024-09-03 11:30:32
原創
631 人瀏覽過

Week Building Interactive Games

第 2 週:建構互動遊戲


第三課:遊戲物理與運動

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 <= 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.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 <= 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 練習

  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 <= 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 背景音樂
  • 您也可以新增在遊戲過程中持續播放的背景音樂。

範例:新增背景音樂

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:

登入後複製

以上是週建構互動遊戲的詳細內容。更多資訊請關注PHP中文網其他相關文章!

來源:dev.to
本網站聲明
本文內容由網友自願投稿,版權歸原作者所有。本站不承擔相應的法律責任。如發現涉嫌抄襲或侵權的內容,請聯絡admin@php.cn
熱門教學
更多>
最新下載
更多>
網站特效
網站源碼
網站素材
前端模板
關於我們 免責聲明 Sitemap
PHP中文網:公益線上PHP培訓,幫助PHP學習者快速成長!