2016-04-27 36 views
-1

我正在制作一款玩家正在寻找宝藏的游戏。一切正常,虽然我的钱系统不起作用。我需要我的代码才能工作,如果用户登陆宝箱,他们会获得10个额外的硬币。然而,他们的硬币数被设置为10,而不是增加10Python游戏逻辑:“硬币”变量不会递增

import easygui 
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(): #function 
    msg = "Would you like to...?" #Users choice to start game 
    buttons = ["start", "quit"] 
    while True: 
     title = "Menu" 
     selection = easygui.buttonbox(msg, title , buttons) 
     if selection == "quit": 
      exit() 
     elif selection == "start": #If users input is to start the game the all of this appears("Welcome to the treasure hunt game!") 
     easygui.msgbox("These are the rules! You have a choice of a grid ranging from a 3x3 choice to a 20x20 choice") 
     easygui.msgbox("In these grids, bandits and treasure chests will spawn at random locations, hidden to you.") 
     easygui.msgbox("You will have a choice of the amount of goblins and treasures you would like to spawn in, ranging from 1-2") 
     easygui.msgbox("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.") 
     easygui.msgbox("Furthurmore, just deciding on a position you would like to move to, would give you an extra 1 piece of gold.") 
     easygui.msgbox("You can only find the same treasure chest two times before it's replaced by a bandit.") 
     easygui.msgbox("To check the amount of gold you have and the amount of bandits and treasure chests in the grid. Simply type 'status'") 
     easygui.msgbox("Don't forget! If you have collected all the treasure chests and you don't have 100 gold, you lose the game!") 
     easygui.msgbox("Good luck, you will now be entered into the game") 
     easygui.msgbox("Ok! 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 = easygui.integerbox("How big would you like the grid to be?")    #User input 
    while int(N) > 20: #Changing N to an integer so computer understamds 
     N = easygui.intergerbox("That number is too high, The grid has to be at a size of under 20x20") 
    else: 
     while int(N) < 3 : # Asking the user to input again as number is too high or low 
     N = easygui.integerbox("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(): #Function used for adding the treasures and goblins in the grid 
    global grid 
    global row 
    global Treasure  
    B = easygui.enterbox(" How many chests would you like in the grid? The amount of chests you like is given by the amount of C's") 
    F = easygui.enterbox(" How many Bandits would you like in the grid? 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 
    gridRunner() 





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 (" ") 
     P = easygui.enterbox("What direction would you like to move in? North (N), South(S), East(E) or West(W)?") 
     if P not in switch: 
      easygui.msgbox("invalid move") 
      continue 
     distance = easygui.integerbox("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") 
     money = 10 
    else: 
     print ("You got nothing") 
     money = 0 


menu() 

回答

1

它看起来你有你的钱下缩进代码()函数首先:

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

压痕是一个意外,虽然它固定后,一旦我登陆宝箱,什么也没有发生。尝试运行代码,你会看到它如何不起作用。虽然 –

2

在这部分在这里:

if player_loc == Treasure: 
     print("Well done, You have gained coins") 
     money = 10 

你的钱设置为10,不加入10.所有你需要做的是:

money += 10 

还要确保def money():不缩进;它不会像目前缩进的那样工作。

+0

虽然如果你运行代码,你会发现你的编辑仍然没有区别,但是缩进是一个意外。对不起 –

+0

如果我理解正确,请检查玩家的(x,y)是否等于宝藏的“T”。仔细检查一下你实际上与那条线相比较。 – Anna