首页 > 解决方案 > Python值错误:解包的值太多?

问题描述

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

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

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)

错误信息:

第 58 行,在 x, z = rotate2d ((x, z), 弧度) ValueError: too many values to unpack (expected 2)

标签: pythonunpack

解决方案


此错误发生在多重赋值期间,您要么没有足够的对象来分配给变量,要么您要分配的对象多于变量,这里您返回三个值

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

我想您需要查看此特定行返回x * c - y * s, y * c + x, s此行需要更改为 x * c - y * s, y * c + x*s


推荐阅读