AlphaPenguino

SNAKE GAME

Nov 23rd, 2023
99
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 3.77 KB | None | 0 0
  1. from tkinter import *
  2. import random
  3.  
  4.  
  5.  
  6. GAME_WIDTH = 700
  7. GAME_HEIGHT = 700
  8. SPEED = 80
  9. SPACE_SIZE = 50
  10. BODY_PARTS = 3
  11. SNAKE_COLOR = "#519940"
  12. FOOD_COLOR = "#a8182d"
  13. BACKGROUND_COLOR = "#000000"
  14.  
  15. def centerWindow(root, width, height):
  16.     screen_width = root.winfo_screenwidth()
  17.     screen_height = root.winfo_screenheight()
  18.  
  19.     x = (screen_width - width) // 2
  20.     y = (screen_height - height) // 2
  21.  
  22.     root.geometry(f"{width}x{height}+{x}+{y}")
  23.  
  24.  
  25. class Snake:
  26.     def __init__(self):
  27.         self.bodyParts = BODY_PARTS
  28.         self.coordinates = []
  29.         self.squares = []
  30.  
  31.         for i in range (0, BODY_PARTS):
  32.             self.coordinates.append([0,0])
  33.  
  34.         for x, y in self.coordinates:
  35.             square = canvas.create_rectangle(x, y, x + SPACE_SIZE, y + SPACE_SIZE, fill=SNAKE_COLOR)
  36.             self.squares.append(square)
  37.  
  38. class Food:
  39.     def __init__(self):
  40.  
  41.         x = random.randint(0,(GAME_WIDTH/SPACE_SIZE-1)) * SPACE_SIZE
  42.         y = random.randint(0, (GAME_HEIGHT / SPACE_SIZE - 1)) * SPACE_SIZE
  43.  
  44.         self.coordinates = [x,y]
  45.  
  46.         canvas.create_oval(x, y, x + SPACE_SIZE, y + SPACE_SIZE, fill=FOOD_COLOR, tag="food")
  47.  
  48. def nextTurn(snake, food):
  49.  
  50.     x, y = snake.coordinates[0]
  51.  
  52.  
  53.     if direction == "up":
  54.         y-= SPACE_SIZE
  55.     elif direction == "down":
  56.         y+= SPACE_SIZE
  57.     elif direction == "left":
  58.         x -= SPACE_SIZE
  59.     elif direction == "right":
  60.         x += SPACE_SIZE
  61.  
  62.     snake.coordinates.insert(0, (x, y))
  63.     square = canvas.create_rectangle(x, y, x + SPACE_SIZE, y + SPACE_SIZE, fill=SNAKE_COLOR)
  64.     snake.squares.insert(0, square)
  65.  
  66.     if x == food.coordinates[0] and y == food.coordinates[1]:
  67.         global score
  68.  
  69.         score += 1
  70.  
  71.         label.config(text = "Score: {}".format(score))
  72.  
  73.         canvas.delete("food")
  74.  
  75.         food = Food()
  76.  
  77.     else:
  78.         del snake.coordinates[-1]
  79.         canvas.delete(snake.squares[-1])
  80.         del snake.squares[-1]
  81.  
  82.     if checkCollisions(snake):
  83.         gameOver()
  84.     else:
  85.         root.after(SPEED, nextTurn, snake, food)
  86.  
  87.  
  88.  
  89. def changeDirection(new_direction):
  90.     global direction
  91.  
  92.     if new_direction == 'left':
  93.         if direction != 'right':
  94.             direction = new_direction
  95.     elif new_direction == 'right':
  96.         if direction != 'left':
  97.             direction = new_direction
  98.     elif new_direction == 'up':
  99.         if direction != 'down':
  100.             direction = new_direction
  101.     elif new_direction == 'down':
  102.         if direction != 'up':
  103.             direction = new_direction
  104.  
  105. def checkCollisions(snake):
  106.  
  107.     x, y = snake.coordinates[0]
  108.  
  109.     if x < 0 or x >= GAME_WIDTH:
  110.         print("GAME OVER")
  111.         return True
  112.     elif y < 0 or y >= GAME_WIDTH:
  113.         print("GAME OVER")
  114.         return True
  115.  
  116.     for bodyPart in snake.coordinates[1:]:
  117.         if x == bodyPart[0] and y == bodyPart[1]:
  118.             print("GAME OVER")
  119.             return True
  120.  
  121.     return False
  122.  
  123. def gameOver():
  124.     canvas.delete(ALL)
  125.     canvas.create_text(canvas.winfo_width()/2, canvas.winfo_height()/2, font=('consolas', 40), text="GAME OVER", fill="red", tag="gameover")
  126.  
  127. root = Tk()
  128. root.title("Snake Game")
  129. root.resizable(False, False)
  130.  
  131. centerWindow(root,800,800)
  132.  
  133. score = 0
  134. direction = "down"
  135.  
  136. label = Label(root,text = "Score: {}".format(score), font=('consolas', 40))
  137. label.pack()
  138.  
  139. canvas = Canvas(root, bg=BACKGROUND_COLOR, height=GAME_HEIGHT, width=GAME_WIDTH)
  140. canvas.pack()
  141.  
  142. root.update()
  143.  
  144. root.bind('<Left>', lambda event: changeDirection('left'))
  145. root.bind('<Right>', lambda event: changeDirection('right'))
  146. root.bind('<Up>', lambda event: changeDirection('up'))
  147. root.bind('<Down>', lambda event: changeDirection('down'))
  148.  
  149. snake = Snake()
  150. food = Food()
  151.  
  152. nextTurn(snake, food)
  153.  
  154. root.mainloop()
Add Comment
Please, Sign In to add comment