【Pygame Zero】ゲームの技03:BGMを 流そう!

こんにちは!
「Pythonしよう!楽しく学べるプログラミング教室」の
ラッチ先生です


スックです。よろしくね!
BGM提供:DOVA-SYNDROME
https://dova-s.jp/
・ 「あさごはんなあに?」 by ゆうり
効果音提供:Chisato’s Website
https://chisatosound.sakura.ne.jp/index.html
・ 「thought_bubble_cloud_02」

基礎プログラムと 画像を入れた
「ゲームの技03 幽霊から逃げろ!」zipフォルダを ダウンロードしてください



この基礎プログラムは、こちらの記事で解説しています

学習の流れ
幽霊から 逃げる
幽霊に捕まると セリフを言う

BGM・効果音をつける
・ 効果音提供:Chisato’s Websiteサイト
・ アニメ:「 thought_bubble_cloud_02」
・ BGM提供 :DOVA-SYNDROMEサイト
・ 「あさごはんなあに?」 by ゆうり
プログラムを 実行してみよう
プログラミングの仕方を説明します
モジュールを 用意する

今回の「幽霊から 逃げろ!」では、
・ boonが幽霊に襲われる
・ 幽霊を ランダムな場所へ
・ boon が セリフを言う
プログラムが あります
そこで、3つのモジュールを 用意しました




モジュールとは、
関数やプログラムが書かれているファイルのことだよ
今回のプログラミングのポイント

今回は、
ゲーム中にBGMを流すプログラムを 作っていきます。
ポイントが 3つあります。

今回のプログラムに「DOVA-SYNDROME」サイトから
・ 「あさごはんなあに?」 by ゆうり
BGMの曲として お借りしました。 ありがとうございます。

今回のプログラムに『Chisato’s Website」サイトから
・ アニメ: thought_bubble_cloud_02.wav
効果音を お借りしました。 ありがとうございます。

2つに 分けたよ
幽霊から 逃げる

最初に、
boonを マウスで 動かす プログラムを 作りましょう
on_mouse_move ( ) 関数で マウスの座標(pos)を
boonの座標(pos)に 代入しましょう
game = "play" #1 変数gameを宣言 初期値:"play"
def on_mouse_move(pos): #2 動いているマウスの座標を取得して posに代入する
global game #3 グローバル変数 game
if game == "play": #4 もし 変数gameが playだったら
boon.pos = pos #5 マウスの座標posを boonの座標posに代入する

これで、boonの動きが 完成だよ

つぎは、
animate( ) 関数を使って 幽霊の動きを 作りましょう

def ghost_move(): #1 幽霊の動きを まとめる
target_x = random.randint(75, Width-75) #2 75~725からランダムに決めて 目的地x座標に 代入する
target_y = random.randint(75, HEIGHT-75) #3 75~515からランダムに決めて 目的地y座標に 代入する
timer = random.uniform(0.5, 1.0) #4 0.5~1.0からランダムに決めて 変数timerに 代入する
animate(ghost, pos = (target_x, target_y), duration = timer, on_finished = ghost_move) #5 幽霊のアニメーション
ghost_move() #6 幽霊が 動く

キーワード引数on_finishedが
ghost_move() に なっているから、何度も繰り返すんだね
ポイント1
ghostの画像が 150 × 150px です。
幽霊が スクリーンからはみ出ないように しました。
35. target_x = random.randint(75, WIDTH – 75)
36. target_y = random.randint(75, HEIGHT – 75)

ポイント2
移動する時間を0.5 ~ 1.0 秒から ランダムに決めて
アニメーションに変化を出しました。
ランダムに選ぶ値が 小数なので uniform( )メゾットを 使いました。
37. timer = random.uniform( 0.5 , 1.0 )

animation( )関数を使うと 簡単にできちゃうね
幽霊に捕まると セリフを言う


