
Code a Snake Game with Python and Pygame 🐍 – Tutorial
freeCodeCamp.org
Views: 292901
Like: 5451
In this Python / Pygame tutorial you will build a snake and apple game. This Python project for beginners is perfect for you to get exposure on how you can build an end-to-end Python project after learning the basics of Python.
✏️ Course created by Dhaval Patel from codebasics. Check out his channel:
💻 Code:
🎥 Watch the Hindi version of this course:
⭐️ Course Contents ⭐️
⌨️ (0:00:00) Intro and create surface
⌨️ (0:21:03) Converting into OOP
⌨️ (0:31:48) Moving block with timer
⌨️ (0:37:10) Draw snake and apple
⌨️ (0:49:34) Snake eats apple, find score
⌨️ (1:07:33) Game over logic
⌨️ (1:20:07) Add background music,image
⌨️ (1:31:45) Convert python to exe
🎉 Thanks to our Champion supporters:
Otis Morgan
DeezMaster
Katia Moran
—
Learn to code for free and get a developer job:
Read hundreds of articles on programming:
09.04.2023
idk why i can't run it in exe file
when i try to run it, it instant disappear
Click baitI want to see how the thumbnail thing is done but anyway your code is very good
How to make more apples?
Thank You sir 👍
why every time I eat an apple I lose?
Can someone let me know what colour theme this is? Thanks!
Is there a way to code for the score/snake size to decrease if they eat rotten food?
Great tutorial! Thanks!
with the collision, since everything is on a grid, you can just make it:
if y1 == y2:
ifx1 == x2:
(collision stuff)
Thanks free code group you are great!!!!!!
actually, you are indian?
i need the jpg of the blocks and apples cause those i find are too small
If you are experiencing the issue where the apple is flickering/ disappearing and reappearing, I believe I discovered the issue. It is most likely because there are two different pygame.display.flip() happening. The way I discovered I could fix this problem was by removing the pygame.display.flip() from the snake class. Removing it from the apple draw will make the apple disappear, but removing it from the Snake draw ends up working fine. Hope this helps anyone who is having this very same issue as I was, and have a nice python experience.
How can I use a picture of my kids as the snakes head?
Thanks for the tutorial. What snake is that? A python?
I've tried to make a few different snake games in python now and none of them work…either I can't move or it just doesn't run altogether. is there something I'm doing wrong? I've even tried to copy paste someone else's code from a website to see if I was doing it wrong and that didn't work either
From the given final code – When the snake eats the first apple, Game Overs………… y so ? I'm unable to fix this error
i cannot understand the collision logic can someone help me
my collision is not working properly
when i try to install the game and turn it into a .exe file it doesn't load. Does anyone know why?
is this tutorial for kids? Because I'm just 10
the block doesn't move
for me
when I draw the apple and run, I can see my apple on the screen but it glitches, anyone know the problem to this?
when i begin to add the surface displays, it gives me errors on every line with the word surface in it. Help plz?
How many lines of code in the game
In one tutorial i found on google, the part that generates the snake's 'food' in random locations is as follows:
if Snake_Head_Location_X == Food_Square_Location_X and Snake_Head_Location_Y== Food_Square_Location_Y:
Food_Square_Location_X = round(random.randrange(0, WIDTH – Size_Of_Snake) / 10.0) * 10.0
Food_Square_Location_Y= round(random.randrange(0, HEIGHT – Size_Of_Snake) / 10.0) * 10.0
Can someone explain to me why it was divided and multiplied by 10?
Every time I try to write this out it never allows me to use the import pygame statement it always goes gray and says it is an unused statement
Can anyone explain why we use
>> self.snake = Snake(self.surface) at 26:23 .
Pls..
gem video @codebasics
Okay tutorial but why are you not testing along the way? Multiple lines of code rearrangement are done without testing their functionality, you kinda just verbally explain it and move on!
can we do it in python idle
how do i get the square that he first uses in replacement with the snake 12:55
awesome video thanks for sharing
ete sech
actually, at 1:10:25 in the vid, it can collide with the second block. how? Ex. if you're going right, and then you start going left, you will collide with the second block, wont you?
Does the surface.fill just cover up the previous blocks or does the blitz remove it?
Is it just stacking on top of it self each time(possible memory issues?)
How do you get this directory on your desktop. was running everything well until I couldnt get the block.jpg in and realized I never downloaded the directory in the beginning because I dont know how?
i need a tutorial on how to make a simple python game using anaconda
can you say the version of python what u is using
After searching for an hour, I found an RGB background color for the game, 102, 167, 67. This color matches the background of the apple.
guys whats the difference between having having draw function called in the init() method and in the while loop? can i have it without the other?
I have a question, suppose my snake is moving in the "right" direction and I don't want it to suddenly change it's direction to left by pressing "left key" and same as in the of moving up , down , right (I dont want my snake to change its direction to "opposite" direction of the current one by pressing the opposite direction key) how can I do this? Please help
46:30
for some reason my block just will not move
Link of code pleasee
awesome
There is no link to download image yaar koi help karo kaha se download Karu 😢
Thats a bad tutorial.
What i find funny is, that he iterates so much through the main concepts and the most important ones. But falls so deep into the explaination hole, when talking about the details, as if someone could watch that and rebuild it himself after that. Its so much more important to understand the main concepts here…
Nicely done. I've noticed that the Score will kind of blink during the game.Not sure how can i prevent that. Also i would like the snake to move more smoothly. Can you help with that ? P.S. I'm doing some research right now and i would like to implement multiplayer. Like 2 snakes going for the apple 🙂