用户:定理查看:0 回复:0 评论:0 创建时间:2022-11-25T15:07:26
【作品展示】

【作品介绍】
难度飙升
【作品源代码】
import pgzrun # 导入游戏库
import random
WIDTH = 800 # 设置窗口的宽度
HEIGHT = 600 # 设置窗口的高度
time=0
hero=Actor("hero")
live=3
livePics=[]
for i in range(live):
livePic=Actor('hero_喵all')
livePic.x=40+i*60
livePic.y=40
livePics.append(livePic)
class Ball:
x = None # 小球的x坐标,初始化再窗口中间
y = None # 小球的y坐标,初始化再窗口中间
speed_x = None # 小球x方向的速度
speed_y = None # 小球y方向的速度
r = None # 小球的半径
color = None # 小球的三个颜色分量
def __init__(self, x, y, speed_x, speed_y, r, color):
self.x = x
self.y = y
self.speed_x = speed_x
self.speed_y = speed_y
self.r = r
self.color = color
def draw(self):
screen.draw.filled_circle((self.x,self.y),self.r,(self.color))
def update(self):
self.x = self.x + self.speed_x
self.y = self.y + self.speed_y
if self.x > WIDTH-self.r or self.x < self.r: # 当小球碰到左右边界时,x方向速度反转
self.speed_x = -self.speed_x
if self.y > HEIGHT-self.r or self.y < self.r: # 当小球碰到上下边界时,y方向速度反转
self.speed_y = -self.speed_y
class SmartBall(Ball):
targetX=None
targetY=None
def __init__(self, x, y, speed_x, speed_y, r, color,targetX,targetY):
super().__init__(x, y, speed_x, speed_y, r, color)
self.targetX=targetX
self.targetY = targetY
def updatefortarget(self):
if self.targetX>self.x:
self.speed_x=random.randint(1,2)
elif self.targetX self.y:
self.speed_y = random.randint(1, 2)
elif self.targetY < self.y:
self.speed_y = random.randint(-2, -1)
balls = []
def draw():
screen.fill('white')
hero.draw()
for i in range(live):
livePics[i].draw()
for ball in balls:
ball.draw()
screen.draw.text(str(time)+'秒(飞了个机开始)',(270,10),fontsize=50,fontname='s',color='green')
if live<=0:
clock.unschedule(count)
if time==20:
screen.draw.text('第二关',(270,150),fontsize=50,fontname='s',color='red')
screen.draw.text('难度飙升(坚持到八十秒就算胜利)',(270,200),fontsize=50,fontname='s',color='red')
if time==80:
screen.draw.text('胜利进入飞机群(进入无尽模式)',(270,150),fontsize=50,fontname='s',color='red')
def update():
global live
if live<=0:
return
for ball in balls:
ball.update()
if abs(hero.x-ball.x)<20 and abs(hero.y-ball.y)<30:
live-=1
ball.y=0
if live<=0:
hero.image='blowup'
def on_mouse_move(pos,rel,buttons):
hero.x=pos[0]
hero.y=pos[1]
def count():
global time
time+=1
x = WIDTH//2
y = random.randint(5, HEIGHT//10)
speed_x = random.choice([-3, -2, -1, 1, 2, 3])
speed_y = random.randint(1, 3)
if time % 3== 0 and len(balls) <= 20:
r=3
color='black'
ball=Ball(x, y,speed_x, speed_y, r,color)
balls.append(ball)
if time%20==0 and len(balls)>=1:
r=5
color='red'
喵artball = SmartBall(x, y, speed_x, speed_y, r, color,hero.x,hero.y)
balls.append(喵artball)
for 喵artball in balls:
if isinstance(喵artball ,SmartBall):
喵artball.targetX=hero.x
喵artball.targetY= hero.y
喵artball.updatefortarget()
clock.schedule_unique(count,1)
count()
pgzrun.go() # 开始执行游戏
【提示】
部分含有Python第三方库相关内容的作品,在海龟编辑器网页端无法运行哦!如遇到这种情况,可以打开下面的链接,下载海龟编辑器客户端:
https://python.codemao.cn