Creating a Simple Snake Game using Python’s Tkinter Library: A Step-by-Step Guide

Creating a Simple Snake Game using Python’s Tkinter Library: A Step-by-Step Guide

Creating a Simple Snake Game using Python’s Tkinter Library: A Step-by-Step Guide
Creating a Simple Snake Game using Python’s Tkinter Library: A Step-by-Step Guide

I. Introduction

Snake is a classic arcade game that has been around for decades. It is a simple game where the player controls a snake that moves around the screen, trying to eat food while avoiding running into the walls or its own tail.

import tkinter as tk

root = tk.Tk()
root.title("Snake Game")
root.geometry("400x400")

II. Setting up the tkinter window

To create the game window, we will use the tkinter library in Python.

import tkinter as tk

root = tk.Tk()
root.title("Snake Game")
root.geometry("400x400")

III. Creating the snake and food

We can create the snake and food using tkinter’s Canvas widget.

canvas = tk.Canvas(root, bg="black", height=400, width=400)
snake = canvas.create_rectangle(10, 10, 20, 20, fill="white")
food = canvas.create_oval(50, 50, 60, 60, fill="red")
canvas.pack()

IV. Moving the snake

To move the snake, we can use the move method of the Canvas widget to update the snake’s position.

def move_snake():
    canvas.move(snake, 5, 0)
    root.after(100, move_snake)

root.after(100, move_snake)

V. Collision detection

To check if the snake has collided with the walls or its own tail, we can use the bbox method of the Canvas widget to get the bounding box of the snake, and then check if it intersects with the walls or any other part of the snake.

def check_collision():
    x1, y1, x2, y2 = canvas.bbox(snake)
    if x1 <= 0 or x2 >= 400 or y1 <= 0 or y2 >= 400:
        game_over()
    root.after(100, check_collision)

root.after(100, check_collision)

VI. Game over conditions

To end the game when the snake collides with the walls or its own tail, we can use the destroy method of the Tk widget to close the game window.

def game_over():
    root.destroy()

Full Code Snake Game

import tkinter as tk
import time

root = tk.Tk()
root.title("Snake Game")
root.geometry("400x400")

canvas = tk.Canvas(root, bg="black", height=400, width=400)
snake = canvas.create_rectangle(10, 10, 20, 20, fill="white")
food = canvas.create_oval(50, 50, 60, 60, fill="red")
canvas.pack()

x_change = 0
y_change = 0

def move_snake():
    global x_change, y_change
    x1, y1, x2, y2 = canvas.coords(snake)
    if x2 > 400 or x1 < 0 or y1 < 0 or y2 > 400:
        game_over()
    x_change = x_change + 5
    canvas.move(snake, x_change, y_change)
    root.after(100, move_snake)

def check_collision():
    global x_change, y_change
    x1, y1, x2, y2 = canvas.coords(snake)
    if x2 > 400 or x1 < 0 or y1 < 0 or y2 > 400:
        game_over()
    if canvas.find_overlapping(x1, y1, x2, y2) == 2:
        x_change = 0
        y_change = 0
    root.after(100, check_collision)

def game_over():
    canvas.create_text(200, 200, text="Game Over", font=("Helvetica", 24))
    time.sleep(3)
    root.destroy()

root.after(100, move_snake)
root.after(100, check_collision)
root.mainloop()

VII. Conclusion

This is a basic example of how to create a simple Snake game using the tkinter library in Python. There are many additional features that can be added, such as scorekeeping, difficulty levels, and multiple levels. Remember to always test your code, and check for possible errors.

See also  Creating a Simple Image Viewer App using Tkinter - A Step-by-Step Guide with Explanation

This code sets up a basic game window using the tkinter library, creates a snake and food using the Canvas widget, and moves the snake using the move method. The snake’s movement is controlled by the x_change and y_change variables, which are incremented by 5 in the move_snake function. The check_collision function is used to detect if the snake has collided with the walls or its own tail, and the game_over function is used to end the game and display a “Game Over” message.

In this article, we will be using the tkinter library in Python to create the Snake game. The tkinter library is a built-in library in Python that provides a set of tools for creating graphical user interfaces (GUIs). For more information on the tkinter library, you can refer to the official Python documentation

Print Friendly, PDF & Email

Author

Leave a Reply