ziheng -接小球遊戲6.0

import random
import pygame as p
import time


def wenzi(screen, ziti_size, wenzi_content, xy, color=(0, 0, 0)):
    # 窗口,字體大小,文字內容,座標,顏色-默認爲黑色
    ziti = p.font.Font("ziti.ttf", ziti_size)
    text3 = ziti.render(wenzi_content, True, color)
    screen.blit(text3, xy)    



def main():
    p.init()
    screen_width, screen_height = 1080, 652
    screen = p.display.set_mode((screen_width, screen_height))
    # 設置標題 captiion --標題
    p.display.set_caption("無限接球")
    # 具體指應用函數的時候寫括號
    ballx = 100
    bally = 150
    # 分別爲:板的x座標,板的y座標,板的寬度,板的高度
    ban_x, ban_y, ban_k, ban_g = 300, screen_height - 40, 120, 40
    # 讓電腦沒過多少毫秒監控一次
    p.key.set_repeat(100, 1)
    # 1.設置字體 font 字體和大小

    score = 0
    smz = 5
    ziticolor = 0
    game_over = False
    win = False
    # time.sleep(3)
    # 精靈圖
    # 加載圖片  iamge圖片   load加載
    sanjitou = p.image.load("ball.png")
    bj = p.image.load("bg1.jpg")
    
    # 音樂初始化 
    p.mixer.init()
    # 背景音樂
    p.mixer.music.load("xxxxx.mp3")
    p.mixer.music.set_volume(0.3)
    p.mixer.music.play(-1)
    # 音效
    die = p.mixer.Sound("xxxxx.wav")
    die.set_volume(0.4)
    
    while True:
        for sj in p.event.get():
            # print(sj)
            if sj.type == p.QUIT:
                p.quit()
            # 判斷事件類型是不是按鍵 鍵 key
            elif sj.type == p.KEYDOWN:
                # 判斷按了哪一個鍵
                if sj.key == p.K_RIGHT:
                    ban_x = ban_x + 10
                elif sj.key == p.K_LEFT:
                    ban_x = ban_x - 10
            # 判斷事件類型是不是鼠標動作 motion動作
            elif sj.type == p.MOUSEMOTION:
                ban_x, _ = sj.pos

        if game_over:
            die.play()
            wenzi(screen, 80, "嗝屁嘍", (330, 250))
        elif win:
            wenzi(screen, 80, "yun win", (330, 250))
        else:
            # 設置背景顏色
            screen.blit(bj, (0, 0))
            bally = bally + 5
            # 判斷沒接到小球 ,生命值減1分
            if bally > screen_height:
                bally = 0
                ballx = ballx + 100
                smz = smz - 1
                ziticolor = ziticolor + 50
            if ballx > screen_width:
                ballx = 0
            # 左邊空氣牆
            if ban_x < -50:
                ban_x = 5
            elif screen_width < ban_x:
                ban_x = screen_width - 100

            # 判斷接到小球才加1分
            if ban_x < ballx < ban_x + ban_k:
                if ban_y < bally < ban_y + ban_g:
                    score = score + 1
                    bally = 0
                    ballx = random.randint(0, screen_width)

            # 判斷咯屁
            if smz == 0:
                game_over = True
            if score > 5:
                win = True

            # 畫一個小球
            screen.blit(sanjitou, (ballx, bally))
            # 畫一個長方形  rect 屏幕 顏色
            p.draw.rect(screen, (172, 85, 255), (ban_x, ban_y, ban_k, ban_g), 0)
            # 2.通過設置的字體大小來設置文字和顏色
            wenzi(screen, 30, "分數:%d" % score, (0, 0))
            wenzi(screen, 30, "生命值:%d" % smz, (screen_width - 180, 0))

        p.display.update()


if __name__ == '__main__':
    main()
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章