Skip to main content
gamedev.net
🔒 Locked

Quick Python Question

Started by llamaSong 21 replies 4.2k views
Original Post
llamaSong
llamaSong
Can't believe Im asking this but: Some code: terrain = [ [ 1, 1, 1, 1, 1, 1 ],/ [ 1, 0, 0, 0, 0, 1 ],/ [ 1, 0, 0, 0, 0, 1 ],/ [ 1, 0, 0, 0, 0, 1 ],/ [ 1, 0, 0, 0, 0, 1 ],/ [ 1, 0, 0, 0, 0, 1 ],/ [ 1, 1, 1, 1, 1, 1 ],] print terrain When it prints it prints like this [[1, 1, 1, 1, 1, 1], [1, 0, 0, 0, 0, 1], [1, 0, 0, 0, 0, 1], [1, 0, 0, 0, 0, 1], [1, 0, 0, 0, 0, 1], [1, 0, 0, 0, 0, 1], [1, 1, 1, 1, 1, 1]] I could have sworn \ would make the it start prining on the next line. I want it to print out like this: [ 1, 1, 1, 1, 1, 1 ] [ 1, 0, 0, 0, 0, 1 ] [ 1, 0, 0, 0, 0, 1 ] [ 1, 0, 0, 0, 0, 1 ] [ 1, 0, 0, 0, 0, 1 ] [ 1, 0, 0, 0, 0, 1 ] [ 1, 1, 1, 1, 1, 1 ] :( Edit: It screwed up.
Ludi83
Ludi83
llen = len(terrain)i = 0while i < llen:  print terrain  i = i + 1


This would be my solution.
__filip
__filip
for i in terrain:	print i
llamaSong
llamaSong
New question.
It could be possible I am going about this wrong.
Heres what I have:
City = [[ 1, 1, 1, 1, 1, 1, 1], [ 1, 2, 0, 2, 0, 0, 1], [ 1, 0, 0, 0, 0, 0, 1], [ 1, 0, 0, 9, 0, 0, 1], [ 1, 0, 0, 0, 0, 0, 1], [ 1, 1, 1, 1, 1, 1, 1]]
llen = len(City)
i = 0
while i < llen:
print City
i = i + 1


What I need to know how to do is to be able to reference a specific spot on that. Preferbly in X,Y Form.

I want to be able to do this:

Pos = X,Y
Move = input("move")
if Move == W:
City(Pos) = 0
Pos = X,Y+1
City(Pos) = 9

elif Move == S:
City(Pos) = 0
Pos = X,Y-1

elif Move == A:
City(Pos) = 0
Pos = X-1,Y

elif Move == D:
City(Pos) = 0
Pos = X+1,Y

pos = City[X,Y]
Oluseyi
Oluseyi
City is a list. It just so happens to only contain lists. To access a specific element in a list contained by City, you must first subscript City to obtain the list in question, then subscript that list to obtain the element:
City = [[ ... ], [ ... ], ... ]# access coordinate pair (i, j)element = City[j]
llamaSong
llamaSong
Thx for the help, I got that working. I have a new question, do I have to list all my global variables that I want to use in a function?
For example:

