Using python + pygame written Snake game

Because the python syntax is simple and easy to use, two days before want to be able to write a little game out with a python, on the Internet search, I found the pygame write this 2D game library. Learn a couple of days and then refer to some information on the Snake began to write this little game. After all, the beginning of the project it seems that this practice hand emmmmpython recommendation of python3 tutorial Liao Xuefeng, pygame pygame tutorial recommended eyes of the blog, pygame is a 2D game development library. Python + Pygame after a brief overview of the project can get started.

 

general idea:

1, the start of the game information display

2, map initialization

3, snake initialization

4, random food

5, to achieve a simple user input controls the direction of change of Snake

6, it is determined whether the snake eat food, if eat foods (same foods coordinates and the coordinates of the head), the length of the +1 snake itself, while Random new food. Otherwise it remains unchanged.

7, to determine whether the snake is encountered boundary, if you encounter a boundary to bring up the death information and scores.

 

(1) define the game as a whole:

cell_size is the size of the Snake, attention cell_size to be divisible by the window size.

 

Map of overall width:

int = map_width (windows_width / cell_size)
map_height = int (windows_height / cell_size)
overall color and direction defined:

White = (255, 255, 255)
Black = (0, 0, 0)
Gray = (230, 230, 230)
dark_gray = (40, 40, 40)
DarkGreen = (0, 155, 0)
Green = (0, 255, 0)
Red = (255, 0, 0)
Blue = (0, 0, 255)
dark_blue = (0,0, 139)


bg_color game background color = Black #

# define the direction of
the UP. 1 =
DOWN = 2
the LEFT. 3 =
. 4 = RIGHT

the hEAD Snake head # 0 = subscript
main functions:

1. Snake initialization:

snake_coords = [{ 'x': startx, 'y': starty}, # initial Snake
{ 'X': the startx -. 1, 'Y': startY},
{ 'X': the startx - 2, 'Y' : startY}]
2. painting Snake:

def draw_snake(screen, snake_coords):
for coord in snake_coords:
x = coord['x'] * cell_size
y = coord['y'] * cell_size
wormSegmentRect = pygame.Rect(x, y, cell_size, cell_size)
pygame.draw.rect(screen, dark_blue, wormSegmentRect)
wormInnerSegmentRect = pygame.Rect( #蛇身子里面的第二层亮绿色
x + 4, y + 4, cell_size - 8, cell_size - 8)
pygame.draw.rect(screen, blue, wormInnerSegmentRect)
3.移动贪吃蛇:

def move_snake(direction, snake_coords):
if direction == UP:
newHead = {'x': snake_coords[HEAD]['x'], 'y': snake_coords[HEAD]['y'] - 1}
elif direction == DOWN:
newHead = {'x': snake_coords[HEAD]['x'], 'y': snake_coords[HEAD]['y'] + 1}
elif direction == LEFT:
newHead = {'x': snake_coords[HEAD]['x'] - 1, 'y': snake_coords[HEAD]['y']}
elif direction == RIGHT:
newHead = {'x': snake_coords[HEAD]['x'] + 1, 'y': snake_coords[HEAD]['y']}

snake_coords.insert(0, newHead)
4.判断蛇是否死亡:

def snake_is_alive(snake_coords):
tag = True
if snake_coords[HEAD]['x'] == -1 or snake_coords[HEAD]['x'] == map_width or snake_coords[HEAD]['y'] == -1 or \
snake_coords[HEAD]['y'] == map_height:
tag = False # 蛇碰壁啦
for snake_body in snake_coords[1:]:
if snake_body['x'] == snake_coords[HEAD]['x'] and snake_body['y'] == snake_coords[HEAD]['y']:
tag = False # 蛇碰到自己身体啦
return tag
5.食物的初始化:

get_random_location DEF ():
return { 'X': the random.randint (0, map_width -. 1), 'Y': the random.randint (0, map_height -. 1)}
6. The snake is determined whether or not to eat foods:

def snake_is_eat_food (snake_coords, food): # If it is a list or dictionary, then modify the parameters of content within the function will affect the object functions in vitro.
snake_coords IF [the HEAD] [ 'X'] == Food [ 'X'] and snake_coords [the HEAD] [ 'Y'] == Food [ 'Y']:
Food [ 'X'] = the random.randint (0, map_width -. 1)
Food [ 'Y'] = the random.randint (0, map_height -. 1) physical position # reset
the else:
del snake_coords # eat if not physical, moves forward [-1], the tail portion of a cell delete the
main function here, behind specific ideas to talk about it, the first time is not very familiar with csdn.
--------------------- 

Guess you like

Origin www.cnblogs.com/ly570/p/10989732.html