【问题标题】:Period in function definition giving invalid syntax error函数定义中的句点给出无效的语法错误
【发布时间】:2021-12-14 22:57:20
【问题描述】:

我正在测试程序生成,我需要在函数中使用 player.xplayer.z,但它在句点上给出了无效的语法错误。

  File "/path/to/file.py", line 17
    def generateheightmap(noise1, noise2, player.x, player.z):
                                                ^
SyntaxError: invalid syntax

我的代码:

from ursina import *
from ursina.prefabs.first_person_controller import FirstPersonController
from ursina.shaders import basic_lighting_shader
from perlin_noise import PerlinNoise
from PIL import Image
from random import *

app = Ursina()

noise1 = PerlinNoise(octaves=1, seed=seed())
noise2 = PerlinNoise(octaves=3, seed=seed())

player = FirstPersonController()

print(player.x,player.z)

def generateheightmap(noise1, noise2, player.x, player.z):
    for x in range(100):
        for z in range(100):
            x = x - 50 + player.x
            z = z - 50 + player.z
            noise1calc = noise1([x/10,z/10])
            noise2calc = noise2([x/10,x/10])
            y = noise1calc * noise2calc
            
def update():
    generateheightmap(noise1, noise2)

app.run()

【问题讨论】:

  • 你得到的错误的全文是什么?

标签: python function syntax-error


【解决方案1】:

player.xplayer.z 是函数参数的无效标识符。

改为接受来自调用者的player

player = FirstPersonController()

print(player.x,player.z)

# def generateheightmap(noise1, noise2, player.x, player.z):  # Replace this
def generateheightmap(noise1, noise2, player):                # with this
    for x in range(100):
        for z in range(100):
            x = x - 50 + player.x
            z = z - 50 + player.z
            noise1calc = noise1([x/10,z/10])
            noise2calc = noise2([x/10,x/10])
            y = noise1calc * noise2calc
            
def update():
    # generateheightmap(noise1, noise2)        # Replace this
    generateheightmap(noise1, noise2, player)  # with this

【讨论】:

    猜你喜欢
    • 2014-02-27
    • 1970-01-01
    • 2015-02-28
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多