Snake's algorithm is still relatively simple, the movement of the snake I was by adding a head block, and then determine which direction should be added to the head of the snake, add the end of the tail to delete the line, if you eat food will not delete the tail of the snake.
Just a greedy snake only need 70 lines of code around it, and then added a score, failed to re-play, pause function As a result, the code is now in a mess.
There must be a better way to re-play, I wrote too messy. Ask the great God for advice. Because the use of the grid, to determine the time to eat is judged by the scope, sometimes some deviation ...
Code:
1 #-*-coding:utf-8-*-2 import pygame, sys, random, time 3 from pygame.locals import * 4 5 pygame.init () 6 font = Pygame.font.Font (U "C:\\windows\\fonts\\msyh.ttf", 7 Mainclock = Pygame.time.Clock () 8 wsurface = pygame.display.se T_mode ((800,600), 0,32) 9 pygame.display.set_caption ("my_snake~") Ten Len = + Snakerect = [] for I in range (10,1 3): Snakerect.append (Pygame. Rect (i * (Len), and Len, Len)) * food = Pygame. Rect (len), ten * (Len), Len, len)-ml = false, Mr = True, mu = false, MD = FALSE score = 0 Black = (0, 0, 0) green = (0, 255, 0) White = (255, 255, 255) from global fpsclock Fpsclock = Pygame.time.Clock ( ) ##################################################### def judge (): If snakerect[0].left-15 <= food.le FT <= snakerect[0].left + snakerect[0].top-15 <= food.top <= snakerect[0].top + 15:29 return T Rue def Judge2 (A, B): If A.left-15 <= B.left <= a.left + a.top-15 <= b.top <= a.top + 15:32 return True def checkforkeypress (): 34 # Checkforquit () for event in Pygame.event.get ([KEYDOWN, KEYUP]): if Event.type = = keyup:37 Continue Event.key-None 40 #################################################### # # # Flagg = True = 8 + true:44 for event in Pygame.event.get (): if Event.type = = QU It:46 Pygame. QUIT () sys.exit () Event.type = = keydown:49 if Event.key = = k_r:50 Snakerect = [] Wuyi for I in Range (10,13): Snakerect.append (Pygame. Rect (i * (Len),, Len, Len)) is food = Pygame. Rect (len), ten * (Len), Len, len), ml = False, Mr = True 56 Mu = False The MD = False 58 Score = 0 Flagg = True Speed = 8 if Event.key = = k_p:62 Wsurface.fill (black) Text_surface1 = Font.render (' pause! ', True, (0, 0, 255)) 64 Wsurface.blit (Text_surface1, (Ten)), while checkforkeypress () = = none:66 Pygame . Display.update () Fpsclock.tick () Event.key = = K_left and Mr = = false:69 ML = True Mr = False 73 Mu = false + MD = False if Event.key = = k_right and ml = = false:74 ml = False all Mr = True M U = False + MD = False if Event.key = = k_up and MD = = false:79 ml = False Mr = False Bayi Mu = True + + MD = False if Event.key = = K _down and mu = false:84 ml = false----Mr = False--MU = false + MD = True 88 Head = Pygame. Rect (snakerect[0].left,snakerect[0].top,snakerect[0].width,snakerect[0].height) if Flagg = = false:90 Conti nue if ml = = true:92 Head.right = head.left-1 if Mr = = true:94 Head.left = head.right + 1 if mu = = true:96 Head.bottom = head.top-1 if MD = = true:98 Head.top = head.bottom + 1 Snakerect.insert (0, head) #判断失败和重新游戏101 if head.right < 0 or Head.left > or Head.bottom < 0 or Head.top > Snakerect[0] in snakerect[1:]:102 wsurface.fill (white) 103 Text_surface2 = Font.re NDEr (' Press R to restart! ', True, (0, 0, 255)) 104 Wsurface.blit (Text_surface2, ()) Pygam E.display.update () 106 while checkforkeypress () = = none:107 Pygame.display.update () 108 FPSC Lock.tiCK () 109 break110 flagg = False111 continue112 Flagg = True113 If Judge (): 114 Score = score + 10115 speed = speed + 1116 while true:117 flag = True118 fo Od.left = Random.randrange (10,800) 119 food.top = Random.randrange (10,600) for temp In snakerect:121 if judge2 (food, temp): 122 flag = False123 if flag = = True : 124 break125 else:126 Snakerect.pop () 127 Wsurface.fill (black)-for-I in range (Len ( Snakerect): 129 pygame.draw.rect (Wsurface,green,snakerect[i]) (pygame.draw.rect) 131 Text_surface = Font.render (u "score:" + str (score), True, (0, 0, 255)) Wsurface.blit py (text_surface, (+)) Game.display.update () 134 Mainclock.tick (speed) 135 136 137 138 139
:
Python Implements Snake