【问题标题】:"Rect argument is invalid"“矩形参数无效”
【发布时间】:2020-08-13 06:17:39
【问题描述】:

我一直在尝试编写游戏代码,但它不断给我一个"rect argument is invalid" 错误。如果有人能告诉我我在这里做错了什么,那就太好了。我相信它与代码中的列表和元组有关,但我已经彻底审查过它,它只是无法正常运行。代码只是不断向我显示相同的错误消息。

我的代码:

import pygame
import sys
import random

pygame.init()

Width = 800
Height = 600

Red = (255, 0, 0)
Blue = (0, 0, 255)
White = (255, 255, 255)
backgroundColor = (0, 0, 0)

playerSize = 50
playerPos = [100, 300]

columnSize = [50, 150]
columnPos1 = [700, random.randint(0,random.randint(0,100))]
columnPos2 = [700, random.randint(0,random.randint(400,500))]

screen = pygame.display.set_mode((Width, Height))

game_over = False

clock = pygame.time.Clock()

while not game_over:

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

            x = playerPos[0]
            y = playerPos[1]

            if event.key == pygame.K_LEFT:
                x -= 20
            elif event.key == pygame.K_RIGHT:
                x += 20
            elif event.key == pygame.K_UP:
                y -= 20
            elif event.key == pygame.K_DOWN:
                y += 20
            
            playerPos = [x,y]

    screen.fill(backgroundColor)
    if columnPos1[0] >= 0 and columnPos1[0] < Width and columnPos2[0] >= 0 and columnPos2[0] < Width:
        columnPos1[0] -= 10
        columnPos2[0] -= 10
    else:
        columnPos1[0] = 700
        columnPos2[0] = 700

        columnPos1[1] = [700, random.randint(0,random.randint(0,100))]
        columnPos2[1] = [700, random.randint(0,random.randint(400,500))]

    pygame.draw.rect(screen, Red, (playerPos[0], playerPos[1], playerSize, playerSize))
    pygame.draw.rect(screen, Red, (columnPos1[0], columnPos1[1], columnSize[0], columnSize[1])) #this is where rect is invalid
    pygame.draw.rect(screen, Red, (columnPos2[0], columnPos2[1], columnSize[0], columnSize[1])) #here as well

    clock.tick(30)

    pygame.display.update()

添加后错误开始发生

columnPos1[1] = [700, random.randint(0,random.randint(0,100))]
columnPos2[1] = [700, random.randint(0,random.randint(400,500))]

【问题讨论】:

    标签: python error-handling pygame


    【解决方案1】:

    columnSize[50, 150],所以你的 rect 参数看起来像

    (columnPos1[0], columnPos1[1], [50, 150], [50, 150])
    

    你可能是说

    (columnPos1[0], columnPos1[1], columnSize[0], columnSize[1])
    

    【讨论】:

      【解决方案2】:

      好的,我解决了这个问题:

      而不是这个:

      columnPos1[1] = [700, random.randint(0,random.randint(0,100))]
      columnPos2[1] = [700, random.randint(0,random.randint(400,500))]
      

      我应该这样做的:

      columnPos1[1] = random.randint(0,random.randint(0,100))
      columnPos2[1] = random.randint(0,random.randint(400,500))
      

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 2020-04-16
        • 1970-01-01
        • 1970-01-01
        • 2011-11-23
        • 1970-01-01
        • 1970-01-01
        • 2016-07-07
        • 2012-04-24
        相关资源
        最近更新 更多