【问题标题】:Python creating money in a gridPython 在网格中创造金钱
【发布时间】:2016-04-12 18:34:33
【问题描述】:

我创建了一段代码,玩家可以在其中四处走动寻找宝藏。当玩家降落在宝物上时,会增加 10 个硬币,而如果玩家降落在地精上,则会扣除所有硬币。我为这个“钱”创建了一个函数,虽然它似乎不起作用。每当我运行代码时,money 函数似乎都不起作用。任何人都可以编辑我的代码以使其正常工作,或提供任何建议,谢谢。这是我的代码:

import time
from random import *
# Set up Initial Variables
Money = 0
grid = []
character = "X" 
# player_loc will hold the x, y location of the player
player_loc = (0, 0)
# These are a mapping of direction
NORTH = "N"
SOUTH = "S"
 EAST  = "E"
WEST  = "W"   #All variables used for Later on
Treasure = "T"
Goblin = "G"


def menu(): #functiom
   c = input(" To quit this program, type 'quit'    To start the game, type 'start'")#Users choice to start game
   if c == "quit": 
         exit()
   elif c == "start": #If users input is to start the game the all of this appears
      print("Welcome to the treasure hunt game!")
      time.sleep(1)
      print(" ")
      print("These are the rules! You have a choice of a grid ranging from a 3x3 choice to a 20x20 choice")
      print(" ")
      time.sleep(2)
      print("in these grids, bandits and treasure chests will spawn at random locations, hidden to you.")
      print(" ")
      time.sleep(3)
      print("You will have a choice of the amount of goblins and treasures you would like to spawn in, ranging from 1-20.")
      print(" ")
      time.sleep(3)
      print("You will move around the map, in search of treasures which will give you 10 gold. Although landing on a goblin would deduct the amount of gold to 0.")
      print(" ")
      time.sleep(3)
      print("Furthurmore, just deciding on a position you would like to move to, would give you an extra 1 piece of gold.")
      print(" ")
      time.sleep(3)
      print("You can only find the same treasure chest two times before it's replaced by a bandit.")
      print(" ")
      time.sleep(3)
      print("To check the amount of gold you have and the amount of bandits and treasure chests in the grid. Simply type 'status'")
      print(" ")
      time.sleep(3)
      print("Don't forget! If you have collected all the treasure chests and you don't have 100 gold, you lose the game!")
      print(" ")
      time.sleep(3)
      print("Good luck, you will now be entered into the game")
      print(" ")
      time.sleep(2)
      x = input("What is your name?")
      username = x
      time.sleep(2)
      print ("Hello,", username,"! Let's jump into the game!")
      setupGrid()
      Chests_and_Goblins() 


def setupGrid(): #New function for creating grid
    global grid #Adding/creating global variables
    global row
    global N
    N = input("How big would you like the grid to be?") #User input
    time.sleep(2)
    while int(N) > 20 : #Changing N to an integer so computer understamds
          N =input("That number is too high or too low, The grid has to be at a size of under 20x20. Or 3x3 and larger. Please try again")
   else:
       while int(N) < 3 : # Asking the user to input again as number is too high or low
          N = input("That number is too low, the grid has to be a size of over 3x3. Please try again")
       for x in range(0, (int(N))):#For everything in range N
            row = [] #The N amount of rows are created
            for y in range(0, (int(N))): #For everything in range N
               if x == player_loc[0] and y == player_loc[1]: #If the positions is equal to th player location
                    row.append(character) # Add the character in
                else:
                  row.append('O') #Add the same amount of 0's as N
           grid.append(row) 


 def Chests_and_Goblins():
   global z
   global grid
   global row
   global N
   global Treasure
   print("How many chests would you like in the grid?")     
   time.sleep(2)
   B = input("The amount of chests you like is given by the amount of C's")
   print("How many Bandits would you like in the grid?")     
   time.sleep(2)
   F = input("The amount of Bandits you like is given by the amount of B's")
   for each in B:
      grid[randint(0, int(N)-1)][randint(0, int(N)-1)] = Treasure
   for each in F:
      grid[randint(0, int(N)-1)][randint(0, int(N)-1)] = Goblin





def moveSouth(n):
    global player_loc
    grid[player_loc[0]][player_loc[1]] = "O"
    grid[player_loc[0] + n][player_loc[1]] = character
    player_loc = (player_loc[0] + n, player_loc[1])
    money()

