量産

【Pygame Zero】量産05:時間差で登場!ねずみたたきゲーム

ratch2025

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

ラッチ先生
ラッチ先生
スック
スック

スックです。よろしくね!

今回のプロジェクトは、こちら!

BGM提供:DOVA-SYNDROME
https://dova-s.jp/
Pursuit Goes on by MFP【Marron Fields Production】

効果音提供:Chisato’s Website
https://chisatosound.sakura.ne.jp/index.html
「Accent. Synth-Bell. E-Major[01C](LFO)」:アクセント
「Power Up Item[01](Fast. Pitch Bend Up」:アクションゲームの効果音

ラッチ先生
ラッチ先生

基礎プログラムと 画像を入れた
「量産05 ねずみ たたき!」zipフォルダを ダウンロードしてください

pythonしよう!量産05:背景画を表示する
pythonしよう!量産05:blit( ) メソッドの解説
スック
スック

今回は、blitブリット( )メソッドを使って 背景画像を 入れてみたよ

pythonしよう!量産05:ねずみ たたき!zipの中身
スポンサーリンク

ねずみが 次々と現れる

ねずみが左端へ到着すると ゲームオーバー

ねずみを クリックすると 消える

BGM・効果音を 入れる

BGM提供:DOVA-SYNDROME
https://dova-s.jp/
Pursuit Goes on by MFP【Marron Fields Production】

効果音提供:Chisato’s Website
https://chisatosound.sakura.ne.jp/index.html
「Accent. Synth-Bell. E-Major[01C](LFO)」:アクセント
「Power Up Item[01](Fast. Pitch Bend Up」:アクションゲームの効果音

プログラムを 実行してみよう

ラッチ先生
ラッチ先生

今回の「ねずみ たたき!」では、
・ ねずみを クリックする
・ ねずみに モザイクをかける
プログラムがあります。

そこで、2つのモジュールを 用意しました

pythonしよう!量産05:pgzhelperモジュールのインポートを解説
pythonしよう!量産05:visualモジュールのインポートを解説
スック
スック

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

ラッチ先生
ラッチ先生

今回は、
ねずみが 次々と現れて左へ動くプログラムを 作っていきます。

scratchスクラッチで言う「クローン」。
pygama zeroでは、次の手順で プログラムします

スック
スック

リスト:nezumisネズミズを作成して、その中にねずみを 追加していくんだ

スック
スック

nezumisネズミズ[:]は、『リストのコピー』と 覚えておいてね

pythonしよう!量産05:ポイント3を解説
スック
スック

VisualEffectビジュアルエフェクト( )クラスによって、
visualビジュアルモジュールに入っているモザイク関連のメソッドが使えるよ

pythonしよう!量産05:ポイント4
を解説
スック
スック

2つに 分けたよ

ラッチ先生
ラッチ先生

最初に
ねずみを作る関数:cleateクリエイト_nezumiネズミ( ) を定義します。

そして、
scheduleスケジュール_intervalインターバル()メソッドを使って
0.8秒経ったら、ねずみを作っていきましょう

Python
nezumis = []    #1 リストnezumisに 空のリストを代入する
Python
def draw():
    screen.blit("background", (0, 0))
    for nezumi in nezumis:   #9 リストnezumisから ねずみを取り出す
        nezumi.draw(screen)  #10 ねずみを 表示する

def create_nezumi():    #2 ねずみを作る関数を 定義する
    x = 750             #3 x座標に 750を代入する                       
    y = random.randint(50, HEIGHT-50)       #4 y座標に 50~750からランダムに決めて 代入する
    nezumi = VisualEffect("nezumi", (x, y)) #5 ねずみを 作る
    nezumi.state = "move"   #6 プロパティ:stateに moveを 代入する
    nezumis.append(nezumi)  #7 ねずみを リストnezumisに 追加する

clock.schedule_interval(create_nezumi, 0.8) #8 0.8秒ごとに create_nezumi()関数を 実行する
pythonしよう!量産05:ポイント3を解説
pythonしよう!量産05:追加プロパティ:stateを解説
pythonしよう!量産05:append()メソッドを解説
pythonしよう!量産05:schedule_interval( )メソッドを解説
pythonしよう!量産05:リストnezumisの中身を解説
スック
スック

ここまでが、ねずみが作られるポイント!
まだ、スクリーンには 表示されないよ

リストに入っているねずみが 表示されるよ

pythonしよう!量産05:for bucket in buckets: bucket.draw( )を解説
スック
スック

この作業を 一瞬で行うんだ

ラッチ先生
ラッチ先生

for 文を使って ねずみを 左へ動かします。

Python
game = "play"       #1 変数gameを 宣言する 初期値:play
Python
def nezumi_move():  #2 ねずみの動きを定義する
    global game     #3 グローバル変数 game

    if game == "play":            #4 もし 変数gameが playなら
        for nezumi in nezumis[:]: #5 リストnezumisから ねずみを取り出す
            if nezumi.state == "move": #6 もし ねずみの状態が moveなら
                nezumi.x -= 5     #7 ねずみのx座標が 5ずつ減っていく
Python
def update():    #8 更新を 定義する
    nezumi_move()  #9 ねずみを 動かす
スック
スック

nezumisネズミズ[:]については、 後で説明するよ

ラッチ先生
ラッチ先生

ねずみが左端に着いたら ゲームオーバーになります

clearクリア( ) メソッドを使って、他のねずみを 削除して
「GAME OVER」を 表示します

Python
message = Actor("gameover", (400, 300))  #6 メッセージを 作る
Python
def draw():
    screen.blit("background", (0, 0))
    if game == "over":    #7 もし 変数gameが overになったら 
        message.draw()    #8 メッセージを 表示する
Python
def nezumi_move(): 
    global game 

    if game == "play": 
        for nezumi in nezumis[:]: 
            if nezumi.state == "move": 
                nezumi.x -= 5 

                if nezumi.x < 50:  #1 もし ねずみのx座標が 50以下になったら
                    game = "over"  #2 変数gameに overを代入する
                    clock.unschedule(create_nezumi)  #3 create_nezumi関数を 中止する
                    nezumis.clear()        #4 リストnezumisを 空にする
                    nezumis.append(nezumi) #5 リストnezumisに ねずみを追加する
スック
スック

変数gameゲームが “overオーバー“になったから
左端にあるねずみは、動かないんだよ

ラッチ先生
ラッチ先生

defデフ onオン_mouseマウス_downダウン( ) 関数を使って
ねずみがクリックされたら モザイクを かけて消えるようにします

Python
def on_mouse_down(pos):       #1 クリックした座標を 引数posに代入する  
    for nezumi in nezumis: #2 リストnezumisから ねずみを 取り出す
        if nezumi.collidepoint_pixel(pos):  #3 もし ねずみにクリックされたら
            nezumi.state = "mosaic"   #4 プロパティ:stateに mosaicを代入する
            nezumi.mosaic_animation()  #5 モザイクアニメーション開始
            break                      #6 ループを 抜ける
ラッチ先生
ラッチ先生

processプロセス_animationアニメーション( ) メソッドを 使って
プロパティstateステートが “mosaicモザイク“になったネズミに モザイクをかけて
削除していきます。

Python
def nezumi_move():
    global game 

    if game == "play": 
        for nezumi in nezumis[:]: 
            if nezumi.state == "move": 
            nezumi.x -= 5 
Python
            elif nezumi.state == "mosaic": #1 もし プロパティ:stateが mosaicなら 
                result = nezumi.process_animation() #2 アニメーションを実行する
                if result == "finished": #3 もし ローカル変数resultが "finished"なら
                    nezumis.remove(nezumi)  #4 リストrezumisから ねずみを削除する
スック
スック

ああっ!
ねずみ「2」が 飛ばされるね

スック
スック

なるほどね!
リストnezumisネズミズ[ : ]は、
最後のねずみを取り出してから コピーするんだね

BGM提供:DOVA-SYNDROME
https://dova-s.jp/
Pursuit Goes on by MFP【Marron Fields Production】

効果音提供:Chisato’s Website
https://chisatosound.sakura.ne.jp/index.html
「Accent. Synth-Bell. E-Major[01C](LFO)」:アクセント
「Power Up Item[01](Fast. Pitch Bend Up」:アクションゲームの効果音

ラッチ先生
ラッチ先生

それでは、BGMを 入れてみましょう

Pythonパイソン zeroゼロには、musicミュージックオブジェクトが標準装備であります。
やり方、以下の手順です

☆ Pygame zeroでは英語の大文字が 使えません。 エラーが出ます
 小文字 に直します

pythonしよう!量産05:musicモジュールの解説
Python
def nezumi_move(): 
    global game 

    if game == "play": 
        for nezumi in nezumis[:]: 
            if nezumi.state == "move": 
                nezumi.x -= 5 

                if nezumi.x < 50: 
                    game = "over" 
                    music.stop() #2 BGMを 止める
Python
music.play("pursuit_goes_on")    #1 BGMを 流す
スック
スック

今回のプログラムに「DOVA-SYNDROME」サイトから
Pursuit Goes on
by MFP【Marron Fields Production】
 BGMの曲として お借りしました。 ありがとうございます。

ラッチ先生
ラッチ先生

最後に
効果音をつけましょう!
 ・ ねずみを クリックする音
 ・ ねずみが 左端に到達した音

Pythonパイソン zeroゼロには、soundsサウンズオブジェクトが標準装備であります。

次の手順で 行います

pythonしよう!量産05:zipの中身
スック
スック

今回のプログラムに
Chisato’s Website」サイトから
「Accent. Synth-Bell. E-Major[01C](LFO)」アクセント
「Power Up Item[01](Fast. Pitch Bend Up」:アクションゲームの効果音

 効果音を お借りしました。 ありがとうございます。

Python
def nezumi_move(): 
    global game 

    if game == "play": 
        for nezumi in nezumis[:]: 
            if nezumi.state == "move": 
                nezumi.x -= 5 

                if nezumi.x < 50: 
                    game = "over" 
                    music.stop() 
                    sounds.accent_synth_bell_e_major_01c_lfo.play()  #1 効果音を入れる
Python
def on_mouse_down(pos): 
    for nezumi in nezumis: 
        if nezumi.collidepoint_pixel(pos): 
            nezumi.state = "mosaic" 
            sounds.power_up_item_01_fast_pitch_bend_up.play()  #2 効果音を入れる

BGM提供:DOVA-SYNDROME
https://dova-s.jp/
Pursuit Goes on by MFP【Marron Fields Production】

効果音提供:Chisato’s Website
https://chisatosound.sakura.ne.jp/index.html
「Accent. Synth-Bell. E-Major[01C](LFO)」:アクセント
「Power Up Item[01](Fast. Pitch Bend Up」:アクションゲームの効果音

スック
スック

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

ラッチ先生
ラッチ先生

今回は、
左へ動くねずみを たたくプログラムを 作りました。

ねずみ たたき!
Python
import pgzrun
from pgzhelper import *
import random
from visual import VisualEffect

WIDTH = 800
HEIGHT = 590

nezumis = [] 
message = Actor("gameover", (400, 300)) 

game = "play" 

def draw():
    screen.blit("background", (0, 0))
    if game == "over":    
        message.draw()
    for nezumi in nezumis: 
        nezumi.draw(screen)

def create_nezumi(): 
    x = 750                           
    y = random.randint(50, HEIGHT-50)
    nezumi = VisualEffect("nezumi", (x, y)) 
    nezumi.state = "move" 
    nezumis.append(nezumi) 

clock.schedule_interval(create_nezumi, 0.8) 

def nezumi_move(): 
    global game 

    if game == "play": 
        for nezumi in nezumis[:]: 
            if nezumi.state == "move": 
                nezumi.x -= 5 

                if nezumi.x < 50: 
                    game = "over" 
                    music.stop() 
                    sounds.accent_synth_bell_e_major_01c_lfo.play() 
                    clock.unschedule(create_nezumi) 
                    nezumis.clear() 
                    nezumis.append(nezumi) 

            elif nezumi.state == "mosaic": 
                result = nezumi.process_animation() 
                if result == "finished":
                    nezumis.remove(nezumi) 
                              
def on_mouse_down(pos): 
    for nezumi in nezumis: 
        if nezumi.collidepoint_pixel(pos): 
            nezumi.state = "mosaic" 
            sounds.power_up_item_01_fast_pitch_bend_up.play() 
            nezumi.mosaic_animation() 
            break 
        
def update(): 
    nezumi_move() 
    
music.play("pursuit_goes_on")  

pgzrun.go()
ラッチ先生
ラッチ先生

リストからキャラクターを削除する場合は、
リストのコピーを 使ってね。

スック
スック

forフォー nezumiネズミ inイン nezumisネズミズ[:] の使い方を 覚えてね!

それじゃ、またね!

スポンサーリンク
ABOUT ME
ラッチ先生
ラッチ先生
こんにちは!
「Pythonしよう!楽しく学べるプログラミング教室」の学長、ラッチです。

scratchのように楽しく学べるPython講座です。
・図やアニメーションを使って、わかりやすく楽しく学べる!
・毎回の授業が新しい発見の連続!
・プログラミングの考え方が身につき、自分のやりたいことが形にできるように。

この3点をモットーにサイトを立ち上げました。 よろしくお願いします
記事URLをコピーしました