Make A Snake Game Using Python And Tkinter || Python Project For Beginners With Source Code
๐๐จ๐จ๐ค ( ๐
๐ซ๐จ๐ฆ ๐๐จ๐๐ ๐๐จ ๐๐จ๐ง๐ง๐๐๐ญ๐ข๐จ๐ง๐ฌ: ๐ ๐๐๐ฏ๐๐ฅ๐จ๐ฉ๐๐ซโ๐ฌ ๐ ๐ฎ๐ข๐๐ ๐ญ๐จ ๐๐ฎ๐ข๐ฅ๐๐ข๐ง๐ ๐ ๐ฌ๐จ๐๐ข๐๐ฅ ๐๐๐ซ๐๐๐ซ )
Amazon India:
Amazon Worldwide:
Gumroad (pdf):
My social Links:
Instagram:
Twitter:
website:
Music Source: NCS
Tags: python for beginners, python, python full course, python tutorial, python programming, python projects, learn python, python tutorial for beginners, python full course in Hindi, python course, python for beginners in Hindi, learn python programming for beginners, python programming, python programming for beginners, what is python programming, python advanced programming, object-oriented programming in python, basic python programming for beginners, python programming full course, python object-oriented programming, advanced python programming, introduction to python programming, dynamic programming python, python project, python project with source code, project in python, python project step by step, Tkinter python project, python project ideas, cool python project, project on python, python project tutorial, python GUI project, python project for beginners, python tkinter project, mini project in python, project using python, small python project, simple python project, python games, snake game using python, how to make a snake game using python, tkinter project in python programming, ui-ux in python
#python #python3 #programming #code #github
PS C:Usersmomna> & C:/Users/momna/AppData/Local/Programs/Python/Python312/python.exe c:/Users/momna/Documents/snake.py
Traceback (most recent call last):
File "c:UsersmomnaDocumentssnake.py", line 150, in <module>
food = Food()
^^^^^^
File "c:UsersmomnaDocumentssnake.py", line 48, in _init_
x = random.randint(0, (GAME_WIDTH/SPACE_SIZE)-1)*SPACE_SIZE
^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
File "C:UsersmomnaAppDataLocalProgramsPythonPython312Librandom.py", line 336, in randint
return self.randrange(a, b+1)
^^^^^^^^^^^^^^^^^^^^^^
File "C:UsersmomnaAppDataLocalProgramsPythonPython312Librandom.py", line 312, in randrange
istop = _index(stop)
^^^^^^^^^^^^
TypeError: 'float' object cannot be interpreted as an integer
PS C:Usersmomna>
Traceback (most recent call last):
File "C:UserskhanhPycharmProjectspythonProjectRevision.py", line 78, in <module>
food = Food()
^^^^^^
File "C:UserskhanhPycharmProjectspythonProjectRevision.py", line 33, in _init_
x = random.randint(0, (GAME_WIDTH / SPACE_SIZE)-1) * SPACE_SIZE
^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ mera ye show kr rha h run krne prpls someone help me if you know anything about it
please give th ecode
it gives bad geometry specifier errorline 142
github ?
https://www.youtube.com/watch?v=hDOGYnqv2jw
Hello, is there any way to stop the snake? Thanks to you, I am using its algorithm.
where can i find source code file bro?
Your welcome
from tkinter import *
import random
from tkinter import font
from turtle import window_height, window_width
window = Tk()
window.title("Snake Game Python")
window.resizable(0, 0)
Label(window, text='SUBSCRIBE', font='arial 20 bold').pack(side=BOTTOM) # footer
score = 0
direction = 'down'
GAME_WIDTH = 700
GAME_HEIGHT = 700
SPEED = 180
SPACE_SIZE = 50
BODY_PARTS = 2
SNAKE_COLOR = '#00FF00'
FOOD_COLOR = '#FF0000'
BACKGROUND_COLOR = '#000000'
label = Label(window, text="Score:{}".format(score), font=('consolas', 40))
label.pack()
canvas = Canvas(window, bg=BACKGROUND_COLOR, height=GAME_HEIGHT, width=GAME_WIDTH)
canvas.pack()
class Snake:
def __init__(self):
self.body_size = BODY_PARTS
self.coordinates = []
self.squares = []
for i in range(0, BODY_PARTS):
self.coordinates.append([0, 0])
for x, y in self.coordinates:
square = canvas.create_rectangle(x, y, x + SPACE_SIZE, y + SPACE_SIZE, fill=SNAKE_COLOR, tag="snake")
self.squares.append(square)
class Food:
def __init__(self):
x = random.randint(0, (GAME_WIDTH/SPACE_SIZE)-1)*SPACE_SIZE
y = random.randint(0, (GAME_HEIGHT/SPACE_SIZE)-1)*SPACE_SIZE
self.coordinates = [x, y]
canvas.create_oval(x, y, x + SPACE_SIZE, y + SPACE_SIZE, fill=FOOD_COLOR, tag='food')
def next_turn(snake, food):
x, y = snake.coordinates[0]
if direction == 'up':
y -= SPACE_SIZE
elif direction == 'down':
y += SPACE_SIZE
elif direction == 'left':
x -= SPACE_SIZE
elif direction == 'right':
x += SPACE_SIZE
snake.coordinates.insert(0, (x, y))
square = canvas.create_rectangle(x, y, x + SPACE_SIZE, y + SPACE_SIZE, fill=SNAKE_COLOR)
snake.squares.insert(0, square)
if x == food.coordinates[0] and y == food.coordinates[1]:
global score
score += 1
label.config(text='Score{}'.format(score))
canvas.delete("food")
food = Food()
else:
del snake.coordinates[-1]
canvas.delete(snake.squares[-1])
del snake.squares[-1]
if check_collisions(snake):
game_over()
else:
window.after(SPEED, next_turn, snake, food)
def change_direction(new_direction):
global direction
if new_direction == 'left':
if direction != 'right':
direction = new_direction
elif new_direction == 'right':
if direction != 'left':
direction = new_direction
elif new_direction == 'up':
if direction != 'down':
direction = new_direction
elif new_direction == 'down':
if direction != 'up':
direction = new_direction
def check_collisions(snake):
x, y = snake.coordinates[0]
if x < 0 or x >= GAME_WIDTH:
return True
elif y <0 or y >= GAME_HEIGHT:
return True
for body_part in snake.coordinates[1:]:
if x == body_part[0] and y == body_part[1]:
return True
return False
def game_over():
canvas.delete(ALL)
canvas.create_text(canvas.winfo_width()/2,
canvas.winfo_height()/2,
font = ('consolas', 70),
text = 'Game Over',
fill = 'red',
tag = 'gameover')
window.update()
window_width = window.winfo_width()
window_height = window.winfo_height()
screen_width = window.winfo_screenwidth()
screen_height = window.winfo_screenheight()
x = int((screen_width/2) – (window_width/2))
y = int((screen_height/2) – (window_height/2))
window.geometry(f"{window_width}x{window_height}+{x}+{y}")
window.bind('<Left>', lambda event: change_direction('left'))
window.bind('<Right>', lambda event: change_direction('right'))
window.bind('<Up>', lambda event: change_direction('up'))
window.bind('<Down>', lambda event: change_direction('down'))
snake = Snake()
food = Food()
next_turn(snake, food)
window.mainloop()
thank you so much! i didn't know where to start but this tutorial helped me very much!
any idea how we would go about adding a "Play again" screen? Feel like this would be a great addition
Please I kept getting SyntaxError window.geometry (fโ(window_width)x(window_height) + (x) + (y)โ)
Hello bhai yeh snake left right nahi ho raha iska koi solution bata do