3

I want to add a grid to my level that stays with the terrain and not the screen. The way I thought of doing it is to add all the lines that form the grid as sprites and move them with the terrain, but I can't figure out how to represent the line as an image.

I tried to do this myself, but had no success.

EDIT: Here's what I've tried

    class Grid():
    def __init__(self):
        self.grid = pygame.Surface(size)
        self.grid.set_colorkey((0,0,0))

    def draw(self):
        # DRAW TILE LINES ----------------------------------------------------------
        grid_x = 0
        grid_y = 0
        for i in range(total_level_width // TILE_SIZE):
            pygame.draw.aaline(self.grid,BLACK,[grid_x,0],[grid_x,total_level_height])
            pygame.draw.aaline(self.grid,BLACK,[0,grid_x],[total_level_width,grid_y])
            grid_x += TILE_SIZE
            grid_y += TILE_SIZE
        # tile test
        pygame.draw.rect(screen,BLACK,(49*TILE_SIZE,34*TILE_SIZE,TILE_SIZE,TILE_SIZE))
        screen.blit(self.grid,(0,0))

Creating the object:

grid = Grid()

Calling class: (in main program loop)

grid.draw()
Alexdr5398
  • 51
  • 1
  • 6

1 Answers1

1

I had a similar problem while i was trying to do a project. I used the following code to get a line onto a surface and then bliting it onto my screen. I hope this entire function might help you.

def blitBoundary(self):
    """ helper function to blit boundary on screen """

    # create a surface
    self.boundSurf=pygame.Surface((1024,768))
    self.boundSurf.set_colorkey((0,0,0))

            """
    if not self.boundary.closePoly:
        (x,y)=pygame.mouse.get_pos()
        pointList=self.boundary.pointList +[[x,y]]
    else:
        pointList=self.boundary.pointList"""

    if len(pointList)>1:
        pygame.draw.aalines(self.boundSurf, (255,255,255), 
            self.boundary.closePoly , pointList, 1)

    self.screen.blit(self.boundSurf,(0,0))

I was trying to draw a polygon. The commented out the if statement that would be most probably not useful for you. All my lines were in a polygon class object. You might want to look into pygame.draw.aalines function.

Vasif
  • 1,393
  • 10
  • 26
  • I tried this using pygame.draw.line() and all I got was a black screen. Any idea what I did wrong? I'll look into aalines and try that as well. – Alexdr5398 Apr 04 '14 at 23:51
  • Did you set_colorkey to ((0,0,0)) . This will essentially make the initial surface to transparent. – Vasif Apr 04 '14 at 23:55
  • Ah that fixed the black screen, but now the lines aren't drawing. I've posted what I tried above. – Alexdr5398 Apr 05 '14 at 00:26
  • You will need to send in a point list ... [ [x1,y1],[x2y2],... ] rather than [x1,y1] , [x2,y2] – Vasif Apr 05 '14 at 01:16
  • it says 'startpos,endpos' on the pygame sight. I used aaline, not aalines. Should I use the latter? – Alexdr5398 Apr 05 '14 at 02:13
  • you missed grid_y after LEFT_BOUNDARY. Also, I have seen you are extending the sprite class. Really not sure, if you want to do that. Anyways you are not taking any inheritance advantage here. – Vasif Apr 05 '14 at 02:17
  • Any tips? Still not drawing. The the Rect I put after the loop draws fine. – Alexdr5398 Apr 05 '14 at 02:18
  • Okay, fixed both of those. It still not drawing :(. And I will be using Sprite inheritance eventually since that's why I wanted to draw these onto a surface – Alexdr5398 Apr 05 '14 at 02:19
  • Try debugging by drawing only 4 lines and not using loop. and then try using loop. – Vasif Apr 05 '14 at 02:21
  • I drew one and still nothing. – Alexdr5398 Apr 05 '14 at 02:23
  • wait.. so you have set your colorkey to (0,0,0) that eradicates all black on your screen. and your lines are BLACK which i believe you have set to 0,0,0 . So, what you will need to do is to get a surface that is white, set its color key to white (255,255,255) and then draw black lines on it if you are specific about getting black lines. Or use the same code , draw aaline of a different colour may be gray. – Vasif Apr 05 '14 at 02:26
  • But shouldn't it have eradicated the little black square that I can get to draw too? I'll try it, though. – Alexdr5398 Apr 05 '14 at 02:31
  • I don't think so, the little black square is being drawn directly on screen. and your grid is being drawn on self.grid surface. – Vasif Apr 05 '14 at 02:34
  • Oh shit, I didn't even notice I was drawing it to the screen -.- Anyway, yea it worked. Thanks so much :D – Alexdr5398 Apr 05 '14 at 02:35