python實現貪吃蛇

來源:互聯網
上載者:User

標籤:log   surface   splay   tin   bottom   coding   lock   ret   dom   

貪吃蛇的演算法還是比較簡單的,蛇的移動我是通過不停添加一個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             break110         flagg = False111         continue112     flagg = True113     if judge():114         score = score + 10115         speed = speed + 1116         while True:117             flag = True118             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 = False123             if flag == True:124                 break125     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實現貪吃蛇

相關文章

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.