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

2014/8/3

Start Menu and Changing Level

1
2
3
VideoGame VideoGame

2014/8/3

#
Hi, Someone can explain me, how to do a start menu which we can run the game ( with spacebar ) to the level 1 ? And how can we change the level for example when we grab a key and touch the door to pass at the next level ? It's a plateform game and thanks for your help.
danpost danpost

2014/8/3

#
(1) create a separate world class that sets world level one active when the space bar is pressed (2) have the door create its key and add a 'get' method to the class of the door to return that key; after creating the door and adding it into the world 'get' its key and add it into the world; have the act method of the door set the new world active only if the player touches it and the key is not in the world.
VideoGame VideoGame

2014/8/5

#
Can you juste help me to code a part of this stuff... ?
danpost danpost

2014/8/5

#
VideoGame wrote...
Can you juste help me to code a part of this stuff... ?
Please show what you have and I/we can help fix it. Explain what part you are having problems with. Use the 'code' link below the 'Post a reply' box to insert your code into the post. Copy/paste, in entirety, any error messages you are getting.
VideoGame VideoGame

2014/8/6

#
Of course: This is the code for the level and the actors which are placed in the level. It's in a unique subclass of World, named "Level". So, the "string" part is for level 1.
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
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
import greenfoot.*;  // (World, Actor, GreenfootImage, Greenfoot and MouseInfo)
 
/**
 * Write a description of class Background here.
 *
 * @author (your name)
 * @version (a version number or a date)
 */
public class Level extends World
{
   String[] map;
   Door door = new Door();
   public void populateWorld() {
        Player p = new Player();
        Mountain m = new Mountain();
        Mountain1 n = new Mountain1();
        Mountain2 w = new Mountain2();
        Pike1 a = new Pike1();
        Pike2 s = new Pike2();
        Lader l = new Lader();
        Knight k = new Knight();
        for( int i = 0; i < map.length; i++) { // la longeur du tabmap
            for( int j = 0; j < map[i].length(); j++) { // la longueur de la String map[i]
                Actor actor = null; // crée un actor null si w,p,m = on ajotue l'actor
                if(map[i].charAt(j) == 'p') {
                    actor = new Player();
                }
                else if(map[i].charAt(j) == 'm') {
                    actor = new Mountain();
                }
                else if(map[i].charAt(j) == 'n') {
                    actor = new Mountain1();
                }
                else if(map[i].charAt(j) == 'w') {
                    actor = new Mountain2();
                }
                else if(map[i].charAt(j) == 'd') {
                    actor = new Door();
                }
                else if(map[i].charAt(j) == 'a') {
                    actor = new Pike1();
                }
                else if(map[i].charAt(j) == 's') {
                    actor = new Pike2();
                }
                else if(map[i].charAt(j) == 'k') {
                    actor = new Knight();
                }
                else if(map[i].charAt(j) == 'l') {
                    actor = new Lader();
                }
                else if(map[i].charAt(j) == 'o') {
                    actor = new ArrowthrowD();
                }
                else if(map[i].charAt(j) == 'q') {
                    actor = new ArrowthrowG();
                }
                else if(map[i].charAt(j) == 'y') {
                    actor = new Key();
                }
                if( actor != null) {
                    addObject( actor, 16+j*32, 16+i*32);  // inversion de i et de j
                }
            }  
        }
   }
    
   public Level()
   {
        super(960, 640, 1);
        // 30x20 charactères
        // 960/30= 32 pixels et 640/20 = 32 pixels
        map = new String[] {"sssssssssssssssssssssssssssssm",
                            "                             m",
                            "             k               m",
                            "            mm              ym",
                            "         m      mml       mmmm",
                            "   k   m          l m        m",
                            "   mm     aaaaaaaaa    mm    m",
                            "          mmmmmmmmm          m",
                            "n                            d",
                            "            k                m",
                            "            mmm   k     m    m",
                            "   w    k         nn    m    m",
                            "      lmmm              q    m",
                            "      l                 o    m",
                            "      l       mm     m  o    m",
                            "      l    k          mmmaaaam",
                            "      l  mmm                 m",
                            "      lmmmmmmm               m",
                            "p k  mmmmmmmmmmm        k    m",
                            "mmmmmmmmmmmmmmmmmmmaaaammmaaam" };
        populateWorld();
   }
}
My problem is: How can I divide this in 6 different levels and how can I change the level when the objectives are completed ( so grab the key and touch the door ). And how can I code a menu which revealed the level 1 after pressing the spacebar
danpost danpost

2014/8/6

