import pygame import random class Game(): def __init__(self, window_width, window_height, playing_field_size): self._is_running = True pygame.init() self._display_size = (window_width,window_height) self._playing_field_size = playing_field_size self.fps = 60 self.on_init() self.clock = pygame.time.Clock() self.run() def draw_cell(self,x,y,color,border_color=(0,0,0)): border_width_as_divisor = 10 rect = pygame.Rect(x*self._cell_size,y*self._cell_size,self._cell_size,self._cell_size) pygame.draw.rect(self._display_surf,color,rect) pygame.draw.rect(self._display_surf,border_color,rect,self._cell_size//border_width_as_divisor) def smaller_side(self,tuple): a,b=tuple if(a<=b): return a else: return b def run(self): while(self._is_running): self._display_surf = pygame.display.set_mode(self._display_size, pygame.HWSURFACE | pygame.DOUBLEBUF | pygame.RESIZABLE) self._cell_size = self.smaller_side(self._display_surf.get_size())//self._playing_field_size for x in range(0,self._playing_field_size): for y in range(0,self._playing_field_size): self.draw_cell(x,y,(150,150,150)) self.draw() self.clock.tick( self.fps ) self.render() for event in pygame.event.get(): if event.type == pygame.QUIT: self._is_running = False if event.type == pygame.VIDEORESIZE: self._display_size = [event.w,event.h] self.on_event(event) pygame.quit() def on_init(self): pass def on_event(self, event): pass def draw(self): pass def render(self): pygame.display.update() class Food(): def __init__(self, _playing_field_size): self._playing_field_size = _playing_field_size self.reset() def genCoord(self,boundary:int) -> int(): return random.randint(0,boundary-1) def genCoords(self, boundary:int) -> tuple: return (self.genCoord(boundary),self.genCoord(boundary)) def get(self): if self.coordFood == None: self.coordFood = self.genCoords(self._playing_field_size) return self.coordFood def reset(self): self.coordFood = None coords = property(get) class player(): def __init__(self, _playing_field_size): self._playing_field_size = _playing_field_size self.coords = [(_playing_field_size//2,_playing_field_size//2)] self.direction = "R" self.alive = True move_r = lambda d : (d[0]+1,d[1]) move_l = lambda d : (d[0]-1,d[1]) move_d = lambda d : (d[0],d[1]+1) move_u = lambda d : (d[0],d[1]-1) def move(self, grow:bool=False): if not self.alive: pass elif self.direction == "R": self.coords.append( player.move_r(self.coords[-1]) ) if not grow: self.coords.pop(0) elif self.direction == "U": self.coords.append( player.move_u(self.coords[-1]) ) if not grow: self.coords.pop(0) elif self.direction == "D": self.coords.append( player.move_d(self.coords[-1]) ) if not grow: self.coords.pop(0) elif self.direction == "L": self.coords.append( player.move_l(self.coords[-1]) ) if not grow: self.coords.pop(0) self.checkCollisions() def turn(self,event): if event == pygame.K_UP and not self.direction == "D": self.direction = "U" if event == pygame.K_DOWN and not self.direction == "U": self.direction = "D" if event == pygame.K_RIGHT and not self.direction == "L": self.direction = "R" if event == pygame.K_LEFT and not self.direction == "R": self.direction = "L" def checkCollisions(self): if self.coords[-1][0] in (-1,self._playing_field_size): self.alive = False elif self.coords[-1][1] in (-1,self._playing_field_size): self.alive = False if self.coords[-1] in self.coords[0:-1]: self.alive = False class Snake(Game): def on_init(self): self.food = Food(self._playing_field_size) self.player = player(self._playing_field_size) self.fpsCounter = 0 def on_event(self, event): if event.type == pygame.KEYDOWN: print(event.key) if event.key in (pygame.K_UP,pygame.K_DOWN, pygame.K_RIGHT,pygame.K_LEFT): self.player.turn(event.key) if event.key == pygame.K_SPACE and self.player.alive == False: self.player = player(self._playing_field_size) def limitFps(self, fps): if self.fpsCounter >= self.fps // fps: self.fpsCounter = 0 return True self.fpsCounter = self.fpsCounter + 1 return False def draw(self): if self.limitFps(2): if self.player.coords[-1] == self.food.coords: self.player.move(True) self.food.reset() else: self.player.move() if not self.player.alive: self.gameOver() self.drawFood() self.draw_player() # #### FOOD #### def drawFood(self): self.draw_cell(*self.food.coords,(200,0,0)) def draw_player(self): for coord in self.player.coords: self.draw_cell(coord[0],coord[1],(0,250,0)) def gameOver(self): pass Snake(500,500,10)