Make a game at home during the epidemic~ Snake - the first real mobile game [python]

Snake's history is much older than we think, the earliest prototype was born in 1976. (Altair 8800 was only released in 1975...)

But this first version was an arcade. The game is called Blockade, it's a two-player game, and the publisher is Gremlin. The company closed in 1984.

The gameplay is quite special. The snake will not move forward, but will only keep its tail still and its head getting longer and longer. Well, in fact, its setting is not a snake, but two villains walking forward and building a wall behind them... The rule is that whoever hits the wall first loses.

The game was quite successful, so it promoted a lot of imitators. , including the famous Atari, they developed a game called Surround in 1978, which is basically a ported and improved version of the Atari 2600 platform of blockade.

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

insert image description here
Please add image description

main content

  1. Create a game window
  2. Drawing greedy snake with food
  3. snake eating food

Snake chessboard model

Please add image description

Please add image description

import module

import pygame
import random
import copy

Create a game window

  • Game initialization
pygame.init()
clock = pygame.time.Clock()  # 设置游戏时钟
pygame.display.set_caption("贪吃蛇-解答、源码、相关资料可私信我")  # 初始化标题
screen = pygame.display.set_mode((500, 500))  # 初始化窗口 窗体的大小为 500  500
  • Initialize the position of the snake snake length 10 10 (XY coordinates of the snake)
snake_list = [[10, 10]]

First, set a running direction of the snake and
then judge the keyboard event to determine the running direction of the
snake. The snake can run.
Next, the snake eats food to increase its length and does not eat food and displays it in different positions

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 the game loop
running = True
while running:
    # 游戏时钟 刷新频率
    clock.tick(20)
  • Fill background with 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)

Please add image description
drawing snake snake

snake_rect = []
for pos in snake_list:
    # 1.7.1 绘制蛇的身子
    snake_rect.append(pygame.draw.circle(screen, [255, 0, 0], pos, 5, 0))

Please add image description
Drawing greedy snake with food

  • Get the length of the snake, move the body of the snake
pos = len(snake_list) - 1
while pos > 0:
    snake_list[pos] = copy.deepcopy(snake_list[pos - 1])
    pos -= 1
  • Change the snake head 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 Control Mobile Positions
for event in pygame.event.get():
    # print(event)
    # 判断按下的按键
    if event.type == pygame.KEYDOWN:
        # 上键
        if event.key == pygame.K_UP:
            move_up = True
            move_down = False
            move_left = False
            move_right = False
        # 下键
        if event.key == pygame.K_DOWN:
            move_up = False
            move_down = True
            move_left = False
            move_right = False
        # 左键
        if event.key == pygame.K_LEFT:
            move_up = False
            move_down = False
            move_left = True
            move_right = False
        # 右键
        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 body of the snake
pos = len(snake_list) - 1
while pos > 0:
    snake_list[pos] = copy.deepcopy(snake_list[pos - 1])
    pos -= 1

Please add image description

snake eating food

  • Collision detection if snake eats food
if food_rect.collidepoint(pos):
    # 贪吃蛇吃掉食物
    snake_list.append(food_point)
    # 重置食物位置
    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()

Please add image description
insert image description here

Finish!

Guess you like

Origin blog.csdn.net/m0_67575344/article/details/123585722