今回はプログラミング言語の「Python」を使って作成しました。
自機が加速するプログラムです。
■ プログラム
import traceback
try:
import sys
from random import randint
import pygame
from pygame.locals import QUIT, Rect, KEYDOWN, K_SPACE, K_LEFT, K_RIGHT, K_UP, K_DOWN
#-----------------------------------------------------------
# Pygame初期設定
#-----------------------------------------------------------
pygame.init()
pygame.key.set_repeat(5, 5)
SURFACE = pygame.display.set_mode((800, 600))
FPSCLOCK = pygame.time.Clock()
#-----------------------------------------------------------
# スーパークラス定義
#-----------------------------------------------------------
class Drawable:
""" 全ての描画オブジェクトのスーパークラス """
def __init__(self, rect, offset0, offset1):
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 Ship(Drawable):
""" 自機オブジェクト """
def __init__(self):
super().__init__(Rect(0, 200, 90, 60), 0, 0)
def main():
""" メインルーチン """
#-----------------------------------------------------------
# インスタンス生成
#-----------------------------------------------------------
ship = Ship()
ship_y = 0
ship_x = 0
rectA = Rect(400 - 50, 300, 100, 100)
velocity = [0, 0]
accel = (0.5, 0)
score = 0
game_over = False
sysfont = pygame.font.SysFont(None, 36)
bang_image = pygame.image.load("bang.png")
#-----------------------------------------------------------
# ループ処理
#-----------------------------------------------------------
while True:
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
# 自機を移動
velocity[0] += accel[0]
ship.move(velocity[0], velocity[1])
if ship.rect.x > 890:
ship.rect.x = 0
velocity[0] = 0
#ship.move(ship_x, ship_y)
# 衝突判定
#if ship.rect.colliderect(rectA):
# score_image = sysfont.render("Hitting!", True, (255, 255, 0))
#else:
# score_image = sysfont.render("Not Hitting", True, (255, 255, 0))
# 描画
SURFACE.fill((0, 0, 0))
""" ブロックを描画する """
#pygame.draw.rect(SURFACE, (255, 255, 0), rectA)
""" スコアを描画する """
#score_image = sysfont.render("score is {}".format(score), True, (0, 0, 225))
score_image = sysfont.render("velocity : {0}".format(velocity[0]), True, (255, 255, 0))
SURFACE.blit(score_image, (600, 20))
""" 爆発エフェクトを描画する """
if game_over:
SURFACE.blit(bang_image, (ship_x, ship_y-40))
""" 自機を描画する """
ship.draw()
pygame.display.update()
FPSCLOCK.tick(30)
if __name__ == '__main__':
main()
except Exception as e:
print("エラー情報\n" + traceback.format_exc())
input()
|
■ 参考書
「Pythonゲームプログラミング 知っておきたい数学と物理の基本」
■ ゲーム用ライブラリ
「Pygame」
■ プログラミング言語



