Skip to content
Snippets Groups Projects
Verified Commit a3c9213a authored by comawill's avatar comawill :computer:
Browse files

Migrate fire.py to python3

parent 6ac5ed5c
No related branches found
No related tags found
1 merge request!2Full migration of all python scripts and libraries to python3
#!/usr/bin/env python2 #!/usr/bin/env python3
import client import client
import random import random
import math import math
...@@ -6,54 +6,63 @@ import math ...@@ -6,54 +6,63 @@ import math
""" Fire effect """ """ Fire effect """
cool_offs = 0 cool_offs = 0
cool_map = [0] * client.HEIGHT * client.WIDTH cool_map = [0,] * (client.HEIGHT * client.WIDTH)
for x in range(1, client.WIDTH-2): for x in range(1, client.WIDTH - 2):
for y in range(1, client.HEIGHT-2): for y in range(1, client.HEIGHT - 2):
cool_map[y*client.WIDTH + x] = random.random() * 3 + math.sin((x + random.random()*10)/480.0*math.pi*7) * math.sin((y + random.random() * 10)/70.0*math.pi*5) * 3 cool_map[y * client.WIDTH + x] = int(random.random() * 3 + math.sin((x + random.random() * 10) / 480.0 * math.pi * 7) * math.sin((y + random.random() * 10) / 70.0 * math.pi * 5) * 3)
def init_fire(): def init_fire() -> bytes:
current = [0] * client.HEIGHT * client.WIDTH current = bytearray(client.HEIGHT * client.WIDTH)
return current return current
# Smooth image and cool (darken) pixels according to cool_map
def avg_cooled(x, y, buf): def avg_cooled(x: int, y: int, buf: bytes) -> int:
res = 0 """
res += buf[(y*client.WIDTH) + x-1] Smooth image and cool (darken) pixels according to cool_map
res += buf[(y*client.WIDTH) + x+1] """
res += buf[(y*client.WIDTH-1) + x] res = 0
res += buf[(y*client.WIDTH+1) + x] res += buf[(y * client.WIDTH) + x - 1]
res += buf[(y*client.WIDTH) + x] res += buf[(y * client.WIDTH) + x + 1]
res = int(res / 5.0 - cool_map[(y+ cool_offs)%client.HEIGHT*client.WIDTH + x]) res += buf[(y * client.WIDTH - 1) + x]
if res < 0: res += buf[(y * client.WIDTH + 1) + x]
res = 0 res += buf[(y * client.WIDTH) + x]
return res res = int(res / 5.0 - cool_map[(y + cool_offs) % client.HEIGHT * client.WIDTH + x])
if res < 0:
# Move everything up one pixel and generate new fire at the bottom res = 0
def move_and_smooth(current, new): return res
#
def move_and_smooth(current: bytes, new: bytearray) -> None:
"""
Move everything up one pixel and generate new fire at the bottom
"""
global cool_offs global cool_offs
for x in range(1, client.WIDTH-2): for x in range(1, client.WIDTH - 2):
for y in range(1, client.HEIGHT-2): for y in range(1, client.HEIGHT - 2):
new[y*client.WIDTH + x] = avg_cooled(x, y+1, current) new[y * client.WIDTH + x] = avg_cooled(x, y + 1, current)
for i in range(5, client.WIDTH-5, 3): for i in range(5, client.WIDTH - 5, 3):
bright = int(random.random() * 180 + 70) bright = int(random.random() * 180 + 70)
new[(client.HEIGHT-1) * client.WIDTH + i] = bright new[(client.HEIGHT - 1) * client.WIDTH + i] = bright
new[(client.HEIGHT-1) * client.WIDTH + i + 1] = bright new[(client.HEIGHT - 1) * client.WIDTH + i + 1] = bright
new[(client.HEIGHT-1) * client.WIDTH + i + 2] = bright new[(client.HEIGHT - 1) * client.WIDTH + i + 2] = bright
new[(client.HEIGHT-2) * client.WIDTH + i] = bright new[(client.HEIGHT - 2) * client.WIDTH + i] = bright
new[(client.HEIGHT-2) * client.WIDTH + i + 1] = bright new[(client.HEIGHT - 2) * client.WIDTH + i + 1] = bright
new[(client.HEIGHT-2) * client.WIDTH + i + 2] = bright new[(client.HEIGHT - 2) * client.WIDTH + i + 2] = bright
# The cool map moves as well # The cool map moves as well
cool_offs = (cool_offs - 1) % client.HEIGHT cool_offs = (cool_offs - 1) % client.HEIGHT
if __name__=="__main__":
if __name__ == "__main__":
current = init_fire() current = init_fire()
new = [0] * client.HEIGHT * client.WIDTH new = bytearray(client.HEIGHT * client.WIDTH)
sending = [(x*x)/256 for x in current] sending = bytes([(x * x) // 256 for x in current])
while (True): while (True):
client.blit(0, 0, client.WIDTH, client.HEIGHT, sending, rec = False) client.blit(0, 0, client.WIDTH, client.HEIGHT, sending, rec=False)
move_and_smooth(current, new) move_and_smooth(current, new)
current = new current = new
# x^2 will work better with the brightness levels we have # x^2 will work better with the brightness levels we have
sending = [(x*x)/256 for x in current] sending = bytes([(x * x) // 256 for x in current])
client.rec() client.rec()
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment