这是我的代码的一部分无法正常工作,我尝试设置按键来使桨向上和向下移动,但我无法弄清楚我的代码出了什么问题。
playerOnePosition = (WINDOWHEIGHT - PADDLESIZE) /2
y = 0
moveY = 0
#Creates Rectangles for ball and paddles.
paddle1 = pygame.Rect(PADDLEOFFSET,(playerOnePosition,(y)), LINETHICKNESS,PADDLESIZE)
drawPaddle(paddle2)
clock = pygame.time.Clock()
while True: #main game loop
for event in pygame.event.get():
if event.type == QUIT:
pygame.quit()
sys.exit()
# mouse movement commands
if (event.type == pygame.KEYDOWN):
if(event.key == pygame.K_DOWN):
moveY = -5
if (event.key == pygame.K_UP):
moveY = 5
if (event.type == pygame.UP):
if(event.key == pygame.K_DOWN):
moveY = 0
if (event.key == pygame.K_UP):
moveY = 0
y += moveY
clock.tick(50)
错误是:
参数必须是矩形样式对象
不起作用的线路是这一行:
paddle1 = pygame.Rect(PADDLEOFFSET,(playerOnePosition, (y)), LINETHICKNESS,PADDLESIZE)
Rect
需要四个(或两个、或一个)参数:
类pygame.Rect
用于存储直角坐标的pygame对象
Rect(left, top, width, height) -> Rect
Rect((left, top), (width, height)) -> Rect
Rect(object) -> Rect
所以你有
PADDLEOFFSET
的 left
,LINETHICKNESS
的 width
,PADDLESIZE
的 height
。
但是对于
top
你有 (playerOnePosition, (y))
,它是一个元组,而不是 Rect
期望的整数。将其更改为整数即可工作。
Argument must be rect style object
是 Rect
类在参数类型错误时引发的通用错误消息。