1

while messing with pygame.draw.polygon I found out polygon can be move by using for point in points. so I came up with a idea to move polygon shape as a player. but I can't keep the player in side the screen when it touch border

import pygame
win = pygame.display.set_mode((500, 400))
trang = (255,255,255)
p1 = pygame.Rect(50, 200, 50, 50)
a = 95
points = [ [100, 50], [180, a], [320, a], [400, 50], [250,35] ] 
x,y = 0,1


def trongluc(y,a):
    if a < 400:
        for point in points:
            point[1] += y
    
def move(key, x):
    if key[pygame.K_d]:
        x += 20
    if key[pygame.K_a]:
        x -= 20
    for point in points:
        point[0] += x
def draw():
    win.fill((0,0,0))
    pygame.draw.polygon(win,trang,points)
    pygame.display.update()

def main():
    clock = pygame.time.Clock()
    run = True
    while run :
        clock.tick(60)
        for event in pygame.event.get():
            if event.type == pygame.QUIT:
                run = False
                pygame.quit()
        #key_board_input
        key = pygame.key.get_pressed()
        #functions
        trongluc(y)
        move(key, x)
        draw()
if __name__ == "__main__":
    main()

I try to replace a < 400 to y = 1 for point in points: if point[1] > 400: y = 0 point[1] += y but the output still the same

Rabbid76
  • 202,892
  • 27
  • 131
  • 174
Need_help
  • 31
  • 4

1 Answers1

0

Calculate the bounding rectangle of the polygon:

lx, ly = zip(*points)
min_x, min_y, max_x, max_y = min(lx), min(ly), max(lx), max(ly)
boundingRect = pygame.Rect(min_x, min_y, max_x - min_x, max_y - min_y)

Copy the bounding rectangle and move it completely inside the window with pygame.Rect.clamp_ip:

shiftedRect = boundingRect.copy()
shiftedRect.clamp_ip(win.get_rect())

Move the points of polygon by the distance the rectangle was moved:

for point in points:
    point[0] += shiftedRect.x - boundingRect.x
    point[1] += shiftedRect.y - boundingRect.y

New move function:

def move(key, x):
    x = (key[pygame.K_d] - key[pygame.K_a]) * 20
    for point in points:
        point[0] += x
    
    lx, ly = zip(*points)
    min_x, min_y, max_x, max_y = min(lx), min(ly), max(lx), max(ly)
    boundingRect = pygame.Rect(min_x, min_y, max_x - min_x, max_y - min_y)
    shiftedRect = boundingRect.copy()
    shiftedRect.clamp_ip(win.get_rect())
    for point in points:
        point[0] += shiftedRect.x - boundingRect.x
        point[1] += shiftedRect.y - boundingRect.y

Complete example:

import pygame

win = pygame.display.set_mode((500, 400))
trang = (255,255,255)
p1 = pygame.Rect(50, 200, 50, 50)
a = 95
points = [ [100, 50], [180, a], [320, a], [400, 50], [250,35] ] 
x,y = 0,1
    
def move(key, x):
    x = (key[pygame.K_d] - key[pygame.K_a]) * 20
    for point in points:
        point[0] += x
    
    lx, ly = zip(*points)
    min_x, min_y, max_x, max_y = min(lx), min(ly), max(lx), max(ly)
    boundingRect = pygame.Rect(min_x, min_y, max_x - min_x, max_y - min_y)
    shiftedRect = boundingRect.copy()
    shiftedRect.clamp_ip(win.get_rect())
    for point in points:
        point[0] += shiftedRect.x - boundingRect.x
        point[1] += shiftedRect.y - boundingRect.y

def draw():
    win.fill((0,0,0))
    pygame.draw.polygon(win,trang,points)
    pygame.display.update()

def main():
    clock = pygame.time.Clock()
    run = True
    while run :
        clock.tick(60)
        for event in pygame.event.get():
            if event.type == pygame.QUIT:
                run = False
            
        key = pygame.key.get_pressed()
        move(key, x)
        draw()

    pygame.quit()

if __name__ == "__main__":
    main()
Rabbid76
  • 202,892
  • 27
  • 131
  • 174