python_snake 悠悠 2022-07-17 02:23 7阅读 0赞 #!/usr/bin/env python3 from threading import Thread from threading import Timer from threading import Event from threading import Lock import tkinter as tk import copy import time dird = {"Right":[1, 0], "Left":[-1, 0], "Up":[0, -1], "Down":[0, 1]} class MyTimer(Thread): def __init__(self, interval, function, args=None, kwargs=None): Thread.__init__(self) self.interval = interval self.function = function self.args = args if args is not None else [] self.kwargs = kwargs if kwargs is not None else {} self.finished = Event() def cancel(self): self.finished.set() def run(self): while not self.finished.is_set(): self.finished.wait(self.interval) if not self.finished.is_set(): self.function(*self.args, **self.kwargs) def set_interval(self, interval): self.interval = interval class Snake(): def __init__(self, map, id = 1, snake_pos = (0,0), snake_length = 3, dir = "Right", color = "black"): self.dir = dird[dir] self.id = id self.map = map self.color = color self.__initSnakeBody__(snake_pos, snake_length) def __initSnakeBody__(self, snake_pos, snake_length): self.body = [] self.body.append(list(snake_pos)) if snake_length < 2: snake_length = 2 for i in range(1, snake_length, 1): self.add() self.map.setMapValue(self.body, self.id) def move(self, is_add = False): self.map.setMapValue(self.body, 0) if is_add == False: self.body.pop(0) self.add() self.map.setMapValue(self.body, self.id) def add(self, node = None): head = self.body[len(self.body) - 1] if not node: node = [head[0] + self.dir[0], head[1] + self.dir[1]] self.body.append(node) def isSnakeDead(self): return self.map.isSnakeDead(self.body, self.dir) def change_dir(self, newdir): newdir = dird[newdir] curdir = [self.body[-1][i]-self.body[-2][i] for i in range(2)] if curdir != newdir: self.dir = newdir class Map(tk.Frame): def __init__(self, width = 100, height = 100, span = 5, bgcolor = "white"): self.root = tk.Tk() self.root.geometry(repr(width*span)+'x'+repr(height*span)) super().__init__(self.root) self.bg = bgcolor self.width = width self.height = height self.span = span self.__initMap__() self.root.resizable(False, False) def __initMap__(self): self.cvs = tk.Canvas(self.root, width = self.width *self.span, height = self.height * self.span, bg = self.bg) self.map = [[0]*self.width for i in range(self.height)] self.cvs.bind('<KeyPress>', self.__getKeyEvent) self.cvs.focus_set() self.cvs.grid(row=0, column=0, columnspan=self.width, rowspan=self.height) def drawRectangle(self, start, color): self.cvs.create_rectangle(start[0]*self.span, start[1]*self.span, (start[0]+1)*self.span, (start[1]+1)*self.span, fill = color, outline = self.bg) def createSnake(self): self.snake = Snake(self) def __getKeyEvent(self, event): print(event.keysym) self.snake.change_dir(event.keysym) pass def isSnakeDead(self, body, dir): head = body[len(body)-1] xpos = head[0]+dir[0] ypos = head[1]+dir[1] if xpos >= self.width or xpos < 0: return True elif ypos >= self.height or ypos < 0: return True elif self.map[xpos][ypos] != 0: return True return False def setMapValue(self, body, value, color = "black"): for n in body: self.map[n[0]][n[1]] = value if value != 0: self.drawRectangle(n, color) else: self.drawRectangle(n, self.bg) def moveSnake(self): self.snake.move() return False def mainloop(self): self.root.mainloop() class GameClient(Thread): def __init__(self): super().__init__() self.map = Map() self.map.createSnake() def startGame(self): self.start() self.map.mainloop() def run(self): while(self.map.moveSnake() == False): #snake run time.sleep(1) else: print("snake dead") self.map.quit() if __name__ == "__main__": game = GameClient() game.startGame()
还没有评论,来说两句吧...