1

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.

user3529201
  • 53
  • 1
  • 8
  • can you access any of the other functionalities of the sprite? (besides .rect?) Also, can you show the code where the error came up? So far you dont use .rect anywhere? I guess you use it in "NO COLLISION"? – Cribber Jan 26 '17 at 11:30
  • You need to define the rectangles before you can use `get_rect`. Something like `self.rect = pygame.Rect(32, 32, 16, 16)`, after which you would be able to do `player1.get_rect()`. – shad0w_wa1k3r Jan 26 '17 at 11:32
  • 1
    I think I also found a logical error: pygame.Rect is a pygame object for storing rectangular coordinates [Rect(left, top, width, height) -> Rect] - you are drawing circles for your characters.... see the problem? ;) If you want to calculate the collision for your circles based on underlying rectangles, your hitbox for each character is going to be bigger/smaller than your circle (at the "corners" of the rectangular hitbox). Also important is what Ashish Nitin Patil said - you dont create any Rects anywhere so far. So on whatever object you call .rect upon, it certainly is not a Rect... – Cribber Jan 26 '17 at 11:46
  • Cribber: That's what I wanted, for the hit box to be a rectangle (technically a square) around the circle. And I don't have .rect anywhere because I have no idea how to use it. I added "NO COLLISION" in place of where it's supposed to detect player1.rect. Ashish: Ohh I add that to the Player class right? So basically I don't put a rectangle around the circle, I just say that the player's rectangle is in the same place? – user3529201 Jan 26 '17 at 12:45
  • BTW: player should have only one `draw()` and you should set different color as argument. – furas Jan 26 '17 at 13:41
  • inside player create `self.rect = pygame.Rect(x,y,width,height)` and use `self.rect.x` or `self.rect.centerx` instead of `self.px` - and then you can check collision `player1.rect.colliderect(player2.rect)` . And draw circle with `self.rect.center` instead of `(self.px, self.py)` – furas Jan 26 '17 at 13:43
  • Thank you furas! That works :) – user3529201 Jan 26 '17 at 13:58
  • BTW: you can also check collision with circle: [pygame.sprite.collide_circle()](http://pygame.org/docs/ref/sprite.html#pygame.sprite.collide_circle) - something like this `pygame.sprite.collide_circle(player1, player2)` - and this function is looking for `player1.rect`, `player2.rect` (it can also look for `player1.radius`, `player2.radius`) – furas Jan 26 '17 at 16:07
  • That's even better, awesome! I appreciate it. So giving a radius attribute will have it collide on that radius, making it an accurate hitbox, instead of a square around the circle yeah? – user3529201 Jan 26 '17 at 18:25

0 Answers0