2d arrays?

This is the place for queries that don't fit in any of the other categories.

2d arrays?

Postby spamynator_1 » Wed Oct 23, 2013 12:59 pm

i am trying to create a 2d array for a text based game that makes it so that when you enter an area(self.locx,self.locy) it generates the biome and the objects within the biome, i am trying to make it so that information is stored and that if one were to return to that location, it would have the same biome and objects as when it was generated. i have figured out how to define an array but i cannot figure out how to write to it. The question is how do i define an array with 3 propernties that makes it so that if self.locx,self.locy == 2,3 and it is desert and there are trees and a house then it would save that information. Any help would be appreciated.
spamynator_1
 
Posts: 63
Joined: Sun Mar 03, 2013 12:45 am

Re: 2d arrays?

Postby Marbelous » Wed Oct 23, 2013 7:07 pm

Well, I was going to say something like:

Simplest: Pass around two arrays. One with your (x,y) location and another with the description, objects or whatever and use the same index value on each array.
Better: Use a dictionary with a tuple of (x,y) as the key and an array of whatever as the value.
Best: Use classes and define methods to get/set your attributes in each biome instance.

But, then I noticed you wrote "area(self.locx,self.locy)" indicating that you were already using OOP. Sooo.. Maybe if you define better what you're trying to do? Or post your code?

Word of Warning: If you're new to OOP, DON'T go overboard with your RPG. Code a simple 4 room house where you pick up coins and look for a key to get out using simple directions (e.g.: Go East, Look Around, Pick up Coins, etc.). If you can't do that blindfolded, you have no business out in the "Biomes". ;)
Marbelous
 
Posts: 155
Joined: Fri May 31, 2013 8:12 pm

Re: 2d arrays?

Postby spamynator_1 » Wed Oct 23, 2013 7:25 pm

