Snake Game Python Tutorial

Snake Game Python Tutorial

FavoriteLoadingAdd to favorites

Learn to code a snake game using Python and Pygame.

This is suited for intermediate level programmers with some experience but anyone is welcome to follow along.

If you are unsure about how to install/download pygame checkout this link:

Download the code:

Tutorial from Tech With Tim.
Check out Tim’s channel:

Learn to code for free and get a developer job:

Read hundreds of articles on programming:

And subscribe for new videos on technology every day:

source

By |2019-05-24T19:18:33+00:00May 24th, 2019|Python Video Tutorials|21 Comments

21 Comments

  1. Dhilan Septa Yudha May 24, 2019 at 7:18 pm - Reply

    in draw

    i = self.pos[0]

    TypeError: 'NoneType' object is not subscriptable

    how to fix?

  2. Grex böhm May 24, 2019 at 7:18 pm - Reply

    Nice Video but i Got Dome questions:

    I have some Trouble adding a scoreboard and a pause function – can someone pls Help me

  3. Shubham Kumar May 24, 2019 at 7:18 pm - Reply

    cant run it on pycharm,its shows the error in line
    " print('Score: ', len(s.body))"
    and the error is "SyntaxError: unexpected character after line continuation character

    "

  4. R S May 24, 2019 at 7:18 pm - Reply
  5. RAWR Coding May 24, 2019 at 7:18 pm - Reply

    Great Video!

  6. DEVIL HUNTER May 24, 2019 at 7:18 pm - Reply

    i have an error plz help me

  7. lamikar May 24, 2019 at 7:18 pm - Reply

    Feels like you're going wayyy ahead of how you should've instructed..

    For beginners like us, wouldn't it be better if we see how the progress of coding is done rather than explaining as if all's been thought-out already?.. :s

  8. dheya jhaveri May 24, 2019 at 7:18 pm - Reply

    for me it is saying invalid syntax for pygame pls help

  9. Meme Dream Queen May 24, 2019 at 7:18 pm - Reply

    I keep getting this error. Even when using the code linked in the description.

    Traceback (most recent call last):

    File "E:/Users/ss/PycharmProjects/ML/venv/snakeez.py", line 155, in <module>

    main()

    File "E:/Users/ss/PycharmProjects/ML/venv/snakeez.py", line 140, in main

    s = snake((255,0,0), (10,10))

    File "E:/Users/ss/PycharmProjects/ML/venv/snakeez.py", line 45, in _init_

    self.head = cube(pos)

    TypeError: cube() takes no arguments

  10. Artur S May 24, 2019 at 7:18 pm - Reply

    i create it, thank you…
    yeah. i'm not an idiot

  11. Frozen Deadbush May 24, 2019 at 7:18 pm - Reply

    pygame is in standart python library right ?

  12. Philip Brunn May 24, 2019 at 7:18 pm - Reply

    Gay ass hoe

  13. Quantiset May 24, 2019 at 7:18 pm - Reply

    Is the snake a python?

    Asking for a friend…

  14. Alex Cruz May 24, 2019 at 7:18 pm - Reply

    how do i run it ??

  15. AIDS May 24, 2019 at 7:18 pm - Reply

    Autism in a good way

  16. piezer4 May 24, 2019 at 7:18 pm - Reply

    I made a few changes to the moves to make it a more traditional Snake game if anyone wants it. I started learning Python like a week ago so there is probably a better way but it works.

    for key in keys:
    if self.dirnx == -1 and self.dirny == 0:
    if keys[pygame.K_UP]:
    self.dirnx = 0
    self.dirny = -1
    self.turns[self.head.pos[:]] = [self.dirnx, self.dirny]

    elif keys[pygame.K_DOWN]:
    self.dirnx = 0
    self.dirny = 1
    self.turns[self.head.pos[:]] = [self.dirnx, self.dirny]

    elif self.dirnx == 1 and self.dirny == 0:
    if keys[pygame.K_UP]:
    self.dirnx = 0
    self.dirny = -1
    self.turns[self.head.pos[:]] = [self.dirnx, self.dirny]

    elif keys[pygame.K_DOWN]:
    self.dirnx = 0
    self.dirny = 1
    self.turns[self.head.pos[:]] = [self.dirnx, self.dirny]

    elif self.dirnx == 0 and self.dirny == -1:
    if keys[pygame.K_LEFT]:
    self.dirnx = -1
    self.dirny = 0
    self.turns[self.head.pos[:]] = [self.dirnx, self.dirny]

    elif keys[pygame.K_RIGHT]:
    self.dirnx = 1
    self.dirny = 0
    self.turns[self.head.pos[:]] = [self.dirnx, self.dirny]

    elif self.dirnx == 0 and self.dirny == 1:
    if keys[pygame.K_LEFT]:
    self.dirnx = -1
    self.dirny = 0
    self.turns[self.head.pos[:]] = [self.dirnx, self.dirny]

    elif keys[pygame.K_RIGHT]:
    self.dirnx = 1
    self.dirny = 0
    self.turns[self.head.pos[:]] = [self.dirnx, self.dirny]

  17. packmen May 24, 2019 at 7:18 pm - Reply

    What can I do to make it show 2 or more snacks

  18. Ollie Cole May 24, 2019 at 7:18 pm - Reply

    'tuple' object has no attribute 'draw'

    Error please help

  19. Final Fantasy May 24, 2019 at 7:18 pm - Reply

    My thoughts regarding the first 3 minutes:
    – Class names start with lowercase letters.
    – method name words separated with underscore but not always. e.g. message_box() and randomSnack()
    – defined snake object like; s = snake() instead of snake = Snake(). s does not make any sense here.
    I don't care if this code works or not but this is a horrible way of coding.

  20. Noel Gomariz May 24, 2019 at 7:18 pm - Reply

    pygame.draw.rect(surface, self.color, (self.pos[0]*dis+1, self.pos[1]*dis+1, dis-2, dis-2))

    TypeError: 'int' object is not subscriptable
    I don't know why is it throwing that error, but is in the draw function and it's about the "pos" argument, could anyone help?

  21. Chinese Connection May 24, 2019 at 7:18 pm - Reply

    Not working for me – I get the canvas window, but no grid lines and no snake or snack. Error message: snake () takes no arguments, despite copying eveything exactly.

Leave A Comment

*