Sign in to follow this  
llamaSong

Quick Python Question

Recommended Posts

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.

Share this post


Link to post
Share on other sites
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 = 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]

Share this post


Link to post
Share on other sites
Guest Anonymous Poster
In X,Y Form: (Y_Position * Width_Of_Map) + X_Position

So, to access position (3,6) of your map, which is, say, 15 x 15 grid, you would use: (6 * 15) + 3. If you want to move west of that position, you would simply do (6 * 15) + 4. And you would obviously want to check that that is a valid position before trying to access it.

Share this post


Link to post
Share on other sites
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[i][j]

Share this post


Link to post
Share on other sites
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 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
global Rome
global Loc
global PosX
global PosY

def 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 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
global Rome
global Loc
global PosX
global PosY

def 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

Share this post


Link to post
Share on other sites
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.

Share this post


Link to post
Share on other sites
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?

Share this post


Link to post
Share on other sites
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

Share this post


Link to post
Share on other sites
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

Share this post


Link to post
Share on other sites
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( )

Share this post


Link to post
Share on other sites
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.

Share this post


Link to post
Share on other sites
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.

Share this post


Link to post
Share on other sites
Quote:
Original post by llamaSong
I could have sworn \ would make the it start prining on the next line.


I'd like to clarify that the syntax used when declaring a Python variable has nothing to do with how it is printed with the print command.

This 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 ],]



is exactly the same as this...
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 ], ]



The / character simply allows you to break a command into multiple lines for ease of development.

- Mike

Share this post


Link to post
Share on other sites
Quote:
Original post by doctorsixstring
Quote:
Original post by llamaSong
I could have sworn \ would make the it start prining on the next line.


I'd like to clarify that the syntax used when declaring a Python variable has nothing to do with how it is printed with the print command.

This code...
*** Source Snippet Removed ***

is exactly the same as this...
*** Source Snippet Removed ***

The / character simply allows you to break a command into multiple lines for ease of development.

- Mike


Except of course that it should, indeed, be a \ character rather than /. :)

(Honestly, I have no idea how *so many* beginning programmers can get them mixed up so much. Seriously, how did you type in the URL for gamedev.net and *get here* successfully? Programming requires attention to detail; never let a / slip in where you mean \ or vice versa.)

(Note that inside source boxes, and possibly in other contexts, the forums will mess up lines that end with a \ (ironically enough, because the forum code also understands the concept of \ being a line-continuation character, but seemingly doesn't understand it in a *useful* way). To get around this, put a space after the backslash, and warn people to remove those spaces if they copy the code locally for testing.)

Share this post


Link to post
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now

Sign in to follow this