pygame simulate

pygame simulate,第1张

概述pygame simulate

下面是内存溢出 jb51.cc 通过网络收集整理的代码片段。

内存溢出小编现在分享给大家,也给大家做个参考。

import pygame,sys,random,timefrom pygame.locals import*FPS = 30green =(0,255,0)darkgreen =(0,155,0)red =(255,0)darkred =(155,0)blue =(0,255)darkblue =(0,155)yellow =(255,0)darkyellow =(155,0)gray = (0,255)purple = (255,255)white = (255,255)black = (0,0)bgcolor = blackflashdelay = 200winx = 640winy = 480boardsize = 200boardgap = 20xmargin = int(winx-(boardsize*2+boardgap))/2ymargin = int(winy-(boardsize*2+boardgap))/2greenrect = pygame.Rect(xmargin,ymargin,boardsize,boardsize)redrect = pygame.Rect(xmargin+boardsize+boardgap,boardsize)bluerect = pygame.Rect(xmargin,ymargin+boardsize+boardgap,boardsize)yellowrect = pygame.Rect(xmargin+boardsize+boardgap,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,redrect)    pygame.draw.rect(disp,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,1),(255,-1)):        for Alpha in range(start,animationspeed*step):            checkforquit()            disp.blit(osurf,(0,0))            flashsurf.fill((r,b,Alpha))            disp.blit(flashsurf,rectangle.topleft)            pygame.display.update()            fpsclock.tick(FPS)                disp.blit(osurf,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 Nonedef changebackgroundanimation(animationspeed = 40):    global bgcolor    newcolor = (random.randint(0,255),random.randint(0,255))    newsurf = pygame.Surface((winx,winy))    newsurf = newsurf.convert_Alpha()    r,b = newcolor    for Alpha in range(0,animationspeed):        disp.fill(bgcolor)        newsurf.fill((r,Alpha))        disp.blit(newsurf,0))        drawbuttons()        pygame.display.update()        fpsclock.tick(FPS)    bgcolor = newcolordef 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,b = color    for i in range(3):        for start,-1)):            for Alpha in range(start,animationspeed*step):                checkforquit()                flashsurf.fill((r,Alpha))                disp.blit(osurf,0))                disp.blit(flashsurf,0))                drawbuttons()                pygame.display.update()                fpsclock.tick(FPS)                if __name__=='__main__':    main()

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

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

总结

以上是内存溢出为你收集整理的pygame simulate全部内容,希望文章能够帮你解决pygame simulate所遇到的程序开发问题。

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

欢迎分享,转载请注明来源:内存溢出

原文地址: http://outofmemory.cn/langs/1199198.html

(0)
打赏 微信扫一扫 微信扫一扫 支付宝扫一扫 支付宝扫一扫
上一篇 2022-06-04
下一篇 2022-06-04

发表评论

登录后才能评论

评论列表(0条)

保存