In general, to arrange a memory leak in python, we must also try, but I did it!

I wrote a snake, and everything worked out pretty well until the moment when I started writing a function that displays a snake. The function copes with its duties, however, a side result of its work is a list containing empty nodes of the scene, inflated to monstrous dimensions.

The code to look here , special attention should be paid to the draw_snake function. With each call, it removes all existing nodes of the scene and draws new ones (which is no longer the most efficient algorithm, since in most cases new nodes appear in place of old ones).

Help fix govnokod.

    2 answers 2

    1. What is "dot" (a hint is a name that says nothing) and what does it do in the Snale class?

    2. Shifting each part of a snake can be useful if each part of it can be something unique (roughly - each part of a different color). In your version, at this stage, you can use optimized movement - if we meet dot, then we do not change the coordinates of the snake's body, simply add dot to the snake. If dot is not found, simply transfer the end of the snake to the beginning.

    3. "get_score" I, too, would have taken out of the bounds of the snake - what does the bill have to do with a snake?

    4. I do not know a panda, with a python is also poorly familiar, but the question is - in panda, aren’t all calculations / rendering done in one method? And if the calculations will be significant?

    5. In addition, as in this case, handle the following situation:

    6. there are two snakes
    7. the speed of one is twice the speed of the other, i.e. one snake at the same time must pass two times more than the other

    Will you use your self.period for each snake? And if a bunch of snakes? Migrate self.period to snakes? Or will you just move the same snakes at different distances? Then read on:

    • One of the snakes (one that must move at a time at a distance more than twice, for example, two cages) is located at the point [X, Y]
    • Dot is at [X-1, Y]
    • The snake moves to the point [X-2, Y]
    • Logically, because the snake should not "jump", then during the passage of [X-1, Y] the next dot may appear in [X-2, Y], so the snake will have to eat it too - and all this in one move (we only have one period)

    And how will you handle it? Will you still move the snake in jerks? And generate dot already after all the movements of the snake? And what kind of twitching will it be?

    Why draw and delete objects if you can simply change the coordinates of existing ones?

     obj.setPos( Point3( pos.getX( ), depth, pos.getY( ) ) ) 

    Those. pass through the body of the snake, the bark has similar crust, if the body is finished and there are still briks - then remove them already, or if on the contrary - there is a body, but there are no briks - add them.

    It is also not quite optimal, you can come up with a better idea, but don’t waste your money on extra load / delete.

    This is not a memory leak; a memory leak is when memory is allotted, but there are no references to this variable, so obviously the memory cannot be released back.
    I do not know on which fv it is done, but in theory you have a fixed number of blocks that may belong to a snake, food or field - they do not need to be redrawn at all, just change the color, at least apply a layer of snake (again a fixed and predictable amount points) on the background.

    and what prevents self.bricks = [] from doing after deleting the old scene?

    • Panda3D framework In principle, I have already done so, but the feeling that this govnokod was not lost ... - Montreal
    • @Fike, according to Wikipedia here is a leak: if self.bricks: for brick in self.bricks: brick.removeNode () I don’t know what removeNode does, but the most important thing is that the brick from self.bricks is not deleted, although it is no longer needed. As a result, unnecessary objects accumulate, gradually eating up all the memory. - BOPOH