見出し画像

Lesson13 ミサイル搭載!

割引あり

前回までに敵キャラをたくさんつくってきました。

敵キャラと接触するとダメージを受けるようになってますね。
でもなんか不公平ですよね、こっちにも攻撃手段が欲しいところです。

というわけで今回はミサイル的な武器をつくっていきましょう!



画像データ

2枚の画像をダウンロードして>game >image フォルダに格納しておいてください。

続いて stage1.py を編集していきましょう。

stage1.py

#### stage1.py ####

############################################################################################
#### ライブラリの読み込み ####

import math

############################################################################################
#### メインプログラム ####
def main(hp,mp,idx):

    ########################################################################################
    #### 初期設定 ####

    new_bullets = pygame.sprite.Group()

    mouse_width = 50
    mouse_height = 50
    image = pygame.image.load("image/aiming.png")
    cursor_image = pygame.transform.scale(image, (mouse_width, mouse_height))
    pygame.mouse.set_visible(False)
    mouse_x, mouse_y = 480, 400
    
    ########################################################################################
    #### プレイヤーのスプライト ####

   
    ########################################################################################
  
    ## 省略 ##
    
    ########################################################################################
    #### ボスキャラのスプライト ####

    
    ########################################################################################
    #### 弾丸のスプライト ####
    del_time_bullet = 5
    image00 = pygame.image.load("image/bullet.png")
    image_bullet = pygame.transform.scale(image00, (20,20))
    def clone_bullet(hp,frame_count,new_bullets,fire):
        if fire == True and player_turn == False:    
            fire = False
            copy_bullet = player
            attack_speed = 10
            attack_x = mouse_x - copy_bullet.x
            attack_y = mouse_y - copy_bullet.y
            aiming_angle = aiming_angle = math.degrees(math.atan2(attack_y, attack_x))
            attack_speed_x = round(attack_speed * math.cos(math.radians(aiming_angle)), 3)
            attack_speed_y = round(attack_speed * math.sin(math.radians(aiming_angle)), 3)
            new_bullet = Player(
                            x=copy_bullet.x+5,
                            y=copy_bullet.y+5,
                            image=image_bullet,
                            speed_x = attack_speed_x,
                            speed_y = attack_speed_y,
                            rotation_speed=copy_bullet.rotation_speed,
                            start_angle=copy_bullet.start_angle,
                            damage_multiplier=copy_bullet.damage,
                            size_multiplier=copy_bullet.size)
            new_bullet.creation_frame = frame_count
            if len(new_bullets.sprites()) <= 10:
                new_bullets.add(new_bullet)

        for new_bullet in new_bullets:
            new_bullet.update(player_turn)
            new_bullet.draw(screen)
            if frame_count - new_bullet.creation_frame >= del_time_bullet * 30:
                new_bullets.remove(new_bullet)
        return hp,fire

    ########################################################################################
    #### ゲームループ ####
    while True:
        screen.fill((0, 0, 0))
 
        #### ステージごとに変える要素 ####
        hp = white(hp)
        hp = blue(hp)
        hp = clone(hp,frame_count,new_enemies)
        hp = sans(hp)
        hp,fire = clone_bullet(hp,frame_count,new_bullets,fire)
        first_player()
    

        #### キー操作設定 ####
        # 単押し
        for event in pygame.event.get():
            if event.type == pygame.QUIT:
                pygame.quit()
                sys.exit()

            if event.type == pygame.MOUSEBUTTONDOWN and event.button == 1:
                fire = True
                #return hp,mp,idx

        
        
        ####省略####



        # 長押し
        if not player_turn:
            player.handle_input_carsol()


        mouse_x, mouse_y = pygame.mouse.get_pos()
        screen.blit(cursor_image, (mouse_x, mouse_y))


        ## 画面表示 ##
        hp_text = font.render(f"HP: {int(hp)}", True, (255, 255, 255))
        screen.blit(hp_text, (10, 10))   

コピペできましたか?
後半部分はすでに追加済みのコードも記載しています。
貼り付けするときに気をつけてくださいね。

では下記を実行してください。

>python main.py

実行結果: マウスクリックで弾丸が出ます

 

マウスの動きに合わせてエイムの十字が動きます。

マウスのクリックで弾丸が発射されます。
ダメージはまだありません。次回以降で敵をぶっ倒します。

では解説に移ります。いつものようにX(旧Twitter)で拡散し無料で鵜墨ください★


ここから先は

11,300字

この記事が気に入ったらサポートをしてみませんか?