Python pygame.Rect.colliderect()给我错误

Python pygame.Rect.colliderect()给我错误,python,pygame,Python,Pygame,我正在做一个游戏,我需要做碰撞检测。我决定使用函数pygame.Rect.colliderect()。但是,它给了我一个错误: pygame 2.0.0.dev6 (SDL 2.0.10, python 3.7.2) Hello from the pygame community. https://www.pygame.org/contribute.html Traceback (most recent call last): File "C:/Users/admin/Pychar

我正在做一个游戏,我需要做碰撞检测。我决定使用函数
pygame.Rect.colliderect()
。但是,它给了我一个错误:

pygame 2.0.0.dev6 (SDL 2.0.10, python 3.7.2)
Hello from the pygame community. https://www.pygame.org/contribute.html
Traceback (most recent call last):
  File "C:/Users/admin/PycharmProjects/ProgrammingLearn/SpaceInvaders.py", line 185, in <module>
    main()
  File "C:/Users/admin/PycharmProjects/ProgrammingLearn/SpaceInvaders.py", line 112, in main
    if pygame.Rect.colliderect(blue_enemy_rect):
TypeError: Argument must be rect style object
我看不出这里有什么不对,因为我相信这就是colliderect的用法。 我真的很快就要完成这个项目了,非常感谢您的帮助。谢谢。

是一种实例方法(请参阅)。该方法测试两个矩形是否重叠。它需要用两个矩形来调用

或者

如果pygame.Rect.colliderect(rect1,rect2):
# [...]

如果rect1.collide rect(rect2):
# [...]

这意味着您可以执行以下操作:

blue\u敌军\u rect=pygame.rect(30,35,80,70)
绿色敌人方阵=pygame.rect(180,35,60,70])
# [...]
尽管如此:
# [...]
如果绿色敌人冲突(蓝色敌人):
蓝色敌人健康=蓝色敌人健康-1

这确实有效!
# importing packages
import pygame
import random
import time
import sys

# Initializing Pygame
pygame.init()

# Setting a display width and height and then creating it
display_width = 700
display_height = 500
display_size = [display_width, display_height]
game_display = pygame.display.set_mode(display_size)
intro_display = pygame.display.set_mode(display_size)

# Setting a display caption
pygame.display.set_caption("Space Bugs")

spaceship = pygame.image.load("spaceship2.png")
blue_enemy = pygame.image.load("blue_enemy.png")
green_enemy = pygame.image.load("green_enemy.png")
orange_enemy = pygame.image.load("orange_enemy.png")
pink_enemy = pygame.image.load("pink_enemy.png")
yellow_enemy = pygame.image.load("yellow_enemy.png")

# Creating a font
pygame.font.init()
font = pygame.font.SysFont("consolas", 30)
large_font = pygame.font.SysFont("consolas", 60)
small_font = pygame.font.SysFont("consolas", 20)


# Creating a way to add text to the screen
def message(sentence, color, x, y, font_type, display):
    sentence = font_type.render(str.encode(sentence), True, color)
    display.blit(sentence, [x, y])


def main():
    global white
    global black
    global clock

    # Spaceship coordinates
    spaceship_x = 300
    spaceship_y = 375
    spaceship_x_change = 0
    blue_enemy_health = 5
    green_enemy_health = 5
    orange_enemy_health = 5
    pink_enemy_health = 5
    yellow_enemy_health = 5

    blue_enemy_rect = pygame.Rect(30, 35, 80, 70)

    # Initializing pygame
    pygame.init()

    # Creating colors
    red = (255, 0, 0)
    blue = (0, 0, 255)

    # clock stuff
    clock = pygame.time.Clock()
    time_elapsed_since_last_action = 0
    time_elapsed_since_last_action2 = 0

    # Creating a loop to keep the program running
    while True:
        # --- Event Processing and controls
        for event in pygame.event.get():
            if event.type == pygame.QUIT:
                pygame.quit()
                sys.exit()
            elif event.type == pygame.KEYDOWN:
                if event.key == pygame.K_RIGHT:
                    spaceship_x_change = 10
                elif event.key == pygame.K_LEFT:
                    spaceship_x_change = -10
            elif event.type == pygame.KEYUP:
                spaceship_x_change = 0

        spaceship_x += spaceship_x_change

        # Preventing the ship from going off the screen
        if spaceship_x > display_width - 140:
            spaceship_x -= 10
        if spaceship_x < 1:
            spaceship_x += 10

        laser_coords = [70, 209, 348, 505, 630]

        random_x_coord = random.choice(laser_coords)

        dt = clock.tick()

        time_elapsed_since_last_action += dt

        if time_elapsed_since_last_action > 500:
            pygame.draw.rect(game_display, blue, [random_x_coord, 85, 6, 305])
            time_elapsed_since_last_action = 0

        time_elapsed_since_last_action2 += dt

        # Setting Display color
        game_display.fill(black)

        message(str(blue_enemy_health), white, 65, 10, font, game_display)
        game_display.blit(blue_enemy, (20, 25))
        blue_hit_box = pygame.draw.rect(game_display, white, blue_enemy_rect, 1)
        # Bookmark
        if pygame.Rect.colliderect(blue_enemy_rect):
            blue_enemy_health = blue_enemy_health - 1

        message(str(green_enemy_health), white, 203, 10, font, game_display)
        game_display.blit(green_enemy, (160, 25))
        green_hit_box = pygame.draw.rect(game_display, white, [180, 35, 60, 70], 1)

        message(str(orange_enemy_health), white, 341, 10, font, game_display)
        game_display.blit(orange_enemy, (300, 25))
        orange_hit_box = pygame.draw.rect(game_display, white, [315, 43, 65, 70], 1)

        message(str(pink_enemy_health), white, 496, 10, font, game_display)
        game_display.blit(pink_enemy, (440, 25))
        pink_hit_box = pygame.draw.rect(game_display, white, [460, 35, 90, 70], 1)

        message(str(yellow_enemy_health), white, 623, 10, font, game_display)
        game_display.blit(yellow_enemy, (580, 25))
        yellow_hit_box = pygame.draw.rect(game_display, white, [590, 40, 85, 70], 1)

        # Creating a spaceship, lasers, and enemies
        laser = pygame.draw.rect(game_display, red, [spaceship_x + 69, 100, 4, 300])
        game_display.blit(spaceship, (spaceship_x, spaceship_y))

        health = 10

        message("Spaceship durability: " + str(health), white, 20, 480, small_font, game_display)

        # Updating Screen so changes take places
        pygame.display.flip()

        # Setting FPS
        FPS = pygame.time.Clock()
        FPS.tick(60)

# Executing the function
if __name__ == "__main__":
    main()