#
This class has nothing to do with your original issues dealing with the menu and the changing of levels. The Door class might help as far as changing levels and a new world class is needed for the menu issue. You do now mention a third issue: making more levels. You can first make the 'map' field 'static' and instead of assigning its value in the constructor, assign it at the top where you declare the field. You can then add another dimension to the array so you can assign multiple arrays (levels) to it. Your world super call can then use the length of the array and of the first string in the array to determine its dimensions. For example:
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
public static Sring[][] map =
{
    {
        {"sssssssssssssssssssssssssssssm",
         "                             m",
         "             k               m",
         "            mm              ym",
         "         m      mml       mmmm",
         "   k   m          l m        m",
         "   mm     aaaaaaaaa    mm    m",
         "          mmmmmmmmm          m",
         "n                            d",
         "            k                m",
         "            mmm   k     m    m",
         "   w    k         nn    m    m",
         "      lmmm              q    m",
         "      l                 o    m",
         "      l       mm     m  o    m",
         "      l    k          mmmaaaam",
         "      l  mmm                 m",
         "      lmmmmmmm               m",
         "p k  mmmmmmmmmmm        k    m",
         "mmmmmmmmmmmmmmmmmmmaaaammmaaam"
    },
    {
        // map for next level
    }
};
 
public Level()
{
    super(30*map[0][0].length(), 30*map[0].length, 1);
    // etc.
You will need a static int field to indicate the current level. When you add your menu class, you can add a line in it to initialize this field. If you are to only have one door and one key per level, your populateWorld method can create them before going through the loop and move them to the 'actor' field when their letters come up. I noticed that lines 14 through 21 above are meaningless; that is, they create actors that are never used within the method. That is where you would create the door with its key and instead of using 'new Door' and 'new Key' on lines 38 and 59, you would use the local fields created above (where lines 14 through 21 were).
VideoGame VideoGame

2014/8/6

#
Ouho, Thanks for your answer. I have a Door and Key class but I don't know how to bind them. I understand the first part of your answer and it's a good idea to code the levels with multiple arrays but I don't understand the part who you talk about the map length. I don't understand too this expression; If you are to only have one door and one key per level, your populateWorld method can create them before going through the loop and move them to the 'actor' field when their letters come up The lines 14 - 21 it's juste a reminder for me. And can't we just create a class 'level' and the subclasses level 1/2/3 etc. ? Sorry, for that, i'm a little bit new in programmation.
danpost danpost

2014/8/6

#
I will explain the binding of door and key later. First, let me explain that creating separate level subclasses would be in effect creating multiple classes with pretty much exactly the same code -- just using a different map. By having a static int field to indicate the current level, you can set the field to the level you want and then create a new Level world from the same class to produce the different mapped levels. For the door/key issue, do the following in the Door class:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
// add instance field
private Key key = new Key();
// add this method
public Key getKey()
{
    return key;
}
// use this act
public void act()
{
    if (key.getWorld() == null && isTouching(Player.class))
    { // key was picked up and player is at door
        Level.level++;
        Greenfoot.setWorld(new Level());
    }
}
Then, in your populateWorld method you can do this:
1
2
3
4
5
6
7
Door door = new Door();
for ...
{
    ...
    if (map[level][i].charAt(j) == "d") actor = door;
    if (map[level][i].charAt(j) == "y") actor = door.getKey();
    ...
danpost danpost

2014/8/6

#
As far as the map length: you want your world to be sized according to the map dimensions (allowing different map dimensions for different levels). The 'super' call should look like the following after you add the 'static int level' field:
1
super(30*map[level][0].length(), 30*map[level].length, 1);
'map.length()' returns the number of characters in the first string in the current level map (the width factor); 'map.length returns the number of string elements in the current level map (the height factor);
VideoGame VideoGame

2014/8/6

#
I add the code in the Door class and add the 'public static Sring map =..." with 3 levels. So, I have this in my Level Class:
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
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
import greenfoot.*;  // (World, Actor, GreenfootImage, Greenfoot and MouseInfo)
 
/**
 * Write a description of class Background here.
 *
 * @author (your name)
 * @version (a version number or a date)
 */
public class Level extends World
{
   public static Sring[][] map =
   {{"sssssssssssssssssssssssssssssm"
         "                             m"
         "             k               m"
         "            mm              ym"
         "         m      mml       mmmm"
         "   k   m          l m        m"
         "   mm     aaaaaaaaa    mm    m"
         "          mmmmmmmmm          m"
         "n                            d"
         "            k                m"
         "            mmm   k     m    m"
         "   w    k         nn    m    m"
         "      lmmm              q    m"
         "      l                 o    m"
         "      l       mm     m  o    m"
         "      l    k          mmmaaaam"
         "      l  mmm                 m"
         "      lmmmmmmm               m"
         "p k  mmmmmmmmmmm        k    m"
         "mmmmmmmmmmmmmmmmmmmaaaammmaaam"},
        {"                             f",
         "                             m",
         "p  mmmmmmmmmmmmmm            m",
         "mmm                m         f",
         "               t             f",
         "            mmmm             m",
         "         t                   m",
         "     mffmm                   f",
         "                    mmm      m",
         "                      m      m",
         "           fmmm     mmm      m",
         "m          f                 m",
         "        m                    m",
         "   m                         m",
         "                             m",
         "m                            m",
         "                mm        aaam",
         "  mf                         m",
         "  mm                    m    d",
         "mmmmaaaaaaaaaaaaaaaaaaaamaammm"},
        {"     ff                    d  ",
         "     ff                    m  ",
         "  mmmmmmmm             mm  m  ",
         "         m                 m  ",
         "m        m   mm  mm        m  ",
         "mmmmmm   m                 m  ",
         "           m   mm   mm     m  ",
         "           m               m  ",
         "  mmmmmmmm maaaaaaaaaaaaaaam  ",
         "         m mmmmmmmmmmmmmmmmm  ",
         "m  w     m mmmmmmmmmmmmmmmmm  ",
         "  mmmmmm m m                  ",
         "  m      m m                 f",
         "f      w m m   t    t    m   f",
         "m m mmmmmm m  mmmmmmmmmmmmmmmm",
         "mfm m      m                 m",
         "mmm m      m                 m",
         "    m  mmmmmmmmmmmmmmmmmmmmm m",
         "p   m                      m m",
         "mmmmm      w    w    w    qmdm"}};
          
   public void populateWorld()
   {
        Player p = new Player();
        Door door = new Door();
        Mountain m = new Mountain();
        Mountain1 n = new Mountain1();
        Mountain2 w = new Mountain2();
        Pike1 a = new Pike1();
        Pike2 s = new Pike2();
        Lader l = new Lader();
        Knight k = new Knight();
        for( int i = 0; i < map.length; i++) { // la longeur du tabmap
            for( int j = 0; j < map[i].length(); j++) { // la longueur de la String map[i]
                Actor actor = null; // crée un actor null si w,p,m = on ajotue l'actor
                if(map[level][i].charAt(j) == 'p') {
                    actor = new Player();
                }
                else if(map[level][i].charAt(j) == 'm') {
                    actor = new Mountain();
                }
                else if(map[level][i].charAt(j) == 'n') {
                    actor = new Mountain1();
                }
                else if(map[level][i].charAt(j) == 'w') {
                    actor = new Mountain2();
                }
                else if(map[level][i].charAt(j) == 'd') {
                    actor = new Door();
                }
                else if(map[level][i].charAt(j) == 'a') {
                    actor = new Pike1();
                }
                else if(map[level][i].charAt(j) == 's') {
                    actor = new Pike2();
                }
                else if(map[level][i].charAt(j) == 'k') {
                    actor = new Knight();
                }
                else if(map[level][i].charAt(j) == 'l') {
                    actor = new Lader();
                }
                else if(map[level][i].charAt(j) == 'o') {
                    actor = new ArrowthrowD();
                }
                else if(map[level][i].charAt(j) == 'q') {
                    actor = new ArrowthrowG();
                }
                else if(map[level][i].charAt(j) == 'y') {
                    actor = new Key();
                }
                if( actor != null) {
                    addObject( actor, 16+j*32, 16+i*32);  // inversion de i et de j
                }
            }  
        }
   }
    
   public Level()
   {
        super(30*map[0][0].length(), 30*map[0].length, 1);
         
 
        // 30x20 charactères
        // 960/30= 32 pixels et 640/20 = 32 pixels
         
        populateWorld();
   }
}
But now, there is a problem. It can't find the symbol string.
danpost danpost

2014/8/6

#
Watch your spelling 'Sring' is not 'String'. Also, you still need to add the class field for the level:
1
public static int level;
and then, complete the changes as per my last two posts.
VideoGame VideoGame

2014/8/6

#
Oh, sorry. I'm incorrigible. I add this line and the code of your last posts. It tells me; 'cannot find symbol - method length()'
danpost danpost

2014/8/6

#
I do not believe that I missed or added an extra set of empty parenthesis in the code I posted. It should be exactly as I posted:
1
super(30*map[level][0].length(), 30*map[level].length, 1);
'length()' (with parenthesis) for the width because we are calling a method that gets the length of the String object and 'length' (without parenthesis) for the height because we are accessing an int field of the Array class.
VideoGame VideoGame

2014/8/6

#
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
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
import greenfoot.*;  // (World, Actor, GreenfootImage, Greenfoot and MouseInfo)
 
/**
 * Write a description of class Background here.
 *
 * @author (your name)
 * @version (a version number or a date)
 */
public class Level extends World
{
   public static int level;
   public static String[][] map =
   {
       {"sssssssssssssssssssssssssssssm"
         "                             m"
         "             k               m"
         "            mm              ym"
         "         m      mml       mmmm"
         "   k   m          l m        m"
         "   mm     aaaaaaaaa    mm    m"
         "          mmmmmmmmm          m"
         "n                            d"
         "            k                m"
         "            mmm   k     m    m"
         "   w    k         nn    m    m"
         "      lmmm              q    m"
         "      l                 o    m"
         "      l       mm     m  o    m"
         "      l    k          mmmaaaam"
         "      l  mmm                 m"
         "      lmmmmmmm               m"
         "p k  mmmmmmmmmmm        k    m"
         "mmmmmmmmmmmmmmmmmmmaaaammmaaam"},
        {"                             f",
         "                             m",
         "p  mmmmmmmmmmmmmm            m",
         "mmm                m         f",
         "               t             f",
         "            mmmm             m",
         "         t                   m",
         "     mffmm                   f",
         "                    mmm      m",
         "                      m      m",
         "           fmmm     mmm      m",
         "m          f                 m",
         "        m                    m",
         "   m                         m",
         "                             m",
         "m                            m",
         "                mm        aaam",
         "  mf                         m",
         "  mm                    m    d",
         "mmmmaaaaaaaaaaaaaaaaaaaamaammm"},
        {"     ff                    d  ",
         "     ff                    m  ",
         "  mmmmmmmm             mm  m  ",
         "         m                 m  ",
         "m        m   mm  mm        m  ",
         "mmmmmm   m                 m  ",
         "           m   mm   mm     m  ",
         "           m               m  ",
         "  mmmmmmmm maaaaaaaaaaaaaaam  ",
         "         m mmmmmmmmmmmmmmmmm  ",
         "m  w     m mmmmmmmmmmmmmmmmm  ",
         "  mmmmmm m m                  ",
         "  m      m m                 f",
         "f      w m m   t    t    m   f",
         "m m mmmmmm m  mmmmmmmmmmmmmmmm",
         "mfm m      m                 m",
         "mmm m      m                 m",
         "    m  mmmmmmmmmmmmmmmmmmmmm m",
         "p   m                      m m",
         "mmmmm      w    w    w    qmdm"}};
                  
   public void populateWorld()
   {
        Player p = new Player();
        Door door = new Door();
        Mountain m = new Mountain();
        Mountain1 n = new Mountain1();
        Mountain2 w = new Mountain2();
        Pike1 a = new Pike1();
        Pike2 s = new Pike2();
        Lader l = new Lader();
        Knight k = new Knight();
        for( int i = 0; i < map.length; i++) { // la longeur du tabmap
            for( int j = 0; j < map[i].length(); j++) { // la longueur de la String map[i]
                Actor actor = null; // crée un actor null si w,p,m = on ajotue l'actor
                if(map[level][i].charAt(j) == 'p') {
                    actor = new Player();
                }
                else if(map[level][i].charAt(j) == 'm') {
                    actor = new Mountain();
                }
                else if(map[level][i].charAt(j) == 'n') {
                    actor = new Mountain1();
                }
                else if(map[level][i].charAt(j) == 'w') {
                    actor = new Mountain2();
                }
                else if(map[level][i].charAt(j) == 'd') {
                    actor = new Door();
                }
                else if(map[level][i].charAt(j) == 'a') {
                    actor = new Pike1();
                }
                else if(map[level][i].charAt(j) == 's') {
                    actor = new Pike2();
                }
                else if(map[level][i].charAt(j) == 'k') {
                    actor = new Knight();
                }
                else if(map[level][i].charAt(j) == 'l') {
                    actor = new Lader();
                }
                else if(map[level][i].charAt(j) == 'o') {
                    actor = new ArrowthrowD();
                }
                else if(map[level][i].charAt(j) == 'q') {
                    actor = new ArrowthrowG();
                }
                else if(map[level][i].charAt(j) == 'y') {
                    actor = door.getKey();
                }
                if( actor != null) {
                    addObject( actor, 16+j*32, 16+i*32);  // inversion de i et de j
                }
            }  
        }
   }
    
   public Level()
   {
        super(30*map[level][0].length(), 30*map[level].length, 1);
        // 30x20 charactères
        // 960/30= 32 pixels et 640/20 = 32 pixels
        
        populateWorld();
   }
}
Sorry, i didn't see that you changed since your third post but always the same problem.
danpost danpost

2014/8/6

#
Oh, the problem is not in what code I supplied; it is in your 'for' statements which were not adjusted due to the addition of another dimension to the array. They should be (lines 86 and 87 above):
1
2
for( int i = 0; i < map[level].length; i++) {
    for( int j = 0; j < map[level][i].length(); j++) {
Also, line 102 needs to be changed to 'actor = door;' or the key added to the world will not work on the door (it will not 'lock' the door by being in the world).
There are more replies on the next page.
1
2
3