Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- from tkinter import *
- import random
- GAME_WIDTH = 700
- GAME_HEIGHT = 700
- SPEED = 80
- SPACE_SIZE = 50
- BODY_PARTS = 3
- SNAKE_COLOR = "#519940"
- FOOD_COLOR = "#a8182d"
- BACKGROUND_COLOR = "#000000"
- def centerWindow(root, width, height):
- screen_width = root.winfo_screenwidth()
- screen_height = root.winfo_screenheight()
- x = (screen_width - width) // 2
- y = (screen_height - height) // 2
- root.geometry(f"{width}x{height}+{x}+{y}")
- class Snake:
- def __init__(self):
- self.bodyParts = 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)
- 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 nextTurn(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 checkCollisions(snake):
- gameOver()
- else:
- root.after(SPEED, nextTurn, snake, food)
- def changeDirection(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 checkCollisions(snake):
- x, y = snake.coordinates[0]
- if x < 0 or x >= GAME_WIDTH:
- print("GAME OVER")
- return True
- elif y < 0 or y >= GAME_WIDTH:
- print("GAME OVER")
- return True
- for bodyPart in snake.coordinates[1:]:
- if x == bodyPart[0] and y == bodyPart[1]:
- print("GAME OVER")
- return True
- return False
- def gameOver():
- canvas.delete(ALL)
- canvas.create_text(canvas.winfo_width()/2, canvas.winfo_height()/2, font=('consolas', 40), text="GAME OVER", fill="red", tag="gameover")
- root = Tk()
- root.title("Snake Game")
- root.resizable(False, False)
- centerWindow(root,800,800)
- score = 0
- direction = "down"
- label = Label(root,text = "Score: {}".format(score), font=('consolas', 40))
- label.pack()
- canvas = Canvas(root, bg=BACKGROUND_COLOR, height=GAME_HEIGHT, width=GAME_WIDTH)
- canvas.pack()
- root.update()
- root.bind('<Left>', lambda event: changeDirection('left'))
- root.bind('<Right>', lambda event: changeDirection('right'))
- root.bind('<Up>', lambda event: changeDirection('up'))
- root.bind('<Down>', lambda event: changeDirection('down'))
- snake = Snake()
- food = Food()
- nextTurn(snake, food)
- root.mainloop()
Add Comment
Please, Sign In to add comment