利用Python制作第一人称射击小游戏 含源代码

简介: 利用Python制作第一人称射击小游戏 含源代码

利用Python制作第一人称射击小游戏 含源代码

涉及知识点
1.sprites
2.pygame混音器
3.图章
4.python基础语法
.代码
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安装此模块。")

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射击效果

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)

效果图

射击效果图

目录
相关文章
|
22天前
|
人工智能 机器人 测试技术
【python】python小游戏——开心消消乐(源码)【独一无二】
【python】python小游戏——开心消消乐(源码)【独一无二】
|
1月前
|
Python
python小游戏7
python小游戏7
|
1月前
|
Python
python小游戏6
python小游戏6
|
1月前
|
Python
python小游戏5
python小游戏5
|
1月前
|
Python
python小游戏4
python小游戏4
|
1月前
|
Python
python小游戏1
python小游戏1
|
1月前
|
安全 C++ Python
小游戏实战-Python实现石头剪刀布+扫雷小游戏
小游戏实战-Python实现石头剪刀布+扫雷小游戏
35 0
|
1月前
|
Python
python小游戏3
python小游戏3
|
1月前
|
Python
python小游戏
python小游戏
|
1天前
|
Shell Python
python|闲谈2048小游戏和数组的旋转及翻转和转置
python|闲谈2048小游戏和数组的旋转及翻转和转置
20 1

热门文章

最新文章