【问题标题】:How can I make this program work properly?我怎样才能使这个程序正常工作?
【发布时间】:2020-03-07 11:26:54
【问题描述】:

首先让我快速总结一下这个程序的作用。这是一个非常基本的点击游戏形式(这里:空格/键盘上的任何按钮/)。我有 2 座建筑物(庄园和助推器)。庄园根据其等级制造金币,而助推器可提高每次点击(按)获得的金币数量。

但是,当我尝试运行程序本身时,我遇到了问题。基本上(因为它会在我将在此处复制粘贴的代码上可见)当我运行程序的第一个功能(制作黄金)时,它会正确生成并保存黄金(你的黄金余额现在是 x),但是当我尝试升级我的建筑/查看我的余额时,它说:金币不足/0。

我试过单独打印步骤(没有 while 函数),它成功了,我什至可以升级我的建筑,但在那之后,它开始奇怪地产生金子(所以不是在 2 级产生 amoung /press,它产生了其他东西,结果甚至出现了负数)。

现在在下面的代码中,我没有完整的 while 函数(我试图修复它,因为我只需要生成和一个升级函数来进行测试,所以我没有用 4 个选项完成该函数( A)生产 B)升级 c)升级 D)显示平衡)但只有前两个。)

感谢您的帮助,祝您有愉快的一天!

using System;
using System.Collections.Generic;
using System.Linq;

namespace Clicker_Game
{
  class Program
  {
    class Buildings
    {
      // two buildings: manor, helper(multiplier) + gold balance // 
      int manorlevel = 0;
      int boosterlevel = 0;
      int goldbalance = 0;

      // level cost of the buildings and their properties
      // on each level index - 1 = level's bonus // 
      int[] mproduce = new int[10] { 1, 5, 7, 10, 15, 20, 25, 30, 40, 50 };
      int[] mlevelcost = new int[9] { 5, 30, 45, 60, 90, 115, 130, 200, 400 };
      int[] bvalue = new int[10] { 1, 1, 1, 2, 2, 2, 2, 3, 4, 6 };
      int[] blevelcost = new int[9] { 5, 30, 45, 60, 90, 115, 130, 200, 400 };

      public int ManorUpgrader()
      {
        Console.WriteLine("Do you really wanna upgrade your manor from " + manorlevel + 
                          " to " + ( manorlevel + 1 ) + "?");
        string answer = Console.ReadLine();

        if ( answer == "yes" )
        {
          if ( goldbalance >= mlevelcost[manorlevel - 1] )
          {
            Console.WriteLine("Congrats. You have successfully upgraded your manor to level" + 
                              ( manorlevel + 1 ) + "!");
            manorlevel += 1;
            goldbalance -= mlevelcost[manorlevel - 1];
          }
          else
          {
            Console.WriteLine("Insufficient funds!");
          }
        }
        return manorlevel;
      }
      public int BoosterUpgrader()
      {
        Console.WriteLine("Do you really wanna upgrade your booster from " + boosterlevel + 
                          " to " + ( boosterlevel + 1 ) + "?");
        string answer = Console.ReadLine();

        if ( answer == "yes" )
        {
          if ( goldbalance >= blevelcost[manorlevel - 1] )
          {
            Console.WriteLine("Congrats. You have successfully upgraded your booster to level" + 
                              ( boosterlevel + 1 ) + "!");
            boosterlevel += 1;
            goldbalance -= blevelcost[manorlevel - 1];
          }
          else
          {
            Console.WriteLine("Insufficient funds!");
          }
        }
        return boosterlevel;
      }

      public int Clicker()
      {
        Console.WriteLine("Here you can click to produce gold! Can we start?");
        string answer = Console.ReadLine();
        Console.WriteLine("If you want to stop just say no!");

        if ( answer == "yes" )
        {
          while ( true == true )
          {
            string a = Console.ReadLine();
            if ( a == "no" )
            {
              Console.WriteLine(goldbalance);
              break;
            }
            goldbalance += mproduce[manorlevel - 1] * bvalue[boosterlevel - 1];
          }
        }

        return goldbalance;
      }
      public int Balance()
      {
        Console.WriteLine("You wanna see your balance?");
        string a = Console.ReadLine();
        if ( a == "yes" )
        {
          Console.WriteLine(goldbalance);
        }
        return goldbalance;
      }
    }

    static void Main(string[] args)
    {
      {
        string answer = "yes";
        while ( answer != "exit" )
        {
          Console.WriteLine("baba");
          answer = Console.ReadLine();
          Buildings app = new Buildings();

          if ( answer == "a" )
          {
            app.Clicker();
          }
          else if ( answer == "b" )
          {
            app.ManorUpgrader();
          }

          Console.ReadKey();
        }
      }
    }
  }
}

【问题讨论】:

  • 你从建筑物的等级开始0?还是我错过了什么?
  • WindowsForms 和 WPF 等桌面显示技术并不是适合游戏编程的工具。安慰?甚至更少。对于游戏开发,您需要带有 GameLoop 的东西。 |让“Building”成为一个嵌套类,也让阅读这本书变得非常困难。它应该是编程的兄弟/同行,而不是子部分。这种方式很容易搞乱值访问。
  • @MarkiianBenovskyi 不,你是对的。我将其更改为 1(作为基本级别),但我仍然面临原来的问题。
  • @Christopher。谢谢你的澄清。我知道游戏制作我需要你提到的其他东西(我不知道是什么,但现在我知道了)。基本上我想用这个程序做的是制作这个点击游戏背后的基本逻辑/算法。
  • 不幸的是,它不能那样工作。游戏和 Web 应用程序都有一个非常独特的结构,它决定了你如何做事。控制台应用程序也是如此。 WinForms/WPF 和其他基于消息泵/事件队列的东西也是如此。 |话虽如此,在控制台上所有不适合的工具中,它可能是最容易模拟/制作游戏循环的工具。最大的问题是获得一种非阻塞方式来读取用户输入,但我想我找到了一些其他的解决方案:stackoverflow.com/a/5620647/3346583 这也是您在游戏编程中处理输入的方式。

标签: c#


【解决方案1】:

基本上你只是打错了 while 循环。你需要做的是移动你的线路:

Buildings app = new Buildings();

while 循环之外。所以你的Main 方法应该是这样的:

static void Main(string[] args)
{
    // You need to create instance this class once
    // earlier you were creating it every time the loop started
    // so your values were reset to default
    var app = new Buildings();

    var answer = "yes";
    while (answer != "exit")
    {
        Console.WriteLine("Start loop");
        answer = Console.ReadLine();

        if (answer == "a")
        {
            app.Clicker();
        }
        else if (answer == "b")
        {
            app.ManorUpgrader();
        }

        Console.ReadKey();
    }
}

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2014-11-13
    • 1970-01-01
    • 2019-06-19
    • 2021-01-30
    • 2012-08-21
    • 1970-01-01
    • 1970-01-01
    • 2018-01-21
    相关资源
    最近更新 更多