今回はプログラミング言語の「Python」を使って作成しました。
下記の本に収録されている物理エンジン「Tiny2D」を使って物理演算をやってみました。
■ プログラム
import traceback try: import sys from random import randint import pygame from pygame.locals import QUIT, Rect, KEYDOWN, K_LEFT, K_RIGHT, K_UP, K_DOWN from tiny_2d import Engine, RectangleEntity, CircleEntity,\ LineEntity, SHAPE_CIRCLE, SHAPE_RECTANGLE, SHAPE_LINE pygame.init() SURFACE = pygame.display.set_mode([550, 600]) FPSCLOCK = pygame.time.Clock() def main(): """ main routine """ colors = [(255, 0, 0), (255, 165, 0), (242, 242, 0), (0, 128, 0), (128, 0, 128), (0, 0, 250)] colors02 = [(255, 255, 255)] engine = Engine(0, 0, 600, 800, 0, 9.8) rect = RectangleEntity(205, 500, 150, 10) rect.color = (255, 255, 255) engine.entities.append(rect) for xpos in range(7): for ypos in range(3): circle = CircleEntity(xpos * 60 + 100, ypos * 60 + 100, 5, True) circle.color = colors02[0] engine.entities.append(circle) for _ in range(80): circle = CircleEntity(randint(0, 400) + 50, randint(0, 200), 10, False) circle.color = colors[randint(0, 5)] circle.velocity.xpos = randint(0, 10) - 5 circle.velocity.ypos = randint(0, 10) - 5 engine.entities.append(circle) pygame.key.set_repeat(5, 5) #---------------------------------------------------- # ループ処理 #---------------------------------------------------- while True: for event in pygame.event.get(): if event.type == QUIT: pygame.quit() sys.exit() elif event.type == KEYDOWN: if event.key == K_UP: for entity in engine.entities: if entity.shape == SHAPE_RECTANGLE: entity.ypos -= 1 if event.key == K_DOWN: for entity in engine.entities: if entity.shape == SHAPE_RECTANGLE: entity.ypos += 1 if event.key == K_LEFT: for entity in engine.entities: if entity.shape == SHAPE_RECTANGLE: entity.xpos -= 1 if event.key == K_RIGHT: for entity in engine.entities: if entity.shape == SHAPE_RECTANGLE: entity.xpos += 1 engine.step(0.01) """ 描画 """ SURFACE.fill((0, 0, 0)) for entity in engine.entities: if entity.shape == SHAPE_RECTANGLE: rect = Rect(entity.xpos, entity.ypos, entity.width, entity.height) pygame.draw.rect(SURFACE, entity.color, rect) elif entity.shape == SHAPE_CIRCLE: pos = (int(entity.xpos), int(entity.ypos)) pygame.draw.circle(SURFACE, entity.color, pos, entity.radius) elif entity.shape == SHAPE_LINE: pos0 = (int(entity.pos0[0]), int(entity.pos0[1])) pos1 = (int(entity.pos1[0]), int(entity.pos1[1])) pygame.draw.line(SURFACE, entity.color, pos0, pos1, width=10) pygame.display.update() FPSCLOCK.tick(60) if __name__ == '__main__': main() except Exception as e: print("エラー情報\n" + traceback.format_exc()) input() |
■ 参考書
「Pythonゲームプログラミング 知っておきたい数学と物理の基本」
■ ゲーム用ライブラリ
「Pygame」
■ プログラミング言語