Seeking Beginner Advice for Text-Only Dungeon Game in C# and Python

Started by
157 comments, last by thebigbossman 12 years, 5 months ago
This should be as such
if playerST == 14:
random.seed ()
playerDam = random.randint(2,12)


Since random.seed () returns None. You actually set the variable to none. There is no need to use random.seed () every time you call random function. You need to use it just once in your program..

Another problem in your code is that you put the initial value of playerWeaponDam in a if block; that variable is not actually initialized unless the if condition succeeds. If the if condition fails for some reason, you'l get another error. Always initialize variables OUTSIDE if blocks.

So always do this kind of thing:


# initial value for damage. Set to 0
playerWeaponDamn = 0
if playerWeapon == "sword":
playerWeaponDam = 3
playerSwDamType = "cut"
playerThDamType = "imp"


I once again suggest you learn variable scoping a bit more, before you proceed further otherwise you'll get confused as to why you get errors when you've apparently initialized a variable and the interpreter says you haven't.
Advertisement
Man, thanks sooo much for answering, but I did try putting random.seed() there.

Here is the results:

randomm.png

You'll notice the damage roll is the same every time for all three times I call the function. :( I need the damage roll to be different every time there is a roll.

Here is the code shown in the screen shot:


import random

playerSwST = 14 #The Player's swing (Sw) strength (ST).
playerThrST = 14 #The player's thrust (Thr) strength (ST).
playerWeapon = "sword" #The character's weapon.

if playerSwST == 14:
random.seed()
playerSwDam = random.randint(2,12) #The basic damage roll for a swinging attack.
if playerThrST == 14:
random.seed()
playerThrDam = random.randint(2,12) #The basic damage roll for a thrusting attack.

if playerWeapon == "sword":
playerWeaponSwDam = 3 #The sword adds 3 points to the basic swing damage roll.
playerWeaponThrDam = 3 #The sword adds 3 points to the basic thrust damage roll.
playerSwDamType = "cut" #Ignore. Not yet used.
playerThrDamType = "imp" #Ignore. Not yet used.
hands = 1 #Ignore. Not yet used. Can be 0, 1 or 2. Can't use shield if 2.
elif playerWeapon == "none":
playerWeaponSwDam = 0 #With no weapon, no damage is added to swing attacks.
playerWeaponThrDam = 0 #With no weapon, no damage is added to thrust attacks.
playerSwDamType = "cr" #Ignore. Not yet used.
playerThrDamType = "cr" #Ignore. Not yet used.
hands = 0 #Ignore. Not yet used. Can be 0, 1 or 2. Can't use shield if 2.

def player_dam():
swDam = playerSwDam + playerWeaponSwDam
thrDam = playerThrDam + playerWeaponThrDam
print "Base swing damage: ", playerSwDam
print "Base thrust damage: ", playerThrDam, "\n"
print "Weapon swing damage bonus: +", playerWeaponSwDam
print "Weapon thrust damage bonus: +", playerWeaponThrDam, "\n"
print "Actual swing damage: ", swDam
print "Actual thrust damage: ", thrDam, "\n"
print "*" * 25

player_dam()
player_dam()
player_dam()



This should be as such
if playerST == 14:
random.seed ()
playerDam = random.randint(2,12)


Since random.seed () returns None. You actually set the variable to none. There is no need to use random.seed () every time you call random function. You need to use it just once in your program..

Another problem in your code is that you put the initial value of playerWeaponDam in a if block; that variable is not actually initialized unless the if condition succeeds. If the if condition fails for some reason, you'l get another error. Always initialize variables OUTSIDE if blocks.

So always do this kind of thing:


# initial value for damage. Set to 0
playerWeaponDamn = 0
if playerWeapon == "sword":
playerWeaponDam = 3
playerSwDamType = "cut"
playerThDamType = "imp"


I once again suggest you learn variable scoping a bit more, before you proceed further otherwise you'll get confused as to why you get errors when you've apparently initialized a variable and the interpreter says you haven't.


I'll initialize them outside of the if blocks. Thanks, man.

I'm trying. I'm trying hard. I spent hours and hours today working on this.

You may have noticed that I finally got what you said about the module and putting "random.randint()" instead of putting "randint()" and from random up at the top. It takes me a bit to even understand your advice due to the fact that I'm such a complete newbie and the fact that I'm only slightly more intelligent than a rock.

Thanks again! I really appreciate the help a lot.
A 30-year-old interested in learning to program by making an old-school, simple, text-only or 2D dungeon fantasy video game.

  • As of 9/28/2011, currently attempting to learn the basics of Python 2.7.
  • As of 10/14/11, Dabbling in C#.
  • As of 10/24/11, decisively surpassed my small knowledge of Python in C#.
That's not the fault of the random.seed () function ;)

You've only called the random.randint () once in the whole program if you notice. That's why you got a random number once, but the same value is used every time you call the function.

The function player_dam() uses the same value every time it is called. The block of code which does the random.randint is outside the function and is executed only once in the program. That's why you get the same number.

