Pratice game.
I thought I should take a few days and read some more, and learn a little more. I decided to take a more easier approach. This 'start' of a simple pirate game, which I did not reference anything for once. So its all wrote from what I know, and not someone else. That said, you can critique it now. It is still very early function of what will become the Shipyard. So overhauling is a must.
# Pirates First function.
# Variables set.
gold = 300
price = 0
def Ship():
print 'Welcome to the Shipyard!\n'
print 'Here you can buy new Ships.\n'
Shiplist = {'1)Sloop':'1500$', '2)Schooner':'3500$', '3)Brigantine':'6500$'}
print 'The current Ships avaliable:\n'
print Shiplist
print ' '
print 'To Exit type 4'
print 'Type a number to choose the corrosponding Ship!\n'
choice = input('Enter Number: ')
if choice == 1:
global gold
price = 1500
if gold >= price:
print 'You bought a Sloop!\n'
gold -= price
print'You now have:',
print gold,
print 'Gold'
elif gold < price:
print 'You do not have enough Gold!'
if choice == 2:
global gold
price = 3500
if gold >= price:
print 'You bought a Schooner!'
gold -= price
print 'You now have:',
print gold,
print 'Gold'
elif gold < price:
print 'You do not have enough Gold!'
if choice == 3:
global gold
price = 6500
if gold >= price:
print 'You have bought a Brigantine!'
gold -= price
print 'You now have:',
print gold,
print 'Gold'
elif gold < price:
print 'You do not have enough Gold!'
if choice == 4:
print 'Exit'
Clueless86
Junior Poster in Training
76 posts since Jul 2009
Reputation Points: 10
Solved Threads: 1
Thats cool, I can change some of that around for sure. My biggest issue here is I need to understand how to exit things better and do loops more efficient that I know how as of now. basic structure of this I am thinking of is the Functions at the top, then as I complete them I can write most of the other program and have these functions that the player can call upon through options. And then somehow put them back into action after calling a function... That confuses me.
Clueless86
Junior Poster in Training
76 posts since Jul 2009
Reputation Points: 10
Solved Threads: 1
Ok, I changed up a few things, I also added a 'Rep' Feature now. And made the start up loop to the main menu. And you can access the Shipyard from the menu and it now loops back to the menu when done. Also added a line to show your money while in the shipyard.
# Pirates First function.
# Variables set.
gold = 300
price = 0
ship = 0
rep = 1
def S():
print 'Welcome to the Shipyard!\n'
print 'Here you can buy new Ships.\n'
print 'You currently have',gold,'Gold.\n'
Shiplist = {'1)Sloop':'1500$', '2)Schooner':'3500$', '3)Brigantine':'6500$'}
print 'The current Ships avaliable:\n'
print Shiplist
print ' '
print 'To Exit type 4'
print 'Type a number to choose the corresponding Ship!\n'
choice = input('Enter Number: ')
if choice in [1, 2, 3]:
global gold
global price
global ship
exit = 4
if choice == 1:
ship = 'Sloop'
price = 1500
elif choice == 2:
ship = 'Schooner'
price = 3500
elif choice == 3:
ship = 'Brigantine'
price = 6500
else:
print 'Invalid input!'
if gold >= price:
print 'You bought a new ship!'
gold -= price
print 'You now have:',
print gold,
print 'Gold\n'
elif gold < price:
print 'You do not have enough Gold!\n'
print 'Try again later!\n'
elif choice == 4:
print 'Exit\n'
def Gold():
global gold
def Rep():
global rep
rep = rep + 1
print 'You have gained more reputation!\n'
print 'Your Reputation is now:',
print rep
choice = 0
loop = 1
while loop == 1:
print 'Welcome to Pirates!\n'
print 'Please Pick one of the following options!\n'
print 'Type the options as shown!'
print '1)Type: S() to enter the Shipyard!'
choice = input('Input an option! \n')
Clueless86
Junior Poster in Training
76 posts since Jul 2009
Reputation Points: 10
Solved Threads: 1
Okay, I have restructured some more things. I have also added a Tavern and a Black Smith shop. I still need to make some statements to check things, and re do the exit loop from the main menu, but its slowly coming along. The next huge thing will be adding different Missions to preform.
I am not sure how big is to big for python code, but its already huge, and most likely will double in size..
# Pirates First function.
# Variables set.
gold = 600
price = 0
ship = 0
rep = 1
weapon = 0
def S():
print 'Welcome to the Shipyard!\n'
print 'Here you can buy new Ships.\n'
print 'You currently have',gold,'Gold.\n'
Shiplist = {'1)Sloop':'1500$', '2)Schooner':'3500$', '3)Brigantine':'6500$'}
print 'The current Ships avaliable:\n'
print Shiplist
print ' '
print 'To Exit type 4'
print 'Type a number to choose the corresponding Ship!\n'
choice = input('Enter Number: ')
if choice in [1, 2, 3]:
global gold
global price
global ship
exit = 4
if choice == 1:
ship = 'Sloop'
price = 1500
elif choice == 2:
ship = 'Schooner'
price = 3500
elif choice == 3:
ship = 'Brigantine'
price = 6500
else:
print 'Invalid input!'
if gold >= price:
print 'You bought a new ship!'
gold -= price
print 'You now have:',
print gold,
print 'Gold\n'
elif gold < price:
print 'You do not have enough Gold!'
print 'Try again later!\n'
elif choice == 4:
print 'Exit\n'
def Tav():
print 'Hello There! Welcome to the Tavern!\n'
print 'Here you can purchase some beer or liquor!\n'
print '1)Beer 10$'
print '2)Liquor 20$'
print '3)Exit\n'
print 'Enter 1, 2, 3\n'
choice = input('What will it be? ')
if choice in [1, 2]:
global gold
global price
exit = 3
if choice == 1:
price = 10
if gold >= price:
gold -= price
print 'You bought a beer!\n'
print 'You now have',gold,'Gold\n'
elif gold < price:
print 'You do not have enough gold!'
if choice == 2:
price = 20
if gold >= price:
gold -= price
print 'You bought a shot of liquor!\n'
print 'You now have',gold,'Gold\n'
elif gold < price:
print 'You do not have enough gold!'
def Gold():
global gold
def Rep():
global rep
global gold
rep = rep + 1
print 'You have gained more reputation!\n'
print 'Your Reputation is now:',
print rep
def Sm():
print 'Hello! I am Gene Daniels, the Black Smith.'
print 'What can I do for you today?'
print 'Here is what I have right now.\n'
wList = {'1)Short Sword': '100$', '2)Long Sword': '250$', '3)Old Cutlass': '400$'}
print wList
print 'Type: 1, 2, or 3:','Type 4 to exit\n'
choice = input('What can I get you? ')
if choice in [1, 2, 3]:
global gold
global price
exit = 4
if choice == 1:
price = 100
weapon = 'Short Sword'
if choice == 2:
price = 250
weapon = 'Long Sword'
if choice == 3:
price = 400
weapon = 'Old Cutlass'
else:
print 'Invalid Input!'
if gold >= price:
print 'You bought a new weapon!'
gold -= price
print 'You now have',gold,'Gold\n'
elif gold < price:
print 'You do not have enough Gold!\n'
elif choice == 4:
print 'Exit'
choice = 0
loop = 1
while loop == 1:
print 'Welcome to Pirates!\n'
print 'Please Pick one of the following options!\n'
print 'Type the options as shown!\n'
print '1)Type: S() to enter the Shipyard!'
print '2)Type: Tav() to enter the Tavern!'
print '3)Type: Sm() to enter the Black Smith Shop!'
print '4)Type: 4 to exit\n'
choice = input('Input an option! \n')
if choice == 4:
print 'You have quit the game.'
loop = 0
I also need to transfer the Function names into numbers for easier input! So that will be done sometime, its not major right now to me.
Clueless86
Junior Poster in Training
76 posts since Jul 2009
Reputation Points: 10
Solved Threads: 1
Still some errors.
Game are looping on one menu.
Give global error on gold.
If you type wrong an exception is trown(NameError)
Try to avoid to use global so much.
Saw you gave opp classes.
That ok,but you can better with your loop-functions design than it is now.
Just a tips on how to design a menu-system with function and loop.
Try to type wrong,you get a warning and get back to menu.
You dont need to put in code to handle exeptions.
def test1():
'''Info about function'''
print 'My code'
raw_input("\nTrykk enter for meny")
def test2():
'''Info about function'''
print 'My code'
raw_input("\nTrykk enter for meny")
def menuloop():
'''Info about function'''
while True:
print 'Welcome to my menu\n'
print '(1) Choice 1'
print '(2) Choice 2'
print '(q) Quit'
choice = raw_input('Enter your choice: ')
if choice == '1':
test1()
elif choice == '2':
test2()
elif choice == 'q':
exit()
else:
print 'Not a correct choice:', choice
if __name__ == '__main__':
menuloop()
snippsat
Practically a Posting Shark
808 posts since Aug 2008
Reputation Points: 353
Solved Threads: 294
I am not sure I fully understand, I know if I misstype things it will error, and that global gold does not hurt anything, just a stupid bug I cant get ride of right now..
EDIT: I fixed the global gold problem..
Clueless86
Junior Poster in Training
76 posts since Jul 2009
Reputation Points: 10
Solved Threads: 1
I am not sure I fully understand, I know if I misstype things it will error
If someone misstyping,there are 2 options.
You write in code that handlig excepitons.
Or you try to write code that dont need excepiton handling.
Like i did in my example. http://docs.python.org/tutorial/errors.html
Sure you can get this to work,but i have to say your code is somewhat messy.
But try to write a game like this you are learing many thing.
snippsat
Practically a Posting Shark
808 posts since Aug 2008
Reputation Points: 353
Solved Threads: 294
Yes, indeed it may be messy, but as you did say..gotta learn some way and some how.. I have a question or 2 about your code bit. Why define the main loop? and what is __name__=='__main__' ?
I do not think I have seen that used..
EDIT: I got the mainmenu to loop if something is not input properly! I just changed my 'input' to raw_input...And making a few quote changes. I also swaped all the 'if' for 1 if and 3 elif's.
Clueless86
Junior Poster in Training
76 posts since Jul 2009
Reputation Points: 10
Solved Threads: 1
if __name__ == '__main__':
It`s a very commen ting to use in pyhon.
You can take it away make no diffrence.
it's in use when you save your program an want to import it. http://effbot.org/pyfaq/tutor-what-is-if-name-main-for.htm
Just to make it very clear what it`s dos.
Save my kode as menu.py (menuloop program)
Now copy this file an run it.
import menu # menu.py
def test(input_name):
print 'Hi!' ,input_name
while True:
a = raw_input('What to play a game? ')
if a == 'yes':
menu.menuloop() # Call my menuloop program
else:
exit()
test('Roy')
Now it run correct first it run my new code and when a call menuloop() it run that after.
Take away __name__ == '__main__': Run the same.
Now menuloop() start before my new code.
Now you understand what if __name__ == '__main__': dos.
snippsat
Practically a Posting Shark
808 posts since Aug 2008
Reputation Points: 353
Solved Threads: 294
Ok, I will try and read and learn how to use this..No promises tho, but I am sure in time I can learn that little bit. One big question I have is.. I have made a loop for the main menu, and you select different functions from there.. Well, how do I loop a function so I can let them stay in that certian function or choose to exit to the main loop? Take the Shipyard for example.
Clueless86
Junior Poster in Training
76 posts since Jul 2009
Reputation Points: 10
Solved Threads: 1
Ignore the above post, I figured it out...Sometimes I ask before I logically think about it.. lol =)
I also added another Ship type, and refined the menu selection to make it easier. Next I will work on a few small bugs in the input's.
Clueless86
Junior Poster in Training
76 posts since Jul 2009
Reputation Points: 10
Solved Threads: 1
Ok everything syntax wise works fine, but I can not seem to get error handling to work right, If I type anything other than the correct number it comes up NameError: But of course, because I can not figure out a smart way to handle this.. Here is an example 'function' from my program.
choice = input('Type 1, 2: ')
if choice == 1:
person = 6
player = random.randrange(1, 10)
elif player > person:
print 'You have robbed someone!'
gold += random.randrange(20, 150)
print 'You now have',gold,'Gold'
Robber()
elif player < person:
print 'You was not able to rob this person!'
gold -= random.randrange(45, 120)
print 'You got beat down and robbed instead!\n'
print 'You now have',gold,'Gold\n'
print 'Better luck next time!\n'
Robber()
elif choice == 2:
print 'Exit!
I had an 'else' statement but that did nothing at all, still errored when anything other than 1 or 2 is input.
EDIT: I used this and it worked, not sure if this is a totally wrong way, but its on python.org.
try:
choice = int(raw_input('Type 1, 2: '))
except (TypeError, NameError, ValueError):
print 'Invalid Input'
Clueless86
Junior Poster in Training
76 posts since Jul 2009
Reputation Points: 10
Solved Threads: 1
I am still having no luck with getting the functions to loop properly and handle errors..Getting to be ridiculous! I have tried everything in my personal knowledge..
Clueless86
Junior Poster in Training
76 posts since Jul 2009
Reputation Points: 10
Solved Threads: 1
Ok I finally worked through this, and got a system that handles the errors I tell it to handle and passes what I tell it to onto the next clause.
try:
choice = int(raw_input('Enter Number: '))
except ValueError:
print 'Invalid Input! Please input only numbers.'
S()
else:
if choice in [1, 2, 3, 4]:
This is just a small snip of it.
So, moving on to the next thingy thing =)
Clueless86
Junior Poster in Training
76 posts since Jul 2009
Reputation Points: 10
Solved Threads: 1
Can I do a clean up code for the try and except statement such as 'Finally'? I do not plan on Calling the S() function anymore giving that it is a menu type function and is only called by the user, but I still see what you are getting at, so is there a way to do a clean up of it?
Clueless86
Junior Poster in Training
76 posts since Jul 2009
Reputation Points: 10
Solved Threads: 1
When I put it like that it just loops the 'Enter number' part, and dose not let the main menu pop up. I have a Main loop on down the script that loops the main menu... So that disturbs it.
Clueless86
Junior Poster in Training
76 posts since Jul 2009
Reputation Points: 10
Solved Threads: 1