Python Forum
[PyGame] Keeping the player within a polygon
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
[PyGame] Keeping the player within a polygon
#1
Hi, I'm trying to implement a polygonal boundary to stop the player from leaving the polygon. The player uses a circular collider. I've looked around online and haven't found anything that really helps. Below is a simple example of what I've got so far. If anyone can help explain an algorithm or at least provide a link to something that might help I would greatly appreciate it.

"""Circle with a polygonal boundary"""
import pygame as pg

pg.init()

display = pg.display.set_mode((1024, 768))

edge_poly = [(10, 10), (400, 10), (350, 300), (150, 400)]
player_pos = pg.Vector2(100, 50)
player_radius = 15
player_speed = .2

running = True
while running:
    # events
    for event in pg.event.get ():
        if event.type == pg.QUIT:
            running = False

    keys_down = pg.key.get_pressed()
    if keys_down[pg.K_w]:
        player_pos.y -= player_speed
    if keys_down[pg.K_s]:
        player_pos.y += player_speed
    if keys_down[pg.K_a]:
        player_pos.x -= player_speed
    if keys_down[pg.K_d]:
        player_pos.x += player_speed
    # updates

    # drawing
    display.fill((255, 255, 255))
    pg.draw.polygon(display, (0, 0, 0), edge_poly, 1)
    pg.draw.circle(display, (0, 0, 0), player_pos, player_radius)
    pg.display.update()

pg.quit()
quit()
Again thanks for taking the time to read, I appreciate any help you guys can offer.
Reply


Messages In This Thread
Keeping the player within a polygon - by Atekka - Feb-27-2021, 12:16 AM
RE: Keeping the player within a polygon - by Atekka - Feb-28-2021, 11:44 PM
RE: Keeping the player within a polygon - by Atekka - Mar-06-2021, 10:28 AM

Forum Jump:

User Panel Messages

Announcements
Announcement #1 8/1/2020
Announcement #2 8/2/2020
Announcement #3 8/6/2020