2022年贪吃蛇python源码宣贯 .pdf

上传人:Q****o 文档编号:26742513 上传时间:2022-07-19 格式:PDF 页数:7 大小:135.76KB
返回 下载 相关 举报
2022年贪吃蛇python源码宣贯 .pdf_第1页
第1页 / 共7页
2022年贪吃蛇python源码宣贯 .pdf_第2页
第2页 / 共7页
点击查看更多>>
资源描述

《2022年贪吃蛇python源码宣贯 .pdf》由会员分享,可在线阅读,更多相关《2022年贪吃蛇python源码宣贯 .pdf(7页珍藏版)》请在得力文库 - 分享文档赚钱的网站上搜索。

1、贪吃蛇源码,初始源码来自于网络。初学 python,做了部分修改,加了些有趣的功能进行测试。增加了部分注释;增加背景设置;增加历史高分统计;增加当前玩家排名;增加幸运食物,吃了有惊喜!开始:回车键暂停:空格键操作:控制上下左右( w、s、a、d ) or( up、down 、left 、right )自动背景开启和关闭: L 手动背景 RGB 值微调:r、g、b 键(L开启才可用)游戏界面Python 源码名师资料总结 - - -精品资料欢迎下载 - - - - - - - - - - - - - - - - - - 名师精心整理 - - - - - - - 第 1 页,共 7 页 - - -

2、 - - - - - - 贪吃蛇 import random import sys import time import copy import pygame from pygame.locals import * from collections import deque SCREEN_WIDTH = 800 # 屏幕宽度SCREEN_HEIGHT = 480 # 屏幕高度SIZE = 20 # 小方格大小LINE_WIDTH = 3 # 网格线宽度# 游戏区域的坐标范围SCOPE_X = (0, SCREEN_WIDTH / SIZE - 1) SCOPE_Y = (2, SCREEN_H

3、EIGHT / SIZE - 1) # 食物的分值及颜色# FOOD_STYLE_LIST = (10, (255, 100, 100), (20, (100, 255, 100), (30, (100, 100, 255) FOOD_STYLE_LIST = ( 10, (255, 0, 0) , ( 20, ( 0, 255, 0) , (30, ( 0, 0, 255) , (100, ( 255, 255, 0) # 幸运食物出现概率(蛇身减一,速度不增加)FOOD_LUCK=( 0,7) # LIGHT = (100, 100, 100) DARK = ( 200, 200, 200

4、) # 蛇的颜色BLACK = ( 0, 0, 0) # 网格线颜色RED = ( 200, 30, 30) # 红色, GAME OVER 的字体颜色BGCOLOR = (40, 40, 60) # 背景色def print_text(screen , font , x, y, text , fcolor=(255, 255, 255): imgText = font.render(text, True, fcolor) screen.blit(imgText, (x , y) # 初始化蛇def init_snake(): snake = deque() snake.append(2, S

5、COPE_Y 0) # snake.append(1, SCOPE_Y0+1), snake.append(1, SCOPE_Y0), snake.append(1, SCOPE_Y0+2) snake.append(1, SCOPE_Y 0) snake.append(0, SCOPE_Y 0) 名师资料总结 - - -精品资料欢迎下载 - - - - - - - - - - - - - - - - - - 名师精心整理 - - - - - - - 第 2 页,共 7 页 - - - - - - - - - return snake def create_food(snake): food_

6、x = random.randint(SCOPE_X0 , SCOPE_X 1) food_y = random.randint(SCOPE_Y0 , SCOPE_Y 1) while (food_x , food_y) in snake: # 如果食物出现在蛇身上,则重来food_x = random.randint(SCOPE_X0 , SCOPE_X 1) food_y = random.randint(SCOPE_Y0 , SCOPE_Y 1) return food_x , food_y def get_food_style(): if random.randint(FOOD_LUC

7、K0 , FOOD_LUCK 1)=FOOD_LUCK1: return FOOD_STYLE_LIST 3 else : return FOOD_STYLE_LISTrandom.randint(0, 2) def main (): pygame.init() screen = pygame.display.set_mode(SCREEN_WIDTH, SCREEN_HEIGHT) #pygame创建窗口pygame.display.set_caption( 贪吃蛇 ) #窗口名称font1 = pygame.font.SysFont(SimHei, 16) # 得分的字体font_ypos

8、 = 12 #位置font2 = pygame.font.Font(None, 72) # GAME OVER 的字体fwidth, fheight = font2.size(GAME OVER) # 如果蛇正在向右移动,那么快速点击向下向左,由于程序刷新没那么快,向下事件会被向左覆盖掉,导致蛇后退,直接GAME OVER # b 变量就是用于防止这种情况的发生b = True # 蛇snake = init_snake() # 食物food = create_food(snake) food_style = get_food_style() # 方向pos = (1, 0) #( 左-1 右

9、 1,上 -1 下 1)game_over = True start = False # 是否开始,当start = True,game_over = True 时,才显示 GAME OVER 名师资料总结 - - -精品资料欢迎下载 - - - - - - - - - - - - - - - - - - 名师精心整理 - - - - - - - 第 3 页,共 7 页 - - - - - - - - - score = 0 # 得分orispeed =0.5 # 原始速度speed = orispeed luck_times=0 last_move_time = None last_draw

10、_time = None pause = False # 暂停BG_r = 40 #手动背景调整BG_g = 40 BG_b = 60 BG_auto=True REC_score= REC_save=False while True: for event in pygame.event.get(): if event.type = QUIT: #关闭窗口事件直接退出sys.exit() elif event.type = KEYDOWN: #处理按键事件if event.key = K_RETURN: #处理回车键事件if game_over: start = True #游戏开始game_

11、over = False b = True snake = init_snake() food = create_food(snake) food_style = get_food_style() pos = (1, 0) # 得分score = 0 REC_save= True REC_score.append(score) last_move_time = time.time() last_draw_time = last_move_time luck_times = 0 elif event.key = K_SPACE: #处理空格键事件暂停和继续if not game_over: pa

12、use = not pause elif event.key in (K_w, K_UP): # 这个判断是为了防止蛇向上移时按了向下键,导致直接 GAME OVER if b and not pos 1: pos = (0, - 1) 名师资料总结 - - -精品资料欢迎下载 - - - - - - - - - - - - - - - - - - 名师精心整理 - - - - - - - 第 4 页,共 7 页 - - - - - - - - - b = False elif event.key in (K_s , K_DOWN): if b and not pos 1: pos = (0,

13、 1) b = False elif event.key in (K_a, K_LEFT): if b and not pos 0: pos = (-1, 0) b = False elif event.key in (K_d, K_RIGHT): if b and not pos 0: pos = (1, 0) b = False elif event.key in (K_r , K_g, K_b): #背景色调整 L 键控制开关 (R,G,B) 键控制 3 原色if event.key=K_r: BG_r=BG_r+5 if BG_r= 125: BG_r=40 elif event.ke

14、y=K_g: BG_g=BG_g+5 if BG_g= 125: BG_g=40 elif event.key=K_b: BG_g=BG_b+5 if BG_b= 125: BG_b=40 elif event.key=K_l: BG_auto=not BG_auto # 填充背景色if BG_auto: screen.fill(BGCOLOR) else : screen.fill(BG_r,BG_g , BG_b) # 画网格线竖线for x in range (SIZE , SCREEN_WIDTH, SIZE): pygame.draw.line(screen, BLACK , (x

15、, SCOPE_Y 0 * SIZE), (x, SCREEN_HEIGHT), LINE_WIDTH) # 画网格线横线for y in range (SCOPE_Y0 * SIZE, SCREEN_HEIGHT, SIZE): pygame.draw.line(screen, BLACK , ( 0, y) , (SCREEN_WIDTH, y) , LINE_WIDTH) 名师资料总结 - - -精品资料欢迎下载 - - - - - - - - - - - - - - - - - - 名师精心整理 - - - - - - - 第 5 页,共 7 页 - - - - - - - - - i

