话不多说,往下看吧!

<>引入需要的模块
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 = 0 counter = 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 = True explode(
m.pos(),explosions) thudsound.play() success = False break init_monster(m) elif
m.collide(bullet): # 如果怪兽碰到子弹 counter += 1 screen.title('奥特曼打怪兽,当前已打死:' + str(
counter) + " 个") explode(m.pos(),explosions) thudsound.play() if counter == 100
: # 如果100个怪兽死了 success = True running = False break init_monster(m) else:
m.movetimes += 1 if m.movetimes > random.randint(60,100): m.movetimes = 0
m.heading(ultraman) if ultraman.dead == False: # 如果奥特曼没有死 mxy = mouse_pos() #
鼠标指针的坐标 if ultraman.distance(mxy) > 50: # 如果到鼠标指针的距离大于50 ultraman.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 # 注意!重定义了speed bullet.show() else:
ultraman.move(0,-15) # 往下掉 if ultraman.ycor() < -height//2 - 20 : running =
False bullet.fd(bullet.speed) if bullet.collide_edge(): bullet.hide()
bullet.speed= 0
<>效果展示:

写完的效果就是这样的~

技术
©2019-2020 Toolsou All rights reserved,
macOS Big Sur无法完成安装 Big Sur为什么安装不了?Pandas与openpyxl库的超强结合,再见,Excel!2021-06-03一个人不是生来就要被打败的关于linux 命令“iptables -F”,不要轻易执行随机数的生成+猜数字游戏OpenCV python 模板匹配(多匹配)Python 引用cfg类型的配置文件(configparser)(telnetlib的使用方法)navicat运行sql文件时错误JVM老年代垃圾回收Full GCplc和单片机哪个更有前途?为什么本科生都不搞plc?