2024年4月15日发(作者:)
python大作业-小恐龙快跑-代码
# by 王帅兵 __541 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 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
=(“images/") 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.__=8 40
41 # 地图的绘制 42
def map_update(self): 1 (,(self.x,self.y)) 2
3 # 定义一个恐龙类 4 class Dinosaur(): 5 def __init__(self): 6 =(0,0,0,0)
7
8 =False 9 =180 10 =310 11 =0 12
13 # 定义一个循环器,用于显示小恐龙的图片 14 =cycle([0,1,2]) 15
=(("images/").convert_
("images/").convert_alpha(),
("images/").convert_alpha()
16 alpha(), 17
18
19 ) 20 = True 21 =("audio/") 22
=_size() 23 self.x=100 24 self.y= 25
t=(self.x,self.y) 26 def update(self): 27 if : 28 () 29
30 def jump(self): 31 =True 32
33 # 小恐龙的跳跃及奔跑 34 def run(self): 35 if : 36 if .y=: 37
=-bv 38 if .y=: 39 =bv 40
.y+= 41 if .y=: 42
=False 1
2 def draw(self): 3 index=next() # 一次取出小恐龙的一张图片 4
([index],(.x,.y)) 5
6 # 播放跳跃的音乐 7 def play(self): 8 () 9
10 # 游戏结束后画面的显示以及音乐的播放 11 def gameover(): 12
bump=("audio/")
sw=().current_w 15
13 () 14
16 sh=().current_h 17 gv=("audio/") 18
() 19 img=("images/").convert_alpha() 20
(img,((_width())/2,(_height())/ 21 2)) 22
23 # 定义一个障碍物类 24 class Obstacle(): 25 score=10 # 越过障碍物的得分 26 def
__init__(self): 27 =(0,0,0,0) 28
29 #加载障碍物的图片 30 le=( 31
32 ("images/").convert_alpha(), 33
34
35
36
37
("images/").convert_alpha(),
("images/").convert_alpha(),
("images/").convert_alpha(),
("images/").convert_alpha(),
("images/").convert_alpha(), 38
39 ) 40 #加载得分显示的图片 41 s=( 42
43
("images/"),
("images/"), 3
1 ("images/"),
("images/"),
2
4
("images/"),
("images/"),
5
7
("images/"),
("images/"),
6
8
("images/"), 9 ("images/") 10 ) 11
12 =("audio/") 13 x=t(1,100) 14
15 # 用于随机生成那种障碍 16 r=0 17 if x50: 18 r=t(0,2) 19 else : 20
r=t(3,5) 21 =le[r] 22 =_size() 23
,= 24
25 self.x=width 26 self.y=/2-230 27 =(self.x,self.y)
28 def move(self): 29 .__=speed 30
31 def draw(self): 32 (,(.x,.y)) 33
34 # 越过障碍物后的加分 35 def getscore(self): 36 tmp= 37 if tmp: 38
() 39 =0 40 return tmp 41
42
# 定义可以被子弹消灭的怪物类 1 class Monster(): 2 score=50
3 def __init__(self): 4
5 =(0,0,0,0) 6 rs=( 7
8 ("images/").convert_alpha(), 9
10
11
("images/").convert_alpha(),
("images/").convert_alpha(),
("images/").convert_alpha(), 12 ) 13
14 x=t(0,3) 15 =rs[x] 16 =cycle([0,1,2,3]) 17
18 s=( 19
20 ("images/"), 21 ("images/"), 22
23 ("images/"), 24 ("images/"), 25
("images/"),
("images/"),
26
28
("images/"),
("images/"),
27
29
("images/"), 30 ("images/") 31
32 ) 33
34 =3 # 怪物的生命 35 =_size() 36
37 ,= 38
39 self.x=width 40 self.y=/2-220 41 =(self.x,self.y)
42
def move(self): 1 .__=speed 2
3 def draw(self): 4 (,(.x,.y)) 5 def getscore(self): 6
tmp= 7 =0 8 return tmp 9
10 # 定义怪物飞龙类 11 class Dragon(): 12 score=100
13 def __init__(self): 14
15 =(0,0,0,0) 16 =( 17
18 ("images/").convert_alpha(), 19
20
21
22
23
("images/").convert_alpha(),
("images/").convert_alpha(),
("images/").convert_alpha(),
("images/").convert_alpha(),
("images/").convert_alpha(), 24 ) 25 =cycle([0,1,2,3,4,5])
26 s=(("images/"), 27
("images/"), 28
29 ("images/"), 30 ("images/"), 31
("images/"),
("images/"),
32
34
("images/"),
("images/"),
33
35
("images/"), 36 ("images/") 37 ) 38
39 =3 # 龙的生命 40
41 =_size() 42 ,= 43
1 self.x=width 2 self.y=/2-260 3 =(self.x,self.y)
4 =speed+2
5 on= (.x,.y) 6
7 def move(self): 8 .__= 9 on= (.x,.y) 10
11 def draw(self,index): 12 ([index],on) 13 def getscore(self):
14 tmp= 15 =0 16 return tmp 17
18 # 定义一个果实类 19 class Fruit(): 20 score=t(10,20) 21
22 def __init__(self): 23 =(0,0,0,0) 24 =( 25
26 ("images/").convert_alpha(), 27
28 ("images/").convert_alpha(), 29
30
31
32
("images/").convert_alpha(),
("images/").convert_alpha(),
("images/").convert_alpha(),
("images/").convert_alpha(), 33
34 ("images/").convert_alpha(), 35
36 ("images/").convert_alpha(), 37
38 ("images/").convert_alpha(),
("images/").convert_alpha(),
("images/").convert_alpha(),
("images/").convert_alpha(),
("images/").convert_alpha(), 42
39
40
41
("images/").convert_alpha(),
("images/").convert_alpha(),
("images/").convert_alpha(),
("images/").convert_alpha(),
("images/").convert_alpha(),
("images/").convert_alpha(),
1
2
3
4
5
6
("images/").convert_alpha(), 7 ) 8 r=t(0,19) 9
=[r] 10 tmp=0 11
12 # 随机生成果实坐标 13 tmp+=t(30,200) 14 .x = tmp+300 15
.y = t(180,280) 16
17 def move(self): 18 .__=speed 19
20 def draw(self): 21 (,(.x,.y)) 22
23 def getscore(self): 24 tmp= 25 if tmp: 26 () 27 =0 28 return
tmp 29 def play(self): 30 ("audio/3") 31
() 32
33 numbers=( 34
35 ("images/"), 36 ("images/"), 37
("images/"), 38 ("images/"), 39
40 ("images/"), 41 ("images/"), 42
("images/"), 43
("images/"),
("images/") 3 ) 4
1 ("images/"), 2
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
(numbers[i],(offset,height*0.1)) 13 offset+=numbers[i].get_width() 14
15 # 定义云朵类,用于装饰游戏背景 16 class Cloud(): 17 def __init__(self): 18
=(0,0,0,0) 19 =( 20
21 ("images/").convert_alpha(), 22
23
24
("images/").convert_alpha(),
("images/").convert_alpha(),
("images/").convert_alpha(), 25 ) 26 r=t(0,3) 27
=[r] 28 .x = width 29 .y = height/10.0 30
31 def move(self): 32 .__=speed 33
34 def draw(self): 35 (,(.x,.y)) 36 # 定义子弹类 37 class
Bullet(): 38 score=100
39 def __init__(self): 40
41 =(0,0,0,0) 42
=("images/").convert_alpha
ion=( 3
1 () 2
4 ("images/").convert_alpha(), 5
6
7
8
9
("images/").convert_alpha(),
("images/").convert_alpha(),
("images/").convert_alpha(),
("images/").convert_alpha(),
("images/").convert_alpha(),
=cycle([0,1,2,3,4,5]) 12 =False 13
10 )
=0
11
14
=("audio/") 15 =_size()
16 ,= 17
18 self.x=width 19 self.y=/2-230 20 =(self.x,self.y)
21 =False
22
23 def move(self): 24 .x+=speed 25
26 def draw(self): 27 (,(.x,.y)) 28 # 用于激活子弹 29
30 def reset(self,position): 31 , = position 32 = True 33
34 def show(self,position): 35 index=next() 36
(ion[index],position) 37
38 def play(self): 39 () 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=() 9 () 10 () 11
12 clock=() 13
14 # 设置屏幕大小 15 screen=_mode(size) 16
17 # 设置游戏标题 18 _caption("小恐龙") 19
20 # 加载背景音乐 21 bg=("audio/") 22
23 () 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
(b) 1
2 while True: 3 delay += 1 4 if delay==100: 5 delay = 0 6 for even in ():
7
8 # 判断是否是结束游戏的事件 9 if ==QUIT: 10 () 11
12 # 按下空格键后使得小恐龙开始跳跃 13 if ==__ and ==K_SPACE: 14 if
.y=: 15 () 16 () 17 if over: 18 main() 19
20 # 按下 A 键后开始发射子弹 21 if ==__ and ==K_a: 22
bullets[bindex].reset() 23 bindex = (bindex + 1) % NUM 24
25 if over==False: 26 _update() 27 _rolling() 28 _update() 29
_rolling() 30 () 31 () 32 end=() 33
34 # 每经过 10 秒就让障碍物移动速度加 0.5 35 if end-start10.0: 36 start=end 37
speed+=0.5 38
39 # 如果每经过 10 秒且障碍物出现间隔大于 1000,就加快障碍物出现间隔 40 if
interval1000: 41 interval-=20 42 for b in bullets: 43
if : 1 () 2 () 3
4 for i in range(len(monsters)-1,-1,-1): 5
6 # 判断子弹是否击中怪物 7 if e_rect_ratio(0.8)(b,monsters[i]): 8
= False 9
10 # 击中后怪物生命减一 11 monsters[i].alive-=1 12 () 13 =True 14
15 # 怪物生命为 0 后就删除怪物 16 if monsters[i].alive1: 17
score+=monsters[i].getscore() 18
19 del monsters[i] 20 for i in range(len(dragons)-1,-1,-1): 21
22 # 判断子弹是否击中大龙 23 if e_rect_ratio(0.8)(b,dragons[i]): 24
25 # 子弹状态改成未激活 26 = False
27
28 # 击中大龙后大龙生命减一 29 dragons[i].alive-=1 30
31 () 32 =True 33
34 # 大龙生命为 0 后就删除大龙 35 if dragons[i].alive1: 36 score+=dragons[i].getscore()
37 del dragons[i] 38
39 # 播放子弹的爆炸效果 40 for b in bullets: 41 if : 42 ((.x,.y)) 43
+=1 1 if ==6: 2 =0 3 =False 4
5 # 怪物出现的频率 6 if interval1400: 7
8 # 产生那种障碍物 9 r=t(1,100) 10
11 if rlevel: 12 obstacle=Obstacle() 13 (obstacle) 14 elif r20: 15 m=Monster()
16 (m) 17 else: 18 # 随机生成大龙出现的位置 19 x=t(1,100) 20
d=Dragon() 21 if x50: 22 .x,.y=on=(width,height/8.0) 23 =speed+5
24 (d) 25
26 interval=0 27
28 for i in range(len(monsters)): 29 monsters[i].move() 30 monsters[i].draw() 31 # 判断小
恐龙是否和怪物发生碰撞 32 if e_rect_ratio(0.7)(dinosaur,monsters[i]): 33
over=True 34 () 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
e_rect_ratio(0.8)(dinosaur,dragons[i]): 3 over=True 4 () 5
gameover() 6
7 # 产生装饰物白云 8 if not len(clouds): 9 c=Cloud() 10 (c) 11 for i in
range(len(clouds)-1,-1,-1): 12 clouds[i].move() 13 clouds[i].draw() 14 if clouds[i].rect.x0: 15 del
clouds[i] 16
17 # 随机生成果实 18 x=t(1,1000) 19 if x8: 20 f=Fruit() 21 (f)
22 for i in range(len(lis)): 23 lis[i].move() 24 lis[i].draw() 25
26 # 判断小恐龙是否和障碍物发生碰撞 27 if
e_rect_ratio(0.8)(dinosaur,lis[i]): 28 over=True 29
30 () 31 gameover() 32 else: 33
34 # 如果未碰撞就加分 35 if lis[i].rect.x+lis[i]..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 e_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 () 12 (fps) 13
14 # 运行游戏
15 if __name__=="__main__": 16 main() 17
发布者:admin,转转请注明出处:http://www.yc00.com/news/1713121814a2187263.html
评论列表(0条)