2016-04-27 6 views
-1

Ich arbeite an einem Spiel, wo der Spieler auf Schatzsuche ist. Alles funktioniert perfekt, obwohl mein Geldsystem nicht funktioniert. Ich brauche meinen Code, damit der Benutzer, wenn er auf einer Schatzkiste landet, 10 zusätzliche Münzen erhält. Allerdings ist ihre Anzahl von Münzen wird auf 10 statt 10.Python Spiel Logik: "Münzen" Variable ist nicht inkrementieren

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() 

Antwort

1

Hinzufügen Es sieht aus wie Sie den Code unter Ihrem Geld einrücken haben() Funktion zuerst:

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

Die Einrückung war ein Unfall, aber selbst nachdem es repariert wurde, passiert nichts, sobald ich auf einer Schatzkiste lande. Versuchen Sie, den Code auszuführen, und Sie werden sehen, wie es nicht funktioniert. Danke, obwohl –

2

In diesem Teil hier:

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

Sie das Geld auf 10 einstellen, nicht 10. Alles, was Sie Hinzufügen tun müssen, ist:

money += 10 

Stellen Sie außerdem sicher, dass def money(): nicht eingerückt ist; Es wird nicht funktionieren, da es derzeit eingerückt ist.

+0

Yeah die Einrückung war ein Unfall, obwohl, wenn Sie den Code ausführen, Sie sehen, dass Ihre Bearbeitung noch keinen Unterschied macht. Entschuldigung –

+0

Wenn ich richtig verstehe, überprüfen Sie, ob die (x, y) des Spielers gleich "T" des Schatzes ist. Überprüfen Sie, was Sie tatsächlich mit dieser Zeile vergleichen. – Anna