python实现贪吃蛇
Posted physique
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了python实现贪吃蛇相关的知识,希望对你有一定的参考价值。
贪吃蛇的算法还是比较简单的,蛇的移动我是通过不停添加一个head方块,然后判断应该加到蛇头的哪个方向,加完后删掉蛇尾就行了,如果吃到食物就不删蛇尾。
只是一个贪吃蛇只需要70行代码左右就可以了,后来又加了计分,失败后重新游戏,暂停功能····结果现在代码乱成渣了。。
重新游戏部分肯定有更好的方法,我写的太乱了。。求大神指教。由于没用网格,判断吃到的时候是用范围判断的,有时候有些偏差···
代码:
1 #-*- coding: utf-8 -*- 2 import pygame, sys, random, time 3 from pygame.locals import * 4 5 pygame.init() 6 font = pygame.font.Font(u"c:\\\\windows\\\\fonts\\\\MSYH.ttf",30) 7 mainClock = pygame.time.Clock() 8 wsurface = pygame.display.set_mode((800,600),0,32) 9 pygame.display.set_caption("My_Snake~") 10 11 Len = 20 12 snakeRect = [] 13 for i in range(10,13): 14 snakeRect.append(pygame.Rect(i * (Len) , 50 , Len, Len)) 15 food = pygame.Rect(10 * (Len), 10 * (Len), Len, Len) 16 ml = False 17 mr = True 18 mu = False 19 md = False 20 score = 0 21 black = (0, 0, 0) 22 green = (0, 255, 0) 23 white = (255, 255, 255) 24 global FPSCLOCK 25 FPSCLOCK = pygame.time.Clock() 26 ##################################################### 27 def judge(): 28 if snakeRect[0].left - 15 <= food.left <= snakeRect[0].left + 15 and snakeRect[0].top - 15 <= food.top <= snakeRect[0].top + 15: 29 return True 30 def judge2(a, b): 31 if a.left - 15 <= b.left <= a.left + 15 and a.top - 15 <= b.top <= a.top + 15: 32 return True 33 def checkForKeyPress(): 34 #checkForQuit() 35 for event in pygame.event.get([KEYDOWN, KEYUP]): 36 if event.type == KEYUP: 37 continue 38 return event.key 39 return None 40 ####################################################### 41 flagg = True 42 speed = 8 43 while True: 44 for event in pygame.event.get(): 45 if event.type == QUIT: 46 pygame.QUIT() 47 sys.exit() 48 if event.type == KEYDOWN: 49 if event.key == K_r: 50 snakeRect = [] 51 for i in range(10,13): 52 snakeRect.append(pygame.Rect(i * (Len) , 50 , Len, Len)) 53 food = pygame.Rect(10 * (Len), 10 * (Len), Len, Len) 54 ml = False 55 mr = True 56 mu = False 57 md = False 58 score = 0 59 flagg = True 60 speed = 8 61 if event.key == K_p: 62 wsurface.fill(black) 63 text_surface1 = font.render(\'Pause!\' , True, (0, 0, 255)) 64 wsurface.blit(text_surface1, (10, 50)) 65 while checkForKeyPress() == None: 66 pygame.display.update() 67 FPSCLOCK.tick() 68 if event.key == K_LEFT and mr == False: 69 ml = True 70 mr = False 71 mu = False 72 md = False 73 if event.key == K_RIGHT and ml == False: 74 ml = False 75 mr = True 76 mu = False 77 md = False 78 if event.key == K_UP and md == False: 79 ml = False 80 mr = False 81 mu = True 82 md = False 83 if event.key == K_DOWN and mu == False: 84 ml = False 85 mr = False 86 mu = False 87 md = True 88 head = pygame.Rect(snakeRect[0].left,snakeRect[0].top,snakeRect[0].width,snakeRect[0].height) 89 if flagg == False: 90 continue 91 if ml == True: 92 head.right = head.left - 1 93 if mr == True: 94 head.left = head.right + 1 95 if mu == True: 96 head.bottom = head.top - 1 97 if md == True: 98 head.top = head.bottom + 1 99 snakeRect.insert(0, head) 100 #判断失败和重新游戏 101 if head.right < 0 or head.left > 800 or head.bottom < 0 or head.top > 600 or snakeRect[0] in snakeRect[1:]: 102 wsurface.fill(white) 103 text_surface2 = font.render(\'Press R to restart!\' , True, (0, 0, 255)) 104 wsurface.blit(text_surface2, (50, 80)) 105 pygame.display.update() 106 while checkForKeyPress() == None: 107 pygame.display.update() 108 FPSCLOCK.tick() 109 break 110 flagg = False 111 continue 112 flagg = True 113 if judge(): 114 score = score + 10 115 speed = speed + 1 116 while True: 117 flag = True 118 food.left = random.randrange(10,800) 119 food.top = random.randrange(10,600) 120 for temp in snakeRect: 121 if judge2(food, temp): 122 flag = False 123 if flag == True: 124 break 125 else: 126 snakeRect.pop() 127 wsurface.fill(black) 128 for i in range(len(snakeRect)): 129 pygame.draw.rect(wsurface,green,snakeRect[i]) 130 pygame.draw.rect(wsurface,white,food) 131 text_surface = font.render(u"分数: " + str(score), True, (0, 0, 255)) 132 wsurface.blit(text_surface, (10, 50)) 133 pygame.display.update() 134 mainClock.tick(speed) 135 136 137 138 139
截图:
以上是关于python实现贪吃蛇的主要内容,如果未能解决你的问题,请参考以下文章