python-forum.org

This is the place to post any code that you want to share with the community. Only completed scripts should be posted here.Note: posts here are not necessarily endorsed by the community, and may represent amateur or even bad practices.

Honestly we shouldn't be using one letter variables in the first place here. One letter variables should really be reserved for counters in loops. Occasionally x,y coordinates; but really it shouldn't be encouraged.

Honestly we shouldn't be using one letter variables in the first place here. One letter variables should really be reserved for counters in loops. Occasionally x,y coordinates; but really it shouldn't be encouraged.

-Mek

+1

I already gave my suggestionsstill no functions/classes and the use of magic numbers

Before proceeding to your next game or considering yourself knowledgeable in the use of python/pygame/2d gaming, i would suggest to tighty up this code to where none of us say there is much we can do. Convert this to use functions. Get rid of the magic numbers. Then switch it over to use classes.

It would take less than 3 minutes to convert it to use functions/classes. The only reason i can think of your reasoning to not be converting it, is that you have yet to learn it. Which is fine. Everyone starts somewhere. I am not sure though why you would proceed in 2d gaming without knowing basic OOP? Trying to proceed in gaming without OOP normally ends in unmaintainable code. To you, the coder, and to us the helpers, both have to deal with it at that point. It really is worth the effort in learning if you are unaware or uncomfortable in using it.

If you are willing to work to improve this, how about we take it in small steps? Try replacing the coordinates of your bug and food with a pygame.Rect and give meaningful variable names to p and l. Also I'm sure you have noticed the patterns on your circles. If you draw filled circles in order of biggest to smallest, you can avoid this issue (although this is SDLs fault; not yours).

I'm afraid I don't have access to an interpreter at the moment so I can't really test your code. Basically you want to create a pygame.Rect for your bug and then everywhere you have x, or y; replace it with bug_rect.x and bug_rect.y. That will at least get us moving in the right direction.

for event in pygame.event.get(): if event.type == pygame.QUIT: pygame.quit() sys.exit()

pygame.display.update() clock.tick(60)

Make sure you understand what is going on completely before continuing.The next small thing you need to do is rename your p and l variables.After you have done that I want you to try tackling writing a function.

Write a function to create the image for your food, so that we don't have to redraw it every time.This will involve creating a surface within the function and then drawing to that instead of to your display surface.