つぎに、
boonを 捕まえた時に幽霊のアニメーションを止めましょう。
アニメーションの止め方
1. 変数ghost_animation を作成する。 初期値:None
2.アニメーションを 変数ghost_animation に 保存する
ここで、アニメーションオブジェクトが できます。
3.stop( ) メゾットで、アニメーションを止める
ghost_animation = None #1 変数ghost_animationを 宣言 初期値:None(無し)
def ghost_move():
global ghost_animation #2 グローバル変数ghost_animation
target_x = random.randint(75, WIDTH-75)
target_y = random.randint(75, HEIGHT-75)
timer = random.uniform(0.5, 1.0)
ghost_animation = animate(ghost, pos = (target_x, target_y), duration= timer, on_finished=ghost_move) #3 アニメーション関数を 変数ghost_animationに 保存する
def update(): #4 更新する
global game #5 グローバル変数 game
if game == "play": #6 もし 変数gameが "play"なら
if boon.collide_pixel(ghost): #7 もし boonが 幽霊に触れたら
game = "over" #8 変数gameに "over"を代入する
ghost_animation.stop() #9 ghost_animationオブジェクトを 止める

boonも 動きを止めたね

そして、
boonの画像を “ouch_boon“に 変えて
say( )メゾットで セリフを言わせましょう

def draw():
screen.fill("white")
boon.draw()
ghost.draw()
text_display.draw(screen) #1 テキストディスプレイを スクリーンに装備する
def update():
global game
if game == "play":
if boon.collide_pixel(ghost):
game = "over"
ghost_animation.stop()
boon.image = "ouch_boon" #2 boonの画像を "ouch_boon"にする
boon.say("Ouch!", 2, color="red", size=70, y_offset=-70) #3 boonが 「Ouch!」と2秒間言う

これで、boon と幽霊の動きは 完了です
BGM・効果音をつける
BGM提供:DOVA-SYNDROME
https://dova-s.jp/
・ 「あさごはんなあに?」 by ゆうり
効果音提供:Chisato’s Website
https://chisatosound.sakura.ne.jp/index.html
・ アニメ: thought_bubble_cloud_02.wav

このゲームに BGMを流しましょう
Python zeroには、musicオブジェクトが標準装備であります。
やり方、以下の手順です
BGM の 流し方
1. MP3ファイルのBGMを 用意する
2. フォルダ『music』フォルダに 入れる

3. play ( ) メゾットで、BGMを 再生する
4. stop( ) メゾットで BGMを 止める


効果音は、soundsオブジェクトで再生します
音源の形式は、wav形式。
BGMと違うからね
効果音提供:Chisato’s Website
https://chisatosound.sakura.ne.jp/index.html
・ アニメ: thought_bubble_cloud_02.wav
def update():
global game
if game == "play":
if boon.collide_pixel(ghost):
game = "over"
ghost_animation.stop()
boon.image = "ouch_boon"
boon.say("Ouch!", 2, color="red", size=70, y_offset=-70)
music.stop() #2 BGMを 止めるつける
sounds.thought_bubble_cloud_02.play() #3 効果音を つける
music.play("asagohan") #1 "あさごはん"のBGMを 再生する
BGM提供:DOVA-SYNDROME
https://dova-s.jp/
・ 「あさごはんなあに?」 by ゆうり
効果音提供:Chisato’s Website
https://chisatosound.sakura.ne.jp/index.html
・ アニメ: thought_bubble_cloud_02.wav

これで、今回の学習は終了! おつかれさま
まとめ

今回は、
ゲームにBGMを入れたプログラムを 作りました。
import pgzrun
import random
from pgzhelper import *
from say import text_display
WIDTH = 800
HEIGHT = 590
boon = Actor("boon", (400, 300))
ghost = Actor("ghost", (700, 100))
game = "play"
ghost_animation = None
def draw():
screen.fill("white")
boon.draw()
ghost.draw()
text_display.draw(screen)
def on_mouse_move(pos):
global game
if game == "play":
boon.pos= pos
def ghost_move():
global ghost_animation
target_x = random.randint(75, WIDTH-75)
target_y = random.randint(75, HEIGHT-75)
timer = random.uniform(0.5, 1.0)
ghost_animation = animate(ghost, pos = (target_x, target_y), duration= timer, on_finished=ghost_move)
ghost_move()
def update():
global game
if game == "play":
if boon.collide_pixel(ghost):
game = "over"
ghost_animation.stop()
boon.image = "ouch_boon"
boon.say("Ouch!", 2, color="red", size=70, y_offset=-70)
music.stop()
sounds.thought_bubble_cloud_02.play()
music.play("asagohan")
pgzrun.go()

BGMは、musicオブジェクトで 簡単に流せます
BGM提供:DOVA-SYNDROME
https://dova-s.jp/
・ 「あさごはんなあに?」 by ゆうり


みんなも好きなBGMを『music』フォルダに入れて 流してみよう
それじゃ、またね!