0

Creating a replica of AimLab Tile Frenzy. Having the crosshair matching the x,y coords of the mouse and the tile having random x,y coords is making me confused on how to detect collision. Right now, even clicking the background is triggering tile.spawn and score.increase. Any help would be appreciated.

    def __init__(self):
        self.image = pygame.image.load("AimLab_TileFrenzy/Assets/Tile.png").convert_alpha()
        self.rect = self.image.get_rect()
        self.rect.topleft = (self.x, self.y)
        self.posX = random.randrange(int(WIDTH*.25), int(WIDTH*.75))
        self.posY = random.randrange(int(HEIGHT*.25), int(HEIGHT*.75))
        self.amount = 0
    def spawn(self):
        self.posX = random.randrange(int(WIDTH*.25), int(WIDTH*.75))
        self.posY = random.randrange(int(HEIGHT*.25), int(HEIGHT*.75))
        self.rect = self.image.get_rect()
        self.amount += 1
    def draw(self, surface):
        surface.blit(self.image, (self.posX,self.posY))
class Crosshair:
    def __init__(self):
        self.image = pygame.image.load("AimLab_TileFrenzy/Assets/crosshair.png").convert_alpha()
        self.rect = self.image.get_rect()
class Score:
    def __init__(self):
        self.points = 0
        self.font = pygame.font.SysFont('monospace', 30, bold = False)
    def increase(self):
        self.points += 100
    def reset(self):
        self.points = 0
    def show(self, surface):
        lbl = self.font.render("Score: " +str(self.points),1, WHITE)
        surface.blit(lbl,(22,5))
class Collision:
    def tileshot(self,tile,crosshair):
        tile = Tile()
        crosshair = Crosshair()
        if tile.rect.colliderect(crosshair.rect):
            return True
        return False
#Main Loop
def main():
    #Objects
    background = Background()
    score = Score()
    crosshair = Crosshair()
    tile = Tile()
    collision = Collision()
    #MainLoop
    while True:
        background.draw(screen)
        score.show(screen)
        tile.draw(screen)
        for event in pygame.event.get():
            if event.type == pygame.QUIT:
                sys.exit()
            if event.type == pygame.MOUSEMOTION:
                crosshair.rect.center = event.pos
            if event.type == pygame.MOUSEBUTTONDOWN:
                if collision.tileshot(tile,crosshair):
                    tile.amount -= 1
                    score.increase()
                    tile.spawn()
                continue
        #Crosshair Movement
        screen.blit(crosshair.image, crosshair.rect)
        pygame.display.update()
main()

0 Answers0