0

I am trying to scroll a world map horizontally using numpy.roll() on a pixels2d object in pygame.

This is the picture I am using, but in .bmp: image1

This is what I get: image2

I tried changing the axis of np.roll(), but it didn't seem to affect the result much.

Am I doing something horribly wrong?

Here is my code:

import pygame as pg, numpy as np

pg.init()

world_map = pg.image.load('Miller.bmp')

screen = pg.display.set_mode(world_map.get_size())

world_map = pg.surfarray.pixels2d(world_map)


while 1:
    np.roll(world_map,1)
    screen.blit(pg.surfarray.make_surface(world_map), (0,0))
    pg.display.flip()
    pg.time.wait(1000)
Stephen Rauch
  • 47,830
  • 31
  • 106
  • 135
Lupilum
  • 363
  • 2
  • 11
  • 1
    It is freezing because you are not clearing the event queue. `pygame.event.get()` or `pygame.event.pump()` Do not use `pygame.time.wait` like this. – Blake O'Hare Jan 13 '17 at 23:50
  • 1
    you can do it without `numpy` - use two `blit()` with correct `dest` and `area` in `blit(source, dest, area=None, special_flags=0)` - see doc: [blit](http://pygame.org/docs/ref/surface.html#pygame.Surface.blit) – furas Jan 13 '17 at 23:55
  • @BlakeO'Hare Thank you. Now it doesn't freeze, but it still has the weird colors and it doesn't scroll. How **are** you supposed to control framerate in pygame? – Lupilum Jan 13 '17 at 23:58
  • @furas That just makes is more complicated. You'd have to use incrementors and modulo, and it becomes much harder to understand. – Lupilum Jan 14 '17 at 00:01
  • No, it is very easy - I already made it – furas Jan 14 '17 at 00:09
  • I disagree with it being much harder. Numpy is overkill. framerate is controlled with `pygame.clock` – Blake O'Hare Jan 14 '17 at 00:10
  • and send on GitHub: [furas/python-examples/pygame/rolling-image](https://github.com/furas/python-examples/tree/master/pygame/rolling-image) – furas Jan 14 '17 at 00:21
  • @furas Thank you, I adapted this method to my code. I made my own answer. – Lupilum Jan 14 '17 at 11:05

1 Answers1

0

Here is the adapted code with the help of this repository from furas:

import pygame as pg

clock = pg.time.Clock()

pg.init()

world_map = pg.image.load('Miller.bmp')
w, h = world_map.get_size()
screen = pg.display.set_mode(world_map.get_size())

FPS = 60
offset = 0
is_running = True
while is_running:
    for event in pg.event.get():
        if event.type == pg.QUIT:
            is_running = False
        elif event.type == pg.KEYDOWN:
            if event.key == pg.K_ESCAPE:
                is_running = False

    offset += 1

    if offset >= w:
        offset = 0

    screen.blit(world_map, (0,0), (offset, 0, w, h))
    screen.blit(world_map, (w-offset,0), (0, 0, offset, h))

    pg.display.flip()
    clock.tick(FPS)

pg.guit()
Lupilum
  • 363
  • 2
  • 11
  • you need `while is_running:`, and `is_running = True` before `while` to exit `while` loop. Now you will never exit program. And use `pygame.quit()` - sometimes window doesn't close if you don't use `pygame.quit()`. – furas Jan 14 '17 at 11:20