Skip to content
GitLab
Explore
Sign in
Register
Primary navigation
Search or go to…
Project
M
mensactrl
Manage
Activity
Members
Labels
Plan
Issues
Issue boards
Milestones
Wiki
Code
Merge requests
Repository
Branches
Commits
Tags
Repository graph
Compare revisions
Build
Pipelines
Jobs
Pipeline schedules
Artifacts
Deploy
Releases
Container Registry
Model registry
Operate
Environments
Monitor
Incidents
Analyze
Value stream analytics
Contributor analytics
CI/CD analytics
Repository analytics
Model experiments
Help
Help
Support
GitLab documentation
Compare GitLab plans
Community forum
Contribute to GitLab
Provide feedback
Terms and privacy
Keyboard shortcuts
?
Snippets
Groups
Projects
Show more breadcrumbs
stratum0
mensactrl
Commits
a3c9213a
Verified
Commit
a3c9213a
authored
1 year ago
by
comawill
Browse files
Options
Downloads
Patches
Plain Diff
Migrate fire.py to python3
parent
6ac5ed5c
No related branches found
No related tags found
Tags containing commit
1 merge request
!2
Full migration of all python scripts and libraries to python3
Changes
1
Hide whitespace changes
Inline
Side-by-side
Showing
1 changed file
python/fire.py
+50
-41
50 additions, 41 deletions
python/fire.py
with
50 additions
and
41 deletions
python/fire.py
+
50
−
41
View file @
a3c9213a
#!/usr/bin/env python
2
#!/usr/bin/env python
3
import
client
import
random
import
math
...
...
@@ -6,54 +6,63 @@ import math
"""
Fire effect
"""
cool_offs
=
0
cool_map
=
[
0
]
*
client
.
HEIGHT
*
client
.
WIDTH
for
x
in
range
(
1
,
client
.
WIDTH
-
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
=
[
0
,
]
*
(
client
.
HEIGHT
*
client
.
WIDTH
)
for
x
in
range
(
1
,
client
.
WIDTH
-
2
):
for
y
in
range
(
1
,
client
.
HEIGHT
-
2
):
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
():
current
=
[
0
]
*
client
.
HEIGHT
*
client
.
WIDTH
def
init_fire
()
->
bytes
:
current
=
bytearray
(
client
.
HEIGHT
*
client
.
WIDTH
)
return
current
# Smooth image and cool (darken) pixels according to cool_map
def
avg_cooled
(
x
,
y
,
buf
):
res
=
0
res
+=
buf
[(
y
*
client
.
WIDTH
)
+
x
-
1
]
res
+=
buf
[(
y
*
client
.
WIDTH
)
+
x
+
1
]
res
+=
buf
[(
y
*
client
.
WIDTH
-
1
)
+
x
]
res
+=
buf
[(
y
*
client
.
WIDTH
+
1
)
+
x
]
res
+=
buf
[(
y
*
client
.
WIDTH
)
+
x
]
res
=
int
(
res
/
5.0
-
cool_map
[(
y
+
cool_offs
)
%
client
.
HEIGHT
*
client
.
WIDTH
+
x
])
if
res
<
0
:
res
=
0
return
res
# Move everything up one pixel and generate new fire at the bottom
def
move_and_smooth
(
current
,
new
):
def
avg_cooled
(
x
:
int
,
y
:
int
,
buf
:
bytes
)
->
int
:
"""
Smooth image and cool (darken) pixels according to cool_map
"""
res
=
0
res
+=
buf
[(
y
*
client
.
WIDTH
)
+
x
-
1
]
res
+=
buf
[(
y
*
client
.
WIDTH
)
+
x
+
1
]
res
+=
buf
[(
y
*
client
.
WIDTH
-
1
)
+
x
]
res
+=
buf
[(
y
*
client
.
WIDTH
+
1
)
+
x
]
res
+=
buf
[(
y
*
client
.
WIDTH
)
+
x
]
res
=
int
(
res
/
5.0
-
cool_map
[(
y
+
cool_offs
)
%
client
.
HEIGHT
*
client
.
WIDTH
+
x
])
if
res
<
0
:
res
=
0
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
for
x
in
range
(
1
,
client
.
WIDTH
-
2
):
for
y
in
range
(
1
,
client
.
HEIGHT
-
2
):
new
[
y
*
client
.
WIDTH
+
x
]
=
avg_cooled
(
x
,
y
+
1
,
current
)
for
i
in
range
(
5
,
client
.
WIDTH
-
5
,
3
):
for
x
in
range
(
1
,
client
.
WIDTH
-
2
):
for
y
in
range
(
1
,
client
.
HEIGHT
-
2
):
new
[
y
*
client
.
WIDTH
+
x
]
=
avg_cooled
(
x
,
y
+
1
,
current
)
for
i
in
range
(
5
,
client
.
WIDTH
-
5
,
3
):
bright
=
int
(
random
.
random
()
*
180
+
70
)
new
[(
client
.
HEIGHT
-
1
)
*
client
.
WIDTH
+
i
]
=
bright
new
[(
client
.
HEIGHT
-
1
)
*
client
.
WIDTH
+
i
+
1
]
=
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
+
1
]
=
bright
new
[(
client
.
HEIGHT
-
2
)
*
client
.
WIDTH
+
i
+
2
]
=
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
+
2
]
=
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
+
2
]
=
bright
# The cool map moves as well
cool_offs
=
(
cool_offs
-
1
)
%
client
.
HEIGHT
if
__name__
==
"
__main__
"
:
if
__name__
==
"
__main__
"
:
current
=
init_fire
()
new
=
[
0
]
*
client
.
HEIGHT
*
client
.
WIDTH
sending
=
[(
x
*
x
)
/
256
for
x
in
current
]
new
=
bytearray
(
client
.
HEIGHT
*
client
.
WIDTH
)
sending
=
bytes
([(
x
*
x
)
//
256
for
x
in
current
]
)
while
(
True
):
client
.
blit
(
0
,
0
,
client
.
WIDTH
,
client
.
HEIGHT
,
sending
,
rec
=
False
)
move_and_smooth
(
current
,
new
)
current
=
new
# x^2 will work better with the brightness levels we have
sending
=
[(
x
*
x
)
/
256
for
x
in
current
]
client
.
rec
()
client
.
blit
(
0
,
0
,
client
.
WIDTH
,
client
.
HEIGHT
,
sending
,
rec
=
False
)
move_and_smooth
(
current
,
new
)
current
=
new
# x^2 will work better with the brightness levels we have
sending
=
bytes
([(
x
*
x
)
//
256
for
x
in
current
]
)
client
.
rec
()
This diff is collapsed.
Click to expand it.
Preview
0%
Loading
Try again
or
attach a new file
.
Cancel
You are about to add
0
people
to the discussion. Proceed with caution.
Finish editing this message first!
Save comment
Cancel
Please
register
or
sign in
to comment