提交 a75d629e 编写于 作者: ZZKCAT's avatar ZZKCAT

更新music/Daocao.mp3, img/bg/bg.jpg, img/WeatherIcon/0.png,...

更新music/Daocao.mp3, img/bg/bg.jpg, img/WeatherIcon/0.png, img/WeatherIcon/1.png, img/WeatherIcon/2.png, img/WeatherIcon/3.png, img/WeatherIcon/4.png, img/WeatherIcon/5.png, img/WeatherIcon/6.png, img/WeatherIcon/7.png, ico/0.ico, font/VladimirScript.ttf, weather game.py
上级
文件已添加
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():
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()
Markdown is supported
0% .
You are about to add 0 people to the discussion. Proceed with caution.
先完成此消息的编辑!
想要评论请 注册