pygame wormy 贪吃蛇

清华大佬耗费三个月吐血整理的几百G的资源,免费分享!....>>>

import pygame,sys,random
from pygame.locals import*

FPS = 10
winx = 640
winy = 480
cellsize = 20

assert (winx%cellsize == 0 and winy%cellsize == 0),'cell number needs to be interger'
cellx = int(winx/cellsize)
celly = int(winy/cellsize)

red = (255,0,0)
green = (0,255,0)
darkgreen = (0,155,0)
gray = (40,40,40)
white = (255,255,255)
black = (0,0,0)

left = 'left'
right = 'right'
down = 'down'
up = 'up'

head = 0

def main():
    global fpsclock,disp

    pygame.init()
    disp = pygame.display.set_mode((winx,winy))
    pygame.display.set_caption('wormy')
    disp.fill(black)    
    fpsclock = pygame.time.Clock()

    startanimation()
    
    while True:
        rungame()
        gameover()
        
        
def rungame():
    global FPS
    
    direction = right
    startx = random.randint(5,cellx - 6)
    starty = random.randint(5,celly - 6)
    wormy = [{'x': startx, 'y': starty},
             {'x': startx-1, 'y': starty},
             {'x': startx-2, 'y': starty}]
    
    apple = randomapple()


    while True:

        for event in pygame.event.get():
            if event.type == QUIT:
                terminal()
            elif event.type == KEYDOWN:
                if event.key == K_LEFT and direction!=right:
                    direction = left
                elif event.key == K_RIGHT and direction!=left:
                    direction = right
                elif event.key == K_UP and direction!=down:
                    direction = up
                elif event.key == K_DOWN:
                    direction = down
                elif event.key == K_ESCAPE:
                    terminal()
                  
        if wormy[head]['x'] == cellx or wormy[head]['x'] == -1 or wormy[head]['y'] == celly or wormy[head]['y'] == -1:
            return
        for wormybody in wormy[1:]:
            if wormybody['x'] == wormy[head]['x'] and wormybody['y'] == wormy[head]['y']:
                return
        if wormy[head]['x'] == apple['x'] and  wormy[head]['y'] == apple['y']:
            apple = randomapple()
            FPS+=1
        else:
            del wormy[-1]
            
        if direction == up:
            newhead = {'x':wormy[head]['x'],'y':wormy[head]['y']-1}
        if direction == down:
            newhead = {'x':wormy[head]['x'],'y':wormy[head]['y']+1}
        if direction == left:
            newhead = {'x':wormy[head]['x']-1,'y':wormy[head]['y']}            
        if direction == right:
            newhead = {'x':wormy[head]['x']+1,'y':wormy[head]['y']}
            
        wormy.insert(0,newhead)
        disp.fill(black)
        drawline()
        
        drawwormy(wormy)
        drawapple(apple)
        drawscore(len(wormy)-3)

        pygame.display.update()
        fpsclock.tick(FPS)

          

      
        
def drawapple(coords):

    pygame.draw.rect(disp,red,(coords['x']*cellsize,coords['y']*cellsize,cellsize,cellsize))


def randomapple():
    applex = random.randint(0,cellx-1)
    appley = random.randint(0,celly-1)
    return {'x':applex,'y':appley}

def drawwormy(wormy):
    for board in wormy:
        x = board['x']*cellsize
        y = board['y']*cellsize
        pygame.draw.rect(disp,darkgreen,(x,y,cellsize,cellsize))
        pygame.draw.rect(disp,green,(x+4,y+4,cellsize-8,cellsize-8))
        

def drawline():
    for i in range(cellx):
        pygame.draw.line(disp,gray,(i*cellsize,0),(i*cellsize,winy))
    for j in range(celly):
        pygame.draw.line(disp,gray,(0,j*cellsize),(winx,j*cellsize))



    
def terminal():
    pygame.quit()
    sys.exit()

def checkforpress():
    if len(pygame.event.get(QUIT))> 0 :
        terminal()
    keypress = pygame.event.get(KEYUP)
    if len(keypress) == 0:
        return None
    elif len(keypress)>0:
        if keypress[0].key == K_ESCAPE:
            terminal()
        return keypress[0].key

def drawkey():
    basicfont2 = pygame.font.Font('freesansbold.ttf',20)
    textsurf = basicfont2.render('press a key to play',1,gray)
    textrect = textsurf.get_rect()
    textrect.topleft = (winx-200,winy-50)
    disp.blit(textsurf,textrect)
    
def startanimation():
    basicfont1 = pygame.font.Font('freesansbold.ttf',100)
    degree1 = 0
    degree2 = 0
    w1surf = basicfont1.render('wormy',1,white,darkgreen)
    w2surf = basicfont1.render('wormy',1,green)
    
    while True:
        disp.fill(black)
        drawkey()
        
        rotate1surf = pygame.transform.rotate(w1surf,degree1)
        rotate1rect = rotate1surf.get_rect()
        rotate1rect.center = (winx-320,winy-240)
        
        
        rotate2surf = pygame.transform.rotate(w2surf,degree2)
        rotate2rect = rotate2surf.get_rect()
        rotate2rect.center = (winx-320,winy-240)
        disp.blit(rotate1surf,rotate1rect)
        disp.blit(rotate2surf,rotate2rect)
        
        degree1+= 3
        degree2+= 7

        if checkforpress():
            pygame.event.get()
            return

        pygame.display.update()
        fpsclock.tick(FPS)

def drawscore(score):

    basicfont3 = pygame.font.Font('freesansbold.ttf',20)
    textsurf = basicfont3.render('score: %d'%score,1,white)
    textrect = textsurf.get_rect()
    textrect.topleft = (winx-100,10)
    disp.blit(textsurf,textrect)

    
def gameover():
    basicfont4 = pygame.font.Font('freesansbold.ttf',100)
    gamesurf = basicfont4.render('Game',1,white)
    gamerect = gamesurf.get_rect()
    gamerect.topleft = (180,80)
    oversurf = basicfont4.render('Over',1,white)
    overrect = oversurf.get_rect()
    overrect.topleft = (200,200)
    disp.blit(gamesurf,gamerect)
    disp.blit(oversurf,overrect)
    drawkey()
    pygame.display.update()
    pygame.time.wait(500)
    checkforpress()

    while True:
        if checkforpress():
            pygame.event.get()
            return



            

if __name__ =='__main__':
    main()