猫史档案馆


【Python作品分享】fps射击游戏【求助帖】

用户:橙成仔吖橙成仔吖查看:10 回复:5 评论:10 创建时间:2023-04-24T21:29:42


【作品展示】

center_image

 

【作品介绍】

左键攻击,可跳上石桩,Tab键可切换空袭模式

 

【作品源代码】

#ursina介绍并且创建一个游戏场景(√)
#相机模式并且实现了射击基本功能(√)
#敌人和血量并且做了射击功能(√)
#完善细节并添加更多敌人()
from ursina import*
from ursina.prefabs.first_person_controller import FirstPersonController
from ursina.shaders import lit_with_shadows_shader

app = Ursina()

Entity.default_shader = lit_with_shadows_shader

DirectionalLight(y = 1,ratation = (0,0,0))

Sky()

ground = Entity(model = "plane",collider = "box",scale = 喵,texture = "grass")

for i in range(16):
    Entity(model = "cube",scale = 2,texture = "brick",
            texture_scale = (1,2),
            x = random.uniform(-8,8),
            z = random.uniform(-8,8) + 8,
            collider = "box",
            scale_y = random.uniform(2,3),
            origin_y = -0.5)

editor_camera = EditorCamera(enabled = False)

def input_pause(key):
    if key == "tab":
        editor_camera.enabled = not editor_camera.enabled
        mouse.locked = not editor_camera.enabled
        player.cursor.enabled = not editor_camera.enabled
        player.visible_self = editor_camera.enabled
        editor_camera.position = player.position

pause_handler = Entity(input = input_pause)

player = FirstPersonController(model = "cube",color = color.orange,z = -10,origin_y = -0.5,speed = 8,collider = "box")

喵 = Entity(model = "cube",parent = camera,scale = (0.2,0.2,0.5),position = (0.5,-0.25,0.5),color = color.red,on_cooldown = False)
喵.flash = Entity(parent = 喵,model = "quad",z = 1,color = color.yellow,enabled = False)

def shoot():
    if not 喵.on_cooldown:
        #print("shooting")
        喵.on_cooldowm = True
        喵.flash.enabled = True
        from ursina.prefabs.ursfx import ursfx
        ursfx([(0.0,0.0),(0.1,0.9),(0.15,0.75),(0.3,0.14),(0.6,0.0)],volume = 0.5,wave = "noise",
              pitch = random.uniform(-13,-12),pitch_change = - 12,speed = 3.0)
        invoke(喵.flash.disable,delay = 0.15)
        invoke(setattr,喵,'on_cooldown',False,delay = 0.15)
        if mouse.hovered_entity and hasattr(mouse.hovered_entity,"hp"):
            mouse.hovered_entity.blink(color.red)
            mouse.hovered_entity.hp -= 10
            if mouse.hovered_entity.hp <= 0:
                destroy(mouse.hovered_entity)
            mouse.hovered_entity.health_bar.scale_x = mouse.hovered_entity.hp/mouse.hovered_entity.max_hp * 1.5

def update():
    if held_keys["left mouse"]:
        shoot()

class Enemy(Entity):
    def __init__(self,add_to_scene_entities = True,**kwargs):
        super().__init__(add_to_scene_entities,
                         model = "cube",collider = "box",
                         scale_y = 2,origin_y = -0.5,
                         color = color.light_gray,**kwargs)
        self.health_bar = Entity(parent = self,model = "cube",color = color.red,y = 1.2,
                                scale = (1.5,0.1,0.1))
        self.max_hp = 100
        self.hp = self.max_hp

    def update(self):
        self.look_at_2d(player.position,"y")

        hit_info = raycast(self.position + Vec3(0,1,0),self.forward,30,ignore = (self,),debug = False)
        if hit_info.entity == player:
            if distance_xz(self.position,player.position) > 2:
                self.position = self.position + self.forward*time.dt*5

for i in range(8):
    Enemy(x = i*5)
            

app.run()

 

【提示】

部分含有Python第三方库相关内容的作品,在海龟编辑器网页端无法运行哦!如遇到这种情况,可以打开下面的链接,下载海龟编辑器客户端:

https://python.codemao.cn


回复

上一页1 页 / 共 1下一页
Dream神god退游中Dream神god退游中

我勒个喵刚

点赞0


评论


Dream神god退游中Dream神god退游中

居然还有人会做Python

点赞0


评论


code猫的code猫的

from ursina import *
from ursina.prefabs.first_person_controller import FirstPersonController
from ursina.shaders import lit_with_喵s_shader
import random

app = Ursina()

Entity.default_shader = lit_with_喵s_shader

DirectionalLight(y=1, rotation=(0, 0, 0))
Sky()

ground = Entity(model="plane", collider="box", scale=64, texture="grass")

# 生成随机物体
for _ in range(16):
    Entity(model="cube", scale=2, texture="brick",
            texture_scale=(1, 2),
            x=random.uniform(-8, 8),
            z=random.uniform(-8, 8) + 8,
            collider="box",
            scale_y=random.uniform(2, 3),
            origin_y=-0.5)

editor_camera = EditorCamera(enabled=False)

