我的直角碰撞不适用于 类

My rect collision isn't working with my classes

我遇到的问题是我无法让我的 Enemy class 使用 Cube 中的 Cubeplayer 矩形 class

import pygame
from random import randint

pygame.init()
pygame.font.init()
pygame.mixer.init()

Sound_Using_TP = pygame.mixer.Sound("Game imag/teleport.wav")
Sound_Get_Item_1 = pygame.mixer.Sound("Game imag/power_up_collect.wav")

HEIGHT = 600
WIDTH = 800
screen = pygame.display.set_mode((WIDTH, HEIGHT))
pygame.display.set_caption("Cube game")
clock = pygame.time.Clock()
TP_staffX = 500
TP_staffY = 500
TP_usage = 0
PhoX = 100
PhoY = -200
FPS = 60
Cube_Vel = 4
HP = 3

CubeX = 600
CubeY = 400

enemyX = 400
enemyY = 100

TP_staff = pygame.image.load("Game imag/TP staff.png")
TP_staff_rect = TP_staff.get_rect(center=(randint(100, 600), (randint(100, 700))))

TP_usage_Stat_Font = pygame.font.Font(None, 30)
TP_usage_Stat = TP_usage_Stat_Font.render(f"TP usages: {TP_usage}", False, "white")

Cube_player = pygame.image.load("Game imag/Cube.png")
Cube_player_rect = Cube_player.get_rect(center=(CubeX, CubeY))

enemy = pygame.image.load("Game imag/enemeycube.png")


class Cube:
    def __init__(self, x, y):
        self.CubeplayerX = x
        self.CubeplayerY = y
        self.Cubeimage = pygame.image.load("Game imag/Cube.png")
        self.Cuberect = self.Cubeimage.get_rect(center=(self.CubeplayerX, self.CubeplayerY))

    def draw(self, screen):
        screen.blit(self.Cubeimage, self.Cuberect)



class Enemy:
    def __init__(self, x, y):
        self.EnemyX = x
        self.EnemyY = y
        self.Enemyimage = pygame.image.load("Game imag/enemeycube.png")
        self.Enemy_rect = self.Enemyimage.get_rect(center=(self.EnemyX, self.EnemyY))

    def draw(self, screen):
        screen.blit(self.Enemyimage, self.Enemy_rect)

    def Collisions(self):
        if self.Enemy_rect(self.Cuberect):
            screen.fill((255, 0, 0))


# Timer
obstacle_timer = pygame.USEREVENT + 1
pygame.time.set_timer(obstacle_timer, 2000)
game_on = True


def draw_text(text, color, size, x, y):
    font = pygame.font.Font(None, size)
    text = font.render(text, False, color)
    text_rect = text.get_rect(center=(600, 50))
    screen.blit(text, text_rect)


while True:
    clock.tick(FPS)
    enemy = Enemy(enemyX, enemyY)
    cube = Cube(CubeX, CubeY)

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

    screen.fill((0, 0, 0))
    screen.blit(TP_staff, TP_staff_rect)
    draw_text(f"TP usages: {TP_usage}", "white", 30, 500, 500)
    cube.draw(screen)

    if event.type == obstacle_timer:
        enemyX = 400
        enemyY = 100
        enemy.draw(screen)
        pygame.display.update()
    enemy.draw(screen)
    enemy.Collisions()
    enemyY += 3
    if Cube_player_rect.colliderect(TP_staff_rect):
        TP_usage += 1
        print(TP_usage)
        TP_staff_rect = TP_staff.get_rect(center=(999, 999))
        Sound_Get_Item_1.play()
        screen.blit(TP_staff, TP_staff_rect)

    if TP_usage > 1:
        TP_usage = 1
        print(TP_usage)

    keys = pygame.key.get_pressed()

    if keys[pygame.K_a] and CubeX - Cube_Vel > 0:
        CubeX -= Cube_Vel

    if keys[pygame.K_d] and CubeX + Cube_Vel < WIDTH:
        CubeX += Cube_Vel

    if keys[pygame.K_w] and CubeY - Cube_Vel > 0:
        CubeY -= Cube_Vel

    if keys[pygame.K_s] and CubeY + Cube_Vel < HEIGHT:
        CubeY += Cube_Vel

    pressed_keys = pygame.mouse.get_pressed()
    if (pressed_keys[0]) and TP_usage > 0:
        TP_usage -= 1
        print(TP_usage)
        mx, my = pygame.mouse.get_pos()
        CubeX = mx
        CubeY = my
        Sound_Using_TP.play()
    if (pressed_keys[0]) and TP_usage < 0:
        pass

    pygame.display.update()

每次我 运行 我的代码都会给我一个 AttributeError: 'Enemy' object has no attribute 'Cuberect' 错误。我已经厌倦了使用 globe,但那没有用,而且我不知道还有其他方法可以让我的碰撞工作。

这是堆栈跟踪错误

Traceback (most recent call last):
  File "C:\Users\fayro\PycharmProjects\Cube game\Cube game.py", line 104, in <module>
    enemy.Collisions()
  File "C:\Users\fayro\PycharmProjects\Cube game\Cube game.py", line 66, in Collisions
    if self.Enemy_rect(self.Cuberect):
AttributeError: 'Enemy' object has no attribute 'Cuberect'

Process finished with exit code 1

您正在尝试访问 Enemy class 中的 Cuberect 属性。但是,此属性仅在 Cube class 中可用。您需要以某种方式将 Cuberect 属性传递给 Enemy class.

最好的方法是在 Collision 函数中添加参数。您还需要稍微更改 if 语句,因为它无论如何都会抛出错误。看看这个新的 Collisions 函数:

def Collisions(self, test_object):
    if self.Enemy_rect.colliderect(test_object.Cuberect):
        screen.fill((255, 0, 0))

您正在调用 colliderect 函数来测试两个矩形对象是否发生碰撞。此外,您现在可以访问 Cuberect 属性,只要您传递给函数的 test_object 在其 class 定义中具有该属性。

要使其正常工作,您还需要将调用 Collision 函数的行更新为:

enemy.Collisions(cube)