python实现贪吃蛇(1)
2013-06-05 00:12
363 查看
这几天学了下pygame,终于要做人生中第一个游戏了⊙﹏⊙
贪吃蛇的算法还是比较简单的,蛇的移动我是通过不停添加一个head方块,然后判断应该加到蛇头的哪个方向,加完后删掉蛇尾就行了,如果吃到食物就不删蛇尾。
只是一个贪吃蛇只需要70行代码左右就可以了,后来又加了计分,失败后重新游戏,暂停功能····结果现在代码乱成渣了。。
重新游戏部分肯定有更好的方法,我写的太乱了。。求大神指教。由于没用网格,判断吃到的时候是用范围判断的,有时候有些偏差···
代码:
截图:
贪吃蛇的算法还是比较简单的,蛇的移动我是通过不停添加一个head方块,然后判断应该加到蛇头的哪个方向,加完后删掉蛇尾就行了,如果吃到食物就不删蛇尾。
只是一个贪吃蛇只需要70行代码左右就可以了,后来又加了计分,失败后重新游戏,暂停功能····结果现在代码乱成渣了。。
重新游戏部分肯定有更好的方法,我写的太乱了。。求大神指教。由于没用网格,判断吃到的时候是用范围判断的,有时候有些偏差···
代码:
#-*- coding: utf-8 -*- import pygame, sys, random, time from pygame.locals import * pygame.init() font = pygame.font.Font(u"c:\\windows\\fonts\\MSYH.ttf",30) mainClock = pygame.time.Clock() wsurface = pygame.display.set_mode((800,600),0,32) pygame.display.set_caption("My_Snake~") Len = 20 snakeRect = [] for i in range(10,13): snakeRect.append(pygame.Rect(i * (Len) , 50 , Len, Len)) food = pygame.Rect(10 * (Len), 10 * (Len), Len, Len) ml = False mr = True mu = False md = False score = 0 black = (0, 0, 0) green = (0, 255, 0) white = (255, 255, 255) global FPSCLOCK FPSCLOCK = pygame.time.Clock() ##################################################### def judge(): if snakeRect[0].left - 15 <= food.left <= snakeRect[0].left + 15 and snakeRect[0].top - 15 <= food.top <= snakeRect[0].top + 15: return True def judge2(a, b): if a.left - 15 <= b.left <= a.left + 15 and a.top - 15 <= b.top <= a.top + 15: return True def checkForKeyPress(): #checkForQuit() for event in pygame.event.get([KEYDOWN, KEYUP]): if event.type == KEYUP: continue return event.key return None ####################################################### flagg = True speed = 8 while True: for event in pygame.event.get(): if event.type == QUIT: pygame.QUIT() sys.exit() if event.type == KEYDOWN: if event.key == K_r: snakeRect = [] for i in range(10,13): snakeRect.append(pygame.Rect(i * (Len) , 50 , Len, Len)) food = pygame.Rect(10 * (Len), 10 * (Len), Len, Len) ml = False mr = True mu = False md = False score = 0 flagg = True speed = 8 if event.key == K_p: wsurface.fill(black) text_surface1 = font.render('Pause!' , True, (0, 0, 255)) wsurface.blit(text_surface1, (10, 50)) while checkForKeyPress() == None: pygame.display.update() FPSCLOCK.tick() if event.key == K_LEFT and mr == False: ml = True mr = False mu = False md = False if event.key == K_RIGHT and ml == False: ml = False mr = True mu = False md = False if event.key == K_UP and md == False: ml = False mr = False mu = True md = False if event.key == K_DOWN and mu == False: ml = False mr = False mu = False md = True head = pygame.Rect(snakeRect[0].left,snakeRect[0].top,snakeRect[0].width,snakeRect[0].height) if flagg == False: continue if ml == True: head.right = head.left - 1 if mr == True: head.left = head.right + 1 if mu == True: head.bottom = head.top - 1 if md == True: head.top = head.bottom + 1 snakeRect.insert(0, head) #判断失败和重新游戏 if head.right < 0 or head.left > 800 or head.bottom < 0 or head.top > 600 or snakeRect[0] in snakeRect[1:]: wsurface.fill(white) text_surface2 = font.render('Press R to restart!' , True, (0, 0, 255)) wsurface.blit(text_surface2, (50, 80)) pygame.display.update() while checkForKeyPress() == None: pygame.display.update() FPSCLOCK.tick() break flagg = False continue flagg = True if judge(): score = score + 10 speed = speed + 1 while True: flag = True food.left = random.randrange(10,800) food.top = random.randrange(10,600) for temp in snakeRect: if judge2(food, temp): flag = False if flag == True: break else: snakeRect.pop() wsurface.fill(black) for i in range(len(snakeRect)): pygame.draw.rect(wsurface,green,snakeRect[i]) pygame.draw.rect(wsurface,white,food) text_surface = font.render(u"分数: " + str(score), True, (0, 0, 255)) wsurface.blit(text_surface, (10, 50)) pygame.display.update() mainClock.tick(speed)
截图:
相关文章推荐
- Python 实现 贪吃蛇大作战 代码分享
- 【python学习笔记】使用类封装实现贪吃蛇
- Python 实现 贪吃蛇大作战 代码分享
- python实现小游戏-贪吃蛇
- python实现贪吃蛇
- Python实现从URL地址提取文件名的方法
- tcpcopy 实现( php + python)
- Python实现PS图像调整之对比度调整功能示例
- python爬虫_实现校园网自动重连脚本的教程
- Python实现App自动签到领取积分功能
- Python 列表实现字典的get功能
- Python实现邮件发送
- Python实现读写sqlite3数据库并将统计数据写入Excel的方法示例
- Python的Numpy实现深度学习常用的函数
- python 实现判断ip连通性的方法总结
- 基于QQ云输入法,用python实现的输入法(demo 版)
- python实现mysql的单引号字符串过滤方法
- Python的迭代和递归实现菲波那切数列(Fibonacci sequence)
- Python中的装饰模式如何实现?
- 决策树ID3算法及其Python实现