python大作业小恐龙快跑代码.docx

上传人:b****6 文档编号:4776417 上传时间:2022-12-08 格式:DOCX 页数:8 大小:19.02KB
下载 相关 举报
python大作业小恐龙快跑代码.docx_第1页
第1页 / 共8页
python大作业小恐龙快跑代码.docx_第2页
第2页 / 共8页
python大作业小恐龙快跑代码.docx_第3页
第3页 / 共8页
python大作业小恐龙快跑代码.docx_第4页
第4页 / 共8页
python大作业小恐龙快跑代码.docx_第5页
第5页 / 共8页
点击查看更多>>
下载资源
资源描述

python大作业小恐龙快跑代码.docx

《python大作业小恐龙快跑代码.docx》由会员分享,可在线阅读,更多相关《python大作业小恐龙快跑代码.docx(8页珍藏版)》请在冰豆网上搜索。

python大作业小恐龙快跑代码.docx

python大作业小恐龙快跑代码

python大作业-小恐龙快跑-代码

#by王帅兵__5411

2importmath3importsys4importpygame5importrandom6importlocale7importtime8fromitertoolsimportcycle9frompygame.localsimport*10importnumbers11

12#屏幕的宽度和高度13width=100014height=60015

16size=(width,height)17

18#更新画面的时间19fps=6020

21#障碍物和小恐龙移动速度22speed=623

24#小恐龙跳跃速度25bv=826

27#定义一个地图类28classMyMap():

29def__init__(self,x,y):

30self.bg=pygame.image.load(“images/background.png")31self.x=x32self.y=y33

34#地图的滚动35defmap_rolling(self):

36ifself.x-(width-10):

37self.x=width38else:

39self.__=840

41#地图的绘制42

defmap_update(self):

1screen.blit(self.bg,(self.x,self.y))2

3#定义一个恐龙类4classDinosaur():

5def__init__(self):

6self.rect=pygame.Rect(0,0,0,0)7

8self.status=False9self.height=18010self.lowest=31011self.value=012

13#定义一个循环器,用于显示小恐龙的图片14self.index=cycle([0,1,2])15self.image=(pygame.image.load("images/dinosaur1.png").convert_16alpha(),17pygame.image.load("images/dinosaur2.png").convert_alpha(),18pygame.image.load("images/dinosaur3.png").convert_alpha()

19)20self.alive=True21self.audio=pygame.mixer.Sound("audio/jump.wav")22self.rect.size=self.image.get_size()23self.x=10024self.y=self.lowest25self.rect.topleft=(self.x,self.y)26defupdate(self):

27ifself.status:

28Dinosaur.run()29

30defjump(self):

31self.status=True32

33#小恐龙的跳跃及奔跑34defrun(self):

35ifself.status:

36ifself.rect.y=self.lowest:

37self.value=-bv38ifself.rect.y=self.lowest-self.height:

39self.value=bv40self.rect.y+=self.value41ifself.rect.y=self.lowest:

42

self.status=False1

2defdraw(self):

3index=next(self.index)#一次取出小恐龙的一张图片4screen.blit(self.image[index],(self.rect.x,self.rect.y))5

6#播放跳跃的音乐7defplay(self):

8self.audio.play()9

10#游戏结束后画面的显示以及音乐的播放11defgameover():

12bump=pygame.mixer.Sound("audio/bump.wav")13bump.play()14sw=pygame.display.Info().current_w15

16sh=pygame.display.Info().current_h17gv=pygame.mixer.Sound("audio/gv.wav")18gv.play()19img=pygame.image.load("images/gameover.png").convert_alpha()20screen.blit(img,((sw-img.get_width())/2,(sh-img.get_height())/212))22

23#定义一个障碍物类24classObstacle():

25score=10#越过障碍物的得分26def__init__(self):

27self.rect=pygame.Rect(0,0,0,0)28

29#加载障碍物的图片30self.obstacle=(31

32pygame.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.load("images/c3.png").convert_alpha(),37pygame.image.load("images/c4.png").convert_alpha(),38

39)40#加载得分显示的图片41self.numbers=(42

43

pygame.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.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)11

12self.audio=pygame.mixer.Sound("audio/score.wav")13x=random.randint(1,100)14

15#用于随机生成那种障碍16r=017ifx50:

18r=random.randint(0,2)19else:

20r=random.randint(3,5)21self.image=self.obstacle[r]22self.rect.size=self.image.get_size()23self.width,self.height=self.rect.size24

25self.x=width26self.y=height-self.height/2-23027self.rect.center=(self.x,self.y)

28defmove(self):

29self.rect.__=speed30

31defdraw(self):

32screen.blit(self.image,(self.rect.x,self.rect.y))33

34#越过障碍物后的加分35defgetscore(self):

36tmp=self.score37iftmp:

38self.audio.play()39self.score=040returntmp41

42

#定义可以被子弹消灭的怪物类1classMonster():

2score=50

3def__init__(self):

4

