0

I'm trying to recreate Evil Clutches from Game Maker tutorials in Python for an assignment. I was able to get everything working, but the collision with mother dragon and the baby dragon/demon etc doesn't seem to work.Like the baby dragons and what not would go through the mother dragon with no detection that it collided with one another. Any help is appreciated.

Here's the some of the code:

import pygame
import random
import os
import sys
import math

class Player(object):
    def __init__(self,x,y,width,height):
        self.x = x
        self.y = y
        self.width = width
        self.height = height
        self.vel = 8
        self.walkCount = 0
        self.floating = True
        self.health = 10
        self.rect = pygame.Rect(self.x, self.y, 140, 160)

class Projectile(object):
    def __init__(self,x,y,width,height):
        self.x = x
        self.y = y
        self.width = width
        self.height = height
        self.vel = 8
        
class BabyDragon(Projectile):
    def __init__(self,x,y,width,height):
        super().__init__(x,y,width,height)
        self.rect = (self.x, self.y, 55, 55)
        
    bbydra = pygame.image.load(os.path.join('images', 'sprite_baby_0.png'))
    def draw(self,win):
        self.hitbox = (self.x, self.y, 55, 55)
        pygame.draw.rect(win, (255,0,0), self.hitbox, 2)
        win.blit(self.bbydra, (self.x,self.y))
        
    def collide(self, rect):
        if rect[0] + rect[2] > self.hitbox[0] and rect[0] < self.hitbox[0] + self.hitbox[2]:
            if rect[1] + rect[3] > self.hitbox[1]:
                return True
        return False
    
class EvilBaby(Projectile):
    def __init__(self,x,y,width,height):
        super().__init__(x,y,width,height)
        self.rect = (self.x, self.y, 55, 55)

        
    evbby = pygame.image.load(os.path.join('images', 'sprite_evilbaby_0.png'))
    def draw(self,win):
        self.hitbox = (self.x, self.y, 55, 55)
        pygame.draw.rect(win, (255,0,0), self.hitbox, 2)
        win.blit(self.evbby, (self.x,self.y))
        
    def collide(self, rect):
        if rect[0] + rect[2] > self.hitbox[0] and rect[0] < self.hitbox[0] + self.hitbox[2]:
            if rect[1] + rect[3] > self.hitbox[1]:
                return True
        return False
    
class Demon(Projectile):
    def __init__(self,x,y,width,height):
        super().__init__(x,y,width,height)
        self.health = 5
        self.rect = pygame.Rect(self.x+10, self.y+20, 120, 120)
        
    dem = pygame.image.load(os.path.join('images', 'sprite_demon_0.png'))
    def draw(self,win):
        self.hitbox = (self.x+10, self.y+20, 120, 120)
        pygame.draw.rect(win, (255,0,0), self.hitbox, 2)
        win.blit(self.dem, (self.x,self.y))
        #pygame.draw.rect(win, (255,0,0),(self.hitbox[0], self.hitbox[1] - 20,120,10))
        #pygame.draw.rect(win, (0,128,0),(self.hitbox[0], self.hitbox[1] - 20,120 - (24 *(5 - self.health)),10))
        
    def collide(self, rect):
        if rect[0] + rect[2] > self.hitbox[0] and rect[0] < self.hitbox[0] + self.hitbox[2]:
            if rect[1] + rect[3] > self.hitbox[1]:
                return True
        return False
mother = Player(15, 200, 64, 64)

bullets = []
babydragon = BabyDragon(50,48,64,64)
evilbaby = EvilBaby(50,48,64,64)
demon = Demon(50,48,64,64)
run = True

while run:
 for bullet in bullets:
    if bullet.x < -64:
        bullets.pop(bullets.index(bullet))
    else:
        bullet.x -= 4
    
    if pygame.sprite.collide_rect(mother,babydragon):
        print('collided with baby dragon')
        score += 100
        bullets.pop(bullets.index(bullet))
    elif pygame.sprite.collide_rect(mother,evilbaby):
        print('collided w/ evil baby')
    elif pygame.sprite.collide_rect(mother,evilbaby):
        print('collided w/ demon')

1 Answers1

0

pygame.sprite.collide_rect uses the .rect attribute for the collision detection. Therefore, when you move the objects, you must update the position stored in the .rect attribute:

for bullet in bullets:
    if bullet.x < -64:
        bullets.pop(bullets.index(bullet))
    else:
        bullet.x -= 4
    bullet.rect.x = bullet.x # <---

I recommend to use pygame.sprite.Sprite and pygame.sprite.Group objects.

Rabbid76
  • 202,892
  • 27
  • 131
  • 174