import time,sys
import pygame
from pygame.locals import *
import random
class Button:
def __init__(self,image,x, y):
self.l = image.get_rect()
self.l.x = x
self.l.y = y
class Drop:
def __init__(self,imag):
self.p=pygame.image.load(imag)
self.x=random.randint(50,750)
self.y=0
class Game:
def __init__(self):
pygame.init()
self.w = pygame.display.set_mode((800, 533))
pygame.display.set_caption('猴子接香蕉')
self.p1=pygame.image.load('背景.jpg')
self.p2=pygame.image.load('猴子.png')
self.p3=pygame.image.load('初始界面.jpg')#将要用得图片赋值给p
self.p4=pygame.image.load('开始.png')
self.p5=pygame.image.load('游戏玩法按钮.png')
self.p6=pygame.image.load('游戏玩法.jpg')
self.p7=pygame.image.load('返回上一级.jpg')
self.p8=pygame.image.load('进度条.png')
self.c = pygame.font.Font('fs.ttf', 25)#字体
self.score=0
self.c2=self.c.render('回到主界面',True,'red')
self.c3=self.c.render(' 退 出 ',True,'red')
self.c4=self.c.render('按空格键重新回到游戏',True,'black')
self.c5=self.c.render('下一关',True,'red')
pygame.mixer.music.load('背景音乐.mp3')
pygame.mixer.music.play()
self.p2x=20
self.p2y=533-89#初始猴子一开始得位置
self.T=time.time()#初始时间
self.score = 0
self.Timer=0#加时钟的个数
self.TIMEADD = []
self.TIMESUB=[]
self.Time=45
self.v=0.3#下落速度
self.jgx=60
self.banananum=8
self.jg=6
self.num = self.jgx // 10
def function(self):
self.flag2=False#第二关的开关
self.flag3=False#第三关的开关
self.flag4=False#赢了的开关
self.befbeg()#调用开始之前得界面
c=self.c.render('''物竞天择,适者生存收集足够多的香蕉第一关60,第二关90,第三关100 ''',True,'white')
self.w.fill('black')
self.w.blit(c,(400-c.get_width()//2,533//2-c.get_height()//2))
pygame.display.update()
time.sleep(3)
self.transfer(1)
self.BANANA = []
while True:
self.change()
self.load()
self.banana()
def banana(self):
if len(self.BANANA)<self.banananum:
self.BANANA.append(Drop('香蕉.png'))
for i in self.BANANA:
if i.y>533:
self.BANANA.remove(i)
if i.y>=self.p2y and i.x>=self.p2x and i.x<=self.p2x+70:
try:
self.BANANA.remove(i)
self.score+=1
except:
pass
def befbeg(self):
while True:
self.T=time.time()
self.w.blit(self.p3,(0,0))
self.w.blit(self.p4,(345,365))
self.w.blit(self.p5,(700,50))
self.BUTTON=[]
self.BUTTON.append(Button(self.p4,345,365))
self.BUTTON.append(Button(self.p5,700,50))
self.BUTTON.append(Button(self.p7, 760, 500))
self.BUTTON.append(Button(self.c2,400-self.c2.get_width()//2,533//2-self.c2.get_height()//2-60))
self.BUTTON.append(Button(self.c3,400-self.c2.get_width()//2,533//2+self.c3.get_height()//2+60))
self.BUTTON.append(Button(self.c5,400-self.c5.get_width()//2,533//2-self.c5.get_height()//2))
if self.catch1()==0:
break
if self.catch1()==1:
break
pygame.display.update()
def catch1(self,n=1):#初始页面按钮捕捉
for e in pygame.event.get():
self.Quit(e)
if e.type==MOUSEBUTTONUP:
for i in range (len(self.BUTTON)):
if self.BUTTON[i].l.collidepoint(e.pos):
if i == 0:
return i # 开始游戏捕捉到
elif i == 1: # 捕捉到游戏玩法
while True:
self.w.blit(self.p6, (0, 0))
self.w.blit(self.p7, (760, 500))
if self.catch2():
return i
pygame.display.update()
elif i==3:
return i
elif i==4:
sys.exit()
elif i==5:
self.T += self.T2 - self.T1
self.transfer(n)
while True:
self.load()
self.change()
self.banana()
def catch2(self):#游戏玩法个界面的按钮捕捉
for e in pygame.event.get():
if e.type == MOUSEBUTTONUP:
for i in range(len(self.BUTTON)):
if self.BUTTON[i].l.collidepoint(e.pos):
if self.BUTTON[i].l.collidepoint(e.pos):
if i==2:
return True
self.Quit(e)
def Quit(self,e):#是X键有用
if e.type == QUIT:
pygame.quit()
sys.exit()
def change(self):#猴子的位置改变
for i in range(len(self.BANANA)):
self.BANANA[i].y+=self.v
for i in range(len(self.TIMEADD)):
self.TIMEADD[i].y+=self.v
for i in range(len(self.TIMESUB)):
self.TIMESUB[i].y+=self.v
e = pygame.key.get_pressed()
if e[pygame.K_RIGHT] and self.p2x< self.p1.get_width() - self.p2.get_width():#右边不出界
self.p2x += 0.4
if e[pygame.K_LEFT] and self.p2x > 0:#左边不出界
self.p2x-= 0.4
pygame.event.pump()
pygame.display.update()
def stop(self):
self.w.blit(self.c4,(400-self.c4.get_width()//2,533//2-self.c4.get_height()//2))
for e in pygame.event.get():
if e.type == KEYUP:
if e.key == K_SPACE:
return 1
if e.type == pygame.QUIT:
pygame.quit()
sys.exit()
pygame.display.update()
return 0
def load(self):#将游戏图片加载到界面上
self.w.blit(self.p1,(0,0))
self.w.blit(self.p2,(self.p2x,self.p2y))
self.T1 = time.time()
if self.Time-self.T1+self.T<0:
if self.score<=self.jgx:
while True:
c = self.c.render('猴子没有接到足够的香蕉,游戏失败', True, 'black')
self.w.blit(c, (400 - c.get_width() // 2, 533 // 2 - c.get_height() // 2))
self.sp()
if self.catch1() == 3:
self.__init__()
self.function()
if self.score*10//self.num>100:
time.sleep(0.2)
if self.flag2 == False:
self.flag2 = True # 第二关钥匙
self.next(2) # 转关界面
if self.flag2 == True:
if self.flag3 == False:
self.flag3 = True
self.jgx = 100
self.jg = 10
self.next(3)
if self.flag3 == True:
self.w.fill('black')
c = self.c.render('谢谢你帮助猴子接到足够的香蕉', True, 'white')
self.w.blit(c, (400 - c.get_width() // 2, 533 // 2 - c.get_height() // 2))
pygame.display.update()
time.sleep(4)
while True:
self.w.fill('black')
self.sp()
if self.catch1() == 3:
self.__init__()
self.function()
if self.flag2:
if self.flag3:
self.banananum=15
self.add()
self.sub()
self.v=0.5
else:
self.add()
if self.num>10:
self.num=10
self.ci = self.c.render('时间%d' % (self.Time-self.T1+self.T), True, 'black')
self.c1 = self.c.render('进度:', True, 'black')
c=self.c.render(' '*(self.score*2//self.num),True,'yellow','yellow')
c1=self.c.render('%d'%(self.score*10/self.num)+'%', True, 'black')
self.w.blit(self.c1, (0, 0))
self.w.blit(self.p8,(self.c1.get_width(),5))
self.w.blit(c,(self.c1.get_width(),5))
self.w.blit(c1,(self.c1.get_width()+self.p8.get_width(),5))
self.w.blit(self.ci,(400-self.ci.get_width()//2,0))#将时间剧中
self.dropjz(self.BANANA)
for e in pygame.event.get():
if e.type == KEYUP:#第一个空格暂停
if e.key == K_SPACE:
while True:
self.T2=time.time()
pygame.display.update()
if self.stop():#第二个空格开始
self.T+=self.T2-self.T1
break
self.Quit(e)
pygame.display.update()
def add(self):
k = int(self.T1 - self.T)
if k % self.jg == 0 and k != 0:
if len(self.TIMEADD) < 1:
self.TIMEADD.append(Drop('加时间.png'))
self.dropjz(self.TIMEADD)
for i in self.TIMEADD:
if i.y > 533:
self.TIMEADD.remove(i)
if i.y >= self.p2y and i.x >= self.p2x and i.x <= self.p2x + 70:
try:
self.TIMEADD.remove(i)
self.Time += 5
except:
pass
def sub(self):
self.dropjz(self.TIMESUB)
if len(self.TIMESUB)<3:
self.TIMESUB.append(Drop('减时间.png'))
for i in self.TIMESUB:
if i.y>533:
self.TIMESUB.remove(i)
if i.y>=self.p2y and i.x>=self.p2x and i.x<=self.p2x+70:
try:
self.TIMESUB.remove(i)
self.Time-=5
except:
pass
def dropjz(self,l):
for i in range (len(l)):
self.w.blit(l[i].p,(l[i].x,l[i].y))
def transfer(self,n):
self.w.fill('black')
c=self.c.render('第%d关'%n,True,'white')
self.w.blit(c,(400-c.get_width()//2,533//2-c.get_height()//2))
pygame.display.update()
time.sleep(1)
def next(self,n):
self.__init__()
self.v=0.3
self.Time=20
self.BANANA=[]
self.jgx=80
while True:
self.T2 = time.time()
c = self.c.render('下一关', True, 'red')
self.w.blit(c, (400 - c.get_width() // 2, 533 // 2 - c.get_height() // 2))
self.sp()
if self.catch1(n) == 3:
self.__init__()
self.function()
def sp(self):
self.w.blit(self.c2,(400-self.c2.get_width()//2,533//2-self.c2.get_height()//2-60))
self.w.blit(self.c3,(400-self.c2.get_width()//2,533//2+self.c3.get_height()//2+60))
pygame.display.update()
def main():
g=Game()
g.function()
if __name__=='__main__':
main()
python课程设计猴子接香蕉一共有三关,基于pygame
资料地址:链接: https://pan.baidu.com/s/1AZvVHILiKGEblFYxXdNmnQ?pwd=vvev 提取码: vvev?
|