最近用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=0
mouseY=0
while state=="normal":
for event in pygame.event.get():
#判断是否按下esc
if 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 state
if mouseX>self.exitTextX and mouseX<self.exitTextX1 and mouseY<self.exitTextY1 and mouseY>self.exitTextY:
state="exit"
return state
if 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=False
while not signOut:
mouseX=0
mouseY=0
#显示点击区
iconX=45
iconY=120
for 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+iconX
rectY=(70-rectH)/2+iconY
self.playSurface.blit(rect,(rectX,rectY))
iconX+=70
iconX=45
iconY+=70
#显示抵消区
offsetX=0
offsetY=430
offsettingAreaListSize=len(self.offsettingAreaList)
offsetOrdinalNumber=1
for 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+offsetX
offsetIconY=(42-offsetIconH)/2+offsetY
self.playSurface.blit(offsetIcon,(offsetIconX,offsetIconY))
offsetOrdinalNumber+=1
offsetX+=42
for 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=30
y=100
for 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=True
return "You win"
if len(self.offsettingAreaList)==7:
signOut=True
return "You lost"
x+=80
x=30
y+=80
if 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 state
if event.type == QUIT:
state="exit"
return state
if 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()
如有漏洞或需要改进的地方,请各位多多指教。