game - Platform ゲーム - [5] Vectorの使用


Pygame Platform ゲームを作成 [5]

Vectorを使用する

Tasks

  • Playerクラスの動きをVectorを使ってよりリアルな動きを表現する

プロジェクトストラクチャー

  • project/ -- 全てを入れるフォルダ(ディレクトリ)
    • main.py -- ゲームをスタートするファイル
    • settings.py -- constantを入れておくファイル
    • sprites.py -- PlayerなどのSpriteのコードを書くファイル
sprites.py

# Sprite classes
import pygame as pg
from settings import *

# NEW!!
# pygameの2Dのベクトルクラスを作成
vec = pg.math.Vector2


class Player(pg.sprite.Sprite):
    def __init__(self):
        super().__init__()
        self.image = pg.Surface((30, 40))
        self.image.fill(YELLOW)
        self.rect = self.image.get_rect()
        self.rect.center = (WIDTH / 2, HEIGHT / 2)

        # pos = 現在値
        self.pos = vec(WIDTH / 2, HEIGHT / 2)
        # vel = 速度
        self.vel = vec(0, 0)
        # acc = 加速
        self.acc = vec(0, 0)


    def update(self):
        # フレームごとにaccを初期化
        self.acc = vec(0, 0)
        keys = pg.key.get_pressed()
        if keys[pg.K_LEFT]:
            # LEFT時にaccを計算 
            self.acc.x = -PLAYER_ACC
        if keys[pg.K_RIGHT]:
            # RIGHT時にaccを計算
            self.acc.x = PLAYER_ACC

        # 摩擦を計算
        self.acc += self.vel * PLAYER_FRICTION  # PLAYER_FRICTIONはsettings.pyから

        # 以下は物理の運動の法則から
        # Velocity に Accelerationを足す
        self.vel += self.acc
        # Position に Velocity を足す
        self.pos += self.vel + 0.5 * self.acc
        # 現在の位置に Positionを設定
        self.rect.center = self.pos

main.py

import pygame as pg
import random
from settings import *
from sprites import *


class Game:
    def __init__(self):
        # ゲームを初期化
        self.running = True
        pg.init()
        pg.mixer.init()
        self.screen = pg.display.set_mode((WIDTH, HEIGHT))
        pg.display.set_caption(TITLE)
        self.clock = pg.time.Clock()
        self.all_sprites = None
        self.playing = False

        self.player = None

    def new(self):
        # ゲームオーバー後のニューゲーム
        self.all_sprites = pg.sprite.Group()
        self.player = Player()
        self.all_sprites.add(self.player)
        self.run()

    def run(self):
        # ゲームループ
        self.playing = True
        while self.playing:
            self.clock.tick(FPS)
            self.events()
            self.update()
            self.draw()

    def update(self):
        # アップデート
        self.all_sprites.update()

    def events(self):
        # イベント
        for event in pg.event.get():
            if event.type == pg.QUIT:
                if self.playing:
                    self.playing = False
                self.running = False

    def draw(self):
        # 描画
        self.screen.fill(BLACK)
        self.all_sprites.draw(self.screen)
        pg.display.flip()

    def show_start_screen(self):
        # ゲームスタート画面
        pass

    def show_go_screen(self):
        # ゲームオーバー画面
        pass


g = Game()
g.show_start_screen()
while g.running:
    g.new()
    g.show_go_screen()

pg.quit()

settings.py

# game options/settings
TITLE = "Jumpy!"
WIDTH = 480
HEIGHT = 600
FPS = 60

# Player properties
PLAYER_ACC = 0.5
PLAYER_FRICTION = -0.12

# define colors
WHITE = (255, 255, 255)
BLACK = (0, 0, 0)
DARKGREY = (27, 140, 141)
LIGHTGREY = (189, 195, 199)
GREEN = (60, 186, 84)
RED = (219, 50, 54)
YELLOW = (244, 194, 13)
BLUE = (72, 133, 237)

Links

Github platformer

Pygame - Platform ゲーム - [4]

Pygame - Platform ゲーム - [3]

Pygame - Platform ゲーム - [2]

Pygame - Platform ゲーム - [1]