0

So i am making a game and i'm at the point where i'm writing the walls. However, i really only know how to make walls in the shape of rectangles. Every solution i looked up online either A, only shows rectangle walls or B, requires using stuff like pygame.Ploygon . Since i draw all my images in MS Paint, it would really be annoying to basically have to draw everything again. Also i mostly use if and elif statements for walls, so if possible, that's how i would like to get this done.

Below is just a test program that i'd use for this.

#START PROGRAM
import pygame
import random
pygame.init()
size = (640,520)
screen = pygame.display.set_mode(size)
pygame.display.set_caption("test")
done = False
clock = pygame.time.Clock()
tb=pygame.image.load("testblock.png")
tm=pygame.image.load("testmap.png")
x=0
y=0
while not done:
    for event in pygame.event.get():
        if event.type == pygame.QUIT:
            done = True
    keys=pygame.key.get_pressed()
    screen.blit(tm,(0,0))
    screen.blit(tb,(x,y))
    if keys[pygame.K_w]:
        y-=2
    if keys[pygame.K_a]:
        x-=2
    if keys[pygame.K_s]:
        y+=2
    if keys[pygame.K_d]:
        x+=2
    pygame.display.flip()
    clock.tick(60)
pygame.quit() 

Any help would be appreciated. I've been stuck on this for many hours now.

  • 3
    I think it's important to pick out the fundamental issue here. A picture is a rectangle, so if you have a rectangular wall you can just use the bounds of the picture as a collider. However, if you have a differently shaped wall, you can no longer use the bounds of the picture, so what do you use? You need to pass some sort of shape to your program defining where collisions happen on the image, which likely means defining a polygon for each wall shape. However, assuming you are reusing the various walls, that should not be overly complex. – dominicm00 Sep 27 '20 at 03:22
  • What would be a good example code to make this work? – Budderman18 Sep 27 '20 at 03:28
  • [QuadTrees](http://www.pygame.org/wiki/QuadTree) might be worth a look. Haven't used them with pygame personally but they've worked well enough for me before. – AdminBenni Sep 27 '20 at 03:36
  • I don't have extensive experience with pygame specifically, but there are numerous python libraries like https://pypi.org/project/Box2D/ and https://pypi.org/project/collision/ that you can use for polygon collision detection. Their documentation includes examples. – dominicm00 Sep 27 '20 at 03:38

0 Answers0