0

I want to modify from the topLeft from midleft.

import pygame
from pygame.locals import*
from sys import exit

ALTO_Y_ANCHO_VENTANA = 960,600
COLOR_ROJO = 255,0,0

# ENEMIGO_UNO_SURF I IMPORTED FROM OTHER FOLDER.

# Enemigo_1
enemigo_1_escalado = pygame.transform.scale(enemigo_uno_surf, (80,70)).convert_alpha()
enemigo_1_rect = enemigo_1_escalado.get_rect(midbottom = (66, 530))

while True:

    lista_eventos = pygame.event.get()

    for event in lista_eventos:

        if event.type == pygame.QUIT:
            pygame.quit()
            exit()
        if event.type == pygame.MOUSEMOTION:
            mouse_pos = pygame.math.Vector2(pygame.mouse.get_pos()) # tipo Vector2

    centro_enemigo = pygame.math.Vector2(enemigo_1_rect.centerx, enemigo_1_rect.centery) # tipo Vector2

    direction = mouse_pos - centro_enemigo

    length = direction.length()
    scaling_factor = 10000 / length
    endpoint = centro_enemigo + direction * scaling_factor
    
    ###### THIS RECT THAT I WANT TO MODIFY THE PIVOT ########
    draw_line = pygame.draw.line(main_screen, 'Pink', centro_enemigo, endpoint, 2) #linea principal

That's because i want the Rect collide whith others. But not like that:RECT from the Rect of the line

Germii
  • 11
  • 4

1 Answers1

1

You must use the middle right point of the player instead of the center point as the starting point for the beam:

startpoint = pygame.math.Vector2(enemigo_1_rect.midright) 

mouse_pos = pygame.math.Vector2(pygame.mouse.get_pos())
direction = mouse_pos - startpoint
direction.normalize()

endpoint = startpoint + direction * 10000

pygame.draw.line(main_screen, 'Pink', startpoint, endpoint, 2)

Note, that the return value of pygame.draw.line is a rectangle that bounds the changed pixels, but not a rotated line or rectangle. The bounding rectangle is aligned with the axis of the screen.
If you want to detect the collision of a rectangle with a line you can use pygame.Rect.clipline, see How do I check collision between a line and a rect in pygame?.
Alternatively, you can also use mask collision detection: Make a line as a sprite with its own collision in Pygame.

Rabbid76
  • 202,892
  • 27
  • 131
  • 174