Python Value Error:Too many values to unpack?

Python Value Error:Too many values to unpack?

我正在尝试旋转我的相机,但它说要解压的值太多?

我试过删除变量,程序运行但相机不旋转。如果这是基本知识,我对此有点陌生,所以很抱歉。我已经查找了这个问题的其他解决方案,但我不明白如何将它们放在我的脚本的上下文中

import pygame, sys, math


def rotate2d(pos, rad):
    x, y = pos;
    s, c = math.sin(rad), math.cos(rad);
    return x * c - y * s, y * c + x, s


class Cam:
    def __init__(self, pos=(0, 0, 0), rot=(0, 0)):
        self.pos = list(pos)
        self.rot = list(rot)

    def update(self, dt, key):
        s = dt * 10

        if key[pygame.K_q]: self.pos[1] += s
        if key[pygame.K_e]: self.pos[1] -= s

        if key[pygame.K_w]: self.pos[2] += s
        if key[pygame.K_s]: self.pos[2] -= s
        if key[pygame.K_a]: self.pos[0] -= s
        if key[pygame.K_d]: self.pos[0] += s


pygame.init()
w, h = 400, 400
cx, cy = w // 2, h // 2
screen = pygame.display.set_mode((w, h))
clock = pygame.time.Clock()

verts = (-1, -1, -1), (1, -1, -1), (1, 1, -1), (-1, 1, -1), (-1, -1, 1), (1, -1, 1), (1, 1, 1), (-1, 1, 1)
edges = (0, 1), (1, 2), (2, 3), (3, 0), (4, 5), (5, 6), (6, 7), (7, 4), (0, 4), (1, 5), (2, 6), (3, 7)

cam = Cam((0, 0, -5))

radian = 0

while True:
    dt = clock.tick() / 1000

    radian += dt

    for event in pygame.event.get():
        if event.type == pygame.QUIT: pygame.quit(); sys.exit()

    screen.fill((205, 255, 255))

    for edge in edges:

        points = []
        for x, y, z in (verts[edge[0]], verts[edge[1]]):
            x -= cam.pos[0]
            y -= cam.pos[1]
            z -= cam.pos[2]

            x, z = rotate2d ((x, z), radian)

            f = 200 / z
            x, y = x * f, y * f
            points += [(cx + int(x), cy + int(y))]
        pygame.draw.line(screen, (0, 0, 0), points[0], points[1], 1)

    pygame.display.flip()

    key = pygame.key.get_pressed()
    cam.update(dt, key)

错误信息:

line 58, in x, z = rotate2d ((x, z), radian) ValueError: too many values to unpack (expected 2)

对于 rotate2d 函数,您 returning 三个值 return x * c - y * s, y * c + x, s。 要消除错误,请为 returned 值再分配一个变量,或者如果 returned 值没有用,请使用 _。 如

_,x, z = rotate2d ((x, z), radian) 

其中

**_ = x*c-y*s**
**x = y*c+x**
**z = s**

此错误发生在 multiple-assignment 期间,您没有足够的对象分配给变量,或者您要分配的对象多于变量 在这里你 return 三个值

def rotate2d(pos, rad):
    x, y = pos;
    s, c = math.sin(rad), math.cos(rad);
    return x * c - y * s, y * c + x, s

我想你需要看看这一行 return x * c - y * s, y * c + x, s 这一行需要改成 x * c - y * s, y * c + x*s