[Python] Pong

Creations

Thierry

4 years ago

I found some very useful C++ tutorials when I first came here.

So I will do my best to propose an equivalent using Python.

Here is my work on Pong

View full creation

te3game

NEW 4 years ago

As the number of variables increases, it tends to be difficult to look over the whole.
So you can make things easier by using classes.

=== class ball:
===     x = 20
===     y = 20
===     speedX = 1
===     speedY = 1
===     size = 4
=== 
>>> b = ball
>>> b.
__module__      __qualname__    speedX          speedY
size            x               y
>>> b.x
20
>>>

I think that it is also useful when persisting data in the future.

Steph

4 years ago

Unfortunately... there are some RAM issues with OOP and current port of CircuitPython on the META...

I wrote a tutorial to learn how to code a Snake game. The implementation is procedural... because I couldn't complete the implementation with OOP.

You can check the object version on my Gist.

The problem occurs between lines 81-98. There is no longer enough RAM available to go further.

If you have a solution, I'm interested :-)

Steph

NEW 4 years ago

te3game te3game

Unfortunately... there are some RAM issues with OOP and current port of CircuitPython on the META...

I wrote a tutorial to learn how to code a Snake game. The implementation is procedural... because I couldn't complete the implementation with OOP.

You can check the object version on my Gist.

The problem occurs between lines 81-98. There is no longer enough RAM available to go further.

If you have a solution, I'm interested :-)

te3game

NEW 4 years ago

Steph

4 years ago

Thank you very much for this idea to go through mpy-cross.
I'm starting Python and I'm discovering things as I go along!

(And sorry for the delay in my answer....)

Indeed, I can continue, it works (for the moment)!
I have other more urgent things to finish first, but I may soon be able to finish the OOP version of Snake...

Thank you again!

Steph

NEW 4 years ago

te3game te3game

Thank you very much for this idea to go through mpy-cross.
I'm starting Python and I'm discovering things as I go along!

(And sorry for the delay in my answer....)

Indeed, I can continue, it works (for the moment)!
I have other more urgent things to finish first, but I may soon be able to finish the OOP version of Snake...

Thank you again!

Thierry

NEW 4 years ago

Once and for all: there is no such thing as "OOP is a problem with Python". It does not clarify anything and Netflix development team could laugh about this.

We don't have enough memory in the very precise context of a SAMD21 with the current CircuitPython home made from unknown sources which ends with an application (and not a more OS like approach on every single other existing CP implementation)

All this is evolving, Soru does an incredible good work and future is bright ;)

Steph

4 years ago

Once and for all: I never said "OOP is a problem with Python"!
Thank you for stopping thinking I'm stupid...

Steph

NEW 4 years ago

Thierry Thierry

Once and for all: I never said "OOP is a problem with Python"!
Thank you for stopping thinking I'm stupid...

Thierry

NEW 4 years ago

I never said no one is stupid, except me sometimes ;)

It is just annoying to do a search on python on the forum and read "OOP" next to "issue" or "problem"

and it is not a good idea for me to do those posts in english, from work, and written form instead of a good talk.

whatever my point is, here are my apologies to whoever may have been hurt by my words.
it won't happen again.

Steph

4 years ago

I am always open to discussion... but when my words are distorted, discussion is no longer possible.

I say and maintain that:

"there are some RAM issues with OOP and current port of CircuitPython on the META..."

Let's take a simple example with which the problem occurs.
Here is a (beginning of) OOP version for the Snake game:

https://gist.github.com/iw4rr10r/8031233d7b050a45e675e199b82fdaa8

If I uncomment the instructions between lines 81 and 98, and delete the comments that expose the problem, the code is exactly 3182 characters long. Now, if I run it on the META, this is what happens:

Traceback (most recent call last):
  File "code.py", line 109, in <module>
MemoryError: memory allocation failed, allocating 10240 bytes

Now, if I comment on lines 84 and 96-98 (which do nothing extraordinary and, in any case, do not consume more RAM), this is what a gc.mem_free() displays:

7040 Bytes

Now let's take the procedural version of the Snake game, which I published in the Academy workshop:

