2017-07-28 169 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

好吧,我会下载它,并尝试一下谢谢 –