微信公众号搜"智元新知"关注
微信扫一扫可直接关注哦!

pygame wormy 贪吃蛇

下面是编程之家 jb51.cc 通过网络收集整理的代码片段。

编程之家小编现在分享给大家,也给大家做个参考。

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)
green = (0,255,0)
darkgreen = (0,155,0)
gray = (40,40,40)
white = (255,255)
black = (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,{'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))
        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),winy))
    for j in range(celly):
        pygame.draw.line(disp,(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',white,darkgreen)
    w2surf = basicfont1.render('wormy',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,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',white)
    gamerect = gamesurf.get_rect()
    gamerect.topleft = (180,80)
    oversurf = basicfont4.render('Over',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()

以上是编程之家(jb51.cc)为你收集整理的全部代码内容,希望文章能够帮你解决所遇到的程序开发问题。

如果觉得编程之家网站内容还不错,欢迎将编程之家网站推荐给程序员好友。

版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。

相关推荐