Advertisement
c0d3dsk1lls

Snakes-v-1-0 CodedSkills.net

Aug 5th, 2022 (edited)
84
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 16.22 KB | None | 0 0
  1.  
  2. ##+++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++##
  3. ##=======================================================================================================================================================================================##
  4. ##----------------------CODEDSKILLS.NET--------------------------------------------------------------------------------------------------------------------------------------------------##
  5. #------------------------------#
  6. import math   #----------------#
  7. import pygame #----------------#
  8. import random #----------------#
  9. import tkinter as tk #---------#
  10. from tkinter import messagebox #
  11. #------------------------------#
  12. width = 1000   #
  13. height = 700   #
  14. cols = 20      #
  15. rows = 15      #
  16. #--------------#
  17. class cube():  #
  18.     rows = 20  #
  19.     w = 500    #----------------------------------------------------#
  20.     def __init__(self, start, dirnx=1, dirny=0, color=(255,0,0)):   #
  21.         self.pos = start    #---------------------------------------#
  22.         self.dirnx = dirnx  #---------------------#
  23.         self.dirny = dirny  # "L", "R", "U", "D"  #
  24.         self.color = color  #---------------------#
  25. #---------------------------#-------#
  26.     def move(self, dirnx, dirny):   #
  27.         self.dirnx = dirnx  #-------#
  28.         self.dirny = dirny  #---------------------------------------------#
  29.         self.pos  = (self.pos[0] + self.dirnx, self.pos[1] + self.dirny)  #
  30. #-------------------------------------------------------------------------#            
  31.     def draw(self, surface, eyes=True):    #
  32.         dis = self.w // self.rows          #
  33.         i = self.pos[0]    #---------------#
  34.         j = self.pos[1]    #
  35. #--------------------------#-----------------------------------------------------------#
  36.         pygame.draw.rect(surface, self.color, (i*dis+1,j*dis+1,dis-2,dis-2))           #
  37.         if eyes: #---------#-----------------------------------------------------------#
  38.             centre = dis//2#
  39.             radius = 4     #-----------------------------------------------#
  40.             circleMiddle = (i*dis+centre-radius,j*dis+9)                   #
  41.             circleMiddle2 = (i*dis + dis -radius*2, j*dis+10)              #
  42.             pygame.draw.circle(surface, (0,0,0), circleMiddle, radius)     #
  43.             pygame.draw.circle(surface, (0,0,0), circleMiddle2, radius)    #
  44. #--------------------------------------------------------------------------#    
  45. class snake():  #
  46.     body = []   #
  47.     turns = {}  #
  48. #---------------#----------------------------------------------------------------------------#  
  49.     def __init__(self, color, pos): #--------------------------------------------------------#
  50.         #pos is given as coordinates on the grid ex (1,5)------------------------------------#
  51.         self.color = color   #---------------------------------------------------------------#
  52.         self.head = cube(pos)#------------#
  53.         self.body.append(self.head)       #
  54.         self.dirnx = 0       #------------#
  55.         self.dirny = 1       #
  56. #----------------------------#
  57.     def move(self): #-----------------------------------#
  58.         for event in pygame.event.get(): #--------------#
  59.             if event.type == pygame.QUIT: #-------------#
  60.                 pygame.quit() #-------------------------#
  61.             keys = pygame.key.get_pressed()             #
  62. #-------------------------------------------------------#
  63.             for key in keys:#-------------------------------------------------#
  64.                 if keys[pygame.K_LEFT]:#--------------------------------------#
  65.                     self.dirnx = -1#------------------------------------------#
  66.                     self.dirny = 0 #------------------------------------------#
  67.                     self.turns[self.head.pos[:]] = [self.dirnx,self.dirny]#---#
  68.                 elif keys[pygame.K_RIGHT]: #----------------------------------#
  69.                     self.dirnx = 1 #------------------------------------------#
  70.                     self.dirny = 0 #------------------------------------------#
  71.                     self.turns[self.head.pos[:]] = [self.dirnx,self.dirny]#---#
  72.                 elif keys[pygame.K_UP]:#--------------------------------------#
  73.                     self.dirny = -1#------------------------------------------#
  74.                     self.dirnx = 0 #------------------------------------------#
  75.                     self.turns[self.head.pos[:]] = [self.dirnx,self.dirny]#---#
  76.                 elif keys[pygame.K_DOWN]:   #---------------------------------#
  77.                     self.dirny = 1 #--------#
  78.                     self.dirnx = 0 #------------------------------------------------------------------#
  79.                     self.turns[self.head.pos[:]] = [self.dirnx,self.dirny]                            #
  80. #-----------------------------------------------#-----------------------------------------------------#  
  81.         for i, c in enumerate(self.body): #-----#
  82.             p = c.pos[:] #----------------------#
  83.             if p in self.turns: #---------------#
  84.                 turn = self.turns[p] #----------#
  85.                 c.move(turn[0], turn[1]) #------#
  86.                 if i == len(self.body)-1: #-----#
  87.                     self.turns.pop(p) #---------#
  88.             else: #-----------------------------#
  89.                 c.move(c.dirnx,c.dirny) #-------#
  90.                                                 #
  91. #-----------------------------------------------#
  92.     def reset(self,pos): #------------#
  93.         self.head = cube(pos) #-------#
  94.         self.body = [] #--------------#
  95.         self.body.append(self.head)   #
  96.         self.turns = {}  #------------#
  97.         self.dirnx = 0   #
  98.         self.dirny = 1   #
  99. #------------------------#
  100.     def addCube(self):   #----------------------------------------#
  101.         tail = self.body[-1] #------------------------------------#
  102.         dx, dy = tail.dirnx, tail.dirny                           #
  103. #-----------------------------------------------------------------#
  104.         if dx == 1 and dy == 0: #------------------------------------------------------------#
  105.             self.body.append(cube((tail.pos[0]-1,tail.pos[1]))) #----------------------------#
  106.         elif dx == -1 and dy == 0:    #------------------------------------------------------#
  107.             self.body.append(cube((tail.pos[0]+1,tail.pos[1]))) #----------------------------#
  108.         elif dx == 0 and dy == 1:     #------------------------------------------------------#
  109.             self.body.append(cube((tail.pos[0],tail.pos[1]-1))) #----------------------------#
  110.         elif dx == 0 and dy == -1:    #------------------------------------------------------#
  111.             self.body.append(cube((tail.pos[0],tail.pos[1]+1))) #----------------------------#
  112.         self.body[-1].dirnx = dx      #------------------------------------------------------#
  113.         self.body[-1].dirny = dy      #
  114. #-------------------------------------#
  115.     def draw(self, surface): #--------------#--------------------+
  116.         for i,c in enumerate(self.body):    #----------------------------+
  117.             if i == 0: #--------------#-----#--------------------------------------------------------+
  118.                 c.draw(surface, True) #
  119.             else:  #------------------#
  120.                 c.draw(surface)       #
  121. #-------------------------------------#
  122. def redrawWindow():
  123.     global win #----------------------------#
  124.     win.fill((0,0,0)) #---------------------#
  125.     drawGrid(width, rows, win) #------------#
  126.     s.draw(win) #---------------------------#
  127.     snack.draw(win) #-----------------------#
  128.     pygame.display.update() #---------------#
  129.     pass #----------------------------------#
  130. #-------------------------------------------#
  131. #--------------------------------------------------#
  132. def drawGrid(w, rows, surface):#-------------------#
  133.     sizeBtwn = w // rows       #-------------------#
  134.     x = 0 #--------------------#
  135.     y = 0 #-------------------------------------------------------#
  136.     for l in range(rows): #---------------------------------------#
  137.         x = x + sizeBtwn #----------------------------------------#
  138.         y = y +sizeBtwn #-----------------------------------------#
  139.         pygame.draw.line(surface, ("red"), (x, 0),(x,w))          #
  140.         pygame.draw.line(surface, ("blue"), (0, y),(w,y))         #
  141. #-----------------------------------------------------------------#
  142. def randomSnack(rows, item): #------------------------------------------#
  143.     positions = item.body #---------------------------------------------#
  144.     while True: #-------------------------------------------------------#
  145.         x = random.randrange(1,rows-1) #--------------------------------#
  146.         y = random.randrange(1,rows-1) #--------------------------------#
  147.         if len(list(filter(lambda z:z.pos == (x,y), positions))) > 0:   #
  148.                continue     #-------------------------------------------#
  149.         else: #-------------#
  150.                break #
  151.     return (x,y)     #
  152. #--------------------#
  153. #---------------------------------------------------------------#
  154. def main(): #---------------------------------------------------#
  155.     global s, snack, win #--------------------------------------#
  156.     win = pygame.display.set_mode((width,height)) #-------------#
  157.     s = snake((255,0,0), (10,10)) #-----------------------------#
  158.     s.addCube() #-----------------------------------------------#
  159.     snack = cube(randomSnack(rows,s), color=("#ffffff"))        #
  160.     flag = True  #---------------------#------------------------#
  161.     clock = pygame.time.Clock()        #
  162. #--------------------------------------#    
  163.     while flag: #--------------------------------------------#----------------------------------------------------------------------------+
  164.         pygame.time.delay(50) #-------------------------------#----------------------+
  165.         clock.tick(10) #---------------------------------------#---------------------------------------------------------+
  166.         s.move()#-----------------------------------------------#-----------------------------------+
  167.         headPos = s.head.pos #-----------------------------------#---------------------------------------------------------------------------------------------+
  168.         if headPos[0] >= 20 or headPos[0] < 0 or headPos[1] >= 20 or headPos[1] < 0:#---------------------------------+
  169.             print("Score:", len(s.body)) #-------------------------#----------------------#
  170.             s.reset((10, 10)) #-------------------------------------#------------------------------------+
  171.         if s.body[0].pos == snack.pos: #-----------------------------#-------------------+------+--------------------------+------------------------+
  172.             s.addCube() #---------------------------------------------#--------------------------------------------------
  173.             snack = cube(randomSnack(rows,s), color=(0,255,0)) #-------#------+-------------------+---------------+          
  174.         for x in range(len(s.body)):  #---------------------------------#----------------+-------------+
  175.             if s.body[x].pos in list(map(lambda z:z.pos,s.body[x+1:])):  #
  176.                 print("Score:", len(s.body))     #------------------------#
  177.                 s.reset((10,10)) #---------------#
  178.                 break#---#-------#                  
  179.         redrawWindow()  #
  180. main()#---------#------#
  181. #--------------#
  182. ##+++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++##
  183. ##=================================================================================================================================================##
  184. ##-------------------------------------------------------------------------------------------------------------------------------------------------##
  185. ##                                                                                                                                                 ##
  186. ##                                                                                        ##   //                                                  ##
  187. ##----+-#########\   ########  -+#######\\-- +##########   #######\\        /#######\\    ##  //     ######---+-##      ##       /#######\\        ##
  188. ##      ##-          ##|--|##    ##-     ##   ##           ##-     ##      ||-----        ## //        ##       ##----+-##      ||-----         +--##
  189. ##      ##           ##|  |##  -+##    +--##  #######      ##       ##  -- \\#######\\    ## \\        ##       ##      ##------\\#######\\-------+##
  190. ##      ##-        +-##|--|##    ##-     ##   ##           ##-     ##         ------||    ##  \\       ##       ##      ##         ------||--------##
  191. ##      #########/   ########  -#######//-  -+##########   #######//       \\######//     ##   \\    ######---+-#####   #####   \\######//------+--##
  192. ##                                                                                                                                                 ##
  193. ##                                                          https://CodedSkills.net                                                                ##
  194. ##-------------------------------------------------------------------------------------------------------------------------------------------------##
  195. ##=================================================================================================================================================##
  196. ##+++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++##
  197.  
  198.  
  199. ##+++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++##
  200. ##=======================================================================================================================================================================================##
  201. ##---------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------##
  202. ##                                                                                                                                                                                       ##
  203. ##           ###       #######\    #######\       #######       ########     ##  ##     ########     ########       #######     ########       ####        ##         /#######\\         ##
  204. ##          #-##            //          //        \   //    =      ##        ##  ##        ##        ##|--|##       ##  //         ##         //  \\       ##        ||-                 ##
  205. ##            ##          ##\\        ##\\           //            ##        ##  ##        ##        ##|  |##       ###\\          ##        //====\\      ##        \\#######\\         ##
  206. ##            ##            //          //          //             ##        ##  ##        ##        ##|--|##       ##  \\         ##       //      \\     ##                 ||         ##
  207. ##       ##########   ########--  ########--       //       =      ##         ####         ##        ########       ##   \\     ########   //        \\    #######   \\######//          ##
  208. ##                                                                                                                                                                                       ##
  209. ##                                                                         https://1337tutorials.net                                                                                     ##
  210. ##---------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------##
  211. ##=======================================================================================================================================================================================##
  212. ##+++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++##
  213.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement