Python 官方文档:入门教程 => 点击学习
本文小编为大家详细介绍“基于python怎么实现射击小游戏”,内容详细,步骤清晰,细节处理妥当,希望这篇“基于Python怎么实现射击小游戏”文章能帮助大家解决疑惑,下面跟着小编的思路慢慢深入,一起来学习新知识吧。1.游戏画面1.1开始1.
本文小编为大家详细介绍“基于python怎么实现射击小游戏”,内容详细,步骤清晰,细节处理妥当,希望这篇“基于Python怎么实现射击小游戏”文章能帮助大家解决疑惑,下面跟着小编的思路慢慢深入,一起来学习新知识吧。
sprites
pygame混音器
图章
Python基础语法
from sprites import *try: import pygame pygame.mixer.init() fire_sound = pygame.mixer.Sound("audio/发射声.wav") cricket_sound = pygame.mixer.Sound('audio/cricket.wav')except: import sys input("本程序需要pygame混音器支持以便配音,请先在cmd下用pip install pygame安装此模块。")
width,height = 480,360screen = Screen()screen.bgpic('res/ghosthouse.jpg')screen.setup(width,height)batimages = ['res/bat1.png','res/bat2.png']batindex = 0bat = Sprite(visible=False,pos=(-50-width//2,100))bat.dx = 3bat.dy = 0bat.alive = Truebat.show()
def bat_alt_costume(): global batindex batindex = 1 - batindex bat.shape(batimages[batindex]) screen.ontimer(bat_alt_costume,90)bat_alt_costume() hole = Sprite(shape='res/Bullet_Hole.png',visible=False)m1 = Mouse(1) # 鼠标左键m3 = Mouse(3) # 鼠标右键clock = Clock() # 时钟对象 start_stamp = Falsewhile True: bat.move(bat.dx,bat.dy) # 掉到地面就盖图章,留下尸体 if bat.ycor() < random.randint(-200,-100): bat.dx = 0 bat.dy = 0 bat.setheading(random.randint(1,360)) bat.stamp() bat.reborn(-500-width//2,100,3,0,delay=2) bat.alive = True bat.setheading(0) # 蝙蝠碰到鼠标指针并且按下了鼠标左键 if bat.collide_mouse() and m1.down() and bat.alive: bat.dy = -10 # 开始往下掉 bat.alive = False try: cricket_sound.play() except:pass # 到了最右边就到最左边去重新开始 if bat.xcor() > width//2 : bat.reborn(-500-width//2,100,3,0,delay=2) bat.alive = True bat.setheading(0) hole.Goto(mouse_position()) # 发射子弹,用盖图章留下弹洞,为防连续发射用了start_stamp变量 if m1.down() and not start_stamp: hole.stamp() start_stamp = True try: fire_sound.play() except: pass # 松开按键后 if not m1.down():start_stamp = False clock.tick(60)
读到这里,这篇“基于Python怎么实现射击小游戏”文章已经介绍完毕,想要掌握这篇文章的知识点还需要大家自己动手实践使用过才能领会,如果想了解更多相关内容的文章,欢迎关注编程网Python频道。
--结束END--
本文标题: 基于Python怎么实现射击小游戏
本文链接: https://www.lsjlt.com/news/326370.html(转载时请注明来源链接)
有问题或投稿请发送至: 邮箱/279061341@qq.com QQ/279061341
下载Word文档到电脑,方便收藏和打印~
2024-03-01
2024-03-01
2024-03-01
2024-02-29
2024-02-29
2024-02-29
2024-02-29
2024-02-29
2024-02-29
2024-02-29
回答
回答
回答
回答
回答
回答
回答
回答
回答
回答
0