2013-05-05 84 views
6

我創建了一個基於文本的labryinth遊戲,但我想它需要一些圖片來說明發生了什麼事情。出於某種原因,當我嘗試去a2時(圖片應該彈出),它就會死亡。我是一個初學者,所以不要評判我的代碼,那麼多的^^打開圖像? Python

import Image 
a1 = ("a1 Go to the start of the Labyrinth", "You're at the start of the labyrinth") #Start 
a2 = ("a2 Go Left", "You've chosen to go left, unfortunatly it leads to nothing.") #Fail route 
a3 = ("a3 Go Right", "You've chosen to go right.") #Right route 
a4 = ("a4 Go Left", "You've chosen to go left") #Fail route 
a5 = ("a5 Go Right", "You've chosen to go right") #Right route 
a6 = ("a6 Go Right", "You've chosen to go right") #Fail route; end 
a7 = ("a7 Go Left", "You've chosen to go left") #Fail route 
a8 = ("a8 Go Left", "You've chosen to go left") #Fail route; end 
a9 = ("a9 Go Right", "You've chosen to go right") #Fail route; end 
a10 = ("a10 Go Forwards", "You've chosen to go forwards") #Right route 
a11 = ("a11 Go left", "You've chosen to go left; Congratulations, you won! You may restart") #Right route; end; victory 
fail = ("fail End game", "You lost!") #End game 
b1 = ("b1 Go Left", "You've chosen to go left") 
b2 = ("b2 Go Right", "You've chosen to go right") 
b3 = ("b3 Go Left", "You've chosen to go left") 

transitions = { 
    a1: (a2, a3), 
    a2: (fail, a1), 
    a3: (b1,), 
    b1: (a4, a5,), 
    a4: (b2,), 
    b2: (a7, a6,), 
    a5: (b3,), 
    b3: (a9, a10,), 
    a6: (fail, a1), 
    a7: (a8,), 
    a8: (fail, a1), 
    a9: (fail, a1), 
    a10: (a11,), 
    a11: (a1,) 
} 

location = a1 

while True: 
    print location[1] 
    print ("Here you can: ") 

    for (i, t) in enumerate(transitions[location]): 
     print i + 1, t[0] 

    choice = int(raw_input("Choose one ")) 
    location = transitions[location][choice - 1] 

    if location == a2: 
     Image.open(picture.jpg) 
     Img.show 

回答

20

而不是

Image.open(picture.jpg) 
Img.show 

你應該有

from PIL import Image 

#... 

img = Image.open('picture.jpg') 
img.show() 

你或許應該想想也是的其他系統顯示你的消息,因爲這樣做將是很多手動工作。看看字符串替換(使用%s.format())。

+0

現在說 NameError:名字「形象」是沒有定義 怎麼辦我定義它? – 2013-05-05 18:45:49

+3

在導入頂部的圖像時應該定義圖像 – Serial 2013-05-05 19:39:24

+0

@CasperOlsson:我在我的代碼中添加了正確的導入,應該可以工作。 – BrtH 2013-05-05 19:54:03

1
if location == a2: 
    img = Image.open("picture.jpg") 
    Img.show 

確保圖像的名字是在括號 這應該工作

0

打開任何文件

import os 
os.startfile(<filepath>)