首页 > 解决方案 > 尝试制作游戏时python代码不起作用

问题描述

import random #imports random module
username = ""
lvl = 0
exp = 0
maxexp = 50
oldexp = 0

def newgame():  # Used for creating a New Account
    username = raw_input("Enter your new nickname : ")
    lvl = 0
    exp = 0
    return lvl
    return exp
    return username
    userinfo(username,lvl,exp)  

def userinfo(username,lvl,exp):
        print lvl,exp
        return lvl
        return exp
        return username
        update_userinfo(lvl,exp,0,0)

def update_userinfo(exp,lvl,maxexp,exp_increase):
    exp_up(exp,exp_increase)
    return exp
    if exp >= maxexp:
        exp = exp - maxexp
        lvl_up(lvl,1)
        maxexp = maxexp * 2
        print "You lvled up"

def exp_up(exp,exp_increase):
    exp = exp + exp_increase

def lvl_up(lvl,lvl_increase):
    lvl = lvl + lvl_increase
    return lvl


def kanto():
    print "Welcome new trainer! You are going to begin your game at the Kanto region. Here you will meet variety types of Pokemons. In this world of pokemons your objective is to Find and Catch as many Pokemons as you can. There are also many challenges that you may face along your adventure. Make sure you face them with your upmost courage and deal with them with all your might. May you be the next Pokemon champion."

def johto():
        print "Welcome new trainer! You are going to begin your game at the Johto region. Here you will meet variety types of Pokemons. In this world of pokemons your objective is to Find and Catch as many Pokemons as you can. There are also many challenges that you may face along your adventure. Make sure you face them with your upmost courage and deal with them with all your might. May you be the next Pokemon champion."

def hoenn():
        print "Welcome new trainer! You are going to begin your game at the Hoenn region. Here you will meet variety types of Pokemons. In this world of pokemons your objective is to Find and Catch as many Pokemons as you can. There are also many challenges that you may face along your adventure. Make sure you face them with your upmost courage and deal with them with all your might. May you be the next Pokemon champion."

def gamestart():
    min = 1
    max = 3
    region = random.randint(min,max)
    if region == 1:
        kanto()
    elif region == 2:
        johto()
    elif region == 3:
        hoenn()

def choose_pokemon_kanto():
    print "Proffessor : Ok.... Lets see. We have three Pokeballs here, and all of them have one pokemon that I had caught in my last field trip. I raised all of them to about level 5, and taught them 2 moves, best for beginners right?"
    cont = raw_input("")
    print "Proffessor : First here we have "


update_userinfo(exp,lvl,maxexp,60)

我试图学习一点python并尝试制作基于文本的口袋妖怪游戏。不工作的部分是:

update_userinfo(exp,lvl,maxexp,60)

我是编码新手,所以请帮忙。我应该使用那么多函数,还是只使用 if else 语句并完成整个代码?为什么在执行更改变量值的函数时,python 不更改变量的值。

标签: python

解决方案


我在那里看到很多错误,但我想你只是想在使用后返回 expexp_up

def exp_up(exp,exp_increase):
    exp = exp + exp_increase

你 decalare 新变量 exp,它只能在函数 exp_up 内访问。如果你想在update_user_info函数中使用它,你需要expexp_up函数中返回它。所以你exp_up看起来像这样:

def exp_up(exp,exp_increase):
    exp = exp + exp_increase
    return exp

和你的update_userinfo

def update_userinfo(exp,lvl,maxexp,exp_increase):
    exp = exp_up(exp,exp_increase)
    return exp
    ...

推荐阅读