pygame small game development - greedy snake

Copyright statement: originality is not easy, plagiarism and reprinting are prohibited in this article, and infringement must be investigated!

1. Development Environment & Requirements Analysis

Development environment: Windows10 Python3.6.4
Third-party library: Pygame1.9.6
IDE: PyCharm/Sublime Text

Requirements analysis:

  • UI interface
  • speed, game score
  • game logic
  • Snake direction, food
  • grid lines, background color

2. Functional modules

Import of third-party libraries:

import random
import sys
import time
import pygame
from pygame.locals import *
from collections import deque

Game initialization and global variable definition:

SCREEN_WIDTH = 600      # 屏幕宽度
SCREEN_HEIGHT = 480     # 屏幕高度
SIZE = 20               # 小方格大小
LINE_WIDTH = 1          # 网格线宽度

# 游戏区域的坐标范围
SCOPE_X = (0, SCREEN_WIDTH // SIZE - 1)
SCOPE_Y = (2, SCREEN_HEIGHT // SIZE - 1)

# 食物的分值及颜色
FOOD_STYLE_LIST = [(10, (255, 100, 100)), (20, (100, 255, 100)), (30, (100, 100, 255))]

LIGHT = (100, 100, 100)
DARK = (200, 200, 200)      # 蛇的颜色
BLACK = (0, 0, 0)           # 网格线颜色
RED = (200, 30, 30)         # 红色,GAME OVER 的字体颜色
BGCOLOR = (40, 40, 60)      # 背景色

Initialize snake:

# 初始化蛇
def init_snake():
    snake = deque()
    snake.append((2, SCOPE_Y[0]))
    snake.append((1, SCOPE_Y[0]))
    snake.append((0, SCOPE_Y[0]))
    return snake

Food Creation:

def create_food(snake):
    food_x = random.randint(SCOPE_X[0], SCOPE_X[1])
    food_y = random.randint(SCOPE_Y[0], SCOPE_Y[1])
    while (food_x, food_y) in snake:
        # 如果食物出现在蛇身上,则重来
        food_x = random.randint(SCOPE_X[0], SCOPE_X[1])
        food_y = random.randint(SCOPE_Y[0], SCOPE_Y[1])
    return food_x, food_y

background color:

# 填充背景色
screen.fill(BGCOLOR)

Gridlines:

# 画网格线 竖线
for x in range(SIZE, SCREEN_WIDTH, SIZE):
    pygame.draw.line(screen, BLACK, (x, SCOPE_Y[0] * SIZE), (x, SCREEN_HEIGHT), LINE_WIDTH)

# 画网格线 横线
for y in range(SCOPE_Y[0] * SIZE, SCREEN_HEIGHT, SIZE):
    pygame.draw.line(screen, BLACK, (0, y), (SCREEN_WIDTH, y), LINE_WIDTH)

Game sprite drawing:

# 画食物
if not game_over:
    # 避免 GAME OVER 的时候把 GAME OVER 的字给遮住了
    pygame.draw.rect(screen, food_style[1], (food[0] * SIZE, food[1] * SIZE, SIZE, SIZE), 0)

# 画蛇
for s in snake:
    pygame.draw.rect(screen, DARK, (s[0] * SIZE + LINE_WIDTH, s[1] * SIZE + LINE_WIDTH,
                                    SIZE - LINE_WIDTH * 2, SIZE - LINE_WIDTH * 2), 0)

print_text(screen, font1, 30, 7, f'速度: {
      
      score//100}')
print_text(screen, font1, 450, 7, f'得分: {
      
      score}')

The main program runs:

def main():
    pygame.init()
    screen = pygame.display.set_mode((SCREEN_WIDTH, SCREEN_HEIGHT))
    pygame.display.set_caption('贪吃蛇')

    font1 = pygame.font.SysFont('SimHei', 24)  # 得分的字体
    font2 = pygame.font.Font(None, 72)  # GAME OVER 的字体
    fwidth, fheight = font2.size('GAME OVER')



3. Game effect

insert image description here

4. Download the complete source code

pygame game development source code download:

  • Follow my original WeChat public account : " Xiaohong Xingkong Technology ", reply " Greedy Snake " to get the source code

5. Author Info

Author: Xiaohong's Fishing Daily, Goal: Make programming more interesting!

Original WeChat public account: " Xiaohong Xingkong Technology ", focusing on algorithms, crawlers, websites, game development, data analysis, natural language processing, AI, etc., looking forward to your attention, let us grow and code together!

Copyright Note: This article prohibits plagiarism and reprinting, and infringement must be investigated!

Guess you like

Origin blog.csdn.net/qq_44000141/article/details/122148019
Recommended