The effect of eating beans

Just move the snakehead code at the end of the code to add a snakehead and dot coordinate judgment

        if snake_x == bean_x and snake_y == bean_y:
            bean_x,bean_y = get_bean_pos()
Copy the code

Beans now disappear when the snake head touches them

The lengthening of the snake body

Previously, we only set one coordinate for the snake body. Since the snake body will grow longer and longer, of course, it is more appropriate to use an array to store the body, so we made some modifications to the variable of the snake body.

After initializing body_x and body_y, add this coordinate to an array called body_arr

body_arr = [(body_x,body_y)]
Copy the code

Change the “PyGame.draw. rect” that draws the snake body and use the traversal group to draw it

For body_x,body_y in body_arr: pygame.draw. Rect (screen,yellow,[body_x-20,body_y-20,40,40],5)Copy the code

The movement of the snake’s body should also be changed to a cycle, with the latter part of the body taking the same position as the previous one

body_arr = [(snake_x,snake_y)]+body_arr[:-1]
Copy the code

We can do this by backing up the position of the last segment of the snake

last_body_x,last_body_y = body_arr[-1]
Copy the code

If you get dots, add the last section of this backup to the end of the body array

body_arr.append((last_body_x,last_body_y))
Copy the code

Snake self – collision game failed to judge

Use a tag to indicate the game state

Game_state = 1 # Game state 1. Normal 2. Said failureCopy the code

Change the move judgment code to tag the judgment to

if game_state == 1 and pygame.time.get_ticks() >= ticks:
Copy the code

Add a head and body, body and body overlap judgment at the end of the move

For body_x,body_y in body_arr: # if snake_x == body_x and snake_y == body_y: game_state = 2 break for i in range(len(body_arr)-1): for j in range(i+1,len(body_arr)): If body_arr[I][0] == body_arr[j][0] and body_arr[I][1] == body_arr[j][1]Copy the code

And then finally the grid line

Finally, attach the complete code

# -*- coding=utf-8 -*- import random import pygame from pygame.locals import KEYDOWN,K_LEFT,K_RIGHT,K_UP,K_DOWN Pygame.init () Screencaption = PyGame.display. Set_mode ((400,400)) screencaption = PyGame.display. Set_mode ((400,400) Snake_y = random.randint(0,9)*40+20 game_state = 1 # game state 1. Def get_bean_pos(): Return random. Randint (0,9)*40+20,random. Randint (0,9)*40+20 yellow = 255,255,0 bean_x,bean_y = get_bean_pos() diff_ticks = 300 # move a snake head event, Ticks = pygame.time.get_ticks() ticks += diff_ticks #dire = random. Randint (0,3) # if snake_x = pygame.time.get_ticks() ticks += diff_ticks #dire = random < 200: if I have a word for every word, I have a word for every word. < 200: if I have a word for every word, I have a word for every word. < 200: if I have a word for every word, I have a word for every word. Body_x = snake_x + 40 else: # If snake_y > 200: body_x = snake_x body_y -= 40 else: # If snake_y > 200: body_x = snake_x body_y -= 40 else: If snake_x-40 > 0: body_x = snake_x body_y += 40 else: If snake_y > 200: body_x = snake_x body_y -= 40 else: body_x = snake_x body_y += 40 body_arr = [(body_x,body_y)] def set_snake_next_pos(snake_x, snake_y): if dire == 0: if snake_x - 40 > 0: snake_x -= 40 if dire == 1: if snake_x + 40 < 400: snake_x += 40 if dire == 2: if snake_y - 40 > 0: snake_y -= 40 if dire == 3: if snake_y + 40 < 400: snake_y += 40 return snake_x,snake_y while True: for event in pygame.event.get(): if event.type == pygame.QUIT: pygame.quit() exit() if event.type == KEYDOWN: if event.key == K_LEFT: if dire! =0 and dire! = 1 and snake_x - > 0:40 # and the current direction is not the same direction or opposite direction and can be left dire = 0 if the event. The key = = K_RIGHT: if dire! =0 and dire! Snake_x =1 and snake_x =1 and snake_x =1 and snake_x = 2 < 100: =2 and dire! = 3 and snake_y - > 0:40 # and the current direction is not the same direction or opposite direction and can move up dire = 2 if the event. The key = = K_DOWN: if dire! =2 and dire! 40 < = 3 and snake_y + 400 # and the current direction is not the same or opposite direction and can move down dire = 3 screen. The fill () (55,66,44) # # set the background color for the x in the range (0400, 40). Draw. Line (screen,(255,255,255),(x,0),(x,400),1) #for y in range(0,400,40): # pygame. The draw. The line (screen, (255255255), (0, y), (400, y), 1) pygame. The draw the circle (screen, yellow, [snake_x snake_y], 20, 2) the for body_x,body_y in body_arr: Pygame. The draw. The rect (screen, yellow, [body_x - 20, 20,40,40 body_y -], 5) pygame. The draw the circle (screen, yellow, [bean_x bean_y], 10, 10) of the if  game_state == 2: Myfont = pygame.font.Font(None,30) White = 255,255,255 textImage = myfont. Render ("Game over", True, white) screen.blit(textImage, (160,190)) pygame.display.update() # If game_state == 1 and pygame.time.get_ticks() >= ticks: last_body_x,last_body_y = body_arr[-1] body_arr = [(snake_x,snake_y)]+body_arr[:-1] snake_x,snake_y = set_snake_next_pos(snake_x,snake_y) ticks += diff_ticks #if snake_x == bean_x and snake_y == bean_y: # bean_x,bean_y = get_bean_pos() # body_arr.append((last_body_x,last_body_y)) for body_x,body_y in body_arr: If snake_x == body_x and snake_y == body_y: # break for I in range(len(body_arr)-1): for j in range(i+1,len(body_arr)): if body_arr[i][0] == body_arr[j][0] and body_arr[i][1] == body_arr[j][1]: Game_state = 2 break if snake_x == bean_x and snake_y == bean_y: bean_x,bean_y = get_bean_pos() body_arr.append((last_body_x,last_body_y))Copy the code

Learn more about Python by programming at your fingertips.