可樂爭霸賽

夜-澤發表於2020-12-06

沒錯,又是我,我又來更新了。這次,我給大家分享的是可樂爭霸賽。

初始設定:

import pygame,sys,time
pygame.init()
screen = pygame.display.set_mode((640, 400))
pygame.display.set_caption("可樂爭霸賽")
#載入所需素材
dSound = pygame.mixer.Sound("dSound.wav")
bg = pygame.image.load("bg.png")
cola = pygame.image.load("cola.png")
after_drink = pygame.image.load("after_drink.png")
before_drink = pygame.image.load("before_drink.png")
#初始變數
click = 0  # 點選次數
colaNum = 0  # 喝可樂數量
t1 = time.time()

主迴圈:

#主迴圈
while True:
    for event in pygame.event.get():
        if event.type == pygame.QUIT:
            pygame.quit()
            sys.exit()

        elif event.type == pygame.MOUSEBUTTONDOWN:
            click = click + 1
            dSound.play()
            print("點選次數", click)
            # 可樂計數
            if click % 5 == 0:
                colaNum = colaNum + 1
                print("已經喝了的可樂數:", colaNum)
            

結束判斷:

    # 結束判斷
    if colaNum >= 3:
        t2 = time.time()
        t = t2 - t1
        print("遊戲結束,用時", t, "秒")
        break
    screen.blit(bg, (0, 0))

    if click % 2 == 0:
        screen.blit(before_drink, (200, 148))
    elif click % 2 == 1:
        screen.blit(after_drink, (200, 148))
    
    screen.blit(cola, (150, 255))
    screen.blit(cola, (300, 255))
    screen.blit(cola, (450, 255))
    
    pygame.display.update()

 

完整程式碼:

import pygame,sys,time
pygame.init()
screen = pygame.display.set_mode((640, 400))
pygame.display.set_caption("可樂爭霸賽")
#載入所需素材
dSound = pygame.mixer.Sound("dSound.wav")
bg = pygame.image.load("bg.png")
cola = pygame.image.load("cola.png")
after_drink = pygame.image.load("after_drink.png")
before_drink = pygame.image.load("before_drink.png")
#初始變數
click = 0  # 點選次數
colaNum = 0  # 喝可樂數量
t1 = time.time()
#主迴圈
while True:
    for event in pygame.event.get():
        if event.type == pygame.QUIT:
            pygame.quit()
            sys.exit()

        elif event.type == pygame.MOUSEBUTTONDOWN:
            click = click + 1
            dSound.play()
            print("點選次數", click)
            # 可樂計數
            if click % 5 == 0:
                colaNum = colaNum + 1
                print("已經喝了的可樂數:", colaNum)
            
    # 結束判斷
    if colaNum >= 3:
        t2 = time.time()
        t = t2 - t1
        print("遊戲結束,用時", t, "秒")
        break
    screen.blit(bg, (0, 0))

    if click % 2 == 0:
        screen.blit(before_drink, (200, 148))
    elif click % 2 == 1:
        screen.blit(after_drink, (200, 148))
    
    screen.blit(cola, (150, 255))
    screen.blit(cola, (300, 255))
    screen.blit(cola, (450, 255))
    
    pygame.display.update()

 

相關文章