pygame simulate

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

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

FPS = 30

green =(0,255,0)
darkgreen =(0,155,0)
red =(255,0,0)
darkred =(155,0,0)
blue =(0,0,255)
darkblue =(0,0,155)
yellow =(255,255,0)
darkyellow =(155,155,0)

gray = (0,255,255)
purple = (255,0,255)
white = (255,255,255)
black = (0,0,0)
bgcolor = black
flashdelay = 200

winx = 640
winy = 480
boardsize = 200
boardgap = 20

xmargin = int(winx-(boardsize*2+boardgap))/2
ymargin = int(winy-(boardsize*2+boardgap))/2

greenrect = pygame.Rect(xmargin,ymargin,boardsize,boardsize)
redrect = pygame.Rect(xmargin+boardsize+boardgap,ymargin,boardsize,boardsize)
bluerect = pygame.Rect(xmargin,ymargin+boardsize+boardgap,boardsize,boardsize)
yellowrect = pygame.Rect(xmargin+boardsize+boardgap,ymargin+boardsize+boardgap,boardsize,boardsize)

def main():
    global fpsclock,disp,beep1,beep2,beep3,beep4

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

    basicfont = pygame.font.Font('freesansbold.ttf',16)
    infosurf = basicfont.render('match the pattern',True,black)
    inforect = infosurf.get_rect()
    inforect.topleft = (10,winy - 25)

    beep1 = pygame.mixer.Sound('beep1.ogg')
    beep2 = pygame.mixer.Sound('beep2.ogg')
    beep3 = pygame.mixer.Sound('beep3.ogg')
    beep4 = pygame.mixer.Sound('beep4.ogg')
    
    pattern = []
    currentstep = 0
    lasttime = 0
    score = 0
    waitingforinput = False

    while True:

        clickedbutton = None
        disp.fill(bgcolor)
        drawbuttons()

        scoresurf = basicfont.render('score:%s'%str(score),1,white)
        scorerect = scoresurf.get_rect()
        scorerect.topleft = (winx-100,10)
        disp.blit(scoresurf,scorerect)
        disp.blit(infosurf,inforect)

        checkforquit()
        for event in pygame.event.get():
            if event.type == MOUSEBUTTONUP:
                mousex,mousey = event.pos
                clickedbutton = getclicked(mousex,mousey)
            elif event.type == KEYDOWN:
                if event.key == K_q:
                    clickedbutton = darkgreen
                elif event.key == K_w:
                    clickedbutton = darkred
                elif event.key ==K_a:
                    clickedbutton == bluerect
                elif event.key == K_s:
                    clickedbutton == yellowrect

        if not waitingforinput:
            pygame.display.update()
            pygame.time.wait(1000)                        
            pattern.append(random.choice((darkgreen,darkred,darkblue,darkyellow)))
            for button in pattern:
                flashbutton(button)
                pygame.time.wait(flashdelay)
            waitingforinput = True
        else:
            if clickedbutton and clickedbutton == pattern[currentstep]:
                flashbutton(clickedbutton)
                currentstep+= 1
                lasttime = time.time()
                
                if currentstep == len(pattern):
                    changebackgroundanimation()
                    score+= 1
                    waitingforinput = False
                    currentstep = 0
            elif (clickedbutton and clickedbutton!= pattern[currentstep]):
                gameoveranimation()
                pattern = []
                currentstep = 0
                waitingforinput = False
                score = 0
                pygame.time.wait(1000)
                changebackgroundanimation()
        pygame.display.update()
        fpsclock.tick(FPS)
            
def terminal():
    pygame.quit()
    sys.exit()

def checkforquit():
    for event in pygame.event.get(QUIT):
        terminal()
    for event in pygame.event.get(KEYUP):
        if event.key == K_ESCAPE:
            terminal()
        pygame.event.post(event)
        
def drawbuttons():
    pygame.draw.rect(disp,darkgreen,greenrect)
    pygame.draw.rect(disp,darkred,redrect)
    pygame.draw.rect(disp,darkblue,bluerect)
    pygame.draw.rect(disp,darkyellow,yellowrect)
    
def flashbutton(color ,animationspeed = 20):
    if color == darkgreen:
        flashcolor = green
        rectangle = greenrect
        beep = beep1
    elif color == darkred:
        flashcolor = red
        rectangle = redrect
        beep = beep2
    elif color == darkblue:
        flashcolor = blue
        rectangle = bluerect
        beep = beep3
    elif color == darkyellow:
        flashcolor = yellow
        rectangle = yellowrect
        beep = beep4
        
        
    osurf = disp.copy()
    flashsurf = pygame.Surface((boardsize,boardsize))
    flashsurf = flashsurf.convert_alpha()
    r,g,b = flashcolor
    beep.play()
    for start,end,step in ((0,255,1),(255,0,-1)):
        for alpha in range(start,end,animationspeed*step):
            checkforquit()
            disp.blit(osurf,(0,0))
            flashsurf.fill((r,g,b,alpha))
            disp.blit(flashsurf,rectangle.topleft)
            pygame.display.update()
            fpsclock.tick(FPS)
            
    disp.blit(osurf,(0,0))
            

def getclicked(x,y):
    if greenrect.collidepoint(x,y):
        return darkgreen
    elif redrect.collidepoint(x,y):
        return darkred
    elif bluerect.collidepoint(x,y):
        return darkblue
    elif yellowrect.collidepoint(x,y):
        return darkyellow
    return None

def changebackgroundanimation(animationspeed = 40):
    global bgcolor
    newcolor = (random.randint(0,255),random.randint(0,255),random.randint(0,255))

    newsurf = pygame.Surface((winx,winy))
    newsurf = newsurf.convert_alpha()
    r,g,b = newcolor
    for alpha in range(0,255,animationspeed):
        disp.fill(bgcolor)
        newsurf.fill((r,g,b,alpha))
        disp.blit(newsurf,(0,0))

        drawbuttons()
        pygame.display.update()
        fpsclock.tick(FPS)
    bgcolor = newcolor

def gameoveranimation(color = white,animationspeed = 50):
    osurf = disp.copy()
    flashsurf = pygame.Surface(disp.get_size())
    flashsurf = flashsurf.convert_alpha()
    beep1.play()
    beep2.play()
    beep3.play()
    r,g,b = color
    for i in range(3):
        for start,end,step in ((0,255,1),(255,0,-1)):
            for alpha in range(start,end,animationspeed*step):
                checkforquit()
                flashsurf.fill((r,g,b,alpha))
                disp.blit(osurf,(0,0))
                disp.blit(flashsurf,(0,0))
                drawbuttons()
                pygame.display.update()
                fpsclock.tick(FPS)
        
        



if __name__=='__main__':
    main()