pygame simulate
发布时间:2020-12-17 17:26:55 所属栏目:Python 来源:网络整理
导读:今天PHP站长网 52php.cn把收集自互联网的代码分享给大家,仅供参考。 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)
以下代码由PHP站长网 52php.cn收集自互联网 现在PHP站长网小编把它分享给大家,仅供参考 import pygame,sys,random,time from pygame.locals import* FPS = 30 green =(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 = 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,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 None def 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 = 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,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() 以上内容由PHP站长网【52php.cn】收集整理供大家参考研究 如果以上内容对您有帮助,欢迎收藏、点赞、推荐、分享。 (编辑:李大同) 【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容! |