-
Notifications
You must be signed in to change notification settings - Fork 32
/
snake.py
1 lines (1 loc) · 3.18 KB
/
snake.py
1
(lambda pygame=__import__('pygame'),random=__import__('random'),WIDTH=800,HEIGHT=600,BOARD_SIZE=40,SNAKE_SIZE=3:(pygame.init(),(lambda win=pygame.display.set_mode((WIDTH,HEIGHT)),draw_square=(lambda window,color,x,y,k=WIDTH/BOARD_SIZE:pygame.draw.rect(window,color,(int(k*x),HEIGHT-int(k*y),int(k),int(k)))):(lambda Snake=type("Snake",(),{"__init__":lambda self,x,y:self.__dict__.update({'x':x,'y':y,'direction':0,'body':[],'add_tail':0,'color':(0,255,0)}),"set_direction":lambda self,direction:self.__dict__.update({'direction':direction}),"move":lambda self:None if self.direction==0 else(self.body.insert(0,(self.x,self.y)),self.body.pop()if self.add_tail==0 else self.__dict__.update({'add_tail':self.add_tail-1}),self.__dict__.update({'y':self.y+1}if self.direction==1 else{'x':self.x+1}if self.direction==2 else{'y':self.y-1}if self.direction==3 else{'x':self.x-1}if self.direction==4 else{}))[0],"draw":lambda self:(draw_square(win,self.color,self.x,self.y),[draw_square(win,self.color,b[0],b[1])for b in self.body])[0]}),Fruit=type("Fruit",(),{"color":(255,0,0),"__init__":lambda self,x,y:self.__dict__.update({'x':x,'y':y}),"draw":lambda self:draw_square(win,self.color,self.x,self.y)}):(lambda board=type("Board",(),{"width":BOARD_SIZE,"height":int(BOARD_SIZE*HEIGHT/WIDTH),"score":0,"gameover":False,"__init__":lambda self:self.__dict__.update({'snake':Snake(int(self.width/2),int(self.height/2)),'fruit':Fruit(*self.get_fruit_position(has_snake=False))}),"update":lambda self:(self.end_game()if not(0<=self.snake.x<self.width and 0<self.snake.y<=self.height)or(self.snake.x,self.snake.y)in self.snake.body else ((self.__dict__.update({'fruit':Fruit(*self.get_fruit_position()),'score':self.score+1}),self.snake.__dict__.update({'add_tail':self.snake.add_tail+SNAKE_SIZE}))[0]if self.snake.x==self.fruit.x and self.snake.y==self.fruit.y else None,self.snake.move()if not self.gameover else None)[0])if not self.gameover else None,"draw":lambda self:(self.snake.draw(),self.fruit.draw())[0],"end_game":lambda self:(self.snake.__dict__.update({'direction':0}),self.__dict__.update({"gameover":True}),print(f"score: {self.score}"))[0],"get_fruit_position":(lambda f:lambda x,**k:f(x,f,**k))(lambda self,f,has_snake=True,x=random.randint(0,BOARD_SIZE-1),y=random.randint(1,int(BOARD_SIZE*HEIGHT/WIDTH)-1):f(self,f,x=random.randint(0,BOARD_SIZE-1),y=random.randint(1,int(BOARD_SIZE*HEIGHT/WIDTH)-1))if has_snake and ((x,y)in self.snake.body or (self.snake.x==x and self.snake.y==y))else (x,y))})(),clock=pygame.time.Clock():(lambda update=(lambda:(win.fill((0,0,0)),board.update(),board.draw(),pygame.display.update(),[(pygame.quit(),__import__('sys').exit())if event.type==pygame.QUIT or(event.type==pygame.KEYDOWN and event.key==pygame.K_SPACE)else(board.snake.set_direction(1)if event.key==pygame.K_UP and board.snake.direction!=3 else board.snake.set_direction(2)if event.key==pygame.K_RIGHT and board.snake.direction!=4 else board.snake.set_direction(3)if event.key==pygame.K_DOWN and board.snake.direction!=1 else board.snake.set_direction(4)if event.key==pygame.K_LEFT and board.snake.direction!=2 else None)if event.type==pygame.KEYDOWN else None for event in pygame.event.get()],clock.tick(10))[0]):[_ for _ in iter(update,0)])())())())()))()