mercredi 18 novembre 2015

A fast way of getting a consistent random result without setting a new seed every time

I'm learning the basics of Pygame and I'm slowly trying to get a mini RPG game built bit by bit. I've done a very basic implementation of getting tiles to display from coordinates and the camera location, however it's just block colours at the moment, so I wanted to vary the colour a bit.

I tried something to the effect of this code below, which works, but it's very slow:

new_rand = random.Random()

#x_range and y_range only draw blocks that are within the bounds of the screen
for x in x_range:
    for y in y_range:
        #get the colour from a dictionary

        new_rand.seed((x, y))
        colour = [min(255, max(0, c + new_rand.uniform(-10, 10))) for c in colour]

        #draw block

I'm positive there should be a better way to do this, just could do with a pointer in the right direction.

Also, I subtract the camera location from (x, y) to get where the blocks should be on screen. Would there be a faster way to do this? That by itself when very zoomed out is not particularly fast.




Aucun commentaire:

Enregistrer un commentaire