Note that random.seed () is used to initialize the random number generator and need be called only once.

You've only called the random.randint once in the whole game.

The function uses the same values every time. The block of code which does the random.randint is outside the function and is executed only once.

That's why you get the same number.

I thought it was something like that, but... Hmmm... I'm not sure how to proceed, then. Should I... I'm not even sure what to ask, now.

My combat() function calls a bunch of other functions, like player_skill() and monster_damage(), etc...

EDIT: Oh, if I put all my playerST things (sorry, don't know what to call them :unsure:) in the player_dam() function, then will it re-seed the number every time? I'll try that, anyways.

playerST values will go from like 1 to 30 or so, so that's going to be a lot.

EDIT x2: OH THAT WORKED!!! THANKS SO MUCH, MAN!!! :D

Thanks for donating your time and patients with me, and for all the advice and help. You're very kind. :)
A 30-year-old interested in learning to program by making an old-school, simple, text-only or 2D dungeon fantasy video game.

  • As of 9/28/2011, currently attempting to learn the basics of Python 2.7.
  • As of 10/14/11, Dabbling in C#.
  • As of 10/24/11, decisively surpassed my small knowledge of Python in C#.
It's really simple. Every time you need a random number, you call the random.randint () function.

In this case, you would call the random.randint () inside the function which computes the damage.

Also forget the "seed" part. random.seed () needs to be called only once in the whole program - it is merely to initalize the random number generator in the module random.

It's really simple. Every time you need a random number, you call the random.randint () function.

In this case, you would call the random.randint () inside the function which computes the damage.

Also forget the "seed" part. random.seed () needs to be called only once in the whole program - it is merely to initalize the random number generator in the module random.

Will do. Thanks again. :)
A 30-year-old interested in learning to program by making an old-school, simple, text-only or 2D dungeon fantasy video game.

  • As of 9/28/2011, currently attempting to learn the basics of Python 2.7.
  • As of 10/14/11, Dabbling in C#.
  • As of 10/24/11, decisively surpassed my small knowledge of Python in C#.
[size="1"]EDIT: I think it's actually pretty painfully obvious to me why it doesn't work now, but I'm not sure how to proceed with my actual game code now. I've been doing a lot of reading and I think I understand that mainly functions (and classes?) can reference variables with "global variableName". I really don't know anything about classes yet...


Why doesn't this work? :(

I've been wrestling with this for some time and I'm sure the problem is along the lines of me not fully understanding namespace or variable scope...

enemy = "none"

if enemy == "orc":
enemyName = "orc"
else:
enemyName = "goblin"

fight = "none"

print "An orc draws near!"
fight = raw_input("Fight (F) or Run (R)?: ").lower()
if fight == "f":
print "You fight the orc!"
enemy = "orc"
print enemyName, "attacks you!"
elif fight == "r":
enemy = "orc"
print "You run from", enemyName
else:
print "Not a valid option."


It will use "goblin" as the enemyName, as I'm sure those who actually know something about programming will immediately see though I can not...

My if block can't change the variable outside of it. I get that. But, how do I make it? When I put global enemy it just locks up.

In my actual game code, I thin I can get this to work if I make enemy a number instead of a word, it seems. But, maybe something's up there too.

Help, please?
A 30-year-old interested in learning to program by making an old-school, simple, text-only or 2D dungeon fantasy video game.

  • As of 9/28/2011, currently attempting to learn the basics of Python 2.7.
  • As of 10/14/11, Dabbling in C#.
  • As of 10/24/11, decisively surpassed my small knowledge of Python in C#.
You're not changing enemyName again.

print "An orc draws near!"
fight = raw_input("Fight (F) or Run (R)?: ").lower()
if fight == "f":
print "You fight the orc!"
enemy = "orc"
print enemyName, "attacks you!"


See, you change the enemy variable, yes, but the enemyName is still goblin.

Changing the variable enemy does not automatically change enemyName every time.

I am not sure whether I understood your problem right though!
You understood me correctly. I'm just that stupid!

The program doesn't re-run the if/else every time you change the variable it's dealing with. I see now.

I guess I'll have to write a function that selects which monster you fight in my game.

Thanks again! Even though you were only stating the blatantly, in-your-face-obvious, I still wasn't understanding that the if/else wouldn't automatically update. :rolleyes:
A 30-year-old interested in learning to program by making an old-school, simple, text-only or 2D dungeon fantasy video game.

  • As of 9/28/2011, currently attempting to learn the basics of Python 2.7.
  • As of 10/14/11, Dabbling in C#.
  • As of 10/24/11, decisively surpassed my small knowledge of Python in C#.

[size="1"]EDIT: Found it!!! I needed "global roll" without quotes in my roll functions!!! Man, I've been working on that for an hour. I'll edit my code for other newbies like me. :) The game appears to work.


I'm afraid you've missed the point. The standard way to communicate information between functions is to pass it in via parameters, and pass it out via the return value.

This topic is closed to new replies.

Advertisement