话不多说,往下看吧!
引入需要的模块
from sprites import *
from pygame import mixer
设置页面格式(宽高等等)
width,height = 800,600
screen = Screen()
screen.setup(width,height)
screen.bgcolor('black')
screen.title('奥特曼打怪兽by 代码看主页领')
封面角色
frames = [f"frames/{i:04d}.png" for i in range(23)]
cover = Sprite(shape=frames)
背景音乐
cover.play('纯音乐 - 迪迦奥特曼主题曲.mp3.wav',loop=True)
for _ in frames:cover.nextcostume()cover.wait(0.15)
for s in range(10,1,-1):cover.scale(s/100)cover.right(30)cover.wait(0.01)
ultraman = cover
ultraman.dead = False
主程序
explosions = ['res/explosion0.png','res/explosion1.png']
costumes = ['res/fantasy7.png','res/ghoul-b.png','res/knight1.png','res/monster1-a.png','res/robot1.png','res/wild1.png','res/witch.png']monsters = Group('monster') # 怪物组
[Sprite(shape=random.choice(costumes),tag='monster',visible=False) for _ in range(15)]def init_monster(m):x = random.randint(-1000,1000)y = random.randint(-1000,1000)while ultraman.distance(x,y) < 400:x = random.randint(-1000,1000)y = random.randint(-1000,1000)m.goto(x,y)m.movetimes = 0 m.heading(ultraman)m.show()[init_monster(m) for m in monsters]bullet = Sprite('res/火png.png',visible=False)
bullet.speed = 0counter = 0 # 计数器
leftkey = Mouse(1) # 鼠标左键
clock = Clock()
success = None # None,False与True
running = True
while running:for m in monsters:m.fd(1)# 如果撞到奥特曼if m.collide(ultraman,scale=0.5) and ultraman.dead == False:ultraman.dead = Trueexplode(m.pos(),explosions)thudsound.play()success = False breakinit_monster(m)elif m.collide(bullet): # 如果怪兽碰到子弹counter += 1screen.title('奥特曼打怪兽,当前已打死:' + str(counter) + " 个")explode(m.pos(),explosions)thudsound.play()if counter == 100 : # 如果100个怪兽死了success = Truerunning = Falsebreakinit_monster(m) else: m.movetimes += 1if m.movetimes > random.randint(60,100):m.movetimes = 0m.heading(ultraman)if ultraman.dead == False: # 如果奥特曼没有死mxy = mouse_pos() # 鼠标指针的坐标if ultraman.distance(mxy) > 50: # 如果到鼠标指针的距离大于50ultraman.heading(mxy) # 朝向鼠标指针ultraman.fd(10) # 移动10个单位if leftkey.down() and bullet.speed == 0: # 按左键发射shootsound.play()bullet.goto(ultraman.pos())bullet.setheading(ultraman.heading())bullet.speed = 20 # 注意!重定义了speedbullet.show()else:ultraman.move(0,-15) # 往下掉if ultraman.ycor() < -height//2 - 20 : running = Falsebullet.fd(bullet.speed)if bullet.collide_edge():bullet.hide()bullet.speed = 0
效果展示:
写完的效果就是这样的~