見出し画像

Lesson8 壁をつくろう

割引あり

今回は、壁をつくります。

壁?

壁です。プレイヤーが動ける範囲を指定することで逃げられなくなります。

このシューティングゲームは敵キャラに触れるとダメージを受けるようにする予定で、プレイヤーが自由に逃げられるだけではあまりに簡単になってしまいます。

そこでプレイヤーの周囲を囲うように行動を制限します。
自由に逃げられないからこそ上手なプレイを目指せるようになります。


それでは早速書いていきましょう。前回の structure.py に追加で書きます。

structure.py

# 四角の基本設定
class Square(pygame.sprite.Sprite):
    def __init__(self, x, y,speed_x,speed_y, width, height):
        super().__init__()
        self.x = x
        self.y = y
        self.speed_x = speed_x
        self.speed_y = speed_y
        self.rect = pygame.Rect(x, y, width, height)
    
    #### 再表示関数 ####
    def update(self):
        self.x = self.x + self.speed_x
        self.y = self.y + self.speed_y
        
    def draw(self, screen):
        pygame.draw.rect(screen,(255, 255, 255), self.rect)


############################################################################################

次に stage1.py にも追加していきます。

stage1.py

#### stage1.py ####

##########################################################################################
#### ライブラリの読み込み ####

from structure import Button, Square

#### メインプログラム ####
def main(hp,mp,idx):     # 追加済み

    ########################################################################################
    #### 壁のスプライト ####
    # 壁ごとのパラメータ設定
    squares_params = [
        {'x': 10, 'y': 10, 'speed_x':0,'speed_y':0,'width':360,'height':10,},
        {'x': 10, 'y': 640, 'speed_x':0,'speed_y':0,'width':360,'height':10,},
        {'x': 10, 'y': 10, 'speed_x':0,'speed_y':0,'width':10,'height':640,},
        {'x': 360, 'y': 10, 'speed_x':0,'speed_y':0,'width':10,'height':640,},
        ]
    squares = [Square(**square_params) for square_params in squares_params]

    def square_structure():
        for square in squares:
            square.draw(screen)

            if player.y < square.y + square.rect.height and player.y + player.rect.height > square.y:
                if square.x < player.x + player.rect.width and square.x > player.x:
                    player.x = square.x - player.rect.width 
                elif square.x + square.rect.width > player.x and square.x + square.rect.width < player.x + player.rect.width:
                    player.x = square.x + square.rect.width

            if player.x < square.x + square.rect.width and player.x + player.rect.width > square.x:
                if square.y < player.y + player.rect.height and square.y > player.y:
                    player.y = square.y - player.rect.height
                elif square.y + square.rect.height > player.y and square.y + square.rect.height < player.y + player.rect.height:
                    player.y = square.y + square.rect.height

    ########################################################################################
    #### ゲームループ ####
    while True:           # 追加済み
        #### ステージ共通の要素 ####

        command_button()                                # 追加済み
        square_structure()

いつものように保存して、下記を実行します。

>python main.py

実行結果1: 画面左に白い四角が出現

周囲に壁が出現しました。これでプレイヤーの行動が制限されるようになります。

実行結果2: 壁にぶつかると進めません

当たり判定をつくったのですり抜けることもありません。4方向すべてに当たり判定をつくっています。

こういった構造物を複数用意することで、迷路ゲームも作れそうですね★
次の講座(ゲーム内容未定)は迷路でもいいですね~


それではまた解説をしていきます。中身が気になる方は有料ゾーンへ★
X(旧Twitter)で拡散してくれると無料で確認することができます

ここから先は

8,910字 / 8画像

この記事が気に入ったらサポートをしてみませんか?