c0d3dsk1lls

Snakes-v-2-0 CodedSkills.net

Aug 7th, 2022
75
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 12.14 KB | None | 0 0
  1.  
  2. ##+++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++##
  3. ##=======================================================================================================================================================================================##
  4. ##----------------------CODEDSKILLS.NET--------------------------------------------------------------------------------------------------------------------------------------------------##
  5. #grid geometry manager
  6. #------------------------#
  7. from tkinter import *    #
  8. import random  #---------#
  9. #--------------#
  10. #+++++++++++++++++++++++++++++++++++++++++++++++++++
  11. #------------------#
  12. GAME_WIDTH = 1000  #
  13. GAME_HEIGHT = 500  #
  14. SPEED = 100 #---#--#
  15. SPACE_SIZE = 50 #
  16. BODY_PARTS = 15 #---#
  17. SNAKE_COLOR = "red" #
  18. FOOD_COLOR = "aqua" #---------#
  19. BACKGROUND_COLOR = "black"    #
  20. #-----------------------------#
  21. #+++++++++++++++++++++++++++++++++++++++++++++++++++
  22. #------------------------------------#
  23. class Snake: #-----------------------#
  24.     def __init__(self): #------------#
  25.         self.body_size = BODY_PARTS  #
  26.         self.coordinates = [] #------#
  27.         self.squares = [] #-----------------#
  28.         for i in range(0, BODY_PARTS): #----#
  29.             self.coordinates.append([0, 0]) #---------------------------------------------------------#
  30.         for x, y in self.coordinates: #-----#---------------------------------------------------------#
  31.             square = canvas.create_rectangle(x, y, x + 50, y + 50, fill=SNAKE_COLOR, tag="snake")     #
  32.             self.squares.append(square)     #---------------------------------------------------------#
  33. #-------------------------------------------#  
  34. #+++++++++++++++++++++++++++++++++++++++++++++++++++
  35. #------------------------------------------------------------------------#
  36. class Food: #------------------------------------------------------------#
  37.     def __init__(self): #------------------------------------------------#
  38.         x = random.randint(0, (GAME_WIDTH / SPACE_SIZE)-1) * SPACE_SIZE  #
  39.         y = random.randint(0, (GAME_HEIGHT / SPACE_SIZE)-1) * SPACE_SIZE #-------------------------#
  40.         self.coordinates = [x, y] #----------------------------------------------------------------#
  41.         canvas.create_oval(x, y, x + SPACE_SIZE, y + SPACE_SIZE, fill=FOOD_COLOR, tag="food"  )    #
  42. #--------------------------------------------------------------------------------------------------#
  43. #+++++++++++++++++++++++++++++++++++++++++++++++++++
  44. #---------------------------#--------#
  45. def next_turn(snake, food): #--------#
  46.     x, y = snake.coordinates[0]      #
  47.     if direction == "up":#-----------#
  48.         y -= SPACE_SIZE  #------#
  49.     elif direction == "down":   #
  50.         y += SPACE_SIZE  #------#
  51.     elif direction == "left":   #
  52.         x -= SPACE_SIZE  #------#
  53.     elif direction == "right":  #
  54.         x += SPACE_SIZE  #------#-------#
  55.     snake.coordinates.insert(0, (x, y)) #------------------------------------------------------#
  56.     square = canvas.create_rectangle(x, y, x + SPACE_SIZE, y + SPACE_SIZE, fill=SNAKE_COLOR)   #
  57.     snake.squares.insert(0, square) #--------------------------#-------------------------------#
  58.     if x == food.coordinates[0] and y == food.coordinates[1]:  #
  59.         global score #-----------------------------------------#
  60.         score += 1   #-----------------------------------#
  61.         label.config(text="Score: {}".format(score))     #
  62.         canvas.delete("food") #--------------------------#
  63.         food = Food() #-------#
  64.     else: #-----------#------------#
  65.         del snake.coordinates[-1] #------#
  66.         canvas.delete(snake.squares[-1]) #-------------#
  67.         del snake.squares[-1] #------------------------#
  68.     window.after(SPEED, next_turn, snake, food)        #
  69. #------------------------------------------------------#
  70. #+++++++++++++++++++++++++++++++++++++++++++++++++++
  71. #------------------------------------#
  72. def change_direction(new_direction): #
  73.     global direction #---------------#----#
  74.     if new_direction == 'left': #---------#
  75.         if direction != 'right': #--------#
  76.             direction = new_direction #---#
  77.     elif new_direction == 'right': #------#
  78.         if direction != 'left': #---------#
  79.             direction = new_direction #---#
  80.     elif new_direction == 'up': #---------#
  81.         if direction != 'down': #---------#
  82.             direction = new_direction #---#
  83.     elif new_direction == 'down': #-------#
  84.         if direction != 'up': #-----------#
  85.             direction = new_direction #---#
  86. #-----------------------------------------#
  87. #+++++++++++++++++++++++++++++++++++++++++++++++++++
  88. #--------------------------------#
  89. def check_collisions(): #--------#
  90.     x, y = snake.coordinates[0]  #
  91.     if x < 0 or x >= GAME_WIDTH: #
  92.         return True #---------------#
  93.     elif y < 0 or y >= GAME_HEIGHT: #
  94.         return True #---------------#-------#-------------#
  95.     for body_part in snake.coordinates[1:]: #-------------#
  96.         if x == body_part[0] and y == body_part[1]: #-----#
  97.             print("Game over") #--------------------------#
  98.             return True #----------#
  99.     return False #------#
  100. #-----------------------#
  101. #+++++++++++++++++++++++++++++++++++++++++++++++++++
  102. #----------------------#
  103. def game_over(): #-----#
  104.     canvas.delete(ALL) #------------------------------------------------#
  105.     canvas.create_text(canvas.winfo_width()/5, canvas.winfo_height()/5, #------------------------#
  106.                         font=("consolas",70),text="GAME OVER", fill="red", tag="gameover") #-----#
  107. #------------------------------------------------------------------------------------------------#
  108. #+++++++++++++++++++++++++++++++++++++++++++++++++++
  109. #----------------------------------#
  110. window = Tk() #--------------------#
  111. window.title("Snake game") #-------#
  112. window.resizable(True, True) #-----#
  113. score = 0 #--------#---------------#
  114. direction = "down" #--------------------------------------------------------#
  115. label = Label(window, text="Score:{}".format(score), font=("consolas", 40)) #
  116. label.pack() #--------------------------------------------------------------#------#
  117. canvas = Canvas(window, bg=BACKGROUND_COLOR, height=GAME_HEIGHT, width=GAME_WIDTH) #-------#
  118. canvas.pack() #-------#--------------------------------------------------------------------#
  119. window.update() #-----#
  120. #---------------------#
  121. #+++++++++++++++++++++++++++++++++++++++++++++++++++
  122. #----------------------------------------------#
  123. window_width = window.winfo_width() #----#-----#
  124. window_height = window.winfo_height() #---#----#
  125. screen_width = window.winfo_screenwidth()  #---#
  126. screen_height = window.winfo_screenheight() #--#
  127. x = int((screen_width/2) - (window_width/2)) #-#
  128. y = int((screen_height/2) - (window_height/2)) #-----------#
  129. window.geometry(f"{window_width}x{window_height}+{x}+{y}") #----#
  130. window.bind("<Left>", lambda event: change_direction("left"))   #
  131. window.bind("<Right>", lambda event: change_direction("right")) #
  132. window.bind("<Up>", lambda event: change_direction("up")) #-----#
  133. window.bind("<Down>", lambda event: change_direction("down"))   #
  134. snake = Snake() #-----------------------------------------------#
  135. food = Food() #--------#
  136. next_turn(snake, food) #
  137. window.mainloop() #----#
  138. #----------------------#
  139. ##+++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++##
  140. ##=================================================================================================================================================##
  141. ##-------------------------------------------------------------------------------------------------------------------------------------------------##
  142. ##                                                                                                                                                 ##
  143. ##                                                                                        ##   //                                                  ##
  144. ##----+-#########\   ########  -+#######\\-- +##########   #######\\        /#######\\    ##  //     ######---+-##      ##       /#######\\        ##
  145. ##      ##-          ##|--|##    ##-     ##   ##           ##-     ##      ||-----        ## //        ##       ##----+-##      ||-----         +--##
  146. ##      ##           ##|  |##  -+##    +--##  #######      ##       ##  -- \\#######\\    ## \\        ##       ##      ##------\\#######\\-------+##
  147. ##      ##-        +-##|--|##    ##-     ##   ##           ##-     ##         ------||    ##  \\       ##       ##      ##         ------||--------##
  148. ##      #########/   ########  -#######//-  -+##########   #######//       \\######//     ##   \\    ######---+-#####   #####   \\######//------+--##
  149. ##                                                                                                                                                 ##
  150. ##                                                          https://CodedSkills.net                                                                ##
  151. ##-------------------------------------------------------------------------------------------------------------------------------------------------##
  152. ##=================================================================================================================================================##
  153. ##+++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++##
  154.  
  155.  
  156. ##+++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++##
  157. ##=======================================================================================================================================================================================##
  158. ##---------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------##
  159. ##                                                                                                                                                                                       ##
  160. ##           ###       #######\    #######\       #######       ########     ##  ##     ########     ########       #######     ########       ####        ##         /#######\\         ##
  161. ##          #-##            //          //        \   //    =      ##        ##  ##        ##        ##|--|##       ##  //         ##         //  \\       ##        ||-                 ##
  162. ##            ##          ##\\        ##\\           //            ##        ##  ##        ##        ##|  |##       ###\\          ##        //====\\      ##        \\#######\\         ##
  163. ##            ##            //          //          //             ##        ##  ##        ##        ##|--|##       ##  \\         ##       //      \\     ##                 ||         ##
  164. ##       ##########   ########--  ########--       //       =      ##         ####         ##        ########       ##   \\     ########   //        \\    #######   \\######//          ##
  165. ##                                                                                                                                                                                       ##
  166. ##                                                                         https://1337tutorials.net                                                                                     ##
  167. ##---------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------##
  168. ##=======================================================================================================================================================================================##
  169. ##+++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++##
  170.  
  171.  
  172.  
  173.  
  174.  
  175.  
  176.  
Advertisement
Add Comment
Please, Sign In to add comment