These "Biomes" are nothing but a single sentence I.E. "You are in a desert". There is no other definition to biome. What i am making is about as simple as your house idea.
my code pardon the harsh language is as follows:
Code: Select all
import random
from numpy import *
print("Welcome to shitstorm")
print("For list of commands, type 'help'")
VARIABLE = "CHODE"
class Game(object):
    def __init__(self):
        self.state = "PLAY"
        self.oldbiome = "fail"
        self.done = False
        #Bars
        self.health = 100
        self.stamina = 100
        self.strength = 100
        self.mana = 100
        #enemyBars
        self.xhealth = 100
        self.xstamina = 100
        self.xstrength = 100
        self.xmana = 100
        self.xdouchebaggery = 0
        #Location
        self.locx = 0
        self.locy = 0
        #Surroundings
        self.trees = False
        self.cave = False
        self.rocks = False
        self.reef = False
        self.notwaves = False
        self.ice = False
        #Print Biome
        self.field = False
        self.forest = False
        self.mountains = False
        self.lake = False
        self.desert = False
        self.taiga = False
        self.tundra = False
        self.jungle = False
        self.plains = False
        self.wastelands = False
        self.ocean = False
        self.doldrum = False
        self.highlands = False
        #Print Surroundings
        self.objects = ""
        #INVENTORY
        self.boat = 0
        self.wood= 0
        self.iron = 0
        self.dirt = 0
        self.rocks = 0
        self.hammer = 0
        self.sword = 0
        self.axe = 0
        self.jacket = 0
        self.skipants = 0
        self.scubagear = 0
        self.nails = 0
        self.screws = 0
        self.metalplate = 0
        self.uarmor = 0
        self.larmor = 0
        self.potion = 0
        self.potion2 = 0
        self.potion3 = 0
        self.potion4 = 0
        self.potion5 = 0
        self.cobweb = 0
        self.loom = 0
        self.silk = 0
        self.sail = 0

    def control(self):
        while not self.done:
            self.input_loop()
       
    def input_loop(self):
        Q = raw_input(": ")
        if Q == ("help"):
            print("This game is made as if it were an actual grid of areas. Everything is based on randomly generated numbers. The commands are: north, south, east, west, stats")
        elif Q == "stats":
            print("Your health is: ")
            print(self.health)
            print("Your stamina is: ")
            print(self.stamina)
            print("Your strength is: ")
            print(self.strength)
            print("Your mana is: ")
            print(self.mana)
        elif Q == "north":
            self.locy += 1
            self.biomegen()
            self.checkbiome()
            self.treegen()
            self.cavegen()
            self.exceptions()
            self.checktree()
            self.checkcave()
            self.checkrocks()
        elif Q == "south":
            self.locy -= 1
            self.biomegen()
            self.checkbiome()
            self.treegen()
            self.cavegen()
            self.exceptions()
            self.checktree()
            self.checkcave()
            self.checkrocks()
        elif Q == "east":
            self.locx += 1
            self.biomegen()
            self.checkbiome()
            self.treegen()
            self.cavegen()
            self.exceptions()
            self.checktree()
            self.checkcave()
            self.checkrocks()
        elif Q == "west":
            self.locx -= 1
            self.biomegen()
            self.checkbiome()
            self.treegen()
            self.cavegen()
            self.exceptions()
            self.checktree()
            self.checkcave()
            self.checkrocks()

    def biomegen(self):
        neegs = random.random()*1000
        if neegs < 40:
            self.field = True
        elif neegs < 60:
            self.forest = True
        elif neegs < 80:
            self.mountains = True
        elif neegs < 82:
            self.lake = True
        elif neegs < 84:
            self.desert = True
        elif neegs < 86:
            self.taiga = True
        elif neegs < 88:
            self.tundra = True
        elif neegs < 90:
            self.jungle = True
        elif neegs < 92:
            self.plains = True
        elif neegs < 94:
            self.wastelands = True
        elif neegs < 96:
            self.ocean = True #must have boat to go into ocean
        elif neegs < 98:
            self.doldrum = True #must be in boat in ocean to generate doldrum
        elif neegs < 100:
            self.highlands = True

    def checkbiome(self):
        if self.field == True:
            print("You are in a grassy field")
        elif self.forest == True:
            print("You are in a forest")
        elif self.mountains == True:
            print("You are in mountains")
        elif self.lake == True:
            print("You are by a lake")
        elif self.desert == True:
            print("You are in a desert")
        elif self.taiga == True:
            print("You are in the taiga")
        elif self.tundra == True:
            print("You are in the tundra")
        elif self.jungle == True:
            print("You are in a jungle")
        elif self.plains == True:
            print("You are in a plains")
        elif self.wastelands == True:
            print("You are in Wastelands")
        elif self.ocean == True:
            print("You are in the ocean")
        elif self.doldrum == True:
            print("You are in a doldrum")
        elif self.highlands == True:
            print("You are in highlands")
           
    def treegen(self):
        trees = random.randint(0,7)
        if trees == 7:
            self.trees = True
        elif trees != 7:
            self.trees = False

    def cavegen(self):
        cave = random.randint(0,25)
        if cave == 25:
            self.cave = True
        elif cave != 25:
            self.cave = False

    def rockgen(self):
        rocks = random.randint(0,7)
        if rocks == 7:
            self.cave = True
        elif rocks != 7:
            self.cave = False

    #Geographical Features
    def checktree(self):
        if self.trees == True:
            self.objects = "There are some trees around"

    def checkcave(self):
        if self.cave == True:
            self.objects = "There is a cave opening"

    def checkrocks(self):
        if self.rocks == True:
            self.objects = "There are some rocks around"

    def checkreefs(self):
        if self.reefs == True:
            self.objects = "There are some coral reefs around"
           
    def exceptions(self):
        if self.forest == True:
            self.trees = True
        elif self.mountains == True:
            self.rocks = True
        elif self.tundra == True:
            self.ice = True

    def matrix(self):#######################
        P = np.array([1, 2, 3], ndmin=2)
       
while VARIABLE == "CHODE":
    exe = Game()
    exe.control()


This code is not currently in working condition but i am asking for focus on the array/matrix part
spamynator_1
 
Posts: 63
Joined: Sun Mar 03, 2013 12:45 am

Re: 2d arrays?

Postby ochichinyezaboombwa » Wed Oct 23, 2013 7:58 pm

This kind of usage of OOP is something I really dislike. All these "self.forest, self.mountains...." – didn't you get tired typing it? What if you want to add another biome ? where should you make changes? (The answer is: everywhere).