5self.rect=pygame.Rect(0,0,0,0)6self.monsters=(7

8pygame.image.load("images/m1.png").convert_alpha(),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)13

14x=random.randint(0,3)15self.image=self.monsters[x]16self.index=cycle([0,1,2,3])17

18self.numbers=(19

20pygame.image.load("images/0.png"),21pygame.image.load("images/1.png"),22

23pygame.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.png")31

32)33

34self.alive=3#怪物的生命35self.rect.size=self.image.get_size()36

37self.width,self.height=self.rect.size38

39self.x=width40self.y=height-self.height/2-22041self.rect.center=(self.x,self.y)

42

defmove(self):

1self.rect.__=speed2

3defdraw(self):

4screen.blit(self.image,(self.rect.x,self.rect.y))5defgetscore(self):

6tmp=self.score7self.score=08returntmp9

10#定义怪物飞龙类11classDragon():

12score=100

13def__init__(self):

14

15self.rect=pygame.Rect(0,0,0,0)16self.image=(17

18pygame.image.load("images/d1.png").convert_alpha(),19pygame.image.load("images/d2.png").convert_alpha(),20pygame.image.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("images/1.png"),28

29pygame.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)38

39self.alive=3#龙的生命40

41self.rect.size=self.image.get_size()42self.width,self.height=self.rect.size43

1self.x=width2self.y=height-self.height/2-2603self.rect.center=(self.x,self.y)

4self.speed=speed+2

5self.position=(self.rect.x,self.rect.y)6

7defmove(self):

8self.rect.__=self.speed9self.position=(self.rect.x,self.rect.y)10

11defdraw(self,index):

12screen.blit(self.image[index],self.position)13defgetscore(self):

14tmp=self.score15self.score=016returntmp17

18#定义一个果实类19classFruit():

20score=random.randint(10,20)21

22def__init__(self):

23self.rect=pygame.Rect(0,0,0,0)24self.fruits=(25

26pygame.image.load("images/fruit1.png").convert_alpha(),27

28pygame.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/fruit6.png").convert_alpha(),33

34pygame.image.load("images/fruit7.png").convert_alpha(),35

36pygame.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_alpha(),40pygame.image.load("images/fruit12.png").convert_alpha(),41pygame.image.load("images/fruit13.png").convert_alpha(),42

pygame.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.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)9self.image=self.fruits[r]10tmp=011

12#随机生成果实坐标13tmp+=random.randint(30,200)14self.rect.x=tmp+30015self.rect.y=random.randint(180,280)16

17defmove(self):

18self.rect.__=speed19

20defdraw(self):

21screen.blit(self.image,(self.rect.x,self.rect.y))22

23defgetscore(self):

24tmp=self.score25iftmp:

26self.play()27self.score=028returntmp29defplay(self):

30pygame.mixer.music.load("audio/fruit.mp3")31pygame.mixer.music.play()32

33numbers=(34

35pygame.image.load("images/0.png"),36pygame.image.load("images/1.png"),37pygame.image.load("images/2.png"),38pygame.image.load("images/3.png"),39

40pygame.image.load("images/4.png"),41pygame.image.load("images/5.png"),42pygame.image.load("images/6.png"),43

pygame.image.load("images/7.png"),1pygame.image.load("images/8.png"),2pygame.image.load("images/9.png")3)4

5defshowscore(score):

6digit=[int(x)forxinstr(score)]7totalwidth=08foriindigit:

9totalwidth+=numbers[i].get_width()10offset=(width-totalwidth)/211foriindigit:

12screen.blit(numbers[i],(offset,height*0.1))13offset+=numbers[i].get_width()14

15#定义云朵类,用于装饰游戏背景16classCloud():

17def__init__(self):

18self.rect=pygame.Rect(0,0,0,0)19self.clouds=(20

21pygame.image.load("images/cloud1.png").convert_alpha(),22pygame.image.load("images/cloud2.png").convert_alpha(),23pygame.image.load("images/cloud3.png").convert_alpha(),24pygame.image.load("images/cloud4.png").convert_alpha(),25)26r=random.randint(0,3)27self.image=self.clouds[r]28self.rect.x=width29self.rect.y=height/10.030

31defmove(self):

32self.rect.__=speed33

34defdraw(self):

35screen.blit(self.image,(self.rect.x,self.rect.y))36#定义子弹类37classBullet():

38score=100

39def__init__(self):

40

41self.rect=pygame.Rect(0,0,0,0)42

self.image=pygame.image.load("images/flame.png").convert_alpha1()2self.explosion=(3

4pygame.image.load("images/bullet1.png").convert_alpha(),5pygame.image.load("images/bullet2.png").convert_alpha(),6pygame.image.load("images/bullet3.png").convert_alpha(),7pygame.image.load("i

展开阅读全文
相关资源
猜你喜欢
相关搜索

当前位置:首页 > 高中教育 > 高考

copyright@ 2008-2022 冰豆网网站版权所有

经营许可证编号:鄂ICP备2022015515号-1