The history of snake eating is much longer than we thought , The earliest prototype was born in 1976 year .(Altair 8800 It's just 1975 Issued in ……)

But the earliest version was an arcade . This game is called Blockade, It's a double game , Publisher is Gremlin. This company 1984 It's closed in .

The playing method is quite special , Snakes don't move forward , And just keep your tail still , The head is getting longer and longer . Well, actually, its setting is not a snake , Instead, two little people built a wall behind them as they walked forward …… The rule is who hits the wall first and who loses .

The game was quite successful , So it promotes many imitators . Including the famous Atari, They are 1978 In, a product called surround Game of , Basically blockade of Atari
2600 Platform migration and improved version .

So let's make a pixel version of greedy snake today ~

<> primary coverage

* Create game window
* Draw greedy snakes and food
* Snakes eat food
<> Chessboard model of greedy snake

<> Import module
import pygame import random import copy
Create game window

* Game initialization pygame.init() clock = pygame.time.Clock() # Set game clock
pygame.display.set_caption(" Greedy snake - answer , Source code , Relevant information can be sent to me in private ") # Initialize title screen =
pygame.display.set_mode((500, 500)) # Initialization window The size of the form is 500 500
* Initialize the location of the snake Snake length 10 10( Serpentine X Y coordinate ) snake_list = [[10, 10]]
First, set a running direction of the snake
Next, judge whether the keyboard event is determining the running direction of the snake
The snake can run
next , Snakes eat food to increase their length and do not eat food in different locations

<> Initial snake direction
move_up = False move_down = False move_left = False move_right = True
* Initialize the location of the food x = random.randint(10, 490) y = random.randint(10, 490)
food_point = [x, y]
* Start game cycle running = True while running: # Game clock refresh frequency clock.tick(20)
* The fill background is white screen.fill([255, 255, 255])
* Draw background for x in range(0, 501, 10): pygame.draw.line(screen, (195, 197, 199),
(x, 0), (x, 500), 1) pygame.draw.line(screen, (195, 197, 199), (0, x), (500,
x), 1) food_rect = pygame.draw.circle(screen, [255, 0, 0], food_point, 15, 0)

Draw snake
snake_rect = [] for pos in snake_list: # 1.7.1 Draw the body of a snake
snake_rect.append(pygame.draw.circle(screen, [255, 0, 0], pos, 5, 0))

Draw greedy snakes and food

* Get the length of the snake , Move the snake's body pos = len(snake_list) - 1 while pos > 0: snake_list[pos] =
copy.deepcopy(snake_list[pos - 1]) pos -= 1
* Change snakehead position if move_up: snake_list[pos][1] -= 10 if snake_list[pos][1] < 0:
snake_list[pos][1] = 500 if move_down: snake_list[pos][1] += 10 if
snake_list[pos][1] > 500: snake_list[pos][1] = 0 if move_left:
snake_list[pos][0] -= 10 if snake_list[pos][0] < 0: snake_list[pos][0] = 500 if
move_right: snake_list[pos][0] += 10 if snake_list[pos][0] > 500:
snake_list[pos][0] = 0
* Keyboard controlled mobile position for event in pygame.event.get(): # print(event) # Judge the pressed key if
event.type == pygame.KEYDOWN: # Up key if event.key == pygame.K_UP: move_up = True
move_down = False move_left = False move_right = False # Down key if event.key ==
pygame.K_DOWN: move_up = False move_down = True move_left = False move_right =
False # Left key if event.key == pygame.K_LEFT: move_up = False move_down = False
move_left = True move_right = False # Right click if event.key == pygame.K_RIGHT:
move_up = False move_down = False move_left = False move_right = True
* Get the length of the snake , Move the snake's body pos = len(snake_list) - 1 while pos > 0: snake_list[pos] =
copy.deepcopy(snake_list[pos - 1]) pos -= 1

<> Snakes eat food

* collision detection If the snake eats the food if food_rect.collidepoint(pos): # Greedy snakes eat food
snake_list.append(food_point) # Reset food location food_point = [random.randint(10, 490),
random.randint(10, 490)] food_rect = pygame.draw.circle(screen, [255, 0, 0],
food_point, 15, 0) break
* If the snake eats itself head_rect = snake_rect[0] count = len(snake_rect) while count >
1: if head_rect.colliderect(snake_rect[count - 1]): running = False count -= 1
pygame.display.update()

<> complete !

Technology
©2019-2020 Toolsou All rights reserved,
C++ of string of compare usage MySQL Basics Commonly used sentence ( Add / delete / modify query )C Language of a Gobang game implementation QT5.9 Use of learning notes QSqlQuery Method of Qt Getting Started tutorial 【 Basic controls 】QCalendarWidget calendar control java Polymorphic array of web Front end signature plug-in _signature_pad Plug in implements electronic signature function centos7 install RabbitMqspringboot use redis Experiment 4 Automated test tools - software test