2

I'm working in a rogue-like and I've manage to implement a camera on the player, the camera shows just the players surroundings and is fixed in the side of the windows.

The problem is, when the player is close to the sides of the map there's a black space in the surface. Like so:

game camera

How do I make the camera 'snap' to the side and don't go any further?

To draw the map

  • I took the Camera Rect positions [topleft and bottomright];
  • Converted it to World Position;
  • Iterate over it, with a enumerator too;
  • Did any lit/visited FOG calcunations with X and Y;
  • And Blited in the screen using the enumerators 'i' and 'j'.

Here's the code:

topleft = Map.toWorld(camera.rect.topleft)
bottomright = Map.toWorld(camera.rect.bottomright)
for i, x in enumerate(xrange(topleft[0], bottomright[0])):
    for j, y in enumerate(xrange(topleft[1], bottomright[1])):
        tile = mymap.tileAt(x, y)
        object = [obj for obj in Object.OBJECTS if obj.pos == (x,y)]
        if tile:
            lit = field_of_view.lit(x, y)
            visited = field_of_view.visited(x, y)
            graphic = tile.graphic
            if lit:
                color = tile.color
            elif visited:
                color = GRAY
            else:
                color = BLACK
            renderedgraphic = myfont.render(ch, 1, graphic)
            screen.blit(renderedgraphic, Map.toScreen((i + 1, j)))
        if object:
            Draw.drawObject(object[0], Map.toScreen((i + 1, j)))

I'm bashing my head in this problem for the whole day and that's the further I got.

I saw HERE a exemple of this but I couldn't adapt the code to my game because it uses sprites.

Community
  • 1
  • 1
f.rodrigues
  • 3,499
  • 6
  • 26
  • 62
  • Try xposting this to http://gamedev.stackexchange.com/ if you're not getting any answers. – PandaConda Jun 21 '14 at 09:48
  • That being said, the solution would involve checking if moving the camera would cause the edge of the screen on any one of the 4 sides to be outside the edge of the map, and not move it if this is the case. – PandaConda Jun 21 '14 at 09:51
  • I loved pygame but it has a lot of problems, I'm sugesting you to look at PyOpenGL, PyOpenGL activate your GPU and is lightning fast compared with pygame. With PyOpenGL you would draw whole world and just position camera as you like, no partial rendering, and in 2D its simple. http://greendalecs.wordpress.com/2012/04/21/3d-programming-in-python-part-1/ I know its not real answer but in the long run...PyOpenGL is the best. –  Jun 21 '14 at 09:54

1 Answers1

1

I guess that your camera object is not tied to a player in any way.

The is no problem to stop the camera at the corners. You can have a big rect representing the world map, and call cameraRect.clamp_ip(mapRect).

The real issue, is returning the camera to have the player at the center. With the simple check above, the player would stay on the corner of the camera.

If you change the camera to follow, the player when possible, it would work. A bit of pseudocode:

Check if player is not in the center of camera
    Check direction of player movement - if moving towards the center of camera
        do nothing
Else
    Check direction of player movement
    Try to move camera
Bartlomiej Lewandowski
  • 10,771
  • 14
  • 44
  • 75