import sys, random, time, pygame
from pygame.locals import *
import time
import random
#定义窗口大小
SCREEN_WIDTH=720
SCREEN_HEIGHT=480
#每次生成数量
EPS=1
#物体大小
ENTITY_WIDTH=80
#物体下落倍速
SPEED_DROP_ENTITY=3
#矩形移动速度
RECT_MOVE_SPEED=3
#矩形的宽高度px
RECTANGLE_WIDTH=80
RECTANGLE_HEIGHT=20
#生成物体时间间隔ms
CYC=500
white = (255,255,255)
yellow = (255,255,0)
color = (125,100,210)
#向屏幕绘制字体
def print_text(font, x, y, text, color=white):
imgText = font.render(text, True, color)
screen.blit(imgText, (x,y))
#定时器
class timer(object):
def __init__(self,cyctime):
self.cyctime=cyctime
self.start=False
def Start(self):
self.sTime=int(time.time()*1000)
self.start=True
self.run()
def Stop(self):
self.start=False
def run(self):
self.nowtime=int(time.time()*1000)
if self.nowtime-self.sTime>=self.cyctime and self.start:
self.Stop()
self.beat()
self.Start()
def beat(self):
length=len(cobos)
for i in range(0,length):
if cobos[length-i-1].y>SCREEN_HEIGHT:
cobos.remove(cobos[length-i-1])
for i in range(0,EPS):
flag = random.randint(0, 2)
if flag:
cobos.append(coin())
else:
cobos.append(bomb())
class entity(object):
def __init__(self):
#重力加速度
self.g=4e-05
self.width=ENTITY_WIDTH
self.x=random.randint(0,SCREEN_WIDTH-self.width)
self.y=-ENTITY_WIDTH
self.birth=int(time.time()*1000)
def changePostion(self,nowtime):
times=int(nowtime*1000)-self.birth
self.y = (int)(self.g*times*times*SPEED_DROP_ENTITY)-ENTITY_WIDTH-200
#计算y的位置
class rect(object):
def __init__(self):
self.x=0
self.y=SCREEN_HEIGHT-40
self.img=pygame.Surface((RECTANGLE_WIDTH,RECTANGLE_HEIGHT))
class coin(entity): #硬币
def __init__(self):
super(coin, self).__init__()
_simg=pygame.image.load('coin.png')
self.img=pygame.transform.scale(_simg, (ENTITY_WIDTH, ENTITY_WIDTH))
class bomb(entity): #炸弹
def __init__(self):
super(bomb, self).__init__()
_simg=pygame.image.load('bomb.png')
self.img=pygame.transform.scale(_simg, (ENTITY_WIDTH, ENTITY_WIDTH))
lightBlue=(204, 255, 255) #按钮颜色
blue=(51, 204, 204) #button按下颜色
class button(object):
def __init__(self,name,text,width,height,x=0,y=0):
self.name=name
self.width=width
self.height=height
self.x=x
self.y=y
self.flag=False
self.Suf=pygame.Surface((width,height))
self.Suf.fill(lightBlue)
self.normalImg=self.Suf
self.downImg=self.Suf.copy()
self.downImg.fill(blue)
self.text=text
self.Img=self.normalImg
self.fontSize=int(height)
self.fontColor=(0,0,0)
self.font = pygame.font.Font(None, self.fontSize)
self.imgText = self.font.render(self.text, True, self.fontColor)
def down(self):
self.Img=self.downImg
self.paint()
def up(self):
self.Img=self.normalImg
self.paint()
def changeFont(self,fontSize,color=(0,0,0)):
self.fontSize=fontSize
self.fontColor=color
self.font = pygame.font.Font(None, self.fontSize)
self.imgText = self.font.render(self.text, True, self.fontColor)
self.Img=self.paint()
def paint(self):
fontx=(self.Img.get_width()-self.imgText.get_width())/2
fonty=(self.Img.get_height()-self.imgText.get_height())/2
self.Img.blit(self.imgText,(fontx,fonty))
return self.Img
def Click(self):
clickName=self.name+'_click'
try:
globals()[clickName]()
except Exception as e:
print(e)
def mouseAction(self,x,y,EVENT):
if EVENT==MOUSEBUTTONDOWN:
print(x,y,self.x,self.y,self.x+self.width,self.y+self.height)
if x>self.x and x<self.x+self.width and y>self.y and y<self.y+self.height:
self.down()
self.flag=True
self.Click()
if EVENT==MOUSEBUTTONUP and self.flag==True:
self.flag=False
self.up()
#按钮只有点击事件//按钮down也算click
#每个新的按钮必须要实现函数对象名_click():方法
def rankbtn_click():
print('rankbtn_click')
def runbtn_click():
game_over=False
def mouse_click_broadcast(x,y,EVENT):
runbtn.mouseAction(x,y,EVENT)
#主程序
cobos=[]
#生成的硬币和炸弹列表
FF=False#游戏结束标志
pygame.init()
pygame.mixer.init()
screen = pygame.display.set_mode((SCREEN_WIDTH,SCREEN_HEIGHT))
pygame.display.set_caption("30s,rich man !")
bg = pygame.image.load('startbg.jpg')
font1 = pygame.font.Font(None, 24)
font2=pygame.font.Font(None,60)
key_flag = False
printFlag=False
score = 0
bombNum=0
mouceClickFlag=False
clock_start = 0
game_over = True
seconds=30
time1=timer(CYC)
start_sound=pygame.mixer.Sound("TAB.WAV")
#开始音提示
start_sound.set_volume(0.5)
#设置声音大小
bomb_sound = pygame.mixer.Sound("BOMB.WAV")
#炸弹声
bomb_sound.set_volume(0.7)
coin_sound = pygame.mixer.Sound("BELL6.WAV")
#吃掉银币的声音
coin_sound.set_volume(0.5)
game_over_sound=pygame.mixer.Sound("1101402.wav")
#游戏结束声音
game_over_sound.set_volume(0.5)
pygame.mixer.music.load("bg.mp3")
#背景声音
pygame.mixer.music.set_volume(1)
runbtn=button('runbtn',"start",width=100,height=30,x=(SCREEN_WIDTH-100)/2,y=(SCREEN_HEIGHT-30)/2)
rankbtn=button('setbtn','rank',width=100,height=30,x=(SCREEN_WIDTH-100)/2,y=(SCREEN_HEIGHT-100)/2)
while True:
for event in pygame.event.get():
if event.type == QUIT:
pygame.quit()
sys.exit()
elif event.type == KEYDOWN:
key_flag = True
elif event.type == KEYUP:
key_flag = False
elif event.type==MOUSEBUTTONUP or event.type==MOUSEBUTTONDOWN:
if event.type==MOUSEBUTTONDOWN:
if game_over:
mouceClickFlag=True
x,y=pygame.mouse.get_pos()
print(x,y)
mouse_click_broadcast(x,y,event.type)
keys = pygame.key.get_pressed()
if keys[K_ESCAPE]:
sys.exit()
if keys[K_RETURN] or mouceClickFlag:
if game_over:
start_sound.play()
printFlag=False
game_over = False
mouceClickFlag=False
screen.blit(bg,(0,0))
rec=rect()
screen.blit(rec.img,(rec.x, rec.y))
print_text(font2,230,100,"game start!",(40,10,40))
pygame.display.update()
time.sleep(1)
pygame.mixer.music.play(-1)
time1.Start()
score = 0
bombNum=0
seconds = 30
clock_start = time.clock()
current = time.clock() - clock_start
if seconds-current < 0:
game_over = True #30秒结束了吗?
FF=True
time1.Stop()
screen.blit(bg,(0,0)) #绘制背景
#screen.blit(bt.paint(), (bt.x, bt.y))
if game_over:
screen.blit(runbtn.paint(), (runbtn.x, runbtn.y))
screen.blit(rankbtn.paint(), (rankbtn.x, rankbtn.y))
print_text(font1, 200, 120, "please click to start...")
if printFlag==False:
pygame.mixer.music.stop()
game_over_sound.play()
printFlag=True
if FF: