python大作业小恐龙快跑代码Word文档格式.docx
- 文档编号:17720773
- 上传时间:2022-12-08
- 格式:DOCX
- 页数:8
- 大小:19.02KB
python大作业小恐龙快跑代码Word文档格式.docx
《python大作业小恐龙快跑代码Word文档格式.docx》由会员分享,可在线阅读,更多相关《python大作业小恐龙快跑代码Word文档格式.docx(8页珍藏版)》请在冰豆网上搜索。
).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
#定义可以被子弹消灭的怪物类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("
),21pygame.image.load("
),22
23pygame.image.load("
),24pygame.image.load("
),25pygame.image.load("
),26pygame.image.load("
),27pygame.image.load("
),28pygame.image.load("
),29pygame.image.load("
),30pygame.image.load("
)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)
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("
),28
29pygame.image.load("
),31pygame.image.load("
),32pygame.image.load("
),33pygame.image.load("
),34pygame.image.load("
),35pygame.image.load("
),36pygame.image.load("
)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"
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
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("
),37pygame.image.load("
),38pygame.image.load("
),39
40pygame.image.load("
),41pygame.image.load("
),42pygame.image.load("
),43
)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"
images/cloud2.png"
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"
images/bullet2.png"
images/bullet3.png"
).convert_alpha(),7pygame.image.load("
i
- 配套讲稿:
如PPT文件的首页显示word图标,表示该PPT已包含配套word讲稿。双击word图标可打开word文档。
- 特殊限制:
部分文档作品中含有的国旗、国徽等图片,仅作为作品整体效果示例展示,禁止商用。设计者仅对作品中独创性部分享有著作权。
- 关 键 词:
- python 作业 恐龙 快跑 代码
![提示](https://static.bdocx.com/images/bang_tan.gif)