Python制作自己的第一人称射击游戏

(仿cf)

作为一杆老枪,我觉得我必须站出来了,现在世风日下,于是我开始用Python自己制作一个射击游戏。

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各种游戏库可以做任何小游戏

  • 9
    点赞
  • 22
    收藏
    觉得还不错? 一键收藏
  • 3
    评论
为了编写一个简单的Python射击游戏,我们可以使用Pygame模块。下面是一个简单的示例代码,它创建了一个窗口并在其中显示一个移动的目标。玩家可以使用鼠标来控制一个瞄准器,并通过点击鼠标来射击目标。 ```python import pygame import random # 初始化Pygame模块 pygame.init() # 设置窗口大小 size = width, height = 640, 480 # 创建窗口 screen = pygame.display.set_mode(size) # 设置窗口标题 pygame.display.set_caption("射击游戏") # 加载图像 target_image = pygame.image.load("target.png") crosshair_image = pygame.image.load("crosshair.png") # 获取图像矩形 target_rect = target_image.get_rect() # 设置目标的初始位置 target_rect.center = (width // 2, height // 2) # 设置瞄准器的初始位置 crosshair_rect = crosshair_image.get_rect() crosshair_rect.center = pygame.mouse.get_pos() # 设置目标的移动速度 speed = [random.randint(-5, 5), random.randint(-5, 5)] # 设置计时器 clock = pygame.time.Clock() # 游戏循环 while True: # 处理事件 for event in pygame.event.get(): if event.type == pygame.QUIT: # 退出游戏 pygame.quit() sys.exit() elif event.type == pygame.MOUSEBUTTONDOWN: # 判断是否击中目标 if target_rect.collidepoint(event.pos): # 重新设置目标的位置 target_rect.center = (random.randint(0, width), random.randint(0, height)) # 移动目标 target_rect = target_rect.move(speed) # 判断是否碰到边界 if target_rect.left < 0 or target_rect.right > width: speed[0] = -speed[0] if target_rect.top < 0 or target_rect.bottom > height: speed[1] = -speed[1] # 更新瞄准器的位置 crosshair_rect.center = pygame.mouse.get_pos() # 绘制图像 screen.fill((255, 255, 255)) screen.blit(target_image, target_rect) screen.blit(crosshair_image, crosshair_rect) # 更新屏幕 pygame.display.flip() # 控制帧率 clock.tick(60) ```

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 3
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值