16、f not game_over: curTime = time.time() if curTime - last_move_time speed: #控制移动速度speed 值越小,刷新越快if not pause: b = True last_move_time = curTime next_s = (snake0 0 + pos0 , snake 0 1 + pos1) #按方向读取下一个前进位if next_s = food: # 吃到了食物snake.appendleft(next_s) score += food_style0 if REC_save: #记录得分REC_score.

17、pop() REC_score.append(score) if food_style0 = FOOD_STYLE_LIST 3 0 and len (snake) 3: snake.pop() snake.pop() luck_times+=1 else : speed = orispeed - 0.03 * (score / 100-luck_times) food = create_food(snake) food_style = get_food_style() else : if SCOPE_X 0 = next_s0 = SCOPE_X 1 and SCOPE_Y 0 = next

18、_s1 0.01 : #闪烁时间pygame.draw.rect(screen, food_style1 , (food0 * SIZE, food 1 * SIZE, SIZE, SIZE) , 0) last_draw_time=curTime else : pygame.draw.rect(screen, food_style1 , (food0 * SIZE, food 1 * SIZE, SIZE, SIZE) , 0) # 画蛇for s in snake: pygame.draw.rect(screen, DARK , (s 0 * SIZE + LINE_WIDTH, s 1

19、* SIZE + LINE_WIDTH, 名师资料总结 - - -精品资料欢迎下载 - - - - - - - - - - - - - - - - - - 名师精心整理 - - - - - - - 第 6 页,共 7 页 - - - - - - - - - SIZE - LINE_WIDTH * 2, SIZE - LINE_WIDTH * 2) , 0) print_text(screen, font1 , 30, font_ypos, f 速度 : score/100-luck_times ) print_text(screen, font1 , 630, font_ypos, f得分 :

20、 score ) if game_over: if start: print_text(screen, font2 , (SCREEN_WIDTH - fwidth) / 2, (SCREEN_HEIGHT - fheight) / 2, GAME OVER, RED) TMP_score=copy.deepcopy(REC_score) TMP_score.sort() TMP_score.reverse() if len (REC_score): print_text(screen, font1 , 230, font_ypos, f最高得分 : TMP_score0 ) print_te

21、xt(screen, font1 , 430, font_ypos, f当前排名 : TMP_score.index(REC_scorelen (REC_score)- 1)+ 1 ) else : print_text(screen, font1 , 230, font_ypos, f最高得分 : score ) print_text(screen, font1 , 430, font_ypos, f当前排名 : score ) pygame.display.update() if _name_ = _main_: main() 名师资料总结 - - -精品资料欢迎下载 - - - - - - - - - - - - - - - - - - 名师精心整理 - - - - - - - 第 7 页,共 7 页 - - - - - - - - -

展开阅读全文
相关资源
相关搜索

当前位置:首页 > 技术资料 > 技术总结

本站为文档C TO C交易模式,本站只提供存储空间、用户上传的文档直接被用户下载,本站只是中间服务平台,本站所有文档下载所得的收益归上传人(含作者)所有。本站仅对用户上传内容的表现方式做保护处理,对上载内容本身不做任何修改或编辑。若文档所含内容侵犯了您的版权或隐私,请立即通知得利文库网,我们立即给予删除!客服QQ:136780468 微信:18945177775 电话:18904686070

工信部备案号:黑ICP备15003705号-8 |  经营许可证:黑B2-20190332号 |   黑公网安备:91230400333293403D

© 2020-2023 www.deliwenku.com 得利文库. All Rights Reserved 黑龙江转换宝科技有限公司 

黑龙江省互联网违法和不良信息举报
举报电话:0468-3380021 邮箱:hgswwxb@163.com