前往小程序,Get更优阅读体验!
立即前往
首页
学习
活动
专区
工具
TVP
发布
社区首页 >专栏 >小白上手小项目,附带源码以及素材

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

作者头像
用户2966292
发布2020-11-03 10:43:25
3900
发布2020-11-03 10:43:25
举报
文章被收录于专栏:CoXie带你学编程CoXie带你学编程

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

准备工作

涉及到以下模块

代码语言:javascript
复制
import pygame
import sys
import random
from pygame.locals import * 
import time

素材:音频、图片、字体

开始写代码

首先,导入上述所说的模块,其次,创建窗口以及准备工作

代码语言:javascript
复制
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]  

然后,记录分数

代码语言:javascript
复制
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))  

鼠标事件以及打地鼠

代码语言:javascript
复制
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  #

载入音乐

代码语言:javascript
复制
pygame.mixer.music.load("bg.mp3")
pygame.mixer.music.play(-1)  
hitsound = pygame.mixer.Sound("hit.wav") 
hurtsound = pygame.mixer.Sound("aiyo2.wav")  

进行最后的细节设置

代码语言:javascript
复制
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(
            "重新关注我的龚总好吧:CoXie带你学编程开始倒计时{}".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

效果展示以及素材获取

效果如下

本文参与 腾讯云自媒体分享计划,分享自微信公众号。
原始发表:2020-11-01,如有侵权请联系 cloudcommunity@tencent.com 删除

本文分享自 Python不等式 微信公众号,前往查看

如有侵权,请联系 cloudcommunity@tencent.com 删除。

本文参与 腾讯云自媒体分享计划  ,欢迎热爱写作的你一起参与!

评论
登录后参与评论
0 条评论
热度
最新
推荐阅读
目录
  • 准备工作
  • 开始写代码
  • 效果展示以及素材获取
领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档