加入收藏 | 设为首页 | 会员中心 | 我要投稿 李大同 (https://www.lidatong.com.cn/)- 科技、建站、经验、云计算、5G、大数据,站长网!
当前位置: 首页 > 编程开发 > Python > 正文

pygame wormy 贪吃蛇

发布时间:2020-12-17 17:14:41 所属栏目:Python 来源:网络整理
导读:今天PHP站长网 52php.cn把收集自互联网的代码分享给大家,仅供参考。 import pygame,sys,randomfrom pygame.locals import*FPS = 10winx = 640winy = 480cellsize = 20assert (winx%cellsize == 0 and winy%cellsize == 0

以下代码由PHP站长网 52php.cn收集自互联网

现在PHP站长网小编把它分享给大家,仅供参考

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()

以上内容由PHP站长网【52php.cn】收集整理供大家参考研究

如果以上内容对您有帮助,欢迎收藏、点赞、推荐、分享。

(编辑:李大同)

【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容!

    推荐文章
      热点阅读