Advertisement
Guest User

Untitled

a guest
Feb 29th, 2020
151
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.24 KB | None | 0 0
  1. from tkinter import *
  2.  
  3.  
  4. WIDTH = 400
  5. HEIGHT = 400
  6. SEG_SIZE = 20
  7.  
  8.  
  9. class Segment:
  10. def __init__(self, x, y):
  11. self.instance = \
  12. c.create_rectangle(
  13. x, y, x + SEG_SIZE, \
  14. y + SEG_SIZE, fill='white')
  15.  
  16.  
  17. class Snake:
  18. def __init__(self, segments):
  19. self.segments = segments
  20. self.dir = {
  21. "Down": (0, 1),
  22. "Right": (1, 0),
  23. "Up": (0, -1),
  24. "Left": (-1, 0)
  25. }
  26. self.vector = self.dir["Down"]
  27.  
  28. def move(self):
  29. for i in range(len(self.segments) - 1):
  30. segment = self.segment[i].instance
  31. x1, y1, x2, y2 = c.coords(
  32. self.segments[i+1].instance)
  33. c.coords(segment, x1, y1, x2, y2)
  34.  
  35. x1, y1, x2, y2 = c.coords(
  36. self.segments[-2].instance)
  37. c.coords(self.segments[-1].instance,
  38. x1 + self.vector[0] * SEG_SIZE,
  39. y1 + self.vector[1] * SEG_SIZE,
  40. x2 + self.vector[0] * SEG_SIZE,
  41. y2 + self.vector[1] * SEG_SIZE)
  42.  
  43. root = Tk()
  44. root.title("Snake")
  45.  
  46. c = Canvas(root, width=WIDTH, \
  47. height=HEIGHT, bg="#4169e1")
  48. c.grid()
  49. c.focus_set()
  50.  
  51. root.mainloop()
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement