【问题标题】:unsupported operand type(s) for +: 'pygame.math.Vector2' and 'int'+ 不支持的操作数类型:“pygame.math.Vector2”和“int”
【发布时间】:2021-05-22 12:49:01
【问题描述】:

我不知道发生了什么,也找不到其他有同样问题的人。 这是我的代码:

import pygame
import math
import random
import os

SWIDTH = 800
SHEIGHT = 600
vec = pygame.math.Vector2
FRI = 0.3
ACC = 1

这是错误

File "C:\Users\USERNAME\Desktop\games\GAME.py", line 137, in update
self.loc += self.vel + 1 + self.aird

TypeError:+ 不支持的操作数类型:“pygame.math.Vector2”和“int”

【问题讨论】:

  • 问题中缺少部分代码。
  • 我猜你正在将一个整数添加到一个向量中。要么将其添加到向量的某个部分(vec.x 或 vec.y),要么将其设为向量并以这种方式添加。
  • 此代码不会产生该错误。发布完整的错误消息(准确告诉您错误在哪一行)和minimal reproducible example。

标签: python vector pygame runtime-error operands


【解决方案1】:

self.vel 是 pygame.math.Vector2。您不能向对象添加整数。

你可以总结2个向量:

self.loc += self.vel

或者您可以为向量的组件添加一个值(标量):

self.loc.x += self.vel.x + 1
self.loc.y += self.vel.y + 1

【讨论】:

    猜你喜欢
    • 2018-11-08
    • 2015-01-17
    • 2016-09-07
    • 2022-01-03
    • 2018-12-13
    • 2018-04-16
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多