TIC-80: In Python, "spr(id, x, y)" crashes if position x or y value is a float.
A python project made with previous version crashed when run using current version with an error expecting int values in spr(id, x, y). Some objects like projectiles had advance movement with acceleration as a float or enemies with variable speed in increments of 0.5.
That project is too big to recreate here. The issue can be recreated by moving a sprite, incrementing its x or y position with a float value.
Similar error noticed for other builtin draw shape functions like pix(), circ(), rect() etc.
Version : 1.1.2837 (be42d6f) OS : Windows 10
Reproduction steps
- new python
- esc to enter code editor
- go to sprite editor, sprites section and bucket fill any color other than black on sprite number 256 or draw rectangle using rect(x,y,w,h,color).
- In the following code, project runs fine if
speed = 1and crashes ifspeed = 1.0
`x = 120 y = 24 w = 8 h = 8 id = 256 speed = 1.0
def TIC():
global x,y,speed
cls(0)
if y > 140:
y = 0
y += speed
#works only if variable y is an int
spr(id, x, y)
#pix(x, y, 2)
#rect(x, y, w, h, 4)`
`
error while running TIC Traceback (most recent call last): File “main.py”, line 28, in TIC spr(id, x, y) TypeError: expected ‘int’, got ‘float’ `
About this issue
- Original URL
- State: closed
- Created 8 months ago
- Reactions: 1
- Comments: 17 (8 by maintainers)
pocketpy integrated box2d a long time ago.
box2dmodule is stable. And it is used for my custom engine.To enable it, just add a line into cmakelists.txt,
then you can
import box2d.tic-80’s pocketpy does not enable box2d by default. Because I think tic-80 aims to restrict users, making them not too powerful.
You can override the builtin functions to simply your work if you don’t want to replace them all.
For example,
This is really impressing!
Some tips:
You can use f-string to format floats, which avoids
0.9999999...Following up on the simple movement vs smooth movement situation,
Converting float values to nearest whole number before using the spr() function results in janky movement as seen in the gif below.
Using, spr(id, round(x), round(y)) works but @blueloveTH has cautioned not to implicitly convert float to int inside the spr() function for potential future compatibility reasons.
Using his following solution of custom spr implementation solves the problem for achieving smooth/gradual movement.
Python code to comapre simple movement vs smooth movement.
I’m currently working on a making course using game engines like Godot, Defold and beginner python gamedev course using PygameZero(pygame minus the boilerplate). So crushed for time.
Just last month discovered TIC80 has python and performs way better than pygame.
It’s a pain to export to web a game made in pygame, which is where TIC80 shines with ease of export to web, desktop etc.
TIC80 has integrated sprite editor, tilemap editor, sfx editor and code editor that works on mobile phones also. This is huge for teaching programming in developing countries where kids dont have laptops but have access to mobile phones.
If TIC80 can ship with box2d enabled it has potential to become the default tool to teach python in schools and make HTML5 games instead of the boilerplate roadblocks the Pygame puts you through.
When using Lua, I can directly increment or decrement
velocitybyacceleration = 0.25per frame to apply the ramp-up effect. Or multiplyvelocitybyfriction = 0.95i.e. 5% reduction per frame instead of stopping instantly to make the floor feel slippery.I will try to implement it in python today. Meanwhile, sample Lua code to compare the two movements:
At the end the pixels positions are integers so you can still use floats but they will be converted to int at some point (but I may misunderstand what you mean). If you need to use floats (especially concerning angle rotation) you could use ttri instead.
I know that you are relying on the previous float conversion and need to change a lot of things. For example,
or,
However, I believe not to do
floattointimplicit conversion is the right implementation and this is better for future 😕This is expected. The old implementation is incorrect. Here is the stub file for the lastest tic-80.
Python allows implicit conversion from
inttofloatbut notfloattoint.