最近用python做了个羊了个羊,项目链接。

项目源代码如下:

#导入头文件
import pygame
from pygame import *
pygame.init()
from sys import exit
from random import randint
from collections import Counter
import msvcrt#定义开始界面
class Start():#初始化def __init__(self):#创建窗口self.startSurface=pygame.display.set_mode((300,500))#设置标题pygame.display.set_caption("weather game")#设置图标ico=pygame.image.load("ico\\0.ico")pygame.display.set_icon(ico)#显示背景self.bg=pygame.transform.scale(pygame.image.load("img\\bg\\bg.jpg"),(300,500))self.startSurface.blit(self.bg,(0,0))#导入字体文件self.font=pygame.font.Font("font\\VladimirScript.ttf",32)self.bigFont=pygame.font.Font("font\\VladimirScript.ttf",60)self.fontColor=pygame.Color((0,0,0))#创建文字对象weathergameText=self.bigFont.render("weather game",True,self.fontColor)#创建文字显示框weathergameTextRect=weathergameText.get_rect(center=(150,50))#显示文字self.startSurface.blit(weathergameText,weathergameTextRect)#显示开始游戏startText=self.font.render("Start the game",True,self.fontColor)startTextRect=startText.get_rect(center=(150,420))self.startSurface.blit(startText,startTextRect)#获取文字显示框坐标self.startTextX=startTextRect[0]self.startTextY=startTextRect[1]self.startTextX1=startTextRect[0]+startTextRect[2]self.startTextY1=startTextRect[1]+startTextRect[3]#显示结束游戏exitText=self.font.render("Exit",True,self.fontColor)exitTextRect=exitText.get_rect(center=(150,460))self.startSurface.blit(exitText,exitTextRect)self.exitTextX=exitTextRect[0]self.exitTextY=exitTextRect[1]self.exitTextX1=exitTextRect[0]+exitTextRect[2]self.exitTextY1=exitTextRect[1]+exitTextRect[3]def start(self):state="normal"mouseX=0mouseY=0while state=="normal":for event in pygame.event.get():#判断是否按下escif event.type == KEYDOWN:if event.key == K_ESCAPE:#结束函数state="exit"return state#判断是否按下关闭if event.type == QUIT:state="exit"return state#如果鼠标按下,就获取坐标if event.type == MOUSEBUTTONDOWN:mouseX,mouseY = pygame.mouse.get_pos()#判断是否按下开始游戏或退出if mouseX>self.startTextX and mouseX<self.startTextX1 and mouseY<self.startTextY1 and mouseY>self.startTextY:state="start game"return stateif mouseX>self.exitTextX and mouseX<self.exitTextX1 and mouseY<self.exitTextY1 and mouseY>self.exitTextY:state="exit"return stateif state=="normal":pygame.display.update()class Game():def __init__(self):#创建窗口self.playSurface=pygame.display.set_mode((300,500))pygame.display.set_caption("weather game")ico=pygame.image.load("ico\\0.ico")pygame.display.set_icon(ico)self.bg=pygame.transform.scale(pygame.image.load("img\\bg\\bg.jpg"), (300,500))self.bgColor=pygame.Color(113,150,159)self.playSurface.blit(self.bg,(0,0))#创建点击区列表self.weatherList = []for i in range(0,3):weatherLine = []for j in range(0,3):weatherLine.append(randint(0,7))self.weatherList.append(weatherLine)#创建图标列表self.weatherIconList = []for i in range(0,8):self.weatherIconList.append(pygame.image.load(f'img\WeatherIcon\{i}.png'))weatherIconSize=self.weatherIconList[i].get_rect()self.weatherIconList[i]=pygame.transform.scale(self.weatherIconList[i], (int(weatherIconSize[2]/2),int(weatherIconSize[3]/2)))for x in range(0,int(weatherIconSize[2]/2)):for y in range(0,int(weatherIconSize[3]/2)):rbga=self.weatherIconList[i].get_at((x,y))if rbga!=(255,255,255,0):self.weatherIconList[i].set_at((x,y),(0,0,0,rbga[3]))#创建抵消区列表self.offsettingAreaList = []#初始化分数self.score=0#导入字体self.font=pygame.font.Font('font\VladimirScript.ttf', 32)self.fontColor = pygame.Color(0,0,0)#显示分数s="Total score:"+str(self.score)scoreText=self.font.render(s,True,self.fontColor)self.playSurface.blit(scoreText,(0,0))def game(self):signOut=Falsewhile not signOut:mouseX=0mouseY=0#显示点击区iconX=45iconY=120for i in range(0,3):for j in range(0,3):rect=self.weatherIconList[self.weatherList[i][j]]rectSize=rect.get_rect()rectW=rectSize[2]rectH=rectSize[3]rectX=(70-rectW)/2+iconXrectY=(70-rectH)/2+iconYself.playSurface.blit(rect,(rectX,rectY))iconX+=70iconX=45iconY+=70#显示抵消区offsetX=0offsetY=430offsettingAreaListSize=len(self.offsettingAreaList)offsetOrdinalNumber=1for i in range(0,7):if offsetOrdinalNumber<=offsettingAreaListSize:offsetIcon=self.weatherIconList[self.offsettingAreaList[i]]else:break;offsetIconSize=offsetIcon.get_rect()offsetIconW=offsetIconSize[2]offsetIconH=offsetIconSize[3]offsetIconX=(42-offsetIconW)/2+offsetXoffsetIconY=(42-offsetIconH)/2+offsetYself.playSurface.blit(offsetIcon,(offsetIconX,offsetIconY))offsetOrdinalNumber+=1offsetX+=42for event in pygame.event.get():if event.type == KEYDOWN:if event.key == K_ESCAPE:return "exit"if event.type == QUIT:return "exit"if event.type == MOUSEBUTTONDOWN:mouseX,mouseY = pygame.mouse.get_pos()x=30y=100for i in range(0,3):for j in range(0,3):#判断鼠标是否点到点击区图标if mouseX>x and mouseX<(x+80) and mouseY>y and mouseY<(y+80):#更行抵消区与点击区区self.offsettingAreaList.append(self.weatherList[i][j])self.weatherList[i][j]=randint(0,7)#判断是否有三个重复的图标,如果有,就删除并加一分offset=Counter(self.offsettingAreaList)for key,value in offset.items():if value==3:for i in range(3):self.offsettingAreaList.remove(key)self.score+=1#更新分数显示self.playSurface.blit(self.bg,(0,0))s="Total score:"+str(self.score)scoreText=self.font.render(s,True,self.fontColor)self.playSurface.blit(scoreText,(0,0))    #判断输赢if self.score==10:signOut=Truereturn "You win"if len(self.offsettingAreaList)==7:signOut=Truereturn "You lost"x+=80x=30y+=80if not signOut:pygame.display.update()# print("press any key to continue...")# while True:#     if ord(msvcrt.getch()):#         break;class End():def __init__(self,gameReturn):self.endSurface=pygame.display.set_mode((300,500))pygame.display.set_caption("weather game")ico=pygame.image.load("ico\\0.ico")pygame.display.set_icon(ico)self.bg=pygame.transform.scale(pygame.image.load("img\\bg\\bg.jpg"),(300,500))self.endSurface.blit(self.bg,(0,0))self.font=pygame.font.Font("font\\VladimirScript.ttf",32)self.bigFont=pygame.font.Font("font\\VladimirScript.ttf",80)self.fontColor=pygame.Color((0,0,0))weathergameText=self.bigFont.render(gameReturn,True,self.fontColor)weathergameTextRect=weathergameText.get_rect(center=(150,250))self.endSurface.blit(weathergameText,weathergameTextRect)def end(self):state="normal"while state=="normal":for event in pygame.event.get():if event.type == KEYDOWN:if event.key == K_ESCAPE:state="exit"return stateif event.type == QUIT:state="exit"return stateif event.type == MOUSEBUTTONDOWN:mouseX,mouseY = pygame.mouse.get_pos()if state=="normal":pygame.display.update()#播放音乐
pygame.mixer.init()
pygame.mixer.music.load('music/Daocao.mp3')
pygame.mixer.music.play(-1,5) startObject=Start()
startReturn=startObject.start()
if startReturn!="exit":gameObject=Game()gameReturn=gameObject.game()if gameReturn!="exit":endObject=End(gameReturn)endObject.end()

