2017-02-12 147 views
-1

所以......我一直在這個問題難住了一段時間。在線研究後,我還沒有找到答案。當我調用該函數:spawnLoc.getX1從播放器拉線,我打印出來,看看它是什麼,它打印此:我期待它打印數量30.任何人都可以伸出援助之手蟒蛇返回函數名稱不值

import simplegui 

#charictor position decliration 
x1 = 30 
x2 = 30 
y1 = 35 
y2 = 35 

class Room: 
    def __init__(self, roomName, sizeX=0, sizeY=0): 
     self.roomName = roomName 
     self.sizeX = sizeX 
     self.sizeY = sizeY 

class player: 
    def __init__(self, name, spawnLoc): 
     self.name = name 
     self.spawnLoc = spawnLoc 

    def draw(canvas, spawnLoc): 
     print spawnLoc.getX1 
     print spawnLoc.getX2 
     print spawnLoc.getY1 
     print spawnLoc.getY2 
     canvas.draw_line((spawnLoc.getX1, spawnLoc.getX2), (spawnLoc.getY1,  spawnLoc.getY2), 6, 'Red') 

class spawn: 
    def __init__(self, spawnName, spawmX1,spawmX2, spawmY1, spawmY2): 
     self.name = spawnName 
     self.x1 = spawmX1 
     self.x2 = spawmX2 
     self.y1 = spawmY1 
     self.y2 = spawmY2 

    def getX1(self): 
     return x1 

    def getX2(self): 
     return x2 

    def getY1(self): 
     return self.y1 

    def getY2(self): 
     return self.y2 

#def Collisions(rooms, selectedPlayer): 
    #for Room in rooms 
    #if room.x == player.x 

rooms = [ 
     Room('Ticket Hall', 300, 500) 
     ] 

#spawns = [spawn('first', x1, x2, y1, y2)] 
spawn('first', x1, x2, y1, y2) 
player1 = [ player('one', spawn)] 
#player('one', spawns) 

# Handler to draw on canvas 
def draw(canvas): 
    #room draw 
    canvas.draw_line((0, 0), (0, 500), 6, 'Red') 
    canvas.draw_line((0, 0), (300, 0), 6, 'Red') 
    canvas.draw_line((300, 0), (300, 500), 6, 'Red') 
    canvas.draw_line((0, 500), (300, 500), 6, 'Red') 
    #player draw 
    #canvas.draw_line((x1, x2), (y1, y2), 5, 'Red') 
    player.draw(canvas, spawn) 

# Create a frame and assign callbacks to event handlers 
frame = simplegui.create_frame("Home", 600, 600) 
#frame.add_button("Click me", click) 
frame.set_draw_handler(draw) 

# Start the frame animation 
frame.start() 
+0

不需要所有這些「get」方法。只需直接訪問屬性。 – Daniel

回答

1

你忘記了括號。

spawnLoc.getX1 

是一個函數句柄。

spawnLoc.getX1() 

調用函數。

+0

謝謝,就是這樣。完全徹底脫掉然後XD – Jhon122