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

 # by 王帅兵 20177710541 1

 2 import math 3 import sys 4 import pygame 5 import random 6 import locale 7 import time 8 from itertools import cycle 9 from pygame.locals import * 10 import numbers 11

 12 # 屏幕的宽度和高度 13 width=1000 14 height=600 15

 16 size=(width,height) 17

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

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

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

 27 # 定义一个地图类 28 class MyMap(): 29 def __init__(self,x,y): 30 self.bg=pygame.image.load("images/background.png") 31 self.x=x 32 self.y=y 33

 34 # 地图的滚动 35 def map_rolling(self): 36 if self.x<-(width-10): 37 self.x=width 38 else: 39 self.x-=8 40

 41 # 地图的绘制 42

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

 3 # 定义一个恐龙类 4 class Dinosaur(): 5 def __init__(self): 6 self.rect=pygame.Rect(0,0,0,0) 7

 8 self.status=False 9 self.height=180 10 self.lowest=310 11 self.value=0 12

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

 19 ) 20 self.alive = True 21 self.audio=pygame.mixer.Sound("audio/jump.wav") 22 self.rect.size=self.image[0].get_size() 23 self.x=100 24 self.y=self.lowest 25 self.rect.topleft=(self.x,self.y) 26 def update(self): 27 if self.status: 28 Dinosaur.run() 29

 30 def jump(self): 31 self.status=True 32

 33 # 小恐龙的跳跃及奔跑 34 def run(self): 35 if self.status: 36 if self.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 if self.rect.y>=self.lowest: 42

 self.status=False 1

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

 6 # 播放跳跃的音乐 7 def play(self): 8 self.audio.play() 9

 10 # 游戏结束后画面的显示以及音乐的播放 11 def gameover(): 12 bump=pygame.mixer.Sound("audio/bump.wav") 13 bump.play() 14 sw=pygame.display.Info().current_w 15

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

 23 # 定义一个障碍物类 24 class Obstacle(): 25 score=10 # 越过障碍物的得分 26 def __init__(self): 27 self.rect=pygame.Rect(0,0,0,0) 28

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

 32 pygame.image.load("images/stone.png").convert_alpha(), 33 pygame.image.load("images/cacti.png").convert_alpha(), 34 pygame.image.load("images/c1.png").convert_alpha(), 35 pygame.image.load("images/c2.png").convert_alpha(), 36 pygame.image.load("images/c3.png").convert_alpha(), 37 pygame.image.load("images/c4.png").convert_alpha(), 38

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

 43

 pygame.image.load("images/0.png"), 1 pygame.image.load("images/1.png"), 2 pygame.image.load("images/2.png"), 3 pygame.image.load("images/3.png"), 4 pygame.image.load("images/4.png"), 5 pygame.image.load("images/5.png"), 6 pygame.image.load("images/6.png"), 7 pygame.image.load("images/7.png"), 8 pygame.image.load("images/8.png"), 9 pygame.image.load("images/9.png") 10 ) 11

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

 15 # 用于随机生成那种障碍 16 r=0 17 if x>50: 18 r=random.randint(0,2) 19 else : 20 r=random.randint(3,5) 21 self.image=self.obstacle[r] 22 self.rect.size=self.image.get_size() 23 self.width,self.height=self.rect.size 24

 25 self.x=width 26 self.y=height-self.height/2-230 27 self.rect.center=(self.x,self.y)

 28 def move(self): 29 self.rect.x-=speed 30

 31 def draw(self): 32 screen.blit(self.image,(self.rect.x,self.rect.y)) 33

 34 # 越过障碍物后的加分 35 def getscore(self): 36 tmp=self.score 37 if tmp: 38 self.audio.play() 39 self.score=0 40 return tmp 41

 42

 # 定义可以被子弹消灭的怪物类 1 class Monster(): 2 score=50

 3 def __init__(self): 4

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

 8 pygame.image.load("images/m1.png").convert_alpha(), 9 pygame.image.load("images/m2.png").convert_alpha(), 10 pygame.image.load("images/m3.png").convert_alpha(), 11 pygame.image.load("images/m4.png").convert_alpha(), 12 ) 13

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

 18 self.numbers=( 19

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

 23 pygame.image.load("images/2.png"), 24 pygame.image.load("images/3.png"), 25 pygame.image.load("images/4.png"), 26 pygame.image.load("images/5.png"), 27 pygame.image.load("images/6.png"), 28 pygame.image.load("images/7.png"), 29 pygame.image.load("images/8.png"), 30 pygame.image.load("images/9.png") 31

 32 ) 33

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

 37 self.width,self.height=self.rect.size 38

 39 self.x=width 40 self.y=height-self.height/2-220 41 self.rect.center=(self.x,self.y)

 42

 def move(self): 1 self.rect.x-=speed 2

 3 def draw(self): 4 screen.blit(self.image,(self.rect.x,self.rect.y)) 5 def getscore(self): 6 tmp=self.score 7 self.score=0 8 return tmp 9

 10 # 定义怪物飞龙类 11 class Dragon(): 12 score=100

 13 def __init__(self): 14

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

 18 pygame.image.load("images/d1.png").convert_alpha(), 19 pygame.image.load("images/d2.png").convert_alpha(), 20 pygame.image.load("images/d3.png").convert_alpha(), 21 pygame.image.load("images/d4.png").convert_alpha(), 22 pygame.image.load("images/d5.png").convert_alpha(), 23 pygame.image.load("images/d6.png").convert_alpha(), 24 ) 25 self.index=cycle([0,1,2,3,4,5]) 26 self.numbers=(pygame.image.load("images/0.png"), 27 pygame.image.load("images/1.png"), 28

 29 pygame.image.load("images/2.png"), 30 pygame.image.load("images/3.png"), 31 pygame.image.load("images/4.png"), 32 pygame.image.load("images/5.png"), 33 pygame.image.load("images/6.png"), 34 pygame.image.load("images/7.png"), 35 pygame.image.load("images/8.png"), 36 pygame.image.load("images/9.png") 37 ) 38

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

 41 self.rect.size=self.image[0].get_size() 42 self.width,self.height=self.rect.size 43

  1 self.x=width 2 self.y=height-self.height/2-260 3 self.rect.center=(self.x,self.y)

 4 self.speed=speed+2

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

 7 def move(self): 8 self.rect.x-=self.speed 9 self.position= (self.rect.x,self.rect.y) 10

 11 def draw(self,index): 12 screen.blit(self.image[index],self.position) 13 def getscore(self): 14 tmp=self.score 15 self.score=0 16 return tmp 17

 18 # 定义一个果实类 19 class Fruit(): 20 score=random.randint(10,20) 21

 22 def __init__(self): 23 self.rect=pygame.Rect(0,0,0,0) 24 self.fruits=( 25

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

 28 pygame.image.load("images/fruit2.png").convert_alpha(), 29 pygame.image.load("images/fruit3.png").convert_alpha(), 30 pygame.image.load("images/fruit4.png").convert_alpha(), 31 pygame.image.load("images/fruit5.png").convert_alpha(), 32 pygame.image.load("images/fruit6.png").convert_alpha(), 33

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

 36 pygame.image.load("images/fruit8.png").convert_alpha(), 37 pygame.image.load("images/fruit9.png").convert_alpha(), 38 pygame.image.load("images/fruit10.png").convert_alpha(), 39 pygame.image.load("images/fruit11.png").convert_alpha(), 40 pygame.image.load("images/fruit12.png").convert_alpha(), 41 pygame.image.load("images/fruit13.png").convert_alpha(), 42

 pygame.image.load("images/fruit14.png").convert_alpha(), 1 pygame.image.load("images/fruit15.png").convert_alpha(), 2 pygame.image.load("images/fruit16.png").convert_alpha(), 3 pygame.image.load("images/fruit17.png").convert_alpha(), 4 pygame.image.load("images/fruit18.png").convert_alpha(), 5 pygame.image.load("images/fruit19.png").convert_alpha(), 6 pygame.image.load("images/fruit20.png").convert_alpha(), 7 ) 8 r=random.randint(0,19) 9 self.image=self.fruits[r] 10 tmp=0 11

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

 17 def move(self): 18 self.rect.x-=speed 19

 20 def draw(self): 21 screen.blit(self.image,(self.rect.x,self.rect.y)) 22

 23 def getscore(self): 24 tmp=self.score 25 if tmp: 26 self.play() 27 self.score=0 28 return tmp 29 def play(self): 30 pygame.mixer.music.load("audio/fruit.mp3") 31 pygame.mixer.music.play() 32

 33 numbers=( 34

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

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

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

 5 def showscore(score): 6 digit=[int(x) for x in str(score)] 7 totalwidth=0 8 for i in digit: 9 totalwidth+=numbers[i].get_width() 10 offset=(width-totalwidth)/2 11 for i in digit: 12 screen.blit(numbers[i],(offset,height*0.1)) 13 offset+=numbers[i].get_width() 14

 15 # 定义云朵类,用于装饰游戏背景 16 class Cloud(): 17 def __init__(self): 18 self.rect=pygame.Rect(0,0,0,0) 19 self.clouds=( 20

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

 31 def move(self): 32 self.rect.x-=speed 33

 34 def draw(self): 35 screen.blit(self.image,(self.rect.x,self.rect.y)) 36 # 定义子弹类 37 class Bullet(): 38 score=100

 39 def __init__(self): 40

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

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

 4 pygame.image.load("images/bullet1.png").convert_alpha(), 5 pygame.image.load("images/bullet2.png").convert_alpha(), 6 pygame.image.load("images/bullet3.png").convert_alpha(), 7 pygame.image.load("images/bullet4.png").convert_alpha(), 8 pygame.image.load("images/bullet5.png").convert_alpha(), 9 pygame.image.load("images/bullet6.png").convert_alpha(), 10 ) 11 self.index=cycle([0,1,2,3,4,5]) 12 self.ok=False 13 self.times=0 14 self.audio=pygame.mixer.Sound("audio/exlposion.wav") 15 self.rect.size=self.image.get_size() 16 self.width,self.height=self.rect.size 17

 18 self.x=width 19 self.y=height-self.height/2-230 20 self.rect.center=(self.x,self.y)

 21 self.active=False

 22

 23 def move(self): 24 self.rect.x+=speed 25

 26 def draw(self): 27 screen.blit(self.image,(self.rect.x,self.rect.y)) 28 # 用于激活子弹 29

 30 def reset(self,position): 31 self.rect.left , self.rect.top = position 32 self.active = True 33

 34 def show(self,position): 35 index=next(self.index) 36 screen.blit(self.explosion[index],position) 37

 38 def play(self): 39 self.audio.play() 40

 41 def main(): 42 delay=0 43

 bindex=0 1 NUM=5 2 score=0 3 over=False 4 idx=0 5

 6 # 将 screen,clock,speed 声明为全局变量 7 global screen,clock,speed 8 start=time.time() 9 pygame.init() 10 pygame.mixer.init() 11

 12 clock=pygame.time.Clock() 13

 14 # 设置屏幕大小 15 screen=pygame.display.set_mode(size) 16

 17 # 设置游戏标题 18 pygame.display.set_caption("小恐龙") 19

 20 # 加载背景音乐 21 bg=pygame.mixer.Sound("audio/background.ogg") 22

 23 bg.play() 24

 25 # 定义两个滚动的地图 26 bg1=MyMap(0,0) 27 bg2=MyMap(width,0) 28 # 实例化小恐龙 29 dinosaur=Dinosaur() 30 interval=0 31 lis=[] 32 fruit=[] 33 level=50 34 monsters=[] 35 bullets=[] 36 dragons=[] 37 clouds=[] 38

 39 # 生成未激活的子弹 40 for _ in range(NUM): 41 b=Bullet() 42

 bullets.append(b) 1

 2 while True: 3 delay += 1 4 if delay==100: 5 delay = 0 6 for even in pygame.event.get(): 7

 8 # 判断是否是结束游戏的事件 9 if even.type==QUIT: 10 sys.exit() 11

 12 # 按下空格键后使得小恐龙开始跳跃 13 if even.type==KEYDOWN and even.key==K_SPACE: 14 if dinosaur.rect.y>=dinosaur.lowest: 15 dinosaur.jump() 16 dinosaur.play() 17 if over: 18 main() 19

 20 # 按下 A 键后开始发射子弹 21 if even.type==KEYDOWN and even.key==K_a: 22 bullets[bindex].reset(dinosaur.rect.midtop) 23 bindex = (bindex + 1) % NUM 24

 25 if over==False: 26 bg1.map_update() 27 bg1.map_rolling() 28 bg2.map_update() 29 bg2.map_rolling() 30 dinosaur.run() 31 dinosaur.draw() 32 end=time.time() 33

 34 # 每经过 10 秒就让障碍物移动速度加 0.5 35 if end-start>10.0: 36 start=end 37 speed+=0.5 38

 39 # 如果每经过 10 秒且障碍物出现间隔大于 1000,就加快障碍物出现间隔 40 if interval>1000: 41 interval-=20 42 for b in bullets: 43

 if b.active: 1 b.move() 2 b.draw() 3

 4 for i in range(len(monsters)-1,-1,-1): 5

 6 # 判断子弹是否击中怪物 7 if pygame.sprite.collide_rect_ratio(0.8)(b,monsters[i]): 8 b.active = False 9

 10 # 击中后怪物生命减一 11 monsters[i].alive-=1 12 b.play() 13 b.ok=True 14

 15 # 怪物生命为 0 后就删除怪物 16 if monsters[i].alive<1: 17 score+=monsters[i].getscore() 18

 19 del monsters[i] 20 for i in range(len(dragons)-1,-1,-1): 21

 22 # 判断子弹是否击中大龙 23 if pygame.sprite.collide_rect_ratio(0.8)(b,dragons[i]): 24

 25 # 子弹状态改成未激活 26 b.active = False

 27

 28 # 击中大龙后大龙生命减一 29 dragons[i].alive-=1 30

 31 b.play() 32 b.ok=True 33

 34 # 大龙生命为 0 后就删除大龙 35 if dragons[i].alive<1: 36 score+=dragons[i].getscore() 37 del dragons[i] 38

 39 # 播放子弹的爆炸效果 40 for b in bullets: 41 if b.ok: 42 b.show((b.rect.x,b.rect.y)) 43

 b.times+=1 1 if b.times==6: 2 b.times=0 3 b.ok=False 4

 5 # 怪物出现的频率 6 if interval>1400: 7

 8 # 产生那种障碍物 9 r=random.randint(1,100) 10

 11 if r>level: 12 obstacle=Obstacle() 13 lis.append(obstacle) 14 elif r>20: 15 m=Monster() 16 monsters.append(m) 17 else: 18 # 随机生成大龙出现的位置 19 x=random.randint(1,100) 20 d=Dragon() 21 if x>50: 22 d.rect.x,d.rect.y=d.position=(width,height/8.0) 23 d.speed=speed+5 24 dragons.append(d) 25

 26 interval=0 27

 28 for i in range(len(monsters)): 29 monsters[i].move() 30 monsters[i].draw() 31 # 判断小恐龙是否和怪物发生碰撞 32 if pygame.sprite.collide_rect_ratio(0.7)(dinosaur,monsters[i]): 33 over=True 34 bg.stop() 35 gameover() 36

 37 # 绘制大龙图像 38 for i in range(len(dragons)): 39 if not(delay%10): 40 idx=(idx+1)%6 41 dragons[i].move() 42 dragons[i].draw(idx) 43

  1 # 判断小恐龙是否和大龙发生碰撞 2 if pygame.sprite.collide_rect_ratio(0.8)(dinosaur,dragons[i]): 3 over=True 4 bg.stop() 5 gameover() 6

 7 # 产生装饰物白云 8 if not len(clouds): 9 c=Cloud() 10 clouds.append(c) 11 for i in range(len(clouds)-1,-1,-1): 12 clouds[i].move() 13 clouds[i].draw() 14 if clouds[i].rect.x<0: 15 del clouds[i] 16

 17 # 随机生成果实 18 x=random.randint(1,1000) 19 if x<8: 20 f=Fruit() 21 fruit.append(f) 22 for i in range(len(lis)): 23 lis[i].move() 24 lis[i].draw() 25

 26 # 判断小恐龙是否和障碍物发生碰撞 27 if pygame.sprite.collide_rect_ratio(0.8)(dinosaur,lis[i]): 28 over=True 29

 30 bg.stop() 31 gameover() 32 else: 33

 34 # 如果未碰撞就加分 35 if lis[i].rect.x+lis[i].rect.width<dinosaur.rect.x: 36 score+=lis[i].getscore() 37 for i in range(len(fruit)-1,-1,-1): 38 fruit[i].move() 39 fruit[i].draw() 40

 41

 # 如果小恐龙吃到果实就加分 1 if pygame.sprite.collide_rect_ratio(1.1)(dinosaur,fruit[i]): 2 score+=fruit[i].getscore() 3 fruit[i].play() 4 del fruit[i] 5

 6 # 显示分数 7 showscore(score) 8 interval+=20 9

 10 # 刷新屏幕显示

 11 pygame.display.flip() 12 clock.tick(fps) 13

 14 # 运行游戏

 15 if __name__=="__main__": 16 main() 17

 18

 19

推荐访问:作业 快跑 恐龙