Тёмный
No video :(

Coding A Snake Game in Python 

NeuralNine
Подписаться 357 тыс.
Просмотров 51 тыс.
50% 1

Today we learn how to code a simple snake game in Python.
Get Tabnine for FREE here: bit.ly/3cO76nS
◾◾◾◾◾◾◾◾◾◾◾◾◾◾◾◾◾
📚 Programming Books & Merch 📚
💻 The Algorithm Bible Book: www.neuralnine.com/books/
🐍 The Python Bible Book: www.neuralnine.com/books/
👕 Programming Merch: www.neuralnine.com/shop
🌐 Social Media & Contact 🌐
📱 Website: www.neuralnine.com/
📷 Instagram: / neuralnine
🐦 Twitter: / neuralnine
🤵 LinkedIn: / neuralnine
📁 GitHub: github.com/NeuralNine
🎵 Outro Music From: www.bensound.com/

Опубликовано:

 

1 авг 2024

Поделиться:

Ссылка:

Скачать:

Готовим ссылку...

Добавить в:

Мой плейлист
Посмотреть позже
Комментарии : 142   
@SparePlayss
@SparePlayss 3 года назад
me after programming snake game in python: "I used the snake to make the snake"
@CaptMirage
@CaptMirage 2 года назад
yes
@maxtv333thesuperstar3
@maxtv333thesuperstar3 2 года назад
@Kirito Kun the program python and the game snake is the same thing
@shakesrear7850
@shakesrear7850 2 года назад
That was so clear. I feel like I learned a lot.
@arturog.2211
@arturog.2211 3 года назад
I'm not kidding when I say I was looking for this yesterday and now you post it. What a coincidence and thank you! 😅
@ChrisLuigiTails
@ChrisLuigiTails 3 года назад
This is almost always the case with me! I think of something, NeuralNine uploads it. What do you want me to think about next?
@NeuralNine
@NeuralNine 3 года назад
That's my super power ^^
@Shanksdan
@Shanksdan 3 года назад
Thank you for your content, man. Really appreciate it!
@deeperblue77
@deeperblue77 3 года назад
Another great video...super excited!
@HarrisonG-vc8dc
@HarrisonG-vc8dc 3 месяца назад
Helped my skill in digital solutions, keep up the amazing work my favorite sigma
@buoccham6813
@buoccham6813 3 года назад
I'm from Vietnam, thank you so much about the clips that you share and I study so much from them. In the present, I want to study Qt library to design Gui for python. Could you create a series about it?
@shasherazi
@shasherazi 3 года назад
jokes on you tabnine is already my favorite vscode extension
@SaltShaker-lf5jq
@SaltShaker-lf5jq 3 года назад
BEST python Snake tutorial on youtube
@bandananandi6665
@bandananandi6665 2 года назад
Really love ur vids ... U r so descriptive
@haokaixuan
@haokaixuan 3 года назад
i was waiting for a pygame project lol :)
@NeuralNine
@NeuralNine 3 года назад
Here it is :D
@overpower3148
@overpower3148 3 года назад
Thanks Brother. Take Love
@carloscruzbarragan3793
@carloscruzbarragan3793 3 года назад
I have several problems in the program, but the first one says "Module 'pygame' has no 'init' member. I don't know how to fix it
@cristianortix8096
@cristianortix8096 3 года назад
thank you, you are great
@markphethean3831
@markphethean3831 3 года назад
Great video!
@b07x
@b07x 3 года назад
Just added Tabnine to my VS Code Right now
@NeuralNine
@NeuralNine 3 года назад
great! :D
@ChrisLuigiTails
@ChrisLuigiTails 3 года назад
Same! First time I find a sponsor useful.
@hemantjaiman8083
@hemantjaiman8083 3 года назад
I used to skip sponser part but if you are recommending it, i will add tabnine. and thank you for amazing videos.
@NeuralNine
@NeuralNine 3 года назад
I continued to use it after this video and I still enjoy it ^^
@hemantjaiman8083
@hemantjaiman8083 3 года назад
@@NeuralNine ❤️ thank you for your hard work
@amaljoshuaa.p.4096
@amaljoshuaa.p.4096 3 года назад
I always like your videos before watching the video because I know your videos are always awesome.
@NeuralNine
@NeuralNine 3 года назад
appreciate it :D
@amaljoshuaa.p.4096
@amaljoshuaa.p.4096 3 года назад
@@NeuralNine please heart my comment please
@vscapetv2937
@vscapetv2937 2 года назад
Thanks a lot mate for this tutorial. Very simple and at the same time, intuitive. Perfect for a beginner like me. Wow, wow.
@matildebrandao6377
@matildebrandao6377 Год назад
were you able to do it? i need some help, can u help me?
@azankhan8058
@azankhan8058 2 года назад
Thank you soooo much
@niceteg
@niceteg 3 года назад
Thanks! Can you tell what software do you use to record such video with screen and camera?
@khaledmechatronics4395
@khaledmechatronics4395 3 года назад
He is using obs studio
@robinferizi9073
@robinferizi9073 3 года назад
Cool, you're getting sponsors now
@s.aravindh6227
@s.aravindh6227 3 года назад
Nice video bro 👍
@akramelomrani8728
@akramelomrani8728 3 года назад
great tutorial , and pygame is still good apparently
@tw4vesx332
@tw4vesx332 Год назад
thank you sm
@matildebrandao6377
@matildebrandao6377 Год назад
hello, NeuralNine did not show the code at the end, i have a few errors can someone help me with that? i really need to do this game but i cant run it, the screen goes all black
@SprintKey
@SprintKey 3 года назад
i have a problem... is this code right??? text = score_font.render("Score: " = str(score), True, orange) because vscode is telling me it's wrong.... please help EDIT: I just removed ' ' off of the code
@shivenchakraborty6120
@shivenchakraborty6120 2 года назад
Thank you for this amazing video. Just a quick question... I wrote this in my code but idk why it is showing the following thing: message_font = pygame.font.SysFont('calibri', 30) score_font = pygame.font.SysFont('calibri', 25) font = int(Font(fontpath, size)) TypeError: int() argument must be a string, a bytes-like object or a number, not 'pygame.font.Font' Can somebody help me regarding this
@theparrot271
@theparrot271 2 года назад
You are trying to convert an object into an integer. pygame.font.SysFont() returns a "Font" object, which isn't a number and can't be converted into an integer. The font size is already an integer, so not sure why you would want to convert it again?
@yassermachkour4291
@yassermachkour4291 3 года назад
coulc you please post the code i am having trouble and need to compare to see what i did wrong
@ayush.kumar.13907
@ayush.kumar.13907 3 года назад
this is like the Hello World of Game Programming
@rutujathombre4009
@rutujathombre4009 2 года назад
hey bro i am getting error saying that name game_over is not defined pls help me i dont know to solve it
@shambhavitiwari508
@shambhavitiwari508 3 года назад
My game window is flashing for a second and then disappears ... what should I do now?
@ABCDEF-ek7ni
@ABCDEF-ek7ni 5 месяцев назад
having this same trouble
@elevendarter112
@elevendarter112 3 года назад
A simple snake game doesn't mean a simple code....lol. I'm a beginner in python and I thought I would try something simple. Don't get me wrong , I enjoyed this video, I just had to reclassify myself as a pre-beginner.
@__3093
@__3093 3 года назад
let's goo first sponsor, I'm getting it
@NeuralNine
@NeuralNine 3 года назад
great choice ^^
@bakariwalton6448
@bakariwalton6448 2 года назад
help needed the program runs however, the snake and the food dosent appear on the screen copied code correclty and also im not getting any errors.
@matildebrandao6377
@matildebrandao6377 Год назад
were you able to solve it?
@crckrbrrs
@crckrbrrs 3 года назад
i got this video as an ad on this video
@DrDelulu
@DrDelulu 2 года назад
It won't let me import well install the pygame or anything
@LittleKDawg_
@LittleKDawg_ 2 года назад
When I crash into myself the game just ends no game over screen. what am I doing wrong
@rmokshagna1216
@rmokshagna1216 2 года назад
It's atomatically getti closed and showing process finished with exit code 0, can any one help^^
@vantunglee6469
@vantunglee6469 2 года назад
amazing guy thank you so much, im from vietnam
@latanyaexe8606
@latanyaexe8606 2 года назад
I copied exactly what was in the video and my code is literally not running at all ... Everytime click run nothing happens :(
@ABCDEF-ek7ni
@ABCDEF-ek7ni 5 месяцев назад
the window for output is not running
@strngsnthngs
@strngsnthngs 3 года назад
When I run the code it wont open correctly. It just shuts down instantly...
@tinytinsley
@tinytinsley 3 месяца назад
Same i cant fix it for the life of me. Please blow this up so he sees it.
@ernesto6535
@ernesto6535 3 года назад
Every time I try to run my code, it keeps shutting down immediately. I've tried using input("text") but it doesn't work. Can anyone help? P.S I'm using Python 3.9.6
@Abk312
@Abk312 2 года назад
Which key to start the game🥺
@ramanchawla1290
@ramanchawla1290 3 года назад
Really appreciate your videos... yesterday i decided to try programming a sudoku solver and saw that you have posted its video a couple of days back..How do you read my mind? Regarding the snake game, i just have one issue with my implementation, the Score is getting overlapped over previous score, so the numbers become unreadable..Any suggestion on what could be the problem? I tried rendering the previous score in Black so as to make it vanish in the background...but that was in vain, still getting numbers overlapped
@snxphy
@snxphy 2 года назад
he isnt a good explainer and goes way too quick and the fact he doesnt show the code at the end is incredibly annoying to people that need to double back on what has been put, i have an error but obviously cant compare the code for an explanation why it wont work because he doesnt answer comments unless its passive aggressive.
@fantom1919
@fantom1919 3 года назад
What Font Are You Using in Your IDE?
@NeuralNine
@NeuralNine 3 года назад
JetBrains Mono
@khaledalhashimi1234
@khaledalhashimi1234 2 года назад
File "c:\Users\Hp\Desktop\snek.py", line 56, in while not game_over:
@thedapperbean
@thedapperbean 2 года назад
when i touched the target it didnt work, every thing else was fine though
@satendrasingh617
@satendrasingh617 3 года назад
Can you tell which software are you using
@LittleKDawg_
@LittleKDawg_ 2 года назад
Pycharm community
@playstationworld7872
@playstationworld7872 2 года назад
When I run it closes suddenly. can anyone help
@aymancassim8944
@aymancassim8944 3 года назад
Can someone help me install Tabnine in Vim plss 😩.
@tanishajaiswal4784
@tanishajaiswal4784 Год назад
This code is not working the game display is closing automatically in milliseconds please help
@IzUrBoiKK
@IzUrBoiKK 3 года назад
Day 2 : pls make a vid on neural networks 🙃🙂
@brightsideethiopia1276
@brightsideethiopia1276 3 года назад
That’s AI
@shitheadjohnson2797
@shitheadjohnson2797 Год назад
snake and python go together! did u know there is also a raster pattern in the form of a snake!
@pokegamerspoint6577
@pokegamerspoint6577 3 года назад
100k soon
@NeuralNine
@NeuralNine 3 года назад
hope so ^^
@omosh7895
@omosh7895 2 года назад
my clock.time variable states that it's not defined by pylance, any help
@matildebrandao6377
@matildebrandao6377 Год назад
im having the same issue, were u able to fix it?
@oMqngo
@oMqngo 2 года назад
So i coded everything like on the tutorial but somehow it first opens a python window and then it opens my snake game and then freezes straight after. How do i fix this? here are the error msges: File "f:\Coding\Python Projects.py\Snake_Game", line 127, in run_game() File "f:\Coding\Python Projects.py\Snake_Game", line 54, in run_game while not game_over: UnboundLocalError: local variable 'game_over' referenced before assignment
@theparrot271
@theparrot271 2 года назад
Sounds like you haven't declared game_over yet. in the beginning of the run_game function, did you declare it as False initially?
@matildebrandao6377
@matildebrandao6377 Год назад
@@theparrot271 hey the parrot, im having trouble on coding this, can you help me?
@varsharupam
@varsharupam 3 года назад
bro give the link of codre in description
@NeuralNine
@NeuralNine 3 года назад
Watch my first Q&A. There I explain why I don't publish any code
@pranav6957
@pranav6957 2 года назад
is it okay to code this in vs code ?
@tinytinsley
@tinytinsley 3 месяца назад
Yes
@luxo1035
@luxo1035 3 года назад
Hey man tons of bots are commenting under your videos. Why is that happening?
@wko_
@wko_ 3 года назад
Hey everyone, i have a question, when i run the code, open the window of the game but then close... idk what is happening, can someone help me ?
@felipemartins2439
@felipemartins2439 3 года назад
yeah mine is doing the same....
@spyros92
@spyros92 3 года назад
Me too
@strngsnthngs
@strngsnthngs 3 года назад
Yeah same, did anyone find a solution?
@spyros92
@spyros92 3 года назад
@@strngsnthngs you guys need to make a while loop i had the same problem
@strngsnthngs
@strngsnthngs 3 года назад
@@spyros92 Thanks man!
@marshallhickey3746
@marshallhickey3746 3 года назад
Nice
@NeuralNine
@NeuralNine 3 года назад
thank you ^^
@noobygamerniper9918
@noobygamerniper9918 3 года назад
@@NeuralNine Noice
@kareemalaridi8791
@kareemalaridi8791 3 года назад
Hello, thanks for the tutorial man it's really amazing. I'm facing a problem where after the game is done and it says "Game Over!", the close button doesn't work. I made sure that I used the same code as you but it still persists. Any help, please? ( if event.type == pygame.QUIT: game_over = True game_close = False)
@jack-ug1vl
@jack-ug1vl 2 года назад
maybe try doing game_close = True and see if it works then
@aliissen
@aliissen 2 года назад
why is that /10.0)*10.0 ?
@baolinhhuynh3683
@baolinhhuynh3683 2 года назад
Same question. So confused 😃
@tripsNtalks
@tripsNtalks 3 года назад
I don't know what's the issue with my program but my pygame window exit on it's own in just 2/3 secs as soon as I run the program can anyone help
@aidenross6814
@aidenross6814 2 года назад
same thing is happening to me. did you get an error saying local variable 'x_speed' referenced before assignment?
@tripsNtalks
@tripsNtalks 2 года назад
@@aidenross6814 my problem is solved I had my game ready with a few changes
@aidenross6814
@aidenross6814 2 года назад
@@tripsNtalks may I ask what you did? I'm still having the problem
@tripsNtalks
@tripsNtalks 2 года назад
@@aidenross6814 I might require your code
@aidenross6814
@aidenross6814 2 года назад
@@tripsNtalks yeah ok but I'm going out so ill be a few hours
@d1fficultgamer609
@d1fficultgamer609 3 года назад
Imagine geting pined by NeuralNine
@danx_x155
@danx_x155 3 года назад
Tried it
@PointedBowl
@PointedBowl 2 года назад
so i followed the tutorial to the end, and when i tested it didnt respond. rip. ill see if it loads. fingers crossed.
@cezawesome187
@cezawesome187 2 года назад
I'm having the same issue. Have you figured it out yet?
@trjaslatta1807
@trjaslatta1807 2 года назад
i like first sound
@abhilasharani7308
@abhilasharani7308 3 года назад
23:17
@noobygamerniper9918
@noobygamerniper9918 3 года назад
?
@saimakhan7713
@saimakhan7713 3 года назад
Why is my snake going only in right direction
@NeuralNine
@NeuralNine 3 года назад
Why do you expect anyone to be able to answer this question without any context??
@nirmaltheprogrammer510
@nirmaltheprogrammer510 3 года назад
day 4 of asking : A video neat
@IzUrBoiKK
@IzUrBoiKK 3 года назад
I also want that
@saifeddine3975
@saifeddine3975 3 года назад
please send me this code
@IzUrBoiKK
@IzUrBoiKK 3 года назад
Really?
@rvmishra9881
@rvmishra9881 3 года назад
Don't install it.. tab nine may even suggest your ideas too...
@amaljoshuaa.p.4096
@amaljoshuaa.p.4096 3 года назад
First
@NeuralNine
@NeuralNine 3 года назад
^^
@Jiggly12
@Jiggly12 2 года назад
i,ve run into a problem, ive written the code and all but when i run the game the food spawns once and after i get the food it doesnt spawn again, why is that? code: import pygame import sys import random pygame.init() w, h = 600, 400 # game variables screen = pygame.display.set_mode((w, h)) pygame.display.set_caption("SnakeGame") clock = pygame.time.Clock() cell_size = 10 snake_v = 15 msg_font = pygame.font.SysFont('Consolas', 30) score_font = pygame.font.SysFont('Consolas', 25) def score(score): text = score_font.render("Score:"+str(score), True, (255, 165, 0)) screen.blit(text, [0, 0]) # snake_p is the snake pixels def draw_snake(cell_size, snake_p): for x in snake_p: pygame.draw.rect(screen, (255, 255, 255), [ x[0], x[1], cell_size, cell_size]) def main(): # game variables game_over = False running = True x = w/2 y = h/2 delta_x = 0 delta_y = 0 snake_p = [] snake_len = 1 food_x = round(random.randrange(0, w-cell_size) / 10.0) * 10.0 food_y = round(random.randrange(0, h-cell_size) / 10.0) * 10.0 # main loop while not game_over: while not running: screen.fill((50, 50, 50)) game_over_msg = msg_font.render("Game Over!", True, (255, 0, 0)) screen.blit(game_over_msg, [w/3, h/3]) score(snake_len - 1) pygame.display.update() for event in pygame.event.get(): if event.type == pygame.KEYDOWN: if event.key == pygame.K_1: running = True game_over = True sys.exit() if event.key == pygame.K_2: main() for event in pygame.event.get(): if event.type == pygame.QUIT: game_over = True elif event.type == pygame.KEYDOWN: if event.key == pygame.K_UP: delta_x = 0 delta_y = -cell_size if event.key == pygame.K_DOWN: delta_x = 0 delta_y = cell_size if event.key == pygame.K_LEFT: delta_x = -cell_size delta_y = 0 if event.key == pygame.K_RIGHT: delta_x = cell_size delta_y = 0 # game_over if the snake bumps into the walls if x >= w or x < 0 or y >= h or y < 0: running = False x += delta_x y += delta_y screen.fill((50, 50, 50)) # draw food pygame.draw.rect(screen, (255, 165, 0), [ food_x, food_y, cell_size, cell_size]) # remove last block each movement snake_p.append([x, y]) if len(snake_p) > snake_len: del snake_p[0] # end game if snake bumps into self for pixel in snake_p[:-1]: if pixel == [x, y]: running = False draw_snake(cell_size, snake_p) score(snake_len - 1) pygame.display.update() if x == food_x and y == food_y: food_x = round(random.randrange(0, w-cell_size / 10.0)) * 10 food_y = round(random.randrange(0, h-cell_size / 10.0)) * 10 snake_len += 1 clock.tick(snake_v) pygame.quit() quit() main()
@tinytinsley
@tinytinsley 3 месяца назад
I need help with this bug: Traceback (most recent call last): line 127, in run_game() line 81, in run_game if event.key == pygame.K_LEFT: AttributeError: 'pygame.event.Event' object has no attribute 'key' PLEASE HELP ME IM 11 AND WANT TOO SHOW THIS TOO MY DAD SO BADLY HE IS FULL TIME SOFTWARE ENGINEER.
@khaledalhashimi1234
@khaledalhashimi1234 2 года назад
from cgitb import text from email import message import pygame import time import random # Init pygame pygame.init() # Define Colors white = (255, 255, 255) black = (0, 0, 0) red = (255, 0, 0) orange = (255, 165, 0) width, height = 600, 400 game_display = pygame.display.set_mode((width, height)) pygame.display.set_caption("i love snek") clock = pygame.time.Clock() snake_size = 10 snake_speed = 15 message_font = pygame.font.SysFont('ubuntu', 30) score_font = pygame.font.SysFont('ubuntu', 25) def print_score(score): text = score_font.render("Score: " + str(score), True, orange) game_display.blit(text, [0,0]) def draw_snake(snake_size, snake_pixels): for pixel in snake_pixels: pygame.draw.rect(game_display, white, [pixel[0], pixel[1], snake_size, snake_size]) def run_game(): game_over = False game_close = False x = width / 2 y = height / 2 x_speed = 0 y_speed = 0 snake_pixels = [] snake_length = 1 target_x = round(random.randrange(0, width-snake_size) / 10.0) * 10.0 target_y = round(random.randrange(0, height-snake_size) / 10.0) * 10.0 while not game_over: while game_close: game_display.fill(black) game_over_message = message_font.render("Game Over!", True, red) game_display.blit(game_over_message, [width / 3, height / 3]) print_score(snake_length - 1) pygame.display.update() for event in pygame.event.get(): if event.type == pygame.KEYDOWN: if event.key == pygame.K_1: game_over = True game_close = False if event.key == pygame.K_2: run_game() if event.type == pygame. QUIT: game_over = True game_close = False for event in pygame.event.get(): if event.type == pygame.QUIT: game_over = True if event.type == pygame.KEYDOWN: if event.key == pygame.K_LEFT: x_speed = -snake_size y_speed = 0 if event.key == pygame.K_RIGHT: x_speed = snake_size y_speed = 0 if event.key == pygame.K_UP: x_speed = 0 y_speed = 0 if event.key == pygame.K_DOWN: x_speed = 0 y_speed = snake_size if x >= width or x < 0 or y >= height or y < 0: game_close = True x += x_speed y += y_speed game_display.fill(black) pygame.draw.rect(game_display, orange, [target_x,target_y,snake_size, snake_size]) snake_pixels.append([x,y]) if len(snake_pixels) > snake_length: del snake_pixels[0] for pixel in snake_pixels[:-1]: if pixel == [x, y]: game_close = True draw_snake(snake_size, snake_pixels) print_score(snake_length - 1) pygame.display.update() if x == target_x and y == target_y: target_x = round(random.randrange(0, width-snake_size) / 10.0) * 10.0 target_y = round(random.randrange(0, height-snake_size) / 10.0) * 10.0 snake_length += 1 clock.tick(snake_speed) pygame.quit() quit() run_game() IndentationError: expected an indented block after function definition on line 37 game_over = False plz help
@waffles5707
@waffles5707 Год назад
heyy thanks for the video i am not able to start the game its coming for a second and vanishing can you please help me! @NeuralNine thank you
Далее
Learning pygame by creating Snake [python tutorial]
2:05:00
5 Good Python Habits
17:35
Просмотров 463 тыс.
Unlocking your CPU cores in Python (multiprocessing)
12:16
Make Pong With Python!
1:12:42
Просмотров 114 тыс.
Snake Game in Python Easy Tutorial | PyGame
20:38
Просмотров 16 тыс.
Water powered timers hidden in public restrooms
13:12
Просмотров 486 тыс.
5 Useful F-String Tricks In Python
10:02
Просмотров 286 тыс.
2 YEARS of PYTHON Game Development in 5 Minutes!
4:54
Просмотров 883 тыс.
Three Python Games in Less Than 15 Minutes
13:19
Просмотров 20 тыс.
Code a Snake Game with Python and Pygame 🐍 - Tutorial
1:35:54