0

So, I'm making an app/game in python using pygame module, and my problem is that, I cannot find a way to check if a sprite is entirely touching another sprite or not.

What I mean is

if (sprite is touching anything else that some white rectangle):
    do some code

Here is my messy code :

main.py :

import pygame, Classes, Groups, Images, random, time
pygame.init()
pygame.font.init()

# App Variables
run_bool = False

# Fonts
ARAL_20_ITALIC = pygame.font.SysFont('Arial' , 20, italic=True)

# Colors
BG = (30, 30, 30)
WHITE = (255, 255, 255)
BLACK = (0, 0, 0)

# Window
size = (800, 600)
scr = pygame.display.set_mode(size)
pygame.display.set_caption('Simulation by Cold Fire (0.1)')

# App Loop
while True:

    time.sleep(0.01)

    # Graphics
    scr.fill(BG)

    """TERRAIN"""
    terrain_hitbox = pygame.draw.rect(scr, WHITE, (300, 100, size[0] - 350, size[1] - 150))
    pygame.draw.rect(scr, BLACK, (300, 100, size[0] - 350, size[1] - 150), width=10)

    """SUBJECTS"""
    for subject in Groups.G_Subject:
        if run_bool == True:
            subject.update__()
        scr.blit(subject.img, subject.rect)

    """GUI"""

    pygame.draw.line(scr, WHITE, (200, 0), (200, size[1]), width=3)

    scr.blit(ARAL_20_ITALIC.render('Subjects' ,False, WHITE), (30, 10))
    add_hitbox = scr.blit(Images.add_img.convert_alpha(), (30, 50))
    remove_hitbox = scr.blit(Images.remove_img.convert_alpha(), (30, 90))
    scr.blit(ARAL_20_ITALIC.render(f'Subjects: {len(Groups.G_Subject)}' , False, WHITE), (30, 130))

    if run_bool == False:
        run_hitbox = scr.blit(Images.run_img.convert_alpha(), (210, size[1] - 40))
    else:
        run_hitbox = scr.blit(Images.stop_img.convert_alpha(), (210, size[1] - 40))

    # Updating Screen
    pygame.display.flip()

    # Events
    for event in pygame.event.get():

        # Quitting App
        if event.type == pygame.QUIT:
            pygame.quit()
            quit()

        # Clicking
        if event.type == pygame.MOUSEBUTTONDOWN:
            mouse = pygame.mouse.get_pos()

            if add_hitbox.collidepoint(mouse[0], mouse[1]):
                rand_x = random.randint(terrain_hitbox[0], terrain_hitbox[0] + terrain_hitbox[2])
                rand_y = random.randint(terrain_hitbox[1], terrain_hitbox[1] + terrain_hitbox[3])
                Classes.Subject(rand_x, rand_y, Images.subject0_img.convert_alpha())

            if remove_hitbox.collidepoint(mouse[0], mouse[1]) and not 1 > len(Groups.G_Subject):
                Groups.G_Subject.remove(random.choice(Groups.G_Subject.sprites()))

            if run_hitbox.collidepoint(mouse[0], mouse[1]):
                if run_bool == True:
                    run_bool = False
                else:
                    run_bool = True

Classes.py :

import pygame, Groups, random, math

class Subject(pygame.sprite.Sprite):
    def __init__(self, x, y, img=pygame.image.load('assets/img/subject0.png').convert_alpha(pygame.display.set_mode((800, 600))), speed=5):
        super().__init__()

        self.img = img
        self.rect = self.img.get_rect()
        self.rect.x = x
        self.rect.y = y

        self.oldx = self.rect.x
        self.oldy = self.rect.y

        self.speed = speed

        Groups.G_Subject.add(self)

    def calculat_new_xy(self, old_xy, speed, angle_in_radians):
        new_x = old_xy.x + (speed * math.cos(angle_in_radians))
        new_y = old_xy.y + (speed * math.sin(angle_in_radians))
        return new_x, new_y

    def update__(self):
        self.oldx, self.oldy = self.rect.x, self.rect.y
        self.rect.x, self.rect.y = self.calculat_new_xy(self.rect, self.speed, random.randint(1, 360))

Images.py :

import pygame

add_img = pygame.transform.scale(pygame.image.load('assets/img/gui/add.png'), (90, 30))
remove_img = pygame.transform.scale(pygame.image.load('assets/img/gui/remove.png'), (90, 30))
subject0_img = pygame.image.load('assets/img/subject0.png')
run_img = pygame.transform.scale(pygame.image.load('assets/img/gui/run.png'), (90, 30))
stop_img = pygame.transform.scale(pygame.image.load('assets/img/gui/stop.png'), (90, 30))

Groups.py :

import pygame

G_Subject = pygame.sprite.Group()

I do know my code is a mess, so if you don't wanna help, it's ok ! Thx in advance :D

Rabbid76
  • 177,135
  • 25
  • 101
  • 146
Cold Fire
  • 91
  • 5
  • What exactly do you mean by *"... entirely touching ..."*? – Rabbid76 Jun 12 '21 at 17:06
  • I mean, if my sprite is touching something else that my white rectangle, some code activate. My sprite is on a white square, and if it touches something else than the square, some code run – Cold Fire Jun 12 '21 at 17:18

0 Answers0