ヘディングキャッチゲーム

ヘディングキャッチゲーム(中1男子/作)
講師から一言
scratchでおなじみのヘディングキャッチゲームですね!

テクノロ生は最初にscratchでこのゲームを作るところから始まります。それをPythonで見事に表現しましたね。

ボールの移動量をvbxとvbyで定義しているので跳ね返る動作が上手にコーディングできています。この部分は移動量を数字で書いてしまうとコードがやたら長くなるのでこの書き方がベストでしょう!課題はリプレイボタンを押すとすぐにゲームが開始されるため、time.sleep()で待ちの動作をいれてみましょう。



Pythonで記述したコード
import pygame,sys,random

#変数定義
cx=50 #ネコのX座標
cy=400 #ネコのy座標
vcx=10 #ネコの移動量
bx=30 #ボールのX座標
by=30 #ボールのy座標
vbx=random.randint(3,5)#ボールの移動量
vby=random.randint(3,5) #ボールの移動量
right_flag=True #右向きフラグ
push_flag=False #ボタン
index =1
score=0
high_score=0

#pygameの初期化
pygame.init()

#ディスプレイの作成
screen=pygame.display.set_mode((800,600))

#タイトル
pygame.display.set_caption("ヘディングキャッチゲーム")

#画像のアップロード
背景=pygame.image.load("image\背景.png")
ネコ右=pygame.image.load("image\ネコ.png")
ボール=pygame.image.load("image\ボール.png")
リプレイボタン=pygame.image.load("image\リプレイボタン.png")

#画像の加工
ネコ左=pygame.transform.flip(ネコ右,True,False)

#rect
cat_rect=pygame.Rect(cx,cy,100,100)
ball_rect = pygame.Rect(bx,by,50,50)

#フォント
font = pygame.font.Font(None,150)
font1 = pygame.font.Font("font\日本語フォント.ttf",50)

game_over = font.render("Game Over",True,"blue")

#音声
BGM=pygame.mixer.Sound("sound\BGM.wav")
ヘディング=pygame.mixer.Sound("sound\ヘディング.wav")
失敗=pygame.mixer.Sound("sound\失敗.wav")
BGM.set_volume(0.3)
BGM.play(-1)

def リプレイ():
    global push_flag,btn,index,score,vbx,vby
    mdown=pygame.mouse.get_pressed()
    (mx,my)=pygame.mouse.get_pos()
    if mdown[0]:
        if btn.collidepoint(mx,my) and push_flag==False:
            index=1
            ball_rect.x=30
            ball_rect.y=30
            push_flag=True
            score=0
            vbx=random.randint(3,5)#ボールの移動量
            vby=random.randint(3,5) #ボールの移動量
    else:
        push_flag=False

def main():
    global right_flag,vcx,vbx,vby,index,btn,score,high_score

    if index==1:
        ヘディング回数 = font1.render("ヘディング回数:"+str(score)+"回",True,"orange")
        #転送設定
        screen.blit(背景,(0,0))
        screen.blit(ボール,ball_rect)
        screen.blit(ヘディング回数,(10,20))
        
        if right_flag==True:
            screen.blit(ネコ右,cat_rect)
        else:
            screen.blit(ネコ左,cat_rect)

        #キー入力の受付
        key = pygame.key.get_pressed()

        #ネコの移動処理
        if key[pygame.K_RIGHT] and cat_rect.x<600:
            right_flag=True
            vcx=10
        elif key[pygame.K_LEFT] and cat_rect.x>0:
            right_flag=False
            vcx=-10
        else:
            vcx=0
        cat_rect.x +=vcx

        #ボールの移動処理
        ball_rect.x +=vbx
        ball_rect.y +=vby

        # ボールが画面の端に触れたら跳ね返る処理
        # 左右の端
        if ball_rect.x <= 0 or ball_rect.x  >= 700:
            vbx = -vbx  # X方向の速度を反転
        # 上下の端
        if ball_rect.y <= 0 :
            vby = -vby  # Y方向の速度を反転

        #ボールとの衝突
        if ball_rect.colliderect(cat_rect):
            vbx = -vbx+random.randint(-2,2)
            vby = -vby+random.randint(-2,2)
            score +=1
            ヘディング.play()

        #地面との衝突
        if ball_rect.y >= 500:
            index=2
            失敗.play()
            
    #ゲームオーバー
    if index ==2:
        screen.blit(game_over,(100,200))
        btn=screen.blit(リプレイボタン,(600,500))
        if score>high_score:
            high_score =score
        ワールドスコア=font1.render("ワールドスコア:"+str(high_score)+"回",True,"gold")
        screen.blit(ワールドスコア,(10,70))
        リプレイ()

while True:
    main()
    pygame.display.update()
    for i in pygame.event.get():
        if i.type==pygame.QUIT:
            pygame.quit()
            sys.exit()