def input_pause(key):
    if key == "tab":
        editor_camera.enabled = not editor_camera.enabled
        mouse.locked = not editor_camera.enabled
        player.cursor.enabled = not editor_camera.enabled
        player.visible_self = editor_camera.enabled
        editor_camera.position = player.position

pause_handler = Entity(input=input_pause)

player = FirstPersonController(model="cube", color=color.orange, z=-10, origin_y=-0.5, speed=8, collider="box")

gun = Entity(model="cube", parent=camera, scale=(0.2, 0.2, 0.5), position=(0.5, -0.25, 0.5), color=color.red, on_cooldown=False)
gun.flash = Entity(parent=gun, model="quad", z=1, color=color.yellow, enabled=False)

def shoot():
    if not gun.on_cooldown:
        gun.on_cooldown = True
        gun.flash.enabled = True
        from ursina.prefabs.ursfx import ursfx
        ursfx([(0.0, 0.0), (0.1, 0.9), (0.15, 0.75), (0.3, 0.14), (0.6, 0.0)], volume=0.5, wave="noise",
              pitch=random.uniform(-13, -12), pitch_change=-12, speed=3.0)
        invoke(gun.flash.disable, delay=0.15)
        invoke(setattr, gun, 'on_cooldown', False, delay=0.15)
        
        if mouse.hovered_entity and hasattr(mouse.hovered_entity, "hp"):
            hit_enemy(mouse.hovered_entity)

def hit_enemy(enemy):
    enemy.blink(color.red)
    enemy.hp -= 10
    if enemy.hp <= 0:
        destroy(enemy)
    enemy.health_bar.scale_x = enemy.hp / enemy.max_hp * 1.5

def update():
    if held_keys["left mouse"]:
        shoot()

class Enemy(Entity):
    def __init__(self, add_to_scene_entities=True, **kwargs):
        super().__init__(add_to_scene_entities,
                         model="cube", collider="box",
                         scale_y=2, origin_y=-0.5,
                         color=color.light_gray, **kwargs)
        self.health_bar = Entity(parent=self, model="cube", color=color.red, y=1.2,
                                 scale=(1.5, 0.1, 0.1))
        self.max_hp = 100
        self.hp = self.max_hp

    def update(self):
        self.look_at_2d(player.position, "y")
        hit_info = raycast(self.position + Vec3(0, 1, 0), self.forward, 30, ignore=(self,), debug=False)
        if hit_info.entity == player:
            if distance_xz(self.position, player.position) > 2:
                self.position += self.forward * time.dt * 5

# 随机生成敌人
for _ in range(8):
    Enemy(x=random.uniform(-8, 8), z=random.uniform(-8, 8))

app.run()

点赞0


评论


python喵呜python喵呜

不错,来不来python爱好者(PE)工作室?

点赞0


评论


Dream神god退游中Dream神god退游中

from math import pi, sin, cos

from direct.showbase.ShowBase import ShowBase
from direct.task import Task
from direct.actor.Actor import Actor
from direct.interval.IntervalGlobal import Sequence
from panda3d.core import Point3


class MyApp(ShowBase):
    def __init__(self):
        ShowBase.__init__(self)

        # Disable the camera trackball controls.
        self.disableMouse()

        # Load the environment model.
        self.scene = self.loader.loadModel("models/environment")
        # Reparent the model to render.
        self.scene.reparentTo(self.render)
        # Apply scale and position transforms on the model.
        self.scene.setScale(0.25, 0.25, 0.25)
        self.scene.setPos(-8, 42, 0)

        # Add the spinCameraTask procedure to the task manager.
        self.taskMgr.add(self.spinCameraTask, "SpinCameraTask")

        # Load and transform the panda actor.
        self.pandaActor = Actor("models/panda-model",
                                {"walk": "models/panda-walk4"})
        self.pandaActor.setScale(0.005, 0.005, 0.005)
        self.pandaActor.reparentTo(self.render)
        # Loop its animation.
        self.pandaActor.loop("walk")

        # Create the four lerp intervals needed for the panda to
        # walk back and forth.
        posInterval1 = self.pandaActor.posInterval(13,
                                                   Point3(0, -10, 0),
                                                   startPos=Point3(0, 10, 0))
        posInterval2 = self.pandaActor.posInterval(13,
                                                   Point3(0, 10, 0),
                                                   startPos=Point3(0, -10, 0))
        hprInterval1 = self.pandaActor.hprInterval(3,
                                                   Point3(180, 0, 0),
                                                   startHpr=Point3(0, 0, 0))
        hprInterval2 = self.pandaActor.hprInterval(3,
                                                   Point3(0, 0, 0),
                                                   startHpr=Point3(180, 0, 0))

        # Create and play the sequence that coordinates the intervals.
        self.pandaPace = Sequence(posInterval1, hprInterval1,
                                  posInterval2, hprInterval2,
                                  name="pandaPace")
        self.pandaPace.loop()

    # Define a procedure to move the camera.
    def spinCameraTask(self, task):
        angleDegrees = task.time * 6.0
        angleRadians = angleDegrees * (pi / 180.0)
        self.camera.setPos(20 * sin(angleRadians), -20 * cos(angleRadians), 3)
        self.camera.setHpr(angleDegrees, 0, 0)
        return Task.cont


app = MyApp()
app.run()

点赞0


评论