• 小白上手小项目,附带源码以及素材


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

    准备工作

    涉及到以下模块

    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获取。这是一个纯交流群。广告勿进

  • 相关阅读:
    VLC 播放完毕后自动退出的问题
    配置Linux文件句柄数
    java获取某一天的邮件
    oracle wm_concat函数将多行转字段函数
    Vue学习(二十五)TS支持
    Vue学习(二十四)render函数
    Vue学习(二十三)路由全攻略
    git commit规范&工具安装使用
    Vue学习(二十二)自定义指令全攻略
    Vue学习(二十一)$emit全攻略
  • 原文地址:https://www.cnblogs.com/CoXieLearnPython/p/13909545.html
Copyright © 2020-2023  润新知