如有漏洞或需要改进的地方,请各位多多指教。

python版羊了个羊相关推荐

  1. python版的羊了个羊,你见过没?

    最近不是出了个超火的游戏嘛,周围小伙伴都说好难玩,玩不过 看了下,发现就是个变种的连连看嘛,就尝试下写一个出来玩玩,当作是练手了 刚好找到一位大佬用C语言写的羊羊,结果发现是windows版的,编译起 ...

  2. 羊了个羊增加版:鸡了个鸡,坤了个坤

    点击上方"优派编程"选择"加入星标",第一时间关注原创干货 羊了个羊增强版:鸡了个鸡,坤了个坤 https://www.fang1688.cn/study-co ...

  3. 羊了个羊游戏h5网页版源码

    羊了个羊游戏h5网页版源码 h5网页版,数据还是官方的,一个小demo. 无后台下载附件源码,上传至服务器访问域名即可 不可商业,不可商业,不可商业 只限交流学习 免广告用道具 下载地址 https: ...

  4. 羊了个羊3D版,迄今我见过还原度最佳游戏!还支持微信授权和教程视频

    因为过不了<羊了个羊>第2关,有一位开发者很是生气.说的是解决不了问题,就解决提出问题的人! 直接自己做一个<羊了个羊>而且还要是个3D的,这就是今天要介绍的一位开发者:花叔. ...

  5. 网页版羊了个羊 Vue3 实现

    1. 成果展示 1.1 动图展示 1.2 主要界面展示 2. 主要功能介绍 Vue3 的羊了个羊主要有两个页面: 入口界面 实现了难度选择,包括简单.苦难.复杂等,不同的难度,卡片的数量.层级不同以及 ...

  6. 实现用java做一个简易版《羊了个羊》小游戏(附源代码)

    该项目是跟着这个b站视频一步一步写出来的,初学java有些地方我看不是很明白,但是讲解很仔细,大家可以看原视频,我没有添加背景音乐和背景图片,做出来的效果也勉勉强强. 代码已经上传到github上了, ...

  7. 复刻一个羊了个羊掘金商城版

    游戏逻辑 与羊了个羊逻辑一致,不再赘述 游戏实现 盛放元素的容器box,临时存储的容器temp,多余元素的容器source与source1,结果元素result <div id="bo ...

  8. python日历gui_[代码全屏查看]-Python版的农历日历Calendar,功能简单

    [1].[代码] [Python]代码 Python语言: Python版的农历日历Calendar,功能简单 #coding=utf-8 #代码修改自wangfei(wangfei@hanwang. ...

  9. 聚观早报 | 羊了个羊幕后推手月流水曾破亿;雷军卸任小米董事长

    今日要闻:<羊了个羊>幕后推手月流水曾破亿:"灵动岛" 已被用来开发小游戏:大疆发布 Osmo Action 3 运动相机:雷军卸任小米电子软件董事长:谷歌和Meta因 ...

