简体   繁体   English

如何在 ursina 引擎中对角色施加重力?

[英]How do I put gravity on a character in ursina engine?

(Python Ursina Engine) (Python Ursina 引擎)

How can I add gravity on ursina engine??如何在 ursina 引擎上添加重力? Here is the code that I'm using: https://github.com/pokepetter/ursina/blob/master/samples/terraria_clone.py这是我正在使用的代码: https://github.com/pokepetter/ursina/blob/master/samples/terraria_clone.py

from ursina import *
from math import floor


app = Ursina()

size = 32
plane = Entity(model='quad', color=color.azure, origin=(-.5,-.5), z=10, collider='box', scale=size) # create an invisible plane for the mouse to collide with
grid = [[Entity(model='quad', position=(x,y), texture='white_cube', enabled=False) for y in range(size)] for x in range(size)] # make 2d array of entities
player = Entity(model='quad', color=color.orange, position=(16,16,-.1))
cursor = Entity(model=Quad(mode='line'), color=color.lime)

def update():
    if mouse.hovered_entity == plane:
        # round the cursor position
        cursor.position = mouse.world_point
        cursor.x = round(cursor.x, 0)
        cursor.y = round(cursor.y, 0)

    # check the grid if the player can move there
    if held_keys['a'] and not grid[int(player.x)][int(player.y)].enabled:
        player.x -= 5 * time.dt
    if held_keys['d'] and not grid[int(player.x)+1][int(player.y)].enabled:
        player.x += 5 * time.dt



def input(key):
    if key == 'left mouse down':
        grid[int(cursor.x)][int(cursor.y)].enabled = True
    if key == 'right mouse down':
        grid[int(cursor.x)][int(cursor.y)].enabled = False


camera.orthographic = True
camera.fov = 10
camera.position = (16,18)

# enable all tiles lower than 16 to make a ground
for column in grid:
    for e in column:
        if e.y <= 15:
            e.enabled = True

app.run()

You can use the built-in 2D platformer controller:您可以使用内置的 2D 平台控制器:

from ursina.prefabs.platformer_controller_2d import PlatformerController2d

player = PlatformerController2d(position=(16, 20, -.1), scale_y=1)

def update():
   # other keys...
   if held_keys['space']:
        player.y += 1

This gives you rudimentary jumping control but a better way to implement that would be to define the level as a mesh entity and set it as the player's traverse_target , as described in the Platformer Tutorial .这为您提供了基本的跳跃控制,但更好的实现方式是将关卡定义为网格实体并将其设置为玩家的traverse_target ,如平台游戏教程中所述

If you are making a 3d game, just put:如果您正在制作 3d 游戏,只需输入:

from ursina.prefabs.first_person_controller import FirstPersonController

Player = FirstPersonController()

into your code.进入你的代码。

Ursina already has the FirstPersonController class. Ursina 已经拥有FirstPersonController class。 It's simple, all you need is to import 2 things,很简单,你只需要导入 2 个东西,

from ursina import * from ursina.prefabs.first_person_controller importFirstPersonController If you want to add gravity and make the player of your own, the code below is a Maze game with minecraft blocks: from ursina import * from ursina.prefabs.first_person_controller importFirstPersonController 如果你想添加重力并制作你自己的玩家,下面的代码是一个带有 minecraft 块的迷宫游戏

from ursina import *
from ursina.prefabs.first_person_controller import FirstPersonController
from ursina.prefabs.sky import Sky
from create import createMaze
app=Ursina()
wall_texture=load_texture("texture/brick.png")
start_texture=load_texture("texture/redstoneblock.jpg")
end_texture=load_texture("texture/greendiamondblock.jpg")
ground_texture=load_texture("texture/plank.jpg")
class Wall(Button):
    def __init__(self,position):
        super().__init__(
            parent=scene,
            model="cube",
            texture=wall_texture,
            color=color.white,
            position=position,
            origin_y=0.5
        )
class Start(Button):
    def __init__(self,position):
        super().__init__(
            parent=scene,
            model="cube",
            texture=start_texture,
            color=color.white,
            position=position,
            origin_y=0.5
        )
class End(Button):
    def __init__(self,position):
        super().__init__(
            parent=scene,
            model="cube",
            texture=end_texture,
            color=color.white,
            position=position,
            origin_y=0.5
        )
class Ground(Button):
    def __init__(self,position):
        super().__init__(
            parent=scene,
            model="cube",
            texture=ground_texture,
            color=color.white,
            position=position,
            origin_y=0.5
        )
class Player(FirstPersonController):
    def __init__(self):
        global startPosition
        super().__init__()
        camera.fov=140
        self.position=startPosition
        self.gravity=0.01
        self.speed=6
        self.mouse_sensitivity=Vec2(160,160)
        def jump(self):
        pass
    maze=createMaze(15,15)
    startPosition=None
    endPosition=None
    banPostions=[]
    for y in range(1,4):
        for x,row in enumerate(maze):
            for z,value in enumerate(row):
                if str(value)=="s":
                    Start((x*2,0,z*2))
                    Start((x*2,0,z*2+1))
                    Start((x*2+1,0,z*2))
                    Start((x*2+1,0,z*2+1))
                    startPosition=(x*2,3,z*2)
                    banPostions.append([x*2,z*2])
                    banPostions.append([x*2,z*2+1])
                    banPostions.append([x*2+1,z*2])
                    banPostions.append([x*2+1,z*2+1])
                elif str(value)=="e":
                    End((x*2,0,z*2))
                    End((x*2,0,z*2+1))
                    End((x*2+1,0,z*2))
                    End((x*2+1,0,z*2+1))
                    endPosition=(x*2,3,z*2)
                    banPostions.append([x*2,z*2])
                    banPostions.append([x*2,z*2+1])
                    banPostions.append([x*2+1,z*2])
                    banPostions.append([x*2+1,z*2+1])
                elif str(value)=="0":
                    Wall((x*2,y,z*2))
                    Wall((x*2,y,z*2+1))
                    Wall((x*2+1,y,z*2))
                    Wall((x*2+1,y,z*2+1))
                    y2=0
        for x2 in range(x*2+1):
            for z2 in range(z*2+1):
                if not ([x2,z2] in banPostions):
                    Ground((x2,y2,z2))
player=Player()
sky=Sky()
app.run()

The code below might not work, as it just an example下面的代码可能不起作用,因为它只是一个示例

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM