I'm trying to create a sprite class with a player 1 and player 2. Both are circles with a radius of 10 and player 1 is blue and player 2 is green. I can draw these circles but I can't get them to collide. Right now, only player 2 moves but will move right through the player 1 circle. Anything with .rect returns an error of "Object has no attribute .get_rect". I'm not sure if I even made the sprite class correctly. I read all the pygame documentation and all the stackoverflow questions on it but it's not making sense. This is my class:
class Player(pygame.sprite.Sprite):
def __init__(self, px, py, boost, health):
pygame.sprite.Sprite.__init__(self)
self.px = px
self.py = py
self.boost = boost
self.health = health
def draw1(self):
bpish = [2, 31, 211]
player1circle = pygame.draw.circle(screen, bpish, (self.px, self.py), 10, 0)
def draw2(self):
neongreen = [14, 208, 0]
player2circle = pygame.draw.circle(screen, neongreen, (self.px, self.py), 10, 0)
player1 = Player(width - 20, height / 2, 0, 100)
player2 = Player(20, height / 2, 500, 100)
And this is where I want them to collide:
if keys_pressed[K_a] and player2.px > 10 AND NO COLLISION: # if the "a" key is pressed and player2 is not hitting the edge of the screen or player1
if keys_pressed[K_LSHIFT] and player2.boost > 0: # and the left shift and player2 has boost available
player2.px -= 4
player2.boost -= 10
else:
player2.px -= 2
if keys_pressed[K_d] and player2.px < width - 10 AND NO COLLISION:
if keys_pressed[K_LSHIFT] and player2.boost > 0:
player2.px += 4
player2.boost -= 10
else:
player2.px += 2
if keys_pressed[K_w] and player2.py > 10:
if keys_pressed[K_LSHIFT] and player2.boost > 0 AND NO COLLISION:
player2.py -= 4
player2.boost -= 10
else:
player2.py -= 2
if keys_pressed[K_s] and player2.py < height -10 AND NO COLLISION:
if keys_pressed[K_LSHIFT] and player2.boost > 0:
player2.py += 4
player2.boost -= 10
else:
player2.py += 2
if player2.boost < 500: # if player2 boost is not full, refill it by 12 points/sec
player2.boost += 0.2 # refills player2 boost 6 points/sec
player2.draw2()
player1.draw1()
pygame.display.update()
clock.tick(60)
I've tried adding pygame.sprite.collide_rect and pygame.sprite.spritecollide and some others but to no avail.