最新文章

  1. PEP 0498 -- Literal String Interpolation 翻译(未完待续)
  2. 实用VUE 开发插件!!前端必备
  3. 打印首选项设置无效_文档打印小技巧分享--请收藏
  4. Windows7 最重要的70个技巧和窍门
  5. 四十一、Android Notification通知详解
  6. ABAP DESCRIBE语句
  7. java 字符串常用函数_Java学习笔记35:Java常用字符串操作函数
  8. 大数据高地,这样炼成!
  9. 400是什么错误_404、403、405、500 | 常见网页错误代码解析
  10. MVC3.0+knockout.js+Ajax 实现简单的增删改查
  11. 【quick-cocos2d-lua】 疯狂牛牛
  12. eclispe/myeclipse中输入法的问题
  13. Android 手势导航(Launcher3 部分)
  14. dell服务器新bois系统设置u盘启动,dell新版biosU盘启动顺序设置教程
  15. rimraf与windows的rmdir简单使用命令方法
  16. 自定义气泡效果(BubbleView)
  17. 名词解释atm网络_电信技术名词解释:什么是ATM技术
  18. java画篮球_PS教程!手把手教你绘制炫酷的科比篮球海报
  19. 电视机接口中英文介绍
  20. 图的遍历 DFS遍历(深学思维)

热门文章

  1. EasyUI - 设置accordion全部展开
  2. 韩服 永恒之塔 验证码识别实例测试[非商业用途]
  3. python入门函数编程_Python入门——面向函数的编程,过程
  4. 信息安全铁人三项赛真题解析_信息安全铁人三项赛二进制部分题解
  5. grafana开发与调试
  6. android生成ios程序,ECMobile:只需一步即可生成iOS或Android原生APP商城应用
  7. 微信小程序 RTMP 音视频 通话 ffmpeg_WebRTC与微信小程序音视频互通方案设计与实现...
  8. TikTok Shop如何入驻和如何批量上货
  9. 【008】【毕业设计】基于51单片机的烟雾报警系统proteus仿真与实物设计
  10. 2018年下半年网络工程师考试试题分析(3)