def moveNorth(n):
    global player_loc
    grid[player_loc[0]][player_loc[1]] = "O"
    grid[player_loc[0] - n][player_loc[1]] = character
    player_loc = (player_loc[0] - n, player_loc[1])
    money()

def moveEast(n):
    global player_loc
    grid[player_loc[0]][player_loc[1]] = "O"
    grid[player_loc[0]][player_loc[1] + n] = character
    player_loc = (player_loc[0], player_loc[1] + n)
    money()

def moveWest(n):
    global player_loc
    grid[player_loc[0]][player_loc[1]] = "O"
    grid[player_loc[0]][player_loc[1] - n] = character
    player_loc = (player_loc[0], player_loc[1] - n)
    money()

def gridRunner():
    while True:
        for row in grid:
            print (row)

         switch = {NORTH : moveNorth,
                  SOUTH : moveSouth,
                  EAST  : moveEast,
                  WEST  : moveWest }
        print (" ")
        time.sleep(2)
        P = input("What direction would you like to move in? North (N), South(S), East(E) or West(W)?").upper()

        if P not in switch:
            print ("invalid move")
            continue

        distance = int(input("How far would you like to move in this direction? (blocks are the units)"))
        switch[P](distance)

def money():
   global player_loc
   global character
   global Treasure
   if player_loc == Treasure:
      print("Well done, You have gained coins")
   else:
      print ("You got nothing")





menu()
gridRunner()

【问题讨论】:

  • 目前,我只在玩家降落在宝藏上时这样做。与哥布林无关。
  • 我建议您阅读stackoverflow.com/help/mcve 并进行货币功能的失败测试。
  • 只是一些想法:仅当您要在函数中修改所述变量时才应使用 global。您的宝藏循环,是否期望“TTT”作为 3 个宝藏的输入,它实际上计算了此刻的字符数,对于地精也是如此。更好的网格打印机是: print '\n'.join([' '.join(x for x in grid[y]) for y in xrange(len(grid))])。在分配宝藏和哥布林之前,您应该检查单元格是否为空。还要检查一个动作是否超出了网格,因为你现在不检查它
  • 哦,money 功能不起作用,因为当您调用它时,网格已更改,现在具有值字符。
  • @Aquiles 谢谢,但您能帮我编辑一下代码吗?我很挣扎。谢谢

标签: python grid currency


【解决方案1】:

您的货币函数甚至不存储/操作货币变量 >.>

它只是根据“if”条件打印输出。是不是应该这样:

if player_loc == Treasure:
      print("Well done, You have gained coins")
      Money += 10
else:
      print ("You got nothing")
      Money = 0

【讨论】:

  • 啊,我刚试过,好像没有什么区别。不过还是谢谢。
【解决方案2】:

我有一个可以工作的代码,但这种东西最好自己做,所以我只是在这里放置一些函数,可以为你指明正确的方向:

def money(x, y):
    global MONEY
    if GRID[x][y] == TREASURE: MONEY += 10; print 'You found a treasure'
    elif GRID[x][y] == GOBLIN: MONEY = 0; print 'You lost your money'

这个money函数会检查网格中给定的位置是否应该用钱来完成,你可以看到我们使用了一个全局值,因为我们要修改全局值的值,这就是为什么我们传递 x,y 而不使用我们的位置将在下一个函数中显示:

def move_west():
    global GRID, POSITION
    x, y = POSITION[0], POSITION[1]
    if y != 0:
        money(x, y-1)
        GRID[x][y], GRID[x][y-1] = EMPTY, AVATAR
        POSITION = (x, y-1)
    else: print 'You\'ve hit a wall'

正如你在这个函数中看到的,首先我们从一个全局值中获取当前位置,在我的例子中是一个元组 (x,y)。我们检查我们移动的方式是否有效,然后我们在改变它之前用我们的未来位置调用货币函数,我们这样做的原因是因为现在值可以是 EMPTY、TREASURE 或 GOBLIN,但是在我们之后move 肯定会是 AVATAR,所以我们会改变它,我们的货币功能永远不会看到那里是否有什么可做的。调用money函数后,我们更新网格和角色在全局变量中的位置。希望这会对您有所帮助。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2017-10-18
    • 2018-09-07
    • 2011-12-12
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多