当前位置: 首页 > 后端技术 > Python

小白开个小项目,有源码和资料

时间:2023-03-26 19:16:38 Python

有点基础,不过不知道的同志可以看看。拿到素材才能看到结局。准备工作包括以下模块importpygameimportsysimportrandomfrompygame.localsimport*导入时间素材:音频,图片,字体开始编写代码首先导入上述模块,其次创建窗口和准备工作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=50tick=0pos=posAll[0]然后,记录得分score=0pygame.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]次=0times_max=10tick_max=30map=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=0gameover_max=100#loadmusicpygame.mixer.music.load("bg.mp3")pygame.mixer.music.play(-1)hitsound=pygame.mixer.Sound("hit.wav")hurtsound=pygame.mixer.Sound("aiyo2.wav")进行最后的细节设置while1:hamsur=ham1ratsur=rat1foreventinpygame.event.get():ifevent.type==pygame.QUIT:sys.exit()elifevent.type==MOUSEBUTTONDOWN:hamsur=ham2hitsound.play()mpos=pygame.mouse.get_pos()dis=pygame.math.Vector2(mpos[0]-pos[0],mpos[1]-pos[1])len=pygame.math.Vector2.length(dis)iflen=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))on.blit(end_on,(100,150))cd=int((gameover_max-gameover)/10)cd_on=score_font.render("重新开始倒计时{}".format(cd),True,(255,0,0))on.blit(cd_on,(100,200))gameover=gameover+1else:on.blit(map,(0,0))score_on=score_font.render("分数:{}/{}!".format(score,times+1),False,(255,0,0))on.blit(score_on,(200,10))iftick>tick_max:times=times+1a=random.randint(0,4)pos=posAll[a]tick=0else:tick=tick+1iftick>5:sur.blit(ratsur,(pos[0]-50,pos[1]-70))sur.blit(hamsur,(mpos[0]-50,mpos[1]-100))window.blit(on,(0,0))pygame.display.flip()time.sleep(0.04)如果游戏结束>gameover_max:pygame.mouse.set_visible(False)次=0score=0gameover=0效果展示和素材获取效果如下获取加群725479218获取