《python大作业小恐龙快跑代码.pdf》由会员分享,可在线阅读,更多相关《python大作业小恐龙快跑代码.pdf(15页珍藏版)》请在taowenge.com淘文阁网|工程机械CAD图纸|机械工程制图|CAD装配图下载|SolidWorks_CaTia_CAD_UG_PROE_设计图分享下载上搜索。
1、python 大作业-小恐龙快跑-代码#by 王帅兵 _541 12 import math 3 import sys 4 import pygame 5 import random 6import locale 7 import time 8 from itertools import cycle 9 frompygame.locals import*10 import numbers 1112#屏幕的宽度和高度 13 width=1000 14 height=600 1516 size=(width,height)1718#更新画面的时间 19 fps=60 2021#障碍物和小恐龙移动速
2、度 22 speed=6 2324#小恐龙跳跃速度 25 bv=8 2627#定义一个地图类 28 class MyMap():29 def _init_(self,x,y):30self.bg=pygame.image.load(“images/background.png)31 self.x=x 32self.y=y 3334#地图的滚动 35 def map_rolling(self):36 if self.x-(width-10):37self.x=width 38 else:39 self._=8 4041#地图的绘制 42def map_update(self):1 screen.
3、blit(self.bg,(self.x,self.y)23#定义一个恐龙类 4 class Dinosaur():5 def _init_(self):6self.rect=pygame.Rect(0,0,0,0)78 self.status=False 9 self.height=180 10 self.lowest=310 11self.value=0 1213#定义一个循环器,用于显示小恐龙的图片 14self.index=cycle(0,1,2)15self.image=(pygame.image.load(images/dinosaur1.png).convert_ 16alpha
4、(),17 pygame.image.load(images/dinosaur2.png).convert_alpha(),18 pygame.image.load(images/dinosaur3.png).convert_alpha()19)20 self.alive=True 21self.audio=pygame.mixer.Sound(audio/jump.wav)22self.rect.size=self.image.get_size()23 self.x=100 24 self.y=self.lowest 25self.rect.topleft=(self.x,self.y)26
5、 def update(self):27 if self.status:28Dinosaur.run()2930 def jump(self):31 self.status=True 3233#小恐龙的跳跃及奔跑 34 def run(self):35 if self.status:36 ifself.rect.y=self.lowest:37 self.value=-bv 38 if self.rect.y=self.lowest-self.height:39 self.value=bv 40 self.rect.y+=self.value 41 ifself.rect.y=self.low
6、est:42self.status=False 12 def draw(self):3 index=next(self.index)#一次取出小恐龙的一张图片 4 screen.blit(self.imageindex,(self.rect.x,self.rect.y)56#播放跳跃的音乐 7 def play(self):8 self.audio.play()910#游戏结束后画面的显示以及音乐的播放 11 def gameover():12bump=pygame.mixer.Sound(audio/bump.wav)13 bump.play()14sw=pygame.display.Inf
7、o().current_w 1516 sh=pygame.display.Info().current_h 17gv=pygame.mixer.Sound(audio/gv.wav)18 gv.play()19img=pygame.image.load(images/gameover.png).convert_alpha()20screen.blit(img,(sw-img.get_width()/2,(sh-img.get_height()/21 2)2223#定义一个障碍物类 24 class Obstacle():25 score=10#越过障碍物的得分 26 def _init_(se
8、lf):27 self.rect=pygame.Rect(0,0,0,0)2829#加载障碍物的图片 30 self.obstacle=(3132 pygame.image.load(images/stone.png).convert_alpha(),33pygame.image.load(images/cacti.png).convert_alpha(),34pygame.image.load(images/c1.png).convert_alpha(),35pygame.image.load(images/c2.png).convert_alpha(),36pygame.image.loa
9、d(images/c3.png).convert_alpha(),37pygame.image.load(images/c4.png).convert_alpha(),3839)40#加载得分显示的图片 41 self.numbers=(4243pygame.image.load(images/0.png),1pygame.image.load(images/1.png),2pygame.image.load(images/2.png),3pygame.image.load(images/3.png),4pygame.image.load(images/4.png),5pygame.image
10、.load(images/5.png),6pygame.image.load(images/6.png),7pygame.image.load(images/7.png),8pygame.image.load(images/8.png),9pygame.image.load(images/9.png)10)1112 self.audio=pygame.mixer.Sound(audio/score.wav)13x=random.randint(1,100)1415#用于随机生成那种障碍 16 r=0 17 if x50:18r=random.randint(0,2)19 else:20 r=r
11、andom.randint(3,5)21self.image=self.obstacler 22 self.rect.size=self.image.get_size()23self.width,self.height=self.rect.size 2425 self.x=width 26 self.y=height-self.height/2-230 27self.rect.center=(self.x,self.y)28 def move(self):29 self.rect._=speed 3031 def draw(self):32 screen.blit(self.image,(se
12、lf.rect.x,self.rect.y)3334#越过障碍物后的加分 35 def getscore(self):36 tmp=self.score 37if tmp:38 self.audio.play()39 self.score=0 40 return tmp 4142#定义可以被子弹消灭的怪物类 1 class Monster():2 score=503 def _init_(self):45 self.rect=pygame.Rect(0,0,0,0)6 self.monsters=(78 pygame.image.load(images/m1.png).convert_alph
13、a(),9pygame.image.load(images/m2.png).convert_alpha(),10pygame.image.load(images/m3.png).convert_alpha(),11pygame.image.load(images/m4.png).convert_alpha(),12)1314 x=random.randint(0,3)15 self.image=self.monstersx 16self.index=cycle(0,1,2,3)1718 self.numbers=(1920 pygame.image.load(images/0.png),21p
14、ygame.image.load(images/1.png),2223 pygame.image.load(images/2.png),24pygame.image.load(images/3.png),25pygame.image.load(images/4.png),26pygame.image.load(images/5.png),27pygame.image.load(images/6.png),28pygame.image.load(images/7.png),29pygame.image.load(images/8.png),30pygame.image.load(images/9
15、.png)3132)3334 self.alive=3#怪物的生命 35 self.rect.size=self.image.get_size()3637 self.width,self.height=self.rect.size 3839 self.x=width 40 self.y=height-self.height/2-220 41self.rect.center=(self.x,self.y)42def move(self):1 self.rect._=speed 23 def draw(self):4 screen.blit(self.image,(self.rect.x,self
16、.rect.y)5 defgetscore(self):6 tmp=self.score 7 self.score=0 8 return tmp 910#定义怪物飞龙类 11 class Dragon():12 score=10013 def _init_(self):1415 self.rect=pygame.Rect(0,0,0,0)16 self.image=(1718 pygame.image.load(images/d1.png).convert_alpha(),19pygame.image.load(images/d2.png).convert_alpha(),20pygame.i
17、mage.load(images/d3.png).convert_alpha(),21pygame.image.load(images/d4.png).convert_alpha(),22pygame.image.load(images/d5.png).convert_alpha(),23pygame.image.load(images/d6.png).convert_alpha(),24)25self.index=cycle(0,1,2,3,4,5)26self.numbers=(pygame.image.load(images/0.png),27pygame.image.load(imag
18、es/1.png),2829 pygame.image.load(images/2.png),30pygame.image.load(images/3.png),31pygame.image.load(images/4.png),32pygame.image.load(images/5.png),33pygame.image.load(images/6.png),34pygame.image.load(images/7.png),35pygame.image.load(images/8.png),36pygame.image.load(images/9.png)37)3839 self.ali
19、ve=3#龙的生命 4041 self.rect.size=self.image.get_size()42self.width,self.height=self.rect.size 431 self.x=width 2 self.y=height-self.height/2-260 3self.rect.center=(self.x,self.y)4 self.speed=speed+25 self.position=(self.rect.x,self.rect.y)67 def move(self):8 self.rect._=self.speed 9 self.position=(self
20、.rect.x,self.rect.y)1011 def draw(self,index):12 screen.blit(self.imageindex,self.position)13 def getscore(self):14 tmp=self.score 15 self.score=0 16 return tmp 1718#定义一个果实类 19 class Fruit():20score=random.randint(10,20)2122 def _init_(self):23 self.rect=pygame.Rect(0,0,0,0)24self.fruits=(2526 pygam
21、e.image.load(images/fruit1.png).convert_alpha(),2728 pygame.image.load(images/fruit2.png).convert_alpha(),29pygame.image.load(images/fruit3.png).convert_alpha(),30pygame.image.load(images/fruit4.png).convert_alpha(),31pygame.image.load(images/fruit5.png).convert_alpha(),32pygame.image.load(images/fr
22、uit6.png).convert_alpha(),3334 pygame.image.load(images/fruit7.png).convert_alpha(),3536 pygame.image.load(images/fruit8.png).convert_alpha(),37pygame.image.load(images/fruit9.png).convert_alpha(),38pygame.image.load(images/fruit10.png).convert_alpha(),39pygame.image.load(images/fruit11.png).convert
23、_alpha(),40pygame.image.load(images/fruit12.png).convert_alpha(),41pygame.image.load(images/fruit13.png).convert_alpha(),42pygame.image.load(images/fruit14.png).convert_alpha(),1pygame.image.load(images/fruit15.png).convert_alpha(),2pygame.image.load(images/fruit16.png).convert_alpha(),3pygame.image
24、.load(images/fruit17.png).convert_alpha(),4pygame.image.load(images/fruit18.png).convert_alpha(),5pygame.image.load(images/fruit19.png).convert_alpha(),6pygame.image.load(images/fruit20.png).convert_alpha(),7)8r=random.randint(0,19)9 self.image=self.fruitsr 10 tmp=0 1112#随机生成果实坐标 13 tmp+=random.rand
25、int(30,200)14self.rect.x=tmp+300 15 self.rect.y=random.randint(180,280)1617 def move(self):18 self.rect._=speed 1920 def draw(self):21 screen.blit(self.image,(self.rect.x,self.rect.y)2223 def getscore(self):24 tmp=self.score 25 if tmp:26 self.play()27self.score=0 28 return tmp 29 def play(self):30py
26、game.mixer.music.load(audio/fruit.mp3)31 pygame.mixer.music.play()3233 numbers=(3435 pygame.image.load(images/0.png),36pygame.image.load(images/1.png),37pygame.image.load(images/2.png),38pygame.image.load(images/3.png),3940 pygame.image.load(images/4.png),41pygame.image.load(images/5.png),42pygame.i
27、mage.load(images/6.png),43pygame.image.load(images/7.png),1pygame.image.load(images/8.png),2pygame.image.load(images/9.png)3)45 def showscore(score):6 digit=int(x)for x in str(score)7totalwidth=0 8 for i in digit:9 totalwidth+=numbersi.get_width()10offset=(width-totalwidth)/2 11 for i in digit:12scr
28、een.blit(numbersi,(offset,height*0.1)13offset+=numbersi.get_width()1415#定义云朵类,用于装饰游戏背景 16 class Cloud():17 def_init_(self):18 self.rect=pygame.Rect(0,0,0,0)19 self.clouds=(2021 pygame.image.load(images/cloud1.png).convert_alpha(),22pygame.image.load(images/cloud2.png).convert_alpha(),23pygame.image.
29、load(images/cloud3.png).convert_alpha(),24pygame.image.load(images/cloud4.png).convert_alpha(),25)26r=random.randint(0,3)27 self.image=self.cloudsr 28 self.rect.x=width 29self.rect.y=height/10.0 3031 def move(self):32 self.rect._=speed 3334 def draw(self):35 screen.blit(self.image,(self.rect.x,self.
30、rect.y)36#定义子弹类 37 class Bullet():38 score=10039 def _init_(self):4041 self.rect=pygame.Rect(0,0,0,0)42self.image=pygame.image.load(images/flame.png).convert_alpha 1()2 self.explosion=(34 pygame.image.load(images/bullet1.png).convert_alpha(),5pygame.image.load(images/bullet2.png).convert_alpha(),6py
31、game.image.load(images/bullet3.png).convert_alpha(),7pygame.image.load(images/bullet4.png).convert_alpha(),8pygame.image.load(images/bullet5.png).convert_alpha(),9pygame.image.load(images/bullet6.png).convert_alpha(),10)11self.index=cycle(0,1,2,3,4,5)12 self.ok=False 13 self.times=0 14self.audio=pyg
32、ame.mixer.Sound(audio/exlposion.wav)15self.rect.size=self.image.get_size()16 self.width,self.height=self.rect.size 1718 self.x=width 19 self.y=height-self.height/2-230 20self.rect.center=(self.x,self.y)21 self.active=False2223 def move(self):24 self.rect.x+=speed 2526 def draw(self):27 screen.blit(s
33、elf.image,(self.rect.x,self.rect.y)28#用于激活子弹 2930 def reset(self,position):31 self.rect.left,self.rect.top=position 32self.active=True 3334 def show(self,position):35 index=next(self.index)36screen.blit(self.explosionindex,position)3738 def play(self):39 self.audio.play()4041 def main():42 delay=0 4
34、3bindex=0 1 NUM=5 2 score=0 3 over=False 4 idx=0 56#将 screen,clock,speed 声明为全局变量 7 globalscreen,clock,speed 8 start=time.time()9 pygame.init()10pygame.mixer.init()1112 clock=pygame.time.Clock()1314#设置屏幕大小 15 screen=pygame.display.set_mode(size)1617#设置游戏标题 18 pygame.display.set_caption(小恐龙)1920#加载背景音
35、乐 21bg=pygame.mixer.Sound(audio/background.ogg)2223 bg.play()2425#定义两个滚动的地图 26 bg1=MyMap(0,0)27bg2=MyMap(width,0)28#实例化小恐龙 29 dinosaur=Dinosaur()30interval=0 31 lis=32 fruit=33 level=50 34 monsters=35 bullets=36dragons=37 clouds=3839#生成未激活的子弹 40 for _ in range(NUM):41 b=Bullet()42bullets.append(b)12
36、 while True:3 delay+=1 4 if delay=100:5 delay=0 6 for even inpygame.event.get():78#判断是否是结束游戏的事件 9 if even.type=QUIT:10 sys.exit()1112#按下空格键后使得小恐龙开始跳跃 13 if even.type=_ andeven.key=K_SPACE:14 if dinosaur.rect.y=dinosaur.lowest:15dinosaur.jump()16 dinosaur.play()17 if over:18 main()1920#按下 A 键后开始发射子弹
37、21 if even.type=_ andeven.key=K_a:22 bulletsbindex.reset(dinosaur.rect.midtop)23 bindex=(bindex+1)%NUM 2425 if over=False:26 bg1.map_update()27 bg1.map_rolling()28bg2.map_update()29 bg2.map_rolling()30 dinosaur.run()31dinosaur.draw()32 end=time.time()3334#每经过 10 秒就让障碍物移动速度加 0.5 35 if end-start10.0:3
38、6start=end 37 speed+=0.5 3839#如果每经过 10 秒且障碍物出现间隔大于 1000,就加快障碍物出现间隔 40 if interval1000:41 interval-=20 42 for b in bullets:43if b.active:1 b.move()2 b.draw()34 for i in range(len(monsters)-1,-1,-1):56#判断子弹是否击中怪物 7 ifpygame.sprite.collide_rect_ratio(0.8)(b,monstersi):8 b.active=False 910#击中后怪物生命减一 11
39、monstersi.alive-=1 12 b.play()13b.ok=True 1415#怪物生命为 0 后就删除怪物 16 if monstersi.alive1:17score+=monstersi.getscore()1819 del monstersi 20 for i in range(len(dragons)-1,-1,-1):2122#判断子弹是否击中大龙 23 ifpygame.sprite.collide_rect_ratio(0.8)(b,dragonsi):2425#子弹状态改成未激活 26 b.active=False2728#击中大龙后大龙生命减一 29 drag
40、onsi.alive-=1 3031 b.play()32 b.ok=True 3334#大龙生命为 0 后就删除大龙 35 if dragonsi.alive1:36score+=dragonsi.getscore()37 del dragonsi 3839#播放子弹的爆炸效果 40 for b in bullets:41 if b.ok:42b.show(b.rect.x,b.rect.y)43b.times+=1 1 if b.times=6:2 b.times=0 3 b.ok=False 45#怪物出现的频率 6 if interval1400:78#产生那种障碍物 9 r=rand
41、om.randint(1,100)1011 if rlevel:12 obstacle=Obstacle()13 lis.append(obstacle)14 elif r20:15 m=Monster()16 monsters.append(m)17 else:18#随机生成大龙出现的位置 19 x=random.randint(1,100)20 d=Dragon()21 if x50:22d.rect.x,d.rect.y=d.position=(width,height/8.0)23 d.speed=speed+5 24dragons.append(d)2526 interval=0 2
42、728 for i in range(len(monsters):29 monstersi.move()30monstersi.draw()31#判断小恐龙是否和怪物发生碰撞 32 ifpygame.sprite.collide_rect_ratio(0.7)(dinosaur,monstersi):33 over=True34 bg.stop()35 gameover()3637#绘制大龙图像 38 for i in range(len(dragons):39 ifnot(delay%10):40 idx=(idx+1)%6 41 dragonsi.move()42dragonsi.draw
43、(idx)431#判断小恐龙是否和大龙发生碰撞 2 ifpygame.sprite.collide_rect_ratio(0.8)(dinosaur,dragonsi):3 over=True 4bg.stop()5 gameover()67#产生装饰物白云 8 if not len(clouds):9 c=Cloud()10clouds.append(c)11 for i in range(len(clouds)-1,-1,-1):12 cloudsi.move()13 cloudsi.draw()14 if cloudsi.rect.x0:15 del cloudsi 1617#随机生成果
44、实 18 x=random.randint(1,1000)19 if x8:20 f=Fruit()21 fruit.append(f)22 for i in range(len(lis):23 lisi.move()24 lisi.draw()2526#判断小恐龙是否和障碍物发生碰撞 27 ifpygame.sprite.collide_rect_ratio(0.8)(dinosaur,lisi):28 over=True 2930 bg.stop()31 gameover()32 else:3334#如果未碰撞就加分 35 iflisi.rect.x+lisi.rect.widthdino
45、saur.rect.x:36 score+=lisi.getscore()37 for iin range(len(fruit)-1,-1,-1):38 fruiti.move()39 fruiti.draw()4041#如果小恐龙吃到果实就加分 1 ifpygame.sprite.collide_rect_ratio(1.1)(dinosaur,fruiti):2score+=fruiti.getscore()3 fruiti.play()4 del fruiti 56#显示分数 7 showscore(score)8 interval+=20 910#刷新屏幕显示11 pygame.display.flip()12 clock.tick(fps)1314#运行游戏15 if _name_=_main_:16 main()17