A much better approach:
Code: Select all
biomes = ["forest", "tundra", "mountains", "reef", "jungle"] # for example
coords = [(x,y) for y in range(10) for x in range(10)] # or 100, or 1000...
world_map = dict(zip(coords, (random.choice(biomes) for c in coords)))

Now if you
Code: Select all
print world_map[(1,7)]
, you should see something like "jungle" or "reef".

Of course if you want some more meaningful initialization (compared to random) you'll probably need to create a list of biomes / a text file with a list of biomes and initialize each world_map(x,y) from there.

The most important point is : you don't need any members of your class like self.mountain. Instead:
Code: Select all
    def where_am_I(self):
        return self.world_map[(locy,locx)
ochichinyezaboombwa
 
Posts: 200
Joined: Tue Jun 04, 2013 7:53 pm

Re: 2d arrays?

Postby spamynator_1 » Fri Oct 25, 2013 7:16 pm

THAT IS GENIUS!
I attempted to replace my old code with what i think you were saying.
Code: Select all
import random
print("Welcome to shitstorm")
print("For list of commands, type 'help'")
VARIABLE = "CHODE"

biomes = ["field", "forest", "mountains", "lake", "desert", "desert", "taiga", "tundra", "jungle", "plains", "wastelands"]
coords = [(x,y) for y in range(10) for x in range(10)]
world_map = dict(zip(coords, (random.choice(biomes) for c in coords)))

class Game(object):
    def __init__(self):
        self.state = "PLAY"
        self.oldbiome = "fail"
        self.done = False
        #Bars
        self.health = 100
        self.stamina = 100
        self.strength = 100
        self.mana = 100
        #enemyBars
        self.xhealth = 100
        self.xstamina = 100
        self.xstrength = 100
        self.xmana = 100
        self.xdouchebaggery = 0
        #Surroundings
        self.trees = False
        self.cave = False
        self.rocks = False
        self.reef = False
        self.notwaves = False
        self.ice = False       
        #Print Surroundings
        self.objects = ""
        #INVENTORY
        self.boat = 0
        self.wood= 0
        self.iron = 0
        self.dirt = 0
        self.rocks = 0
        self.hammer = 0
        self.sword = 0
        self.axe = 0
        self.jacket = 0
        self.skipants = 0
        self.scubagear = 0
        self.nails = 0
        self.screws = 0
        self.metalplate = 0
        self.uarmor = 0
        self.larmor = 0
        self.potion = 0
        self.potion2 = 0
        self.potion3 = 0
        self.potion4 = 0
        self.potion5 = 0
        self.cobweb = 0
        self.loom = 0
        self.silk = 0
        self.sail = 0

    def control(self):
        while not self.done:
            self.input_loop()
       
    def input_loop(self):
        Q = raw_input(": ")
        if Q == ("help"):
            print("This game is made as if it were an actual grid of areas. Everything is based on randomly generated numbers. The commands are: north, south, east, west, stats")
        elif Q == "stats":
            print("Your health is: ")
            print(self.health)
            print("Your stamina is: ")
            print(self.stamina)
            print("Your strength is: ")
            print(self.strength)
            print("Your mana is: ")
            print(self.mana)
        elif Q == "north":
            y += 1
            self.checkbiome()
            self.treegen()
            self.cavegen()
            self.exceptions()
            self.checktree()
            self.checkcave()
            self.checkrocks()
        elif Q == "south":
            y -= 1
            self.checkbiome()
            self.treegen()
            self.cavegen()
            self.exceptions()
            self.checktree()
            self.checkcave()
            self.checkrocks()
        elif Q == "east":
            x += 1
            self.checkbiome()
            self.treegen()
            self.cavegen()
            self.exceptions()
            self.checktree()
            self.checkcave()
            self.checkrocks()
        elif Q == "west":
            x -= 1
            self.checkbiome()
            self.treegen()
            self.cavegen()
            self.exceptions()
            self.checktree()
            self.checkcave()
            self.checkrocks()
           
    def treegen(self):
        trees = random.randint(0,7)
        if trees == 7:
            self.trees = True
        elif trees != 7:
            self.trees = False

    def cavegen(self):
        cave = random.randint(0,25)
        if cave == 25:
            self.cave = True
        elif cave != 25:
            self.cave = False

    def rockgen(self):
        rocks = random.randint(0,7)
        if rocks == 7:
            self.cave = True
        elif rocks != 7:
            self.cave = False

    #Geographical Features
    def checktree(self):
        if self.trees == True:
            self.objects = "There are some trees around"

    def checkcave(self):
        if self.cave == True:
            self.objects = "There is a cave opening"

    def checkrocks(self):
        if self.rocks == True:
            self.objects = "There are some rocks around"

    def checkreefs(self):
        if self.reefs == True:
            self.objects = "There are some coral reefs around"
           
    def exceptions(self):
        if self.forest == True:
            self.trees = True
        elif self.mountains == True:
            self.rocks = True
        elif self.tundra == True:
            self.ice = True

while VARIABLE == "CHODE":
    exe = Game()
    exe.control()

This returned:
Traceback (most recent call last):
File "F:\Text Based\Shitstorm new.py", line 164, in <module>
exe.control()
File "F:\Text Based\Shitstorm new.py", line 64, in control
self.input_loop()
File "F:\Text Based\Shitstorm new.py", line 80, in input_loop
y += 1
UnboundLocalError: local variable 'y' referenced before assignment



Any ideas? i know this is probably something stupid but i cant seem to figure it out
spamynator_1
 
Posts: 63
Joined: Sun Mar 03, 2013 12:45 am

Re: 2d arrays?

Postby ochichinyezaboombwa » Tue Oct 29, 2013 4:11 am

Sorry I couldn't look at this earlier. You must have figured it out yourself already, but in case: to add something to a variable you must define it 1st somewhere else.

Code: Select all
>>> a = 0
>>> a += 1
>>> y += 1
NameError: name 'y' is not defined
Your y and x are never defined.
ochichinyezaboombwa
 
Posts: 200
Joined: Tue Jun 04, 2013 7:53 pm

Re: 2d arrays?

Postby spamynator_1 » Tue Oct 29, 2013 1:58 pm

sorry im probably completely bastardizing your code with my stupidity, but i ran into an interesting error. I redid a bit of my code in this improved format.
Code: Select all
import random
print("Welcome to shitstorm")
print("For list of commands, type 'help'")
VARIABLE = "CHODE"
#Location
x = 0
y = 0
#Biome stuff
biomes = ["field", "forest", "mountains", "lake", "desert", "desert", "taiga", "tundra", "jungle", "plains", "wastelands"]
coords = [(x,y) for y in range(10) for x in range(10)]
world_map = dict(zip(coords, (random.choice(biomes) for c in coords)))
#Objects/Features
objects = ["trees", "rocks", "caves"]
world_objects = dict(zip(coords, (random.choice(objects) for c in coords)))

class Game(object):
    def __init__(self):
        self.state = "PLAY"
        self.done = False
        #Bars
        self.health = 100
        self.stamina = 100
        self.strength = 100
        self.mana = 100
        #enemyBars
        self.xhealth = 100
        self.xstamina = 100
        self.xstrength = 100
        self.xmana = 100
        self.xdouchebaggery = 0
    def control(self):
        while not self.done:
            self.input_loop()
       
    def input_loop(self):
        global x
        global y
        Q = raw_input(": ")
        if Q == ("help"):
            print("This game is made as if it were an actual grid of areas. Everything is based on randomly generated numbers. The commands are: north, south, east, west, stats")
        elif Q == "stats":
            print("Your health is: ")
            print(self.health)
            print("Your stamina is: ")
            print(self.stamina)
            print("Your strength is: ")
            print(self.strength)
            print("Your mana is: ")
            print(self.mana)
        elif Q == "north":
            y += 1
            self.where_am_i()
            self.what_is_here()
        elif Q == "south":
            y -= 1
            self.where_am_i()
            self.what_is_here()
        elif Q == "east":
            x += 1
            self.where_am_i()
            self.what_is_here()
        elif Q == "west":
            x -= 1
            self.where_am_i()
            self.what_is_here()

    def where_am_i(self):
        return world_map[(x,y)]

    def what_is_here(self):
        return world_objects[(x,y)]

while VARIABLE == "CHODE":
    exe = Game()
    exe.control()

the error i get when you attempt to go a direction is:

Traceback (most recent call last):
File "E:\Text Based\Shitstorm new.py", line 75, in <module>
exe.control()
File "E:\Text Based\Shitstorm new.py", line 33, in control
self.input_loop()
File "E:\Text Based\Shitstorm new.py", line 52, in input_loop
self.where_am_i()
File "E:\Text Based\Shitstorm new.py", line 68, in where_am_i
return world_map[(x,y)]
KeyError: (9, 10)
>>>
spamynator_1
 
Posts: 63
Joined: Sun Mar 03, 2013 12:45 am

Re: 2d arrays?

Postby ochichinyezaboombwa » Tue Oct 29, 2013 5:05 pm

I have no idea how / where you make your y == 10, but there is no such coordinate in your coordinates (hence in world_map), that's why key error.
Details:

You init your coordinates as
Code: Select all
coords = [(x,y) for y in range(10) for x in range(10)]

which means both x and y belong to ranges(10), where range(10) is [0...9]:
Code: Select all
>>> range(10)
[0, 1, 2, 3, 4, 5, 6, 7, 8, 9]

In other words, it doesn't include 10; but your y (or x) is 10.

To demonstrate:
Code: Select all
>>> d = dict(zip( range(5), [n*10 for n in range(5)]) )
>>> d
{0: 0, 1: 10, 2: 20, 3: 30, 4: 40}
>>>
>>> d[0]
0
>>> d[4]
40
>>> d[5]
KeyError: 5
>>>
ochichinyezaboombwa
 
Posts: 200
Joined: Tue Jun 04, 2013 7:53 pm

Re: 2d arrays?

Postby spamynator_1 » Tue Oct 29, 2013 7:39 pm

ok i fixed that by ensuring that you start in the middle of the map by making it so you start at 500,500. Two things, Is the 'return' function supposed to print what the value is?, if not how do i do that. Also when i exit the program or break it. It gives me a EOFerror while reading line at line 44 where my raw_input is. THank you for all of your help

Code: Select all
import random
print("Welcome to shitstorm")
print("For list of commands, type 'help'")
VARIABLE = "CHODE"
#Location
x = 0
y = 0
#Biome stuff
biomes = ["field", "forest", "mountains", "lake", "desert", "desert", "taiga", "tundra", "jungle", "plains", "wastelands"]
coords = [(x,y) for y in range(1000) for x in range(1000)]
world_map = dict(zip(coords, (random.choice(biomes) for c in coords)))
#Objects/Features
objects = ["trees", "rocks", "caves"]
world_objects = dict(zip(coords, (random.choice(objects) for c in coords)))

class Game(object):
    def __init__(self):
        self.state = "START"
        self.done = False
        #Bars
        self.health = 100
        self.stamina = 100
        self.strength = 100
        self.mana = 100
        #enemyBars
        self.xhealth = 100
        self.xstamina = 100
        self.xstrength = 100
        self.xmana = 100
        self.xdouchebaggery = 0
    def control(self):
        global x
        global y
        while not self.done:
            if self.state == "START":
                x = 500
                y = 500
                self.state = "PLAY"
            self.input_loop()
       
    def input_loop(self):
        global x
        global y
        Q = raw_input(": ")
        if Q == "help":
            print("This game is made as if it were an actual grid of areas. Everything is based on randomly generated numbers. The commands are: north, south, east, west, stats")
        elif Q == "stats":
            print("Your health is: ")
            print(self.health)
            print("Your stamina is: ")
            print(self.stamina)
            print("Your strength is: ")
            print(self.strength)
            print("Your mana is: ")
            print(self.mana)
        elif Q == "north":
            y += 1
            self.where_am_i()
            self.what_is_here()
        elif Q == "south":
            y -= 1
            self.where_am_i()
            self.what_is_here()
        elif Q == "east":
            x += 1
            self.where_am_i()
            self.what_is_here()
        elif Q == "west":
            x -= 1
            self.where_am_i()
            self.what_is_here()

    def where_am_i(self):
        return world_map[(x,y)]

    def what_is_here(self):
        return world_objects[(x,y)]

while VARIABLE == "CHODE":
    exe = Game()
    exe.control()
spamynator_1
 
Posts: 63
Joined: Sun Mar 03, 2013 12:45 am

Re: 2d arrays?

Postby ochichinyezaboombwa » Thu Oct 31, 2013 4:17 am

ok i fixed that by ensuring that you start in the middle of the map by making it so you start at 500,500
- No you didn't. You just prolonged the agony. Sometime your x/y will reach either -1 or 1001 and that will crash your program.

You need to write something like this:

Code: Select all
if Q == "west":
   if x == 0:
        print "At the westernmost point already; cannot go in this direction anymore";
        return
and of course guard 3 remaining directions similarly.

Also, I don't see any points in your calls
Code: Select all
            self.where_am_i()
            self.what_is_here()
as they don't do anything and you never use what they return.


You can use try/except to deal with raw_input errors but I wouldn't worry about it at this point. Make it work correctly 1st.
ochichinyezaboombwa
 
Posts: 200
Joined: Tue Jun 04, 2013 7:53 pm

Re: 2d arrays?

Postby spamynator_1 » Thu Oct 31, 2013 5:57 pm

OKay, here is what i got now, i fixed up the code a bit and now it does what i want:
Code: Select all
import random
print("Welcome to shitstorm")
print("For list of commands, type 'help'")
print("Wait for the ':', to begin")
VARIABLE = "CHODE"
#Location
x = 0
y = 0
#Biome stuff
biomes = ["field", "forest", "mountains", "lake", "desert", "desert", "taiga", "tundra", "jungle", "plains", "wastelands"]
coords = [(x,y) for y in range(1000) for x in range(1000)]
world_map = dict(zip(coords, (random.choice(biomes) for c in coords)))
#Objects/Features
objects = ["trees", "rocks", "caves"]
world_objects = dict(zip(coords, (random.choice(objects) for c in coords)))
#Creatures
creatures = ["Rock Creature", "Death Llama", "Sentient Calculator", "Giant Snail"]
critter = dict(zip(coords, (random.choice(creatures) for c in coords)))

class Game(object):
    def __init__(self):
        self.state = "START"
        self.done = False
        #Bars
        self.health = 100
        self.stamina = 100
        self.strength = 100
        self.mana = 100
        #enemyBars
        self.xhealth = 100
        self.xstamina = 100
        self.xstrength = 100
        self.xmana = 100
        self.xdouchebaggery = 0
    def control(self):
        global x
        global y
        while not self.done:
            if self.state == "START":
                x = 500
                y = 500
                self.state = "PLAY"
            self.input_loop()
       
    def input_loop(self):
        global x
        global y
        Q = raw_input(": ")
        if Q == "help":
            print("This game is made as if it were an actual grid of areas. Everything is based on randomly generated numbers. The commands are: north, south, east, west, stats")
        elif Q == "stats":
            print("Your health is: ")
            print(self.health)
            print("Your stamina is: ")
            print(self.stamina)
            print("Your strength is: ")
            print(self.strength)
            print("Your mana is: ")
            print(self.mana)
        elif Q == "north":
            if y == 1000:
                y = 0
                return
            y += 1
            self.display()
        elif Q == "south":
            if y == 0:
                y = 1000
                return
            y -= 1
            self.display()
        elif Q == "east":
            if x == 1000:
                x = 0
                return
            x += 1
            self.display()
        elif Q == "west":
            if x == 0:
                x = 1000
                return
            x -= 1
            self.display()

    def display(self):
        print ("You are in: ")
        print world_map[(x,y)]
        print (" ")
        print ("There is/are: ")
        print world_objects[(x,y)]
        print (" ")
        print ("There are some:")
        print critter[(x,y)]

while VARIABLE == "CHODE":
    exe = Game()
    exe.control()


Now how do i make it so when it is generating all of this stuff that is contained in every position, i can make it that, if you are in a forest there is always trees and in the mountains there is always rocks. Thank you.
spamynator_1
 
Posts: 63
Joined: Sun Mar 03, 2013 12:45 am

Re: 2d arrays?

Postby ochichinyezaboombwa » Thu Oct 31, 2013 8:14 pm

I'd again use a dictionary for that, e.g.:

Code: Select all
things = {
  "forest": [ "trees", "mad squirrels", "mushrooms"],
  "jungle": [ "trees", "giant tigers"],
  "mountain": [ "roks", "and rocks", "and more rocks"],
  ...
}

Then, someplace else :
Code: Select all
def whats_around(self, biome):
    if biome in things:
       print "You are surrounded by", ", ".join(things[biome])
ochichinyezaboombwa
 
Posts: 200
Joined: Tue Jun 04, 2013 7:53 pm

Re: 2d arrays?

Postby spamynator_1 » Fri Nov 01, 2013 3:56 pm

as im sure you can tell im new to dictionaries and i yet again appologize for my stupidity, i took on oop before dictionaries. SO, i tried to encorperate what i think you were getting at and this is what i got after fixing a few errors.
Code: Select all
import random
print("Welcome to shitstorm")
print("For list of commands, type 'help'")
print("Wait for the ':', to begin")
VARIABLE = "CHODE"
#Location
x = 0
y = 0
#Biome stuff
biomes = ["field", "forest", "mountains", "lake", "desert", "taiga", "tundra", "jungle", "plains", "wastelands"]
coords = [(x,y) for y in range(1000) for x in range(1000)]
world_map = dict(zip(coords, (random.choice(biomes) for c in coords)))
#Objects/Features
objects = {
    "field": ["grass"],
    "forest": ["trees"],
    "mountains": ["rocks"],
    "lake": ["water"],
    "desert": ["sand"],
    "taiga": ["snow"],
    "tundra": ["snow"],
    "jungle": ["trees"],
    "plains": [],
    "wastelands": [],
    }

class Game(object):
    def __init__(self):
        self.state = "START"
        self.done = False
        #Bars
        self.health = 100
        self.stamina = 100
        self.strength = 100
        self.mana = 100
        #enemyBars
        self.xhealth = 100
        self.xstamina = 100
        self.xstrength = 100
        self.xmana = 100
        self.xdouchebaggery = 0
    def control(self):
        global x
        global y
        while not self.done:
            if self.state == "START":
                x = 500
                y = 500
                self.state = "PLAY"
            self.input_loop()
       
    def input_loop(self):
        global x
        global y
        Q = raw_input(": ")
        if Q == "help":
            print("This game is made as if it were an actual grid of areas. Everything is based on randomly generated numbers. The commands are: north, south, east, west, stats")
        elif Q == "stats":
            print("Your health is: ")
            print(self.health)
            print("Your stamina is: ")
            print(self.stamina)
            print("Your strength is: ")
            print(self.strength)
            print("Your mana is: ")
            print(self.mana)
        elif Q == "north":
            if y == 1000:
                y = 0
                return
            y += 1
            self.display()
            self.around(biomes)
        elif Q == "south":
            if y == 0:
                y = 1000
                return
            y -= 1
            self.display()
            self.around(biomes)
        elif Q == "east":
            if x == 1000:
                x = 0
                return
            x += 1
            self.display()
            self.around(biomes)
        elif Q == "west":
            if x == 0:
                x = 1000
                return
            x -= 1
            self.display()
            self.around(biomes)

    def display(self):
        print ("You are in: ")
        print world_map[(x,y)]

    def around(self, biomes):
        if biomes in objects:
            print "You are surrounded by", ", ".join(objects[biomes])

while VARIABLE == "CHODE":
    exe = Game()
    exe.control()


this returns the error:
Traceback (most recent call last):
File "G:\Text Based\Shitstorm_0.03.py", line 106, in <module>
exe.control()
File "G:\Text Based\Shitstorm_0.03.py", line 50, in control
self.input_loop()
File "G:\Text Based\Shitstorm_0.03.py", line 73, in input_loop
self.around(biomes)
File "G:\Text Based\Shitstorm_0.03.py", line 101, in around
if biomes in objects:
TypeError: unhashable type: 'list'

please tell me if im just completely bastardizing this coding language. THank you for tolerating me
spamynator_1
 
Posts: 63
Joined: Sun Mar 03, 2013 12:45 am


Return to General Coding Help

Who is online

Users browsing this forum: Compu-Python and 4 guests