Python贪吃蛇 (完整代码+详细注释+粘贴即食)

文章目录

代码

#!/usr/bin/env python
# -*- coding: utf-8 -*-
# author:Wangdali time:2021年1月20日16:08:44
#python实现:贪吃蛇
'''
游戏玩法:回车开始游戏;空格暂停游戏/继续游戏;方向键/wsad控制小蛇走向
'''
'''
思路:用列表存储蛇的身体;用浅色表示身体,深色背景将身体凸显出来;
蛇的移动:仔细观察,是:身体除头和尾不动、尾部消失,头部增加,所以,新添加的元素放在列表头部、删除尾部元素;
游戏结束判定策略:超出边界;触碰到自己的身体:蛇前进的下一格子为身体的一部分(即在列表中)。
'''
#注:因为在列表中需要频繁添加和删除元素,所以用deque容器代替列表;是因为deque具有高效的插入和删除效率
#初始化蛇,长度为3,放置在屏幕左上角;
#导包
import random
import sys
import time
import pygame
from pygame.locals import *
from collections import deque
#基础设置
Screen_Height=480
Screen_Width=600
Size=20#小方格大小
Line_Width=1
#游戏区域的坐标范围
Area_x=(0,Screen_Width//Size-1) #0是左边界,1是右边界 #注:python中//为整数除法;/为浮点数除法
Area_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)
Back_Ground=(40,40,60)
#文本输出格式设置
def Print_Txt(screen,font,x,y,text,fcolor=(255,255,255)):
    #font.render参数意义:.render(内容,是否抗锯齿,字体颜色,字体背景颜色)
    Text=font.render(text,True,fcolor)
    screen.blit(Text,(x,y))
#初始化蛇
def init_snake():
    snake=
  • 30
    点赞
  • 216
    收藏
    觉得还不错? 一键收藏
  • 20
    评论
以下是一个简单的Python贪吃蛇完整代码示例: ```python import pygame import random # 初始化pygame pygame.init() # 定义颜色 BLACK = (0, 0, 0) WHITE = (255, 255, 255) GREEN = (0, 255, 0) RED = (255, 0, 0) # 设置屏幕尺寸 screen_width = 800 screen_height = 600 screen = pygame.display.set_mode((screen_width, screen_height)) pygame.display.set_caption("贪吃蛇游戏") # 定义蛇的初始位置和速度 snake_block = 10 snake_speed = 30 x1 = screen_width / 2 y1 = screen_height / 2 x1_change = 0 y1_change = 0 # 定义食物的初始位置 foodx = round(random.randrange(0, screen_width - snake_block) / 10.0) * 10.0 foody = round(random.randrange(0, screen_height - snake_block) / 10.0) * 10.0 # 定义蛇的身体 snake_body = [] snake_length = 1 clock = pygame.time.Clock() font_style = pygame.font.SysFont(None, 50) score_font = pygame.font.SysFont(None, 35) def score(score): value = score_font.render("Score: " + str(score), True, WHITE) screen.blit(value, [0, 0]) def snake(snake_block, snake_body): for x in snake_body: pygame.draw.rect(screen, GREEN, [x[0], x[1], snake_block, snake_block]) def game_loop(): game_over = False game_close = False while not game_over: while game_close: screen.fill(BLACK) message = font_style.render("Game Over! Press Q-Quit or C-Play Again", True, RED) screen.blit(message, [screen_width / 6, screen_height / 3]) score(snake_length - 1) pygame.display.update() for event in pygame.event.get(): if event.type == pygame.KEYDOWN: if event.key == pygame.K_q: game_over = True game_close = False if event.key == pygame.K_c: game_loop() for event in pygame.event.get(): if event.type == pygame.QUIT: game_over = True if event.type == pygame.KEYDOWN: if event.key == pygame.K_LEFT: x1_change = -snake_block y1_change = 0 elif event.key == pygame.K_RIGHT: x1_change = snake_block y1_change = 0 elif event.key == pygame.K_UP: y1_change = -snake_block x1_change = 0 elif event.key == pygame.K_DOWN: y1_change = snake_block x1_change = 0 if x1 >= screen_width or x1 < 0 or y1 >= screen_height or y1 < 0: game_close = True x1 += x1_change y1 += y1_change screen.fill(BLACK) pygame.draw.rect(screen, RED, [foodx, foody, snake_block, snake_block]) snake_head = [] snake_head.append(x1) snake_head.append(y1) snake_body.append(snake_head) if len(snake_body) > snake_length: del snake_body[0] for x in snake_body[:-1]: if x == snake_head: game_close = True snake(snake_block, snake_body) score(snake_length - 1) pygame.display.update() if x1 == foodx and y1 == foody: foodx = round(random.randrange(0, screen_width - snake_block) / 10.0) * 10.0 foody = round(random.randrange(0, screen_height - snake_block) / 10.0) * 10.0 snake_length += 1 clock.tick(snake_speed) pygame.quit() game_loop() ``` 这是一个简单的贪吃蛇游戏的代码,使用了Pygame库来实现图形化界面和游戏逻辑。你可以将代码复制到Python环境中运行,并使用方向键来控制贪吃蛇的移动。

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 20
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值