This site requires JavaScript, please enable it in your browser!
Greenfoot back
xbLank
xbLank wrote ...

2018/1/15

[RELEASE] Implementation of Cheat Codes in your Project

xbLank xbLank

2018/1/15

#
I was wondering what the best way would be to implement the usage of cheat codes in say.. my main menu. No UI. The user just types. If the user doesnt type something in a given amount of time the string resets. Hoping for suggestions and that it might help someone.
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
import greenfoot.*;
public class MyWorld extends World
{
    private String key = "",
                  code = "";
    private int wait = 0;
    public MyWorld()
    {   
        super(600, 400, 1);
    }
    public void act()
    {
        if(wait > 0)
            wait--;
        else code = "";
        if((key = Greenfoot.getKey()) != null)
        {
            wait = 60;
            code = HandleKey(key);
            key = "";
        }
        switch(code.toLowerCase())
        {
         //this is the part you actually check for the entered string.
            default:
                break;
            case "unlockall":
                //unlock All
                break;
            case "easterEgg":
                //something
                break;
        }
    }
    private String HandleKey(String key)
    {
        switch(key)
        {
            default:
                return code+key;
            case "space":
                return code+" ";
            case "backspace":
                return (code.length() > 0) ? code.substring(0,code.length()-1) : code; //simulate backspace(remove last character from string if possible)
        }
    }
}
CxVercility CxVercility

2018/1/16

#
You might not want to let this run in the background indefinitely, and entering it once should disable it after shouldn't it? Solid else.
xbLank xbLank

2018/1/16

#
line 30: Should be "easteregg" ofcourse since we are calling lowercase.
CxVercility wrote...
You might not want to let this run in the background indefinitely, and entering it once should disable it after shouldn't it? Solid else.
Actually It is just running while you are in the main menu of your game. Besides this is just a sample. Ofcourse people can choose whether or not they want to let this run the whole time. In case you are planning to only implement one cheat code then ofcourse you can disable it after entering it once else that wouldnt make much sense. In the string check switch block (line 22 ff) you can ofcourse implement a check to see whether or not the cheat code has been used already.
You need to login to post a reply.