gpt4 book ai didi

python - Pygame:类型错误:无效的结束位置参数

转载 作者:行者123 更新时间:2023-12-01 06:07:28 37 4
gpt4 key购买 nike

运行此代码时:

for p in self.particles :
pos1 = p.pos
pos2 = p.pos[0]+2*p.vel[0], p.pos[1]+2*p.vel[1]
pygame.draw.line(self.screen, p.color, pos1 , pos2 , 2)

我不断收到此错误:

TypeError: Invalid end position argument`

所以我使用 print p.pos 语句来查看 p.pos 是什么,它神奇地工作了!
但唯一的问题是打印出来会使游戏以每秒 10 帧的速度运行......
那么如何修复这个错误呢?

最佳答案

docs ref: pygame.draw.line(表面、颜色、start_pos、end_pos、width=1)

所以我们需要做:

import pygame
# This makes event handling, rect, and colors simpler.
# Now you can refer to `Sprite` or `Rect()` vs `pygame.sprite.Sprite` or `pygame.Rect()`
from pygame.locals import *
from pygame import Color, Rect, Surface

def render():
p1 = (100,100)
p2 = (p1[0] + 2*v[0], p2[1] + 2*v[1] )
pygame.draw.line(screen, Color("white"), p1, p2, width=1)

注意:您的代码可以通过使用向量来简化。

(无论是来自 numpy 还是 euclid)。代码变为:其中 pos2、pos1、速度都是向量。

pos2 = pos1 + 2* velocity

关于python - Pygame:类型错误:无效的结束位置参数,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/7455106/

37 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com