嗨,我是编程新手,我正在努力修改商店中的币种。该代码可以运行,但每个项目的修改货币金额相同,尽管我已将其编码为对不同项目采用不同金额。
我尝试过修改每个金额,但它会从第一项中提取金额并将其应用于所有项目
import math
import random
import os
import sys
import time
import pygame
pygame.init()
displayw = 800
displayh = 600
#pygame.mixer.music.load('')
#pygame.mixer.music.play('')
#pygame.mixer.music.pause('')
#pygame.mixer.music.stop('')
#pygame.mixer.Sound('')
#win = pygame.display.set_mode((displayw, displayh))
#pygame.display.set_caption('The Lodge')
clock = pygame.time.Clock()
inventory = []
###################### ITEM LIST
lighter = 'lighter'
axe = 'axe'
bandage = 'bandage'
jacket = 'jacket'
###################### CLASSES
class user:
def __init__(self, name):
self.name = name
self.health = 100
self.gold = 100
self.lvl = 1
class enemy:
def __init__(self, health, goldg):
self.name = 'Monster LVL 1'
self.health = 25
self.goldg = 15
###################### MAIN SKELETON INTRO ############################################################################################
def main():
global player
global weapon
global item
global itemadd
print('# version 1.0.0, decription - multichoice text based RPG, enter numbers or KEYWORDS')
time.sleep(5)
print('\t\t########## Chapter 1 ##########')
time.sleep(1)
print('What is your name?')
option = input(':> ')
player = user(option)
print('Be careful', player.name, ', enemies can be anywhere!')
run()
def run():
print('\t\t########## THE LODGE ##########')
time.sleep(1)
os.system('cls')
print('Hi', player.name, 'Welcome to the lodge, here are your stats')
print('\n\t\t', player.name, '| Health', player.health, '| Gold', player.gold, '| LVL', player.lvl)
print('')
print('Here is what you have in your inventory: ', inventory)
time.sleep(2)
os.system('cls')
print('The room is well lit from the fire burning, there is no one in here other than the receptionist, hunched over the log counter reading an old paper.')
print('You approach the receptionist cautiously...')
time.sleep(3)
print('He looks overly tired..like he hasnt slept in weeks. Im not surprised with this place though, it could do with a real good makeover')
time.sleep(3)
os.system('cls')
print('**Receptionist** - phewwww, you must be ', player.name, '.Your room is ready..feel free to look around before you go up. We have some new things in the store')
option1 = input('Where do you want to go?\n1. Your room\n2. The store\n3. Look around\n:> ')
if option1 == '1':
lodgeroom()
elif option1 == '2':
store()
elif option1 == '3':
explore()
else:
print(error)
lodge()
def error():
print('Oops thats not an option, please enter a valid choice')
########################################### AFTER SKELETON #########################################################################################
def explore(): ###kitchen ## landing ## cabinet ## currentroom ## bookcase
print('**Receptionist** - There are plenty of books to read if you are bored.')
print('The room is well maintained and the fire is still burning hot. You see there are 2 chairs placed facing each other with a table directly inbetween them.')
time.sleep(3)
print('Over on the other side of the room is the entrance to the Kitchen just besides the stair case that leads to your room.')
print('You notice a bookshelf on the right stacked with leather bound books next to the exit sign that leads to the court yard')
time.sleep(4)
option1 = input('Where do you want to explore?\n:> ')
if option1 =='kitchen' or 'Kitchen':
kitchen()
elif option1 == 'bookcase' or 'Bookcase':
bookcase()
elif option1 == 'armchair' or 'Armchair' or 'arm chair' or 'Arm chair' or 'Arm Chair':
puzzle()
elif option1 == 'court yard' or 'Court yard':
courtyard()
if option1 == 'exit' or 'Exit':
courtyard()
else:
print(error)
def lodgeroom():
print('\t\t########## LODGE BEDROOM ##########')
time.sleep(1)
print('The door creeks open as the cold brass handle rattles in your hand. The smell of pine sniks deep into your nostrils')
print('This is your room, you can save your game here or rest to gain health')
time.sleep(3)
option1 = input('1. Rest\n2.Save\n3.Exit\n:> ')
if option1 == '1':
print('You take off your clothes and lay down to res in the old quilted bed, when you wake you will be back in the lodge. Dont worry your belongings are safe here')
if player.health < 100:
player.health = 100
lodge()
if option1 == '2':
pass
if option1 == '3':
print('You close the door behind you and return to the lodge main room')
lodge()
else:
print(error)
############################################## MAIN STORE ###########################################################################################
def store():
print('\t\t########## Store ##########')
print('Items you already have are listed below\n', inventory)
time.sleep(1)
print('Items available\n\t\t1. lighter: 15\n\t\t2. axe: 70\n\t\t3. bandage: 50\n\t\t4. jacket: 500\n\t\t5. EXIT')
time.sleep(3)
buy = input('What do you need?\n:> ')
if buy == '1' and player.gold > 15:
blighter()
if buy == '2' and player.gold > 70:
baxe()
if buy == '3' and player.gold > 50:
bbandage()
if buy == '4' and player.gold > 500:
bjacket()
if buy == '5':
lodge()
else:
print('You do not have enough gold to buy this item, please come back when you have enough!')
########################################## STORE END #############################################################################
def lodge():
print('\t\t########## THE LODGE ##########')
time.sleep(1)
os.system('cls')
print('Hi', player.name, 'Welcome back to the lodge, here are your stats')
print('\n\t\t', player.name, '| Health', player.health, '| Gold', player.gold, '| LVL', player.lvl)
print('')
print('Here is what you have in your inventory: ', inventory)
option1 = input('Where do you want to go?\n1. Your room\n2. The store\n3. Look around\n4. Exit to the Court Yard\n:> ')
if option1 == '1':
lodgeroom()
elif option1 == '2':
store()
elif option1 == '3':
explore()
elif option1 == '4':
courtyard()
else:
print(error)
lodge()
def courtyard():
print('A Monster is approaching!\nIts dark outside and you can just about make the shape of the creature. It is on all fours progressing slowly towards you...you turn to run back inside and notice a sharp stick on the ground')
option = input('Do you grab the stick to "fight" or do you "run" back inside?\n:> ')
if option == 'fight' or 'Fight':
print('As you bend down to pick up the stick the beast starts moving quicker. You can hear it drawing close. Just as it is about to pounce you turn and jam the stick through its neck')
print('The beast bleeds out and dies, it looks like some sort of hybrid between a wolf and a human')
player.gold =+ enemy.goldg
print('You have gained', enemy.goldg, 'gold and Leveled up!')
player.lvl += 1
lodge()
if option == 'run' or 'Run':
print('As you go to run back inside the beast pounches on your back taking a chunk of skin')
player.health -= enemy.health
print('You should go get some rest to recover your health!')
lodge()
else:
print(error)
##################### MAIN STORY LINES #####################
def kitchen():
pass
def puzzle():
pass
def armchair():
pass
def bookcase():
pass
###################### ITEM LIST #########################
def blighter():
print('You bought a lighter!')
player.gold -= 15
inventory.append(lighter)
def baxe():
print('You bought an Axe!')
player.gold -= 70
inventory.append(axe)
def bbandage():
print('You bought a bandage!')
player.gold -= 50
inventory.append(bandage)
def bjacket():
print('You bought a jacket')
player.gold -= 500
inventory.append(jacket)
main()
没有错误显示,但我希望货币金额能正确修改
答案 0 :(得分:0)
您需要使用无限循环来始终存储在存储中,而不是在每个函数结束时返回存储。让我们检查以下代码段。
def blighter():
print('You bought a lighter!')
player.gold -= 15
inventory.append(lighter)
def baxe():
print('You bought an Axe!')
player.gold -= 70
inventory.append(axe)
def bbandage():
print('You bought a bandage!')
player.gold -= 50
inventory.append(bandage)
def bjacket():
print('You bought a jacket')
player.gold -= 500
inventory.append(jacket)
def lodge():
pass
def store():
min_gold_list = [15, 70, 50, 500, None]
function_list = [blighter, baxe, bbandage, bjacket, lodge]
while True:
print('\t\t########## Store ##########')
print('Items you already have are listed below\n', inventory)
time.sleep(1)
print('Items available\n\t\t1. lighter: 15\n\t\t2. axe: 70\n\t\t3. bandage: 50\n\t\t4. jacket: 500\n\t\t5. EXIT')
time.sleep(3)
item = int(input('What do you need?\n:> '))
if 1 <= item <= 5:
idx = item - 1
if item == 5 or player.gold > min_gold_list[idx]:
function_list[idx]()
else:
print('You do not have enough gold for this item, come back soon')
else:
print('Item not found!')