global Strglobal MHpglobal Hpglobal Agilglobal MMnglobal Mnglobal Mpglobal Gglobal Lvlglobal Xpglobal Skpglobal Fstglobal Equipedglobal Buffsglobal BuffsEnglobal Romeglobal Locglobal PosXglobal PosYdef warriorstart( ):    global Str    global MHp    global Hp    global Agil    global MMn    global Mn    global Mp    global G    global Lvl    global Xp    global Skp    global Fst    global Equiped    global Buffs    global BuffsEn    print "You have chosen the warrior!"    Str = 18    Hp = 150    Mhp = 150    Agil = 10    MMn = 10    Mn = 10    Mp = 0    G = 10    Lvl = 1    Xp = 0    Skp = 1    Fst = 5    Equiped = [ 'W', 'A', 'S', 'T']    stats( )def stats( ):    global Str    global MHp    global Hp    global Agil    global MMn    global Mn    global Mp    global G    global Lvl    global Xp    global Skp    global Fst    global Equiped    global Buffs    global BuffsEn    print "Level,", Lvl    print "Xp,", Xp    print "Skill Points,", Skp    print "Free Stat Points,", Fst    print "Strength,", Str    print "Agility,", Agil    print "Magic Power,", Mp    print "Health Points,", Hp    print "Mana,", Mn    print "Gold,", G    print "Equiped:", Equiped    print "Assign Stats: 1"    print "Not now: 0"    NowStats = input("Assign Stats Now?")    if NowStats == 1:        while NowStats == 1:            print "Strength: 1"            print "Agility: 2"            print "Magic Power: 3"            print "Mana: 4"            print "Health Points: 5"            print "None: 0"            WhichStat = input("Which Stat")            if WhichStat != 0:                HowMuch = input("How Much")                if HowMuch <= Fst:                    Fst = Fst - HowMuch                    if WhichStat == 1:                        Str = Str + HowMuch                    if WhichStat == 2:                        Agil = Agil + HowMuch                    if WhichStat == 3:                        Mp = Mp + HowMuch                    if WhichStat == 4:                        MMn = MMn + HowMuch                        Mn = Mn + HowMuch                    if WhichStat == 5:                        Hp = Hp + HowMuch                        MHp = MHp + HowMuch            if WhichStat == 0:                   NowStats = 0


It is annoying to keep having to say global X global Y in each of my functions, when I want to use variables X and Y in that function. Is there anyway I can get around having to list every variable each time?

I would much rather have my code look like this:
global Strglobal MHpglobal Hpglobal Agilglobal MMnglobal Mnglobal Mpglobal Gglobal Lvlglobal Xpglobal Skpglobal Fstglobal Equipedglobal Buffsglobal BuffsEnglobal Romeglobal Locglobal PosXglobal PosYdef warriorstart( ):    print "You have chosen the warrior!"    Str = 18    Hp = 150    Mhp = 150    Agil = 10    MMn = 10    Mn = 10    Mp = 0    G = 10    Lvl = 1    Xp = 0    Skp = 1    Fst = 5    Equiped = [ 'W', 'A', 'S', 'T']    stats( )def stats( ):    print "Level,", Lvl    print "Xp,", Xp    print "Skill Points,", Skp    print "Free Stat Points,", Fst    print "Strength,", Str    print "Agility,", Agil    print "Magic Power,", Mp    print "Health Points,", Hp    print "Mana,", Mn    print "Gold,", G    print "Equiped:", Equiped    print "Assign Stats: 1"    print "Not now: 0"    NowStats = input("Assign Stats Now?")    if NowStats == 1:        while NowStats == 1:            print "Strength: 1"            print "Agility: 2"            print "Magic Power: 3"            print "Mana: 4"            print "Health Points: 5"            print "None: 0"            WhichStat = input("Which Stat")            if WhichStat != 0:                HowMuch = input("How Much")                if HowMuch <= Fst:                    Fst = Fst - HowMuch                    if WhichStat == 1:                        Str = Str + HowMuch                    if WhichStat == 2:                        Agil = Agil + HowMuch                    if WhichStat == 3:                        Mp = Mp + HowMuch                    if WhichStat == 4:                        MMn = MMn + HowMuch                        Mn = Mn + HowMuch                    if WhichStat == 5:                        Hp = Hp + HowMuch                        MHp = MHp + HowMuch            if WhichStat == 0:                   NowStats = 0
Emmanuel77
Emmanuel77
Why don't you use a global class with all your useful vars ?


Emmanuel
llamaSong
llamaSong
Of course.
That would be so much easier in general too.
Oluseyi
Oluseyi
Why don't you create objects and explicitly pass them around? That way you can audit access/ownership.
drakostar
drakostar
Quote:
Original post by Emmanuel77
Why don't you use a global class with all your useful vars ?

Because that's horrible design? If you're working with an object-oriented language, your system should be modular. Each class should only know about the objects that it needs. Global variables = evil. Singletons used as a substitute for global variables = evil.
llamaSong
llamaSong
If anyone could link to information/tutorial on objects that would be great. I don't understand what they are.

