2016-04-27 97 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