game - Platform ゲーム - [2] settings.pyの作成


Pygame Platform ゲームを作成 [2]

settings.py を作成

Tasks

  • settings.pyを作成
  • main.pyにあるconstantをsettings.pyに移動する
  • main.pyにsettings.pyのconstantをimportする

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

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

# game options/settings
TITLE = "My Game"
WIDTH = 360
HEIGHT = 480
FPS = 30

# 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)

main.py

import pygame
import random
# NEW!!
from settings import *

# initialize pygame and create window
pygame.init()
pygame.mixer.init()
# NEW!! WIDTH と HEIGHT はsettings.pyから読み込み
screen = pygame.display.set_mode((WIDTH, HEIGHT))
# NEW!! TITLE はsettings.pyから読み込み
pygame.display.set_caption(TITLE)
clock = pygame.time.Clock()

all_sprites = pygame.sprite.Group()

# Game Loop
running = True
while running:
    # keep loop running at the right speed
    clock.tick(FPS)
    # Process input (events)
    for event in pygame.event.get():
        if event.type == pygame.QUIT:
            running = False

    # update
    all_sprites.update()

    # Draw / render
    screen.fill(BLACK)
    all_sprites.draw(screen)

    pygame.display.flip()

pygame.quit()


Links

Github platformer

Pygame - Platform ゲーム - [1]