プログラミング言語「Python」で制作 Part93 | Photoshop CC Tutorials

今回はプログラミング言語の「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, 300, 90, 60), 0, 0)

    def main():
        """ メインルーチン """
        #-----------------------------------------------------------
        # インスタンス生成
        #-----------------------------------------------------------
        ship = Ship()
        ship_y = 0
        ship_x = 0
        rectA = Rect(400 - 50, 300, 100, 100)

        #-----------------------------------------------------------
        # プロパティ定義
        #-----------------------------------------------------------
        slope = randint(1, 6)
        walls = 80
        score = 0
        game_over = False
        hitFlag = False
        sysfont = pygame.font.SysFont(None, 36)
        bang_image = pygame.image.load("bang.png")
        holes = []
        for xpos in range(walls):
            holes.append(Rect(xpos * 10, 100, 10, 300))
        #-----------------------------------------------------------
        # ループ処理
        #-----------------------------------------------------------
        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)

                # 洞窟をスクロール
                edge = holes[-1].copy()
                test = edge.move(0, slope)
                if test.top <= 0 or test.bottom >= 400:
                    slope = randint(1, 6) * (-1 if slope > 0 else 1)
                    edge.inflate_ip(0, -40)
                edge.move_ip(10, slope)
                holes.append(edge)
                del holes[0]
                holes = [x.move(-10, 0) for x in holes]

                # 自機と洞窟の衝突
                hitFlag = False
                for hole in holes:
                    if ship.rect.colliderect(hole):
                        if hole.top < ship.rect.y and hole.bottom > ship.rect.y + 60:
                            hitFlag = True

                if not hitFlag:
                    game_over = True
            # 描画
            SURFACE.fill((0, 255, 0))

            """ 洞窟を描画する """
            for hole in holes:
                pygame.draw.rect(SURFACE, (0, 0, 0), hole)

            """ ブロックを描画する """
            #pygame.draw.rect(SURFACE, (255, 255, 0), rectA)

            """ スコアを描画する """
            score_image = sysfont.render("score is {}".format(score), True, (0, 0, 225))
            SURFACE.blit(score_image, (600, 20))

            """ 爆発エフェクトを描画する """
            if game_over:
                SURFACE.blit(bang_image, (ship.rect.x, ship.rect.y - 30))
            
            """ 自機を描画する """
            ship.draw()

            pygame.display.update()
            FPSCLOCK.tick(20)

    if __name__ == '__main__':
        main()

except Exception as e:
    print("エラー情報\n" + traceback.format_exc())

input()

 

■ 参考書

「ゲームを作りながら楽しく学べるPythonプログラミング (Future Coders(NextPublishing))」

 

■ ゲーム用ライブラリ

「Pygame」

 

■ プログラミング言語