vendredi 30 juin 2017

Making a payment system (Game) (Solution)

Let me prefix by saying, I know stackoverflow is for questions, but I saw none that fit this problem, and as a noob I figured someone else MUST have run into this issue at some point, so I decided to put it on here as a guide so-to-say, please don't downvote just because it isn't a question.

Okay, so I am making a game solely for me and my mate who love 'Idle' games, simply so that I can make it how the consumer(me) wants it to be, but I ran into a problem, to explain it I will give an example from another popular 'Idle' game, Cookie Clicker.

In Cookie Clicker, you can buy "Cursor's" which add a click-less form of gaining cookies, or in my case, money, since I just want this to be as SIMPLE as possible, I am trying to do that, I start the timers with loading of the form instead of first purchase since that would start a new timer every time, but I need to check if the money is equal to or more than the price.

Now here was the problem, Too make it start with 0 I set "cursor's" or in my case, "hammer's" to 0, with int hammer = 0; now I need to work out payment, so in the picture of a hammer, in other words the buy button, I do int pay = hammer * 15; since the way to work out the price for this item is the amount of hammer's times fifteen, all well and good now, So I go to add the if statements to check, and I test, thinking everything was fine, but I was able to buy a hammer with 0 cash, now I am wondering why, my code seems foolproof, it looks complete, its even organised neatly, but something is wrong, why can I buy stuff with 0 cash, then it hit me, later that day... its working out (hammers x 15) but, I set hammers to be 0, and 0 times anything equals 0, so within the hour I revised my code to work, I will share that with you now for any suffering the same problem.

int pay = hammers * 15 + 15;
        if (hammers == 0)
        {
            if (cash >= 15)
            {
                money.Text = cash; 
                hammers += 1;
                cash -= 15;
            }
            else if (cash <= 15)
            {
                notifyIcon1.BalloonTipText = "ERROR: You do not have enough cash to buy this!";
                notifyIcon1.ShowBalloonTip(1000);
            }
        }
        else if (Hammers >= 1)
        {
            if (cash < pay)
            {
                notifyIcon1.BalloonTipText = "Error: You do not have enough cash to buy this!"; notifyIcon1.ShowBalloonTip(1000);
            }
            else if (cash >= pay)
            {
                money.Text = cash;
                hammers += 1;
                cash -= pay;
            }
        }

There is the solution, Enjoy!

Aucun commentaire:

Enregistrer un commentaire