贪吃蛇python源码.docx

上传人:1595****071 文档编号:33790558 上传时间:2022-08-12 格式:DOCX 页数:7 大小:111.92KB
返回 下载 相关 举报
贪吃蛇python源码.docx_第1页
第1页 / 共7页
贪吃蛇python源码.docx_第2页
第2页 / 共7页
点击查看更多>>
资源描述

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

1、_贪吃蛇源码,初始源码来自于网络。初学python,做了部分修改,加了些有趣的功能进行测试。增加了部分注释;增加背景设置;增加历史高分统计;增加当前玩家排名;增加幸运食物,吃了有惊喜!开始:回车键暂停:空格键操作:控制上下左右( w、s、a、d ) or( up、down、left、right)自动背景开启和关闭: L手动背景RGB值微调:r、g、b 键(L开启才可用)游戏界面Python 源码贪吃蛇import randomimport sysimport timeimport copyimport pygamefrom pygame.locals import *from collecti

2、ons import dequeSCREEN_WIDTH = 800 # 屏幕宽度SCREEN_HEIGHT = 480 # 屏幕高度SIZE = 20 # 小方格大小LINE_WIDTH = 3 # 网格线宽度# 游戏区域的坐标范围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)FOOD_STYLE_L

3、IST = (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) # 蛇的颜色BLACK = (0, 0, 0) # 网格线颜色RED = (200, 30, 30) # 红色,GAME OVER 的字体颜色BGCOLOR = (40, 40, 60) # 背景色def print_text(screen, font, x, y, te

4、xt, fcolor=(255, 255, 255): imgText = font.render(text, True, fcolor) screen.blit(imgText, (x, y)# 初始化蛇def init_snake(): snake = deque() snake.append(2, SCOPE_Y0) # snake.append(1, SCOPE_Y0+1), snake.append(1, SCOPE_Y0), snake.append(1, SCOPE_Y0+2) snake.append(1, SCOPE_Y0) snake.append(0, SCOPE_Y0)

5、 return snakedef create_food(snake): food_x = random.randint(SCOPE_X0, SCOPE_X1) food_y = random.randint(SCOPE_Y0, SCOPE_Y1) while (food_x, food_y) in snake: # 如果食物出现在蛇身上,则重来 food_x = random.randint(SCOPE_X0, SCOPE_X1) food_y = random.randint(SCOPE_Y0, SCOPE_Y1) return food_x, food_ydef get_food_sty

6、le(): if random.randint(FOOD_LUCK0,FOOD_LUCK1)=FOOD_LUCK1: return FOOD_STYLE_LIST3 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(Sim

7、Hei, 16) # 得分的字体 font_ypos = 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_st

8、yle() # 方向 pos = (1, 0) #(左-1 右1,上-1 下1) game_over = True start = False # 是否开始,当start = True,game_over = True 时,才显示 GAME OVER score = 0 # 得分 orispeed =0.5 # 原始速度 speed = orispeed luck_times=0 last_move_time = None last_draw_time = None pause = False # 暂停 BG_r = 40 #手动背景调整 BG_g = 40 BG_b = 60 BG_auto

9、=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_over = False b = True snake = init_snake() food = create_food(snake) fo

10、od_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: pause = not pause elif event.key in (K_w, K_UP): # 这个判断是为了防止蛇向上移时按了向下键,

11、导致直接 GAME OVER if b and not pos1: pos = (0, -1) b = False elif event.key in (K_s, K_DOWN): if b and not pos1: pos = (0, 1) b = False elif event.key in (K_a, K_LEFT): if b and not pos0: pos = (-1, 0) b = False elif event.key in (K_d, K_RIGHT): if b and not pos0: pos = (1, 0) b = False elif event.key

12、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.key=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

13、(BG_r,BG_g,BG_b) # 画网格线 竖线 for x in range(SIZE, SCREEN_WIDTH, SIZE): pygame.draw.line(screen, BLACK, (x, SCOPE_Y0 * 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) if not gam

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

15、score.append(score) if food_style0 = FOOD_STYLE_LIST30 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_X0 = next_s0 = SCOPE_X1 and SCOPE_Y0 = next_s1 0.01: #闪烁时间 py

16、game.draw.rect(screen, food_style1, (food0 * SIZE, food1 * SIZE, SIZE, SIZE), 0) last_draw_time=curTime else: pygame.draw.rect(screen, food_style1, (food0 * SIZE, food1 * SIZE, SIZE, SIZE), 0) # 画蛇 for s in snake: pygame.draw.rect(screen, DARK, (s0 * SIZE + LINE_WIDTH, s1 * SIZE + LINE_WIDTH, SIZE -

17、 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得分: score) if game_over: if start: print_text(screen, font2, (SCREEN_WIDTH - fwidth) / 2, (SCREEN_HEIGHT - fheight) / 2, GAME OVER, RED) TMP_scor

18、e=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_text(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_

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

当前位置:首页 > 教育专区 > 小学资料

本站为文档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