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

2017/12/12

Making extra walls, that an actor cannot pass through

XD-69 XD-69

2017/12/12

#
I am making pac man ( i am quite new to greenfoot so i want to learn, so i decided to by making pacman) I want to add walls so that pacman cannot pass through them (stops at the walls) Here is my pacman code
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
import greenfoot.*;
 
/**
 * Write a description of class PacMan here.
 *
 * @author (your name)
 * @version (a version number or a date)
 */
public class PacMan extends Actor
{
    /**
     * Act - do whatever the PacMan wants to do. This method is called whenever
     * the 'Act' or 'Run' button gets pressed in the environment.
     */
    public void act()
    {
        if (Greenfoot.isKeyDown("up"))
        {
            setRotation(0);
            turn(-90);
            move(5);
        }
        if (Greenfoot.isKeyDown("down"))
        {
            setRotation(0);
            turn(90);
            move(5);
        }
        if (Greenfoot.isKeyDown("left"))
        {
            setRotation(0);
            turn(0);
            move(-5);
        }
        if (Greenfoot.isKeyDown("right"))
        {
            setRotation(0);
            turn(0);
            move(5);
        }
    }   
}
I just want PacMan to not be able to pass through walls (that you add in, which i dont know how to do so if you could help me with that, that would be nice) Thanks
Super_Hippo Super_Hippo

2017/12/12

#
In my Pac-Man game, I saved the map as an array in the world, so Pac-Man can check if there is a "wall" on the side. You can download the game and check the code. The alternative is to have walls as actors in the world. Then Pac-Man can move, and if it hits a wall, you place it back to where it came from.
You need to login to post a reply.