The Problem im having is i cant get my Enemy class to work with the Cubeplayer rect in the Cube 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()
Everytime I run my code it gives me a AttributeError: 'Enemy' object has no attribute 'Cuberect' error. i've tired using globe but that didn't work and i don't know a other way to get my collision work.
and this is the stack trace error
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
You're trying to access the Cuberect attribute in the Enemy class. However, this attribute is only available in the Cube class. You need to pass the Cuberect attribute somehow to the Enemy class.
The best way to do this would be an argument within the Collision function. You also need to change the if statement a little bit, because it would throw an error anyway. Take a look at this new Collisions function:
def Collisions(self, test_object):
if self.Enemy_rect.colliderect(test_object.Cuberect):
screen.fill((255, 0, 0))
You're calling the colliderect
function to test wether two rect objects collide. Also, you can now access the Cuberect attribute, as long as the test_object that you passed to the function has that attribute within its class definition.
To make it work, you will need to update the line where you call the Collision function as well to:
enemy.Collisions(cube)