I am programming an arcade game in pygame. I want every black pixel to be a walking surface for the player. I am trying to get_at every black pixel on screen and add it to a dict. Then, I want to compare the x and y coords of the player to the dict entries. When I print the dict Pixels, it only prints y=699 and x values range from 0 to 1250. How can I make walkable black surfaces by adding every black Pixel to a dict for comparison with player coords?
Pixels = {}
for x1 in range(0, 1250):
for y2 in range(0, 700):
xy12 = disp.get_at((x1, y2))
if xy12[0] <= 3 and xy12[1] <= 3 and xy12[2] <= 3: #the <= just makes it so rgb values of 3 are close enough to black that it is acceptable...
Pixels[x1] = y2
def pglc():
global groundlim
if playerx in Pixels:
if playery == Pixels[playerx]:
groundlim = playery
print Pixels
This line is your problem
Pixels[x1] = y2
In the inner loop you are overwriting the value in each step, so the last value you see is 699
(but it had all the values from 0 to 698 before when the condition was met). You could make two dimensions:
Pixels[x1] = list()
And then in the inner loop:
Pixels[x1].append(y2)
In total:
Pixels = {}
for x1 in range(0, 1250):
Pixels[x1] = list()
for y2 in range(0, 700):
xy12 = disp.get_at((x1, y2))
if xy12[0] <= 3 and xy12[1] <= 3 and xy12[2] <= 3:
Pixels[x1].append(y2)
Or - if you're into dict comprehensions (plus it uses all(...)
):
Pixels = {x1:
[y2
for y2 in range(0, 700)
for xy12 in [disp.get_at((x1, y2))]
if (all(cond <= 3 for cond in xy12[0:2]))]
for x1 in range(0, 1250)}
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With