kno 发表于 2022-3-5 20:58:51

贪吃蛇代码(简单易懂)

import pygame,sys,time,random,math
import collections
import copy
import time
screen_width,screen_height=(600,600)
rect_color=(255,255,255)
snake_color=(100,100,100)
food_color=(50,50,50)
bgcolor=(0,0,0)
direction='up'
last_direction='up'

snake=collections.deque()

def draw_screen(screen,rect_color):
    for y in range(30):
      for x in range(30):
            pygame.draw.rect(screen,rect_color,((20*y,20*x),(19,19)),0)

def init_snake(snake):
    snake.appendleft(pygame.Rect((80, 80),(19, 19)))
    snake.appendleft(pygame.Rect((80, 100), (19, 19)))
    snake.appendleft(pygame.Rect((80, 120), (19, 19)))
    return snake


def draw_snake(screen,snake_color,snake):
    snake_copy=copy.deepcopy(snake)
    while snake_copy:
      pygame.draw.rect(screen,snake_color,snake_copy.pop(),0)

def create_food(snake):
    food_x=random.randrange(0,600,20)
    food_y=random.randrange(0,600,20)

    food_rect=pygame.Rect(((food_x, food_y),(19, 19)))

    snake_copy = copy.deepcopy(snake)
    while snake_copy:
      invalid_food=snake_copy.pop()
      if food_rect==invalid_food:
            food_x = random.randrange(0, 600, 20)
            food_y = random.randrange(0, 600, 20)
            food_rect = pygame.Rect(((food_x, food_y), (19, 19)))
    return food_rect

def draw_food(screen,food_color,food_rect):
    pygame.draw.rect(screen,food_color,food_rect,0)

def snake_auto_move(snake,direction):
    if direction=='up':
      rect= copy.deepcopy(snake) #小心浅复制
      rect.top-=20
      snake.popleft()
      snake.append(rect)
    if direction == 'down':
      rect = copy.deepcopy(snake)
      rect.top += 20
      snake.popleft()
      snake.append(rect)
    if direction == 'right':
      rect = copy.deepcopy(snake)
      rect.left += 20
      snake.popleft()
      snake.append(rect)
    if direction == 'left':
      rect = copy.deepcopy(snake)
      rect.left -= 20
      snake.popleft()
      snake.append(rect)

def game_over():
    print('you lose')
    pygame.quit()
    sys.exit()

def move_conflit(direction,last_direction):
    if (direction=='up' and last_direction=='down') or (direction=='down' and last_direction=='up') :
      game_over()
    if (direction=='left' and last_direction=='right') or (direction=='right' and last_direction=='left'):
      game_over()

def add_snake(snake,direction):
    if direction=='up':
      rect=copy.deepcopy(snake)
      rect.top+=20
      snake.appendleft(rect)
    if direction=='down':
      rect=copy.deepcopy(snake)
      rect.top-=20
      snake.appendleft(rect)
    if direction=='right':
      rect=copy.deepcopy(snake)
      rect.left-=20
      snake.appendleft(rect)
    if direction=='left':
      rect=copy.deepcopy(snake)
      rect.left+=20
      snake.appendleft(rect)

def get_food(snake,food_rect,direction):
    if snake==food_rect:
      add_snake(snake,direction)
      food_rect=create_food(snake)
    return food_rect

def collision_snake(snake):
    if snake.left<0 or snake.left>screen_width:
      game_over()
    if snake.top<0 or snake.left>screen_height:
      game_over()

def difficulty_choose():
    print('选择难度')
    print('{},{},{}'.format('a:简单','b:中等','c:困难'))
    choice=input()
    if choice=='a':
      return 0.2
    elif choice=='b':
      return0.15
    elif choice=='c':
      return 0.1
    else:
      print('叫你乱按,席勒')
      return 0

def difficulty_uplift(snake,speed):
    difficuty_limit=0.005
    if not (len(snake)%3) and (speed>difficuty_limit):
      speed-=0.0004
    return speed


food_rect=create_food(snake)
last_time_piece_move=time.time()
speed=difficulty_choose()

pygame.init()
screen=pygame.display.set_mode((screen_width,screen_height))
pygame.display.set_caption('贪吃蛇')
screen.fill(bgcolor)

time.sleep(2)

init_snake(snake)
while True:
    screen.fill(bgcolor)
    draw_screen(screen,rect_color)
    draw_snake(screen,snake_color,snake)
    draw_food(screen,food_color,food_rect)
    pygame.display.update()

    for event in pygame.event.get():
            if event.type==pygame.QUIT:
                pygame.quit()
                sys.exit()
            if event.type==pygame.KEYDOWN:
                if event.key==pygame.K_UP:
                  direction='up'
                if event.key == pygame.K_DOWN:
                  direction='down'
                if event.key==pygame.K_LEFT:
                  direction='left'
                if event.key==pygame.K_RIGHT:
                  direction='right'
    if time.time()-last_time_piece_move>speed:
      difficulty_uplift(snake,speed)
      move_conflit(direction,last_direction)
      collision_snake(snake)
      snake_auto_move(snake,direction)
      food_rect=get_food(snake,food_rect,direction)
      last_direction=direction
      last_time_piece_move=time.time()



熊_熊 发表于 2022-3-20 22:27:15

嗯 感谢分享

c3106169682 发表于 2022-9-15 15:38:00

谢谢

HelloVitamin 发表于 2022-9-16 21:24:18

xiexie

chentyping 发表于 2022-9-21 10:03:50

感谢分享!

lymbwx 发表于 2022-9-26 09:20:07

Traceback (most recent call last):
File "D:/Program Files/Python37/test/贪吃蛇.py", line 1, in <module>
    import pygame,sys,time,random,math
ModuleNotFoundError: No module named 'pygame'

运行报错是什么情况哈,谢谢了.

teddyxu 发表于 2022-9-30 09:36:20

谢谢

hela520 发表于 2022-10-8 10:55:44

{:5_106:}

yeshi 发表于 2022-10-14 09:05:32

666

2081862820 发表于 2022-10-28 19:32:29

lymbwx 发表于 2022-9-26 09:20
运行报错是什么情况哈,谢谢了.

pygame库没安装

jgz1818 发表于 2022-11-19 23:35:12

虽然很简单,但现在的我还是看不懂

fishc.love 发表于 2022-11-25 19:24:10

好像没给主函数

xxxx.xxxx 发表于 2022-11-28 11:27:16

学习学习

PINSHI 发表于 2023-3-8 10:41:49

感谢分享
页: [1]
查看完整版本: 贪吃蛇代码(简单易懂)