Anyways, new Question.
class Wolf:    Name = 'Wolf'    Hp = 50    Str = 10    Agil = 40    G = 2    Xp = 1    def Attack( ):        print "The Wolf attacks dealing", Wolf.Str , "damage"        Stat.Hp = Stat.Hp - Wolf.Str	        print "Your Hp now is", Stat.Hp def arena( ):    move = 0    victory = 0    ranval = random.randint(0, 10)    if ranval <= 7:        En = Wolf    print "You are in the Colosseum fightning a", En.Name    Eny.Hp = En.Hp    while move != 3:        if En.Agil > Stat.Agil:            En.Attack( )        print "[1] = Attack"        print "[2] = Spell/Ability"        print "[3] = Flee"        move = input("Choose your move")        if move == 1:            print "You attack the", En.Name , "dealing", Stat.Str , "damage"             Eny.Hp = Eny.Hp - Stat.Str            if Eny.Hp < 0:                Eny.Hp = 0            print "The ", En.Name , "health is", Eny.Hp        if En.Agil <= Agil:            En.Attack( )        if Eny.hp <= 0:            move = 3            print "You killed the" , En.Name            print "You gained", En.G , "Gold"            Stat.G = Stat.G +En.G            print "You gained", En.Xp , "Xp"            Stat.Xp = Stat.Xp + En.Xp


What I am trying to do here with classes, is in the Arena function have En change depending on who your up against. Currently, I want each instance of En to be "Wolf" so that when it says En.Name, the program prints out Wolf, which is from Wolf.Name.

This works fine, except for
En.Attack( )

The error I get is this:
TypeError: unbound method Attack() must be called with Wolf instance as first argument (got nothing instead)

What I think that means is that the program wants me to use Wolf.Attack( ) instead of En.Attack. I would really much rather not do this.

So what should I do?
Kainnvin
Kainnvin
Function whithin a class must have self as argument. So it should be

def Attack(self):
Programming is easy, just copy/paste tutorials...
llamaSong
llamaSong
Thanks

Arg, I changed the Def Attack( ): to Def Attack(self):
and I changed the En.Attack( ) to En.Attack(self)
Now I get
File "/Users/chris/Desktop/NewGame.py", line 103, in arena
En.Attack(self)
NameError: global name 'self' is not defined
Emmanuel77
Emmanuel77
Quote:

Thanks

Arg, I changed the Def Attack( ): to Def Attack(self):
and I changed the En.Attack( ) to En.Attack(self)
Now I get
File "/Users/chris/Desktop/NewGame.py", line 103, in arena
En.Attack(self)
NameError: global name 'self' is not defined


When you call the method, don't provide the self argument. It is already the 'En' object that you call the method on.

define :
def Attack( self ):

And call
En.Attack()


Emmanuel
llamaSong
llamaSong
Quote:
Original post by drakostar
It sounds like you need to read Dive Into Python. Get through the objects section at least.


Thx
Gunna go through the whole thing.
llamaSong
llamaSong
I cant for the life of me get a function inside a class to work.
Heres the error I get.
File "/Projects/NewGame.py", line 108, in EnemyCheck
Wolf.Attack( )
TypeError: unbound method Attack() must be called with Wolf instance as first argument (got nothing instead)

def EnemyCheck( ):
global EnemyName
if EnemyName == Wolf.Name:
Wolf.Attack( )
Fruny
Fruny
Sounds like Wolf is a class, not an object. You are trying to call a method without providing an object. This is probably closer to what you want:
w = Wolf()w.Attack()


Although, if you have an "EnemyName", I assume you also have an "Enemy" ... which should be an object (possibly of type 'Wolf') in the first place.
"Debugging is twice as hard as writing the code in the first place. Therefore, if you write the code as cleverly as possible, you are, by definition, not smart enough to debug it." — Brian W. Kernighan
doctorsixstring
doctorsixstring
Name, Hp, Str, Agil, G, and Xp are currently defined as class variables. That means that the values will be shared between all Wolf objects. Since you probably want each Wolf object to have its own attributes, and you probably want them unique for each object. Something like this:

class Wolf:    def __init__(self, name, hp):        self.name = name        self.hp = hp    def attack(self):        print "A wolf named", self.name, "with", self.hp, "hit points is attacking!"w = Wolf("Big Bad")w.Attack()


The __init__ method is called when a new object is created. I only implemented the name and hp attributes, so I'll leave the other variables as an exercise for you.

Topic Locked

This topic has been locked by a moderator. New replies are not allowed.

Sign in to reply to this topic.