今回はプログラミング言語の「Python」を使って作成しました。
岩が放物運動をするプログラムです。
■ プログラム
import traceback try: import sys import pygame from pygame.locals import QUIT, Rect, KEYDOWN, K_SPACE, K_LEFT, K_RIGHT, K_UP, K_DOWN import random pygame.init() pygame.key.set_repeat(5, 5) SURFACE = pygame.display.set_mode((600, 600)) FPSCLOCK = pygame.time.Clock() class Ship(): """ 自機オブジェクト """ def __init__(self, rect): self.ship = pygame.image.load("ship.png") self.rect = rect def move(self, diff_x, diff_y): """ オブジェクトを移動 """ self.rect.move_ip(diff_x, diff_y) def draw(self): """ オブジェクトを描画 """ #pygame.draw.rect(SURFACE, (255, 250, 0), self.rect) SURFACE.blit(self.ship, self.rect.topleft) class Ball(): """ ボールオブジェクト """ def __init__(self, rect): self.rock = pygame.image.load("rock.png") self.rect = rect self.velocityInit = [random.uniform(1.0, 10.0), -20] self.velocity = self.velocityInit.copy() self.accel = (0, random.random()) self.col = (random.randint(0, 255), random.randint(0, 255), random.randint(0, 255)) def move(self, diff_x, diff_y): """ オブジェクトを移動 """ self.rect.move_ip(diff_x, diff_y) def draw(self): """ オブジェクトを描画 """ SURFACE.blit(self.rock, self.rect.topleft) #pygame.draw.circle(SURFACE, self.col, (self.rect.x + 5, self.rect.y + 5), 10) #pygame.draw.rect(SURFACE, (random.randint(0, 255), random.randint(0, 255), random.randint(0, 255)), self.rect) def main(): """ ボールインスタンス生成 """ ballList = [Ball(Rect(600, 300, 64, 64)) for i in range(15)] """ 自機インスタンス生成 """ ship = Ship(Rect(0, 0, 90, 60)) ship_y = 0 ship_x = 0 """ 爆発エフェクト """ bang_image = pygame.image.load("bang.png") """ スコア """ score = 0 sysfont = pygame.font.SysFont(None, 36) game_over = False """ メインルーチン """ while True: if not game_over: score += 10 is_space_down = False is_left_down = False is_right_down = False is_up_down = False is_down_down = False for event in pygame.event.get(): if event.type == QUIT: pygame.quit() sys.exit() elif event.type == KEYDOWN: if event.key == K_SPACE: is_space_down = True elif event.key == K_LEFT: is_left_down = True ship_x -= 0.5 elif event.key == K_RIGHT: is_right_down = True ship_x += 0.5 elif event.key == K_UP: is_up_down = True ship_y -= 0.5 elif event.key == K_DOWN: is_down_down = True ship_y += 0.5 """ 自機移動 """ ship.move(ship_x, ship_y) """ ボール移動 """ for ball in ballList: ball.velocity[1] += ball.accel[1] ball.move(-1 * ball.velocity[0], ball.velocity[1]) if ball.rect.x < 0: ball.rect.x = 600 if ball.rect.y > 600: ball.velocity[1] = ball.velocityInit[1] #ball.accel = (0, random.random() * 2) """ 自機とボールの衝突 """ for ball in ballList: if ship.rect.colliderect(ball): game_over = True """ 描画 """ SURFACE.fill((0, 0, 0)) """ 自機を描画する """ ship.draw() """ ボールを描画する """ for ball in ballList: ball.draw() """ 爆発エフェクトを描画する """ if game_over: SURFACE.blit(bang_image, (ship.rect.x, ship.rect.y - 30)) """ スコアを描画する """ score_image = sysfont.render("score is {0}".format(score), True, (255, 255, 0)) SURFACE.blit(score_image, (400, 20)) #pygame.draw.rect(SURFACE, (255, 255, 255), rect) #pygame.time.delay(100) pygame.display.update() FPSCLOCK.tick(30) if __name__ == '__main__': main() except Exception as e: print("エラー情報\n" + traceback.format_exc()) input() |
■ 参考書
「Pythonゲームプログラミング 知っておきたい数学と物理の基本」
■ ゲーム用ライブラリ
「Pygame」
■ プログラミング言語