15

Im creating a text based labryinth game, but I figured it needed some pictures to illustrate whats going on. For some reason it just dies when I try to go to a2(Where the picture should pop up). I am a beginner so dont judge my code so much ^^

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

4 Answers 4

52

Instead of

Image.open(picture.jpg)
Img.show

You should have

from PIL import Image

#...

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

You should probably also think about an other system to show your messages, because this way it will be a lot of manual work. Look into string substitution (using %s or .format()).

5
  • 1
    Now it says NameError: name 'Image' is not defined How do I define it? Commented May 5, 2013 at 18:45
  • 3
    Image should be defined when you imported Image at the top
    – Serial
    Commented May 5, 2013 at 19:39
  • @CasperOlsson: I added the correct import to my code, that should work.
    – BrtH
    Commented May 5, 2013 at 19:54
  • 1
    @CasperOlsson This is correct, it should be marked as such.
    – mginn
    Commented Dec 11, 2014 at 18:48
  • Can I convert the image into grayscale by sending any argument?
    – Trect
    Commented Aug 30, 2018 at 11:45
7

Open any file

import os
os.startfile(<filepath>)
1
  • it gives me: AttributeError: module 'os' has no attribute 'startfile'
    – Leo
    Commented Jan 26, 2023 at 20:22
1

This is how to open any file:

from os import path

filepath = '...' # your path
file = open(filepath, 'r')
0
if location == a2:
    img = Image.open("picture.jpg")
    Img.show

Make sure the name of the image is in parantheses this should work

Not the answer you're looking for? Browse other questions tagged or ask your own question.