https://gist.github.com/iw4rr10r/690f22fe8506ab7a2739dd6b40377b94

This code contains exactly 6432 characters (almost twice as many as the OOP version above). If I run it on the META, here is what a gc.mem_free() displays:

6144 Bytes

So... even less available memory than with the OOP version. And yet... the procedural code works perfectly...

Do you still think I'm wrong (?) when I say:

"there are some RAM issues with OOP and current port of CircuitPython on the META..."


Besides, you say:

It is just annoying to do a search on python on the forum and read "OOP" next to "issue" or "problem"

Why is this so embarrassing, since there is really a problem that still needs to be clarified regarding the possibility of using the OOP with CircuitPython's port to the META? Why would you want to keep this problem quiet?

And why convey the idea I'm trolling (I'm quoting your expression from Discord) about the implementation of the object layer in Python? It was never about that! I'm just noting a clear problem with the opportunity of using OOP with the current port of CirctuitPython to the META... nothing more!

I'm not trolling.

I do not denigrate the Python language.
I do not denigrate CircuitPython which, by the way, is a great project.
I do not denigrate Soru's work, which makes us happy every day.

I'm just saying that there's a problem, and I'm looking for a solution, or at least a clear explanation.

Steph

NEW 4 years ago

Thierry Thierry

I am always open to discussion... but when my words are distorted, discussion is no longer possible.

I say and maintain that:

"there are some RAM issues with OOP and current port of CircuitPython on the META..."

Let's take a simple example with which the problem occurs.
Here is a (beginning of) OOP version for the Snake game:

https://gist.github.com/iw4rr10r/8031233d7b050a45e675e199b82fdaa8

If I uncomment the instructions between lines 81 and 98, and delete the comments that expose the problem, the code is exactly 3182 characters long. Now, if I run it on the META, this is what happens:

Traceback (most recent call last):
  File "code.py", line 109, in <module>
MemoryError: memory allocation failed, allocating 10240 bytes

Now, if I comment on lines 84 and 96-98 (which do nothing extraordinary and, in any case, do not consume more RAM), this is what a gc.mem_free() displays:

7040 Bytes

Now let's take the procedural version of the Snake game, which I published in the Academy workshop:

https://gist.github.com/iw4rr10r/690f22fe8506ab7a2739dd6b40377b94

This code contains exactly 6432 characters (almost twice as many as the OOP version above). If I run it on the META, here is what a gc.mem_free() displays:

6144 Bytes

So... even less available memory than with the OOP version. And yet... the procedural code works perfectly...

Do you still think I'm wrong (?) when I say:

"there are some RAM issues with OOP and current port of CircuitPython on the META..."


Besides, you say:

It is just annoying to do a search on python on the forum and read "OOP" next to "issue" or "problem"

Why is this so embarrassing, since there is really a problem that still needs to be clarified regarding the possibility of using the OOP with CircuitPython's port to the META? Why would you want to keep this problem quiet?

And why convey the idea I'm trolling (I'm quoting your expression from Discord) about the implementation of the object layer in Python? It was never about that! I'm just noting a clear problem with the opportunity of using OOP with the current port of CirctuitPython to the META... nothing more!

I'm not trolling.

I do not denigrate the Python language.
I do not denigrate CircuitPython which, by the way, is a great project.
I do not denigrate Soru's work, which makes us happy every day.

I'm just saying that there's a problem, and I'm looking for a solution, or at least a clear explanation.

Codnpix

4 years ago

Could there be a way to have a equivalent to Python tracemalloc https://docs.python.org/3/library/tracemalloc.html in CircuitPython ?

I don't think that kind of python lib are available for CircuitPython/MicroPython but maybe we could find a way to get a similar kind of objects memory allocation output. Maybe this could help for debuging while building a game with CircuitPython...

Codnpix

NEW 4 years ago

Steph Steph

Could there be a way to have a equivalent to Python tracemalloc https://docs.python.org/3/library/tracemalloc.html in CircuitPython ?

I don't think that kind of python lib are available for CircuitPython/MicroPython but maybe we could find a way to get a similar kind of objects memory allocation output. Maybe this could help for debuging while building a game with CircuitPython...