| | |
game creation issue
Please support our Python advertiser: Programming Forums - DaniWeb Sister Site
![]() |
•
•
Join Date: Jun 2009
Posts: 5
Reputation:
Solved Threads: 0
Hello.I'm working on a d20 based game system and I'm pretty new to python.What I've done so far is:
create 6 random rolls with values 3-18
rollOne
rollTwo
rollThree etc.
There are 6 stats that need theese rolls assigned
statOne
statTwo
etc.
I've done the part on how to assign rolls and it works,but:
What can I do to prevent using same roll for two stats?(eg.rollOne can be used for statOne and other stats,and it shouldn't).thank you.for complete source code and further explications contact me by e-mail muntean.horia@yahoo.com
create 6 random rolls with values 3-18
rollOne
rollTwo
rollThree etc.
There are 6 stats that need theese rolls assigned
statOne
statTwo
etc.
I've done the part on how to assign rolls and it works,but:
What can I do to prevent using same roll for two stats?(eg.rollOne can be used for statOne and other stats,and it shouldn't).thank you.for complete source code and further explications contact me by e-mail muntean.horia@yahoo.com
The easiest way to do this is to use python's inbuilt random library, with the randint function. This function takes two integers, which define the range between which you want to get a random number.
You can use it like so:
Enjoy the gaming! This was how I got into python too.
You can use it like so:
Python Syntax (Toggle Plain Text)
from random import * def setStats(numberOfStats): stats = [] while numberOfStats > 0: stat = randint(3, 18) stats.append(stat) numberOfStats -= 1 print stats setStats(6)
Enjoy the gaming! This was how I got into python too.
•
•
Join Date: Jun 2009
Posts: 5
Reputation:
Solved Threads: 0
thank you for the fast reply.I have used the random function,this works just fine.
I have #This will simulate 6 rolls of 3d6
rollOne = random.randint(3, 18)
rollTwo = random.randint(3, 18)
rollThree = random.randint(3,18)
rollFour = random.randint(3, 18)
rollFive = random.randint(3, 18)
rollSix = random.randint(3, 18)
and
#I've done the assignment system.can't write the whole code
strength = ' '
dexterity = ' '
constitution = ' '
intelligence = ' '
wisdom = ' '
charisma = ' '
The problem I was talking about is,that I CAN assign rollOne(for eg.) for more than one stat.and each of the six stats must have ONE DIFFERENT ROLL assigned...what can I do to prevent this?
If I use a lot of 'if' after each stat has been defined it won't work,because,look at this example:
#this will assign rollOne for strength stat
def chooseStrength():
strengthChosen = raw_input()
strengthChosen = int(strengthChosen)
if strengthChosen == 1:
rollOne = str(rollOne)
print 'You have chosen the first roll as your strength.Your strength is ' + rollOne + '.'
strength = rollOne
if I do something like:
if dexterityChosen == strengthChosen:
chooseStrenght()
it's not OK because "if dexterityChosen == strengthChosen:" means: if int(dexterityChosen) == int(strengthChosen) , and both rollOne and rollTwo can have the same value...
I have #This will simulate 6 rolls of 3d6
rollOne = random.randint(3, 18)
rollTwo = random.randint(3, 18)
rollThree = random.randint(3,18)
rollFour = random.randint(3, 18)
rollFive = random.randint(3, 18)
rollSix = random.randint(3, 18)
and
#I've done the assignment system.can't write the whole code
strength = ' '
dexterity = ' '
constitution = ' '
intelligence = ' '
wisdom = ' '
charisma = ' '
The problem I was talking about is,that I CAN assign rollOne(for eg.) for more than one stat.and each of the six stats must have ONE DIFFERENT ROLL assigned...what can I do to prevent this?
If I use a lot of 'if' after each stat has been defined it won't work,because,look at this example:
#this will assign rollOne for strength stat
def chooseStrength():
strengthChosen = raw_input()
strengthChosen = int(strengthChosen)
if strengthChosen == 1:
rollOne = str(rollOne)
print 'You have chosen the first roll as your strength.Your strength is ' + rollOne + '.'
strength = rollOne
if I do something like:
if dexterityChosen == strengthChosen:
chooseStrenght()
it's not OK because "if dexterityChosen == strengthChosen:" means: if int(dexterityChosen) == int(strengthChosen) , and both rollOne and rollTwo can have the same value...
•
•
Join Date: Dec 2006
Posts: 1,062
Reputation:
Solved Threads: 299
•
•
•
•
What can I do to prevent using same roll for two stats?
Python Syntax (Toggle Plain Text)
import random ## random can come up with the same number twice, so append ## integers until 6 unique numbers are in the list def random_rolls(): rolls_list = [] ctr = 0 while ctr < 6: x = random.randint(3,18) if x not in rolls_list: rolls_list.append(x) ctr += 1 return rolls_list #this will assign rollOne for strength stat def chooseStrength(rolls_list): strengthChosen = raw_input("Choose a roll number between 1 and %d: " \ % (len(rolls_list))) ## arrays are accessed via memory offset, so use ## zero through 5 instead of 1 --> 6 (one less) strengthChosen_int = int(strengthChosen) - 1 strength = rolls_list[strengthChosen_int] print 'You have chosen the %s roll as your strength.Your strength is %d.' \ % (strengthChosen, strength) ## now delete that number so it's not used again del rolls_list[strengthChosen_int] return strength, rolls_list ##------------------------------------------------------------------- rolls_list = random_rolls() print "You would use these numbers for the rolls:" for x in rolls_list: print x print "\nLet's do it again to test for different numbers" rolls_list = random_rolls() print "You would use these numbers for the rolls:" for x in rolls_list: print x ## copy the list if you want to keep the original orig_rolls = rolls_list[:] ## now chooseStrength as an example of how to use it strength, rolls_list = chooseStrength(rolls_list) print "Strength has been set to", strength print "\nand rolls_list is now smaller" for x in rolls_list: print x
self.rolls_list = []
and it will be visible throughout the class, i.e. you don't have to pass and return to/from functions.
Last edited by woooee; Jun 2nd, 2009 at 9:31 pm.
woooee, I don't think he/she's concerned with having unique stat rolls (at least that's not the case in most d20 games).
Horia.Muntean, I think your problem is that you have too many functions. You shouldn't have one for each stat, but rather one function that can handle them all.
Here's how I would do it. Give it a try and see how you like it. It creates a list of stats based on the number the user enters (so six for D&D), then pops one stat out of the list each time the user asks for a specific stat, like Strength.
Horia.Muntean, I think your problem is that you have too many functions. You shouldn't have one for each stat, but rather one function that can handle them all.
Here's how I would do it. Give it a try and see how you like it. It creates a list of stats based on the number the user enters (so six for D&D), then pops one stat out of the list each time the user asks for a specific stat, like Strength.
Python Syntax (Toggle Plain Text)
from random import * def setStats(numberOfStats): stats = [] while numberOfStats > 0: stat = randint(3, 18) stats.append(stat) numberOfStats -= 1 return stats def chooseStat(statsList): statName = raw_input("Choose the stat you're rolling for now: ") stat = statsList.pop() print "Your " + statName + " stat is " + str(stat) + "." def rollStats(): numberOfStats = raw_input("Choose how many stats you want to roll for: ") numberOfStats = int(numberOfStats) statsList = setStats(numberOfStats) while statsList: chooseStat(statsList) rollStats()
•
•
Join Date: Jun 2009
Posts: 5
Reputation:
Solved Threads: 0
another little question if you don't mind.I have several python text editors on my linux OS all can run the program and print the output,but what I would like is a program that could show the value change without having to write "print" for each variable.I have something like that for Java.for eg. if I run the program, it shows directly the variable progression like:
HP = 20(at the begining of the fight)
after successful hit was made on the subject it shows the value of the hit and the remaining HP.is there something like that for python on linux platform?or windows if necessary..the reason why I need this is because I'm trying only to make the game system for further use,not a full python game
HP = 20(at the begining of the fight)
after successful hit was made on the subject it shows the value of the hit and the remaining HP.is there something like that for python on linux platform?or windows if necessary..the reason why I need this is because I'm trying only to make the game system for further use,not a full python game
![]() |
Similar Threads
- geforce 8600 GT blurry line issue (Monitors, Displays and Video Cards)
- Interested in making a game? (Game Development)
- NEW PDF File creation issue using PHP (PHP)
- PHP Game Creation Help (Game Development)
- PC Game Video Issue (Troubleshooting Dead Machines)
- What language to start with (Computer Science)
- Big Game need progrmmers (C++)
- New To Programming = ME!! (C++)
Other Threads in the Python Forum
- Previous Thread: Biopython related question
- Next Thread: How can I fetch web pages in Python using sockets (not urllib)?
| Thread Tools | Search this Thread |
Tag cloud for Python
ansi assignment avogadro backend beginner binary bluetooth character cmd code customdialog data decimals dictionary drive dynamic error examples excel exe file float format ftp function gnu graphics gui heads homework http ideas import input java leftmouse line linux list lists logging loop module mouse newb number numbers output parsing path pointer port prime program programming progressbar projects push py2exe pygame pyqt python random recursion recursive refresh schedule screensaverloopinactive script scrolledtext sqlite ssh statistics stdout string strings sudokusolver sum table terminal text thread threading time tkinter tlapse tricks tuple tutorial ubuntu unicode update urllib urllib2 variable wikipedia windows write wxpython xlib






