基于Python实现射击小游戏的制作
作者:五包辣条! 时间:2021-05-02 17:13:48
1.游戏画面
1.1开始
1.2射击怪物
2.涉及知识点
1.sprites
2.pygame混音器
3.图章
4.python基础语法
3.代码
3.1发射声
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安装此模块。")
3.2背景
width,height = 480,360
screen = Screen()
screen.bgpic('res/ghosthouse.jpg')
screen.setup(width,height)
batimages = ['res/bat1.png','res/bat2.png']
batindex = 0
bat = Sprite(visible=False,pos=(-50-width//2,100))
bat.dx = 3
bat.dy = 0
bat.alive = True
bat.show()
3.3射击效果
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 = False
while 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)
4.经验总结
利用python各种游戏库可以做任何小游戏
来源:https://blog.csdn.net/AI19970205/article/details/123943265
标签:Python,射击,游戏
![](/images/zang.png)
![](/images/jiucuo.png)
猜你喜欢
Python pygame绘制文字制作滚动文字过程解析
2022-06-10 13:21:11
如何使用Python逆向抓取APP数据
2022-03-12 11:03:41
![](https://img.aspxhome.com/file/2023/0/121600_0s.png)
Python爬虫辅助利器PyQuery模块的安装使用攻略
2023-10-18 02:19:34
五步让你成为GO 语言高手
2023-06-16 18:00:41
![](https://img.aspxhome.com/file/2023/3/86293_0s.jpg)
python创建文件备份的脚本
2023-04-07 06:17:44
python函数形参用法实例分析
2023-09-08 21:07:09
ASP将Excel数据导入到SQLServer的实现代码
2011-03-10 10:38:00
python3实现无权最短路径的方法
2023-07-11 23:26:40
![](https://img.aspxhome.com/file/2023/2/76872_0s.png)
python TF-IDF算法实现文本关键词提取
2022-10-08 16:06:25
asp获取远程网页的指定内容的实现代码
2011-02-16 10:41:00
pytorch+sklearn实现数据加载的流程
2022-05-15 14:44:27
![](https://img.aspxhome.com/file/2023/4/101234_0s.jpg)
Python网络编程使用select实现socket全双工异步通信功能示例
2023-04-10 21:06:59
Python利用命名空间解析XML文档
2021-10-06 05:45:32
详解Python中的字符串格式化
2023-09-10 22:38:14
![](https://img.aspxhome.com/file/2023/9/83879_0s.png)
详细总结Python常见的安全问题
2022-04-22 14:19:20
多个应用共存的Django配置方法
2021-06-28 03:25:37
![](https://img.aspxhome.com/file/2023/3/82593_0s.jpg)
Django零基础入门之模板变量详解
2023-03-19 11:17:16
![](https://img.aspxhome.com/file/2023/2/82722_0s.jpg)
解决hive中导入text文件遇到的坑
2023-06-30 16:28:26
python GUI框架pyqt5 对图片进行流式布局的方法(瀑布流flowlayout)
2022-07-07 17:14:01
python计算二维矩形IOU实例
2022-03-07 23:40:18
![](https://img.aspxhome.com/file/2023/8/95208_0s.jpg)