การเขียนโปรแกรม แสดงเนื้อเพลง ด้วยภาษา python โมดูล pygame
โปรแกรมแบ่งเป็น 2 ไฟล์
ไฟล์ที่ 1 ชื่อ showlyric.pyw
  • เป็นโปรแกรมหลักในการใช้งาน โดยใช้โมดูล pygame
  • โปรแกรมจะตรวจจับคีย์บอร์ด
    • ถ้าเคาะ space ให้ ขึ้นเนื้อเพลงถัด ไป แสดงผลครั้งละ 5 บรรทัด
    • เคาะ n คือเพลงถัดไป (next song)
    • เคาะ p คือเพลงก่อนหน้า (previous song)
    • ถ้าเพลงหมดแล้วจะขึ้นว่า no more song
ไฟล์ที่ 2 ชื่อ getfile.pyw 
  • จะมีฟังก์ชัน getlyric(n,b)
    • n คือ เพลงที่ n
    • b คือ ตำแหน่งในเพลง อ่านครั้งละ 5 บรรทัด มาแสดง
  • โปรแกรมจะอ่านไฟล์ ที่ลงท้ายด้วย .txt
  • แล้ว เก็บไฟล์ไว้ที่ name.song
ตัวโปรแกรม 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...

 



Create Date : 28 มิถุนายน 2563
Last Update : 28 มิถุนายน 2563 7:01:50 น.
Counter : 1502 Pageviews.

0 comments
ชื่อ :
Comment :
 *ใช้ code html ตกแต่งข้อความได้เฉพาะสมาชิก
 

thaiger_u
Location :
  

[ดู Profile ทั้งหมด]
 ฝากข้อความหลังไมค์
 Rss Feed
 Smember
 ผู้ติดตามบล็อก : 3 คน [?]



Dancinga
@ฟรี
โปรแกรมปฏิทิน 2564 - Free android app


@ รับสอน เขียนโปรแกรม Python ระดับมัธยมปลาย
มิถุนายน 2563

 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
29
30
 
 
All Blog