小白上手小項目,附帶源碼以及素材

有點基礎,但是不知道能幹嘛的同志可一看。獲取素材看最後。

準備工作

涉及到以下模塊

import pygame
import sys
import random
from pygame.locals import * 
import time

素材:音頻、圖片、字體

開始寫代碼

首先,導入上述所說的模塊,其次,創建窗口以及準備工作

pygame.init() 
window = pygame.display.set_mode([600, 400])  

sur = pygame.Surface([600, 400])  
clr = (0, 0, 255)
posAll = [[100, 150], [300, 150], [500, 150], [200, 300], [400, 300]]
rad = 50
tick = 0 
pos = posAll[0]  

然後,記錄分數

score = 0  
pygame.font.init()  
score_font = pygame.font.Font("MicrosoftYaqiHeiLight-2.ttf", 30)  
score_sur = score_font.render(str(score), False, (255, 0, 0))  

鼠標事件以及打地鼠

pygame.mouse.set_visible(False)  
mpos = [300, 200]  

times = 0  
times_max = 10 
tick_max = 30 
map = pygame.image.load("dds-map.jpg")
rat1 = pygame.image.load("rat1.png")
rat2 = pygame.image.load("rat2.png")  
ham1 = pygame.image.load("hammer1.png")  
ham2 = pygame.image.load("hammer2.png")  

gameover = 0  
gameover_max = 100  #

載入音樂

pygame.mixer.music.load("bg.mp3")
pygame.mixer.music.play(-1)  
hitsound = pygame.mixer.Sound("hit.wav") 
hurtsound = pygame.mixer.Sound("aiyo2.wav")  

進行最後的細節設置

while 1:
    hamsur = ham1
    ratsur = rat1
    for event in pygame.event.get():
        if event.type == pygame.QUIT:
            sys.exit()
        elif event.type == MOUSEBUTTONDOWN:  
            hamsur = ham2 
            hitsound.play()  
            mpos = pygame.mouse.get_pos()  
            dis = pygame.math.Vector2(mpos[0] - pos[0], mpos[1] - pos[1])  
            len = pygame.math.Vector2.length(dis)
            if len < rad:
                tick = 1000  
                score = score + 1  
                ratsur = rat2 
                hurtsound.play()  
        elif event.type == MOUSEMOTION:  
            mpos = pygame.mouse.get_pos() 

    if times >= times_max:
        sur.fill((0, 0, 0))  
        pygame.mouse.set_visible(True)
        end_font = pygame.font.Font("MicrosoftYaqiHeiLight-2.ttf", 48)  
        end_sur = score_font.render(
            "你的分數是:{}/{}!".format(score, times_max), True, (255, 0, 0)
        )  
        sur.blit(end_sur, (100, 150))
        cd = int((gameover_max - gameover) / 10)
        cd_sur = score_font.render(
            "重新開始倒計時{}".format(cd), True, (255, 0, 0)
        )  
        sur.blit(cd_sur, (100, 200)) 
        gameover = gameover + 1 
    else:
        sur.blit(map, (0, 0)) 
        score_sur = score_font.render(
            "分數:{}/{}!".format(score, times + 1), False, (255, 0, 0)
        )  
        sur.blit(score_sur, (200, 10))  
        if tick > tick_max:  
            times = times + 1  
            a = random.randint(0, 4)  
            pos = posAll[a]  
            tick = 0  
        else:  
            tick = tick + 1  
        if tick > 5:  
            sur.blit(ratsur, (pos[0] - 50, pos[1] - 70)) 
        sur.blit(hamsur, (mpos[0] - 50, mpos[1] - 100))  


    window.blit(sur, (0, 0))
    pygame.display.flip()  
    time.sleep(0.04)  


    if gameover > gameover_max:
        pygame.mouse.set_visible(False)
        times = 0
        score = 0
        gameover = 0

效果展示以及素材獲取

效果如下
加羣725479218獲取

獲取
加羣725479218獲取。這是一個純交流羣。廣告勿進

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