有点基础,但是不知道能干嘛的同志可一看。获取素材看最后。

准备工作

涉及到以下模块

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获取


CoXie带你学编程
195 声望13 粉丝