|
| 1 | +import queue, tkinter, random, time, threading |
| 2 | + |
| 3 | + |
| 4 | +class Food(object): |
| 5 | + |
| 6 | + def __init__(self, q): |
| 7 | + self.queue = q |
| 8 | + self.new_food() |
| 9 | + |
| 10 | + def new_food(self): |
| 11 | + x = random.randrange(55, 485, 10) |
| 12 | + y = random.randrange(55, 285, 10) |
| 13 | + self.position = x, y |
| 14 | + self.queue.put({"food": self.position}) |
| 15 | + |
| 16 | + |
| 17 | +class Snake(threading.Thread): |
| 18 | + |
| 19 | + def __init__(self, world, q): |
| 20 | + threading.Thread.__init__(self) |
| 21 | + self.world = world |
| 22 | + self.queue = q |
| 23 | + self.points_earned = 0 |
| 24 | + self.food = Food(self.queue) |
| 25 | + self.direction = "Left" |
| 26 | + self.snake_points = [(495, 55), (485, 55), (475, 55), (465, 55), (455, 55)] |
| 27 | + self.start() |
| 28 | + |
| 29 | + def run(self): |
| 30 | + |
| 31 | + if self.world.is_game_over: |
| 32 | + self._delete() |
| 33 | + while not self.world.is_game_over: |
| 34 | + self.queue.put({"move": self.snake_points}) |
| 35 | + time.sleep(0.1) |
| 36 | + self.move() |
| 37 | + |
| 38 | + def move(self): |
| 39 | + new_snake_points = self.cal_new_pos() |
| 40 | + if self.food.position == new_snake_points: |
| 41 | + self.points_earned += 1 |
| 42 | + self.queue.put({"points_earned": self.points_earned}) |
| 43 | + self.food.new_food() |
| 44 | + else: |
| 45 | + self.snake_points.pop(0) |
| 46 | + self.check_game_over(new_snake_points) |
| 47 | + self.snake_points.append(new_snake_points) |
| 48 | + |
| 49 | + def cal_new_pos(self): |
| 50 | + last_x, last_y = self.snake_points[-1] |
| 51 | + if self.direction == "Up": |
| 52 | + new_snake_point = last_x, last_y - 10 |
| 53 | + elif self.direction == "Left": |
| 54 | + new_snake_point = last_x - 10, last_y |
| 55 | + elif self.direction == "Down": |
| 56 | + new_snake_point = last_x, last_y + 10 |
| 57 | + elif self.direction == "Right": |
| 58 | + new_snake_point = last_x + 10, last_y |
| 59 | + return new_snake_point |
| 60 | + |
| 61 | + def check_game_over(self, snake_point): |
| 62 | + x, y = snake_point |
| 63 | + if x >= 505 or x <= -5 or y <= -5 or y >= 305 or (snake_point in self.snake_points): |
| 64 | + self.queue.put({"game_over": True}) |
| 65 | + |
| 66 | + |
| 67 | +class World(): |
| 68 | + |
| 69 | + def __init__(self, q): |
| 70 | + self.queue = q |
| 71 | + self.root = tkinter.Tk() |
| 72 | + self.canvas = tkinter.Canvas(self.root, width=500, height=300, bg="white") |
| 73 | + self.canvas.pack() |
| 74 | + self.is_game_over = False |
| 75 | + self.Snake = Snake(self, self.queue) |
| 76 | + self.canvas.bind_all("<KeyPress-Up>", self.key_pressed) |
| 77 | + self.canvas.bind_all("<KeyPress-Down>", self.key_pressed) |
| 78 | + self.canvas.bind_all("<KeyPress-Left>", self.key_pressed) |
| 79 | + self.canvas.bind_all("<KeyPress-Right>", self.key_pressed) |
| 80 | + |
| 81 | + self.snake = self.canvas.create_line(0, 0, 0, 0, fill="black", width=10) |
| 82 | + self.food = self.canvas.create_rectangle(0, 0, 0, 0, fill="#FFCC4C", outline='#FFCC4C') |
| 83 | + self.points_earned = self.canvas.create_text(450, 20, fill="white", text='SCORE: 0') |
| 84 | + self.queue_handler() |
| 85 | + self.root.mainloop() |
| 86 | + |
| 87 | + def key_pressed(self, e): |
| 88 | + |
| 89 | + self.Snake.direction = e.keysym |
| 90 | + |
| 91 | + def queue_handler(self): |
| 92 | + try: |
| 93 | + while True: |
| 94 | + task = self.queue.get(block=False) |
| 95 | + |
| 96 | + if task.get("game_over"): |
| 97 | + self.game_over() |
| 98 | + elif task.get("move"): |
| 99 | + points = [x for point in task['move'] for x in point] |
| 100 | + self.canvas.coords(self.snake, *points) |
| 101 | + elif task.get("food"): |
| 102 | + x, y = task.get("food") |
| 103 | + points = [x - 5, y - 5, x + 5, y + 5] |
| 104 | + self.canvas.coords(self.food, *points) |
| 105 | + elif task.get("points_earned"): |
| 106 | + points = task['points_earned'] |
| 107 | + self.canvas.itemconfig(self.points_earned, text="SCORE: {}".format(points)) |
| 108 | + except queue.Empty: |
| 109 | + if not self.is_game_over: |
| 110 | + self.canvas.after(100, self.queue_handler) |
| 111 | + |
| 112 | + def game_over(self): |
| 113 | + self.is_game_over = True |
| 114 | + self.canvas.create_text(250, 120, fill="green", text="Game Over") |
| 115 | + qb = tkinter.Button(self.root, bg="green", text="Quit", command=self.root.destroy) |
| 116 | + qb.pack() |
| 117 | + rb = tkinter.Button(self.root, bg="green", text="Again", command=start) |
| 118 | + rb.pack() |
| 119 | + |
| 120 | + |
| 121 | +def start(): |
| 122 | + q = queue.Queue() |
| 123 | + world = World(q) |
| 124 | + world.root.mainloop() |
| 125 | + |
| 126 | + |
| 127 | +if __name__ == "__main__": |
| 128 | + start() |
0 commit comments