2016-04-12 6 views
-1

私は、プレーヤーが宝を探して移動できるコードを作成しました。プレイヤーが宝の上に着くと、10コインが追加され、ゴブリンに着陸するとすべてのコインが控除されます。私はこの「お金」のための関数を作ったが、うまくいかないようだ。私はコードを実行するたびに、お金機能から何も働かないように見えます。誰でも私のコードを編集して、動作するようにしてください。ここに私のコードは次のとおりです。グリッドでお金を稼ぐPython

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

今のところ、プレイヤーが宝の上に着くと、私はそれを作りました。まだゴブリンとは関係ありません。 – Micheal

+0

https://stackoverflow.com/help/mcveを読んで、money関数のテストに失敗したことをお勧めします。 –

+0

ちょうどいくつかの考え:あなたの関数内で上記の変数を変更する場合は、グローバルは使用する必要があります。 Treasuresループは、3つの宝のための入力として 'TTT'を期待しています。実際にはゴブリンの場合と同じように、現時点での文字数をカウントします。より良いグリッドプリンタは、xrange(len(grid))のyのprint '\ n'.join([' '.join(xはグリッド[y]のx))です。 TreasuresとGoblinsを割り当てる前に、セルが空であることを確認する必要があります。グリッドが変更され、現在は値の文字を持っているので、 – Aquiles

答えて

0

あなたのマネー機能も、>お金の変数を操作する/保存しない>

それは単に「あれば」の条件に基づいて出力を出力します。私は、作業コードを持っているが、それはそれを自分で行うためのもの、この種の方が良いでしょうが私はちょうどであなたを指すことができ、ここでいくつかの機能を配置するつもりです

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

ああ、私はちょうどそれを試し、それは違いがないように見えます。しかし、ありがとう。 – Micheal

0

:それは次のようにすべきではありません右方向:私たちはの値を変更しようとしているので、それはお金で行われるべき何か、あなたが見ることができるように私たちが使用する世界だ場合

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' 

このマネー機能は、グリッド内の位置を与えられたチェックしますグローバル値であるため、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のいずれかになる可能性があるからです。私たちはそれを変更し、私たちのお金の機能は、そこに何かがあるかどうかを決して見ないだろうので、移動は間違いなくアバターになるでしょう。 money関数を呼び出した後、グリッドとグローバル変数内のキャラクタの位置の両方を更新します。うまくいけば、これはあなたを助けるでしょう。