Python Forum
[PyGame] Made my first Python program: Snake. Please help me improve it
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
[PyGame] Made my first Python program: Snake. Please help me improve it
#2
You have a few bad habits that i see. Ill start with those.

first of all vars is a bad name to name a module. You are overwriting the built-in vars. Whether you use it or not, its not a good habit to get into as it can cause you a lot of headaches later.

You should only ever have one statement in your entire game of pygame.display.update(). By having more than one i know your structure is fragmented. Your trying to make two game states (playing and end screen with score) incorrectly. The proper (or better) way is to use classes and inheritance. More info here.

You are not using pygame's built-in Rect. Which is a simple way to handle collision (such as collision with snake and food). Python's 3rd party libraries have methods and built-in ways to handle things. This is to create simplicity in code, so others can easily read it. But it also helps as the 3rd party libraries perfect the code to make it fast and correct. Instead of checking the distance between the two, i would just check for rect collision.

(Feb-17-2019, 05:29 PM)andrerocha1998 Wrote: I achieved this by limiting the FPS number based on the current score but it seems like a cheap way to do it.
Yes you are right. It should be set at 60 for almost any game and should not change. You can move an object by making it jump X number of pixels or use delta time. More described here.

For changing the speed based on how long the snake is you could limit the snake movement with a delay
timer = 0
delay = 50
...

    if pygame.time.get_ticks()-timer > delay:
        timer = pygame.time.get_ticks()
        snake.move(eat)
The shorter delay the faster the snake moves. You can decrease delay every time the snake gets longer.

However then you come upon another problem. A function should only ever do one thing. So snake.move() should only move, not move and eat, not move and change its size, just move.

Also you might want to set a large delay in the beginning because sometimes the snake runs off the screen at the start of the game giving no chance of success to the user.
Recommended Tutorials:
Reply


Messages In This Thread
RE: Made my first Python program: Snake. Please help me improve it - by metulburr - Feb-17-2019, 09:59 PM

Possibly Related Threads…
Thread Author Replies Views Last Post
  how to add segments to the snake body blacklight 1 2,954 Sep-13-2023, 07:33 AM
Last Post: angelabarrios
  [PyGame] Snake game: how to get an instance for my snake's body multiple times? hajebazil 2 2,209 Jan-30-2022, 04:58 AM
Last Post: hajebazil
  help with snake game blacklight 3 2,656 Jul-30-2020, 01:13 AM
Last Post: nilamo
  Snake Game - obstacle problem Samira 3 5,725 Oct-31-2019, 02:58 PM
Last Post: Samira
  Creating Snake game in Turtle Shadower 1 8,695 Feb-11-2019, 07:00 PM
Last Post: woooee
  [PyGame] Basic Snake game (using OOP) PyAlex 1 12,629 Sep-10-2018, 09:02 PM
Last Post: Mekire
  [PyGame] Snake not changing directions in Snake Game Bjdamaster 4 5,031 Aug-13-2018, 05:09 AM
Last Post: Bjdamaster
  [PyGame] Snake controls not working jakegold98 5 6,523 Dec-12-2017, 01:45 AM
Last Post: Windspar

Forum Jump:

User Panel Messages

Announcements
Announcement #1 8/1/2020
Announcement #2 8/2/2020
Announcement #3 8/6/2020