ตอนจบของสามก๊กสอนว่า ความดีเท่านั้นที่จะยังคงอยู่ตลอดไป
|
||||
การเขียนโปรแกรม แสดงเนื้อเพลง ด้วยภาษา python โมดูล pygame โปรแกรมแบ่งเป็น 2 ไฟล์ ไฟล์ที่ 1 ชื่อ showlyric.pyw
=================== import pygame from openfile import getlyric def renderText(surface, text, font, position): x, y = position[0], position[1] width, height = font.size(text) render = font.render(text, 1, (0,0,0)) surface.blit(render, (position[0]+3, position[1]+2 )) render = font.render(text, 1, (0,255,255)) surface.blit(render, (position[0], position[1] )) pygame.init() screen = pygame.display.set_mode((1100, 500)) done = False is_blue = True x = 30 y = 30 songn=0 blockn=0 osongn=-1 oblockn=-1 clock = pygame.time.Clock() while not done: for event in pygame.event.get(): if event.type == pygame.QUIT: done = True if event.type == pygame.KEYDOWN and event.key == pygame.K_n: songn += 1 blockn = 0 if event.type == pygame.KEYDOWN and event.key == pygame.K_p: songn -= 1 if (songn<0):songn=0 blockn = 0 if event.type == pygame.KEYDOWN and event.key == pygame.K_SPACE: blockn += 1 pressed = pygame.key.get_pressed() if pressed[pygame.K_q]: done = True screen.fill((0, 0, 0)) if is_blue: color = (0, 128, 255) else: color = (255, 100, 0) # pygame.draw.rect(screen, color, pygame.Rect(x, y, 60, 60)) font = pygame.font.SysFont('Sans', 30, bold=True) if (osongn == songn and oblockn == blockn ): i=1 else: osongn = songn oblockn = blockn tt = getlyric(songn,blockn) if (tt[0] == 'no more song'): songn -= 1 osongn = songn if (tt[0] == 'end of lyric'): blockn -= 1 oblockn = blockn i = 1 for x in tt: renderText(screen,x,font,(50,50+i*30)) i = i+1 pygame.display.flip() clock.tick(50) ตัวโปรแกรม getfile.pyw ================= from os import listdir # from os.path import isfile, join def getlyric(n,b): fdir = 'D:phototmp2' print('n=') print(n) print('b=') print(b) arr_txt = [x for x in listdir(fdir) if x.endswith(".txt")] # print(arr_txt) file1 = open(fdir + 'name.song','w') for x in arr_txt : print(x) file1.write(x +'n') file1.close() file1 = open(fdir + 'name.song',"r") # print "Output of Readlines after writing" x = file1.readlines() file1.close() if (n >= len(x)) : return ['no more song'] file1 = open(fdir + '' + x[n].replace('n',''),"r") # print "Output of Readlines after writing" y = file1.readlines() file1.close() i=1 z=[] if (5*b >= len(y)) : return ['end of lyric'] for x in range(0+5*b,5+5*b): if (x < len(y) ): z.append(y[x]) return z ตัวอย่างไฟล์ เพลง 1.song.txt ==================== Out of Reach ...Knew the signs...Wasn't right...I was stupid for a while. ..Swept away by you...And now I feel like a fool... So confused,...My heart's bruised...Was I ever loved by you?. ..Out of reach, so far...I never had your heart...Out of reach. ..Couldn't see...We were never...Meant to be...Catch myself. ..From despair...I could drown...If I stay here...Keeping busy everyday. ..I know I will be OK...But I was...So confused...My heart's bruised. ..Was I ever loved by you?...Out of reach, so far.. .I never had your heart...Out of reach...Couldn't see. ..We were never...Meant to be...So much hurt...So much pain. ..Takes a while...To regain...What is lost inside.. .And I hope that in time...You'll be out of my mind. ..And I'll be over you...But now I'm...So confused. ..My heart's bruised...Was I ever loved by you?...Out of reach. ..So far...I never had your heart...Out of reach... Couldn't see...We were never...Meant to be...Out of reach. ..So far...You never gave your heart...In my reach, I can see. ..There's a life out there...For me... |
thaiger_u
Rss Feed Smember ผู้ติดตามบล็อก : 3 คน [?] @ฟรี โปรแกรมปฏิทิน 2564 - Free android app @ รับสอน เขียนโปรแกรม Python ระดับมัธยมปลาย Group Blog All Blog
|
|||
Pantip.com | PantipMarket.com | Pantown.com | © 2004 BlogGang.com allrights reserved. |