Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- from tkinter import *
- WIDTH = 400
- HEIGHT = 400
- SEG_SIZE = 20
- class Segment:
- def __init__(self, x, y):
- self.instance = \
- c.create_rectangle(
- x, y, x + SEG_SIZE, \
- y + SEG_SIZE, fill='white')
- class Snake:
- def __init__(self, segments):
- self.segments = segments
- self.dir = {
- "Down": (0, 1),
- "Right": (1, 0),
- "Up": (0, -1),
- "Left": (-1, 0)
- }
- self.vector = self.dir["Down"]
- def move(self):
- for i in range(len(self.segments) - 1):
- segment = self.segment[i].instance
- x1, y1, x2, y2 = c.coords(
- self.segments[i+1].instance)
- c.coords(segment, x1, y1, x2, y2)
- x1, y1, x2, y2 = c.coords(
- self.segments[-2].instance)
- c.coords(self.segments[-1].instance,
- x1 + self.vector[0] * SEG_SIZE,
- y1 + self.vector[1] * SEG_SIZE,
- x2 + self.vector[0] * SEG_SIZE,
- y2 + self.vector[1] * SEG_SIZE)
- root = Tk()
- root.title("Snake")
- c = Canvas(root, width=WIDTH, \
- height=HEIGHT, bg="#4169e1")
- c.grid()
- c.focus_set()
- root.mainloop()
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement