Make A Snake Game Using Python And Tkinter || Python Project For Beginners With Source Code - playproduction.de

Make A Snake Game Using Python And Tkinter || Python Project For Beginners With Source Code

Bug Ninza
Views: 8468
Like: 99
Hello guys, My name is Rohit Kumar Thakur and In this video, I am gonna show you how to make snake game using python programming and tkinter library. Well, it’s not that much difficult. You just have to follow the video. All the important stuffs are commented out in this python program video. If I miss something then let me know in the comment section

๐๐จ๐จ๐ค ( ๐…๐ซ๐จ๐ฆ ๐‚๐จ๐๐ž ๐“๐จ ๐‚๐จ๐ง๐ง๐ž๐œ๐ญ๐ข๐จ๐ง๐ฌ: ๐€ ๐ƒ๐ž๐ฏ๐ž๐ฅ๐จ๐ฉ๐ž๐ซโ€™๐ฌ ๐ ๐ฎ๐ข๐๐ž ๐ญ๐จ ๐›๐ฎ๐ข๐ฅ๐๐ข๐ง๐  ๐š ๐ฌ๐จ๐œ๐ข๐š๐ฅ ๐œ๐š๐ซ๐ž๐ž๐ซ )
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

12 Comments

  1. 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

  2. it gives bad geometry specifier errorline 142

  3. Hello, is there any way to stop the snake? Thanks to you, I am using its algorithm.

  4. where can i find source code file bro?

  5. 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()

  6. thank you so much! i didn't know where to start but this tutorial helped me very much!

  7. any idea how we would go about adding a "Play again" screen? Feel like this would be a great addition

  8. Please I kept getting SyntaxError window.geometry (fโ€(window_width)x(window_height) + (x) + (y)โ€)

  9. Hello bhai yeh snake left right nahi ho raha iska koi solution bata do

Leave a Reply

Your email address will not be published.