2017-07-28 168 views
-2

我正在製作一個樂譜應用程序,現在我需要創建一個列表(或任何可以讓我這樣做),將所有這些信息(見下文)存儲爲一個項目,然後將其打印出來或更好地插入它變成我的代碼通過一組函數來操作...創建函數列表?

print('Note' + '(' + str(wnote) + ', ' + repr(staff) + ', ' + str(measure) + ', ' + repr(note) + ', ' + repr(notetype) + ')' + '.ExNote()') 

所有打印出這樣的事情...

Note(8, '4R', 4, 'c', 'Ethnote').ExNote() 

,當硬編碼到我的代碼經過這些類函數並在我的樂譜上打印出第八張音符......

class Note: 
    def __init__(self, Num, staff, measure, note, notetype): 
     self.staff = staff 
     self.measure = measure 
     self.note = note 
     self.notetype = notetype 
     self.Num = Num 
    def Wmeasure(self): 
     return (self.measure-1)*153 

    def Wnotetype(self): 
     if self.notetype == 'Ethnote': 
      X= {'1':x+5, '2':x+22, '3':x+39, '4':x+56, '5':x+73, '6':x+90, '7':x+107, '8':x+124} 
     elif self.notetype == 'Fourthnote': 
      X={'1':x+19, '2':x+50, '3':x+81, '4':x+112} 
     elif self.notetype == 'Halfnote': 
      X={'1':x+39, '2':x+90} 
     elif self.notetype == 'note1': 
      X={'1':x+64, '2': x+64} 
     return X[str(self.Num)] 
    def Wnote(self): 
     YL={'b': y+76, 'a': y+80, 'g':y+84, 'f':y+88, 'e':y+92, 'd':y+96, 'c':y+100, 'b2':y+104, 'a2':y+108, 'a3': y+112} 
     YR= {'c': 62, 'd': 58, 'e': 54, 'f': 50, 'g':46, 'a':42, 'b':38, 
     'c2':34, 'd2':28 , 'e2':24, 'f2':20, 'g2':16, 'a2':12, 'b2':8, 'c3':4, 'd3':0} 
     if self.staff in ['1L', '2L', '3L', '4L']: 
     #self.staff == '1L': # or '2L' or '3L' or '4L': 
      return YL[self.note] #+ self.Wstaff() 
     else: #if self.staff == '1R' or '2R' or '3R' or '4R': 
      return YR[self.note] #+ self.Wstaff() 
    def Wstaff(self): 
     if self.staff in ['1L', '1R']: 
      j = 0 
     elif self.staff in ['2L', '2R']: 
      j = 160 
     elif self.staff in ['3L', '3R']: 
      j = 320 
     elif self.staff in ['4L', '4R']: 
      j = 480 
     return j 
    def getcoord(self): 
     return (self.Wmeasure() + self.Wnotetype()), (self.Wstaff() + self.Wnote()) 
    def ExNote(self): 
     if self.notetype == 'Ethnote': 
      screen.blit(EthnoteIMG, self.getcoord()) 
     elif self.notetype == 'Fourthnote': 
      screen.blit(FourthnoteIMG, self.getcoord()) 
     elif self.notetype == 'Halfnote': 
      screen.blit(HalfnoteIMG, self.getcoord()) 
     elif self.notetype == 'note1': 
      screen.blit(note1IMG, self.getcoord()) 

所以我的下一個步驟是讓一個列表或東西存儲此...

('Note' + '(' + str(wnote) + ', ' + repr(staff) + ', ' + str(measure) + ', ' + repr(note) + ', ' + repr(notetype) + ')' + '.ExNote()') 

...作爲一個項目,然後我必須做出一個函數,它在所有的項目列表並以某種方式將它們插入到我的代碼中,因爲只是將它們打印出來將不會執行任何操作。
好吧,所以我試過這並不能解決整個問題,但肯定會讓我更加密切,但它不工作,我不知道爲什麼。我在一個單獨的文件,因爲這是更容易測試的這一切,並有

Note on Sheet Music IMAGE

+0

我已經添加了一些基本的格式並內嵌了您的圖片。請[編輯]您的問題,並將您的代碼粘貼爲_text_,而不是張貼屏幕截圖。您可以選擇它並按下Ctrl + K或單擊「{}」按鈕進行正確格式化。 – Chris

+0

我試過了。 {}按鈕和Ctrl K不做任何事情,它只是說代碼沒有正確格式化,因爲它不是打算即使它。 –

+0

@Chris OK !!它終於奏效了!我把代碼放在正常的地方 –

回答

0

如果我理解你(是的,我看了你的備用quesion,但我喜歡這個更好),你想要一個沒有任何錯誤或任何東西你可以很容易地轉換出來的文字表示法。如果這是正確的,那麼我建議是這樣jsonpickle這將大致是這樣的:

from note import Note 
import jsonpickle 

note = Note(8, '4R', 4, 'c', 'Ethnote') 

text = jsonpickle.encode(note) 

print(text) 

object = jsonpickle.decode(text) 

object.ExNote() 

輸出

> python3 test.py 
{"py/object": "note.Note", "Num": 8, "measure": 4, "note": "c", "notetype": "Ethnote", "staff": "4R"} 
... 

(然後一堆錯誤,因爲我沒有Pygame的加載或任何你用來做你的screen.blit())。

您可以自定義jsonpickle來決定從JSON視角對事物進行編碼的方式/方式。

+0

好吧,我會下載它,並嘗試一下謝謝 –