Java-Gaming.org    
Featured games (79)
games approved by the League of Dukes
Games in Showcase (476)
Games in Android Showcase (106)
games submitted by our members
Games in WIP (532)
games currently in development
News: Read the Java Gaming Resources, or peek at the official Java tutorials
 
    Home     Help   Search   Login   Register   
Pages: [1]
  ignore  |  Print  
  pacman - navigating through the tiles  (Read 832 times)
0 Members and 1 Guest are viewing this topic.
Offline javatypo

Junior Member





« Posted 2003-12-30 21:07:01 »

hi,

im making a pacman game, and im having some trouble figuring out how the pacman should navigate through the tiles. the way i have it setup now, is that their is a variable called nextDir which is like a queue for the next direction. it is set when the user presses on of the keys. then in a method called canMove(incX, incY). here is that method:

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  
if (nextDir != currDir || nextDir != NONE) {
                        if (nextDir == LEFT && canMove(-2,0)) currDir = nextDir;
                        if (nextDir == RIGHT && canMove(2,0)) currDir = nextDir;
                        if (nextDir == UP && canMove(0,-2)) currDir = nextDir;
                        if (nextDir == DOWN && canMove(0,2)) currDir = nextDir;
                  }

this is before the draw method, that calls the canMove method.

int _x = (pacmanR.x)/40;
            int _y = (pacmanR.y)/40;
            int mapTile = _y*20+_x;
                              
            pacmanR.x+=incX;pacmanR.y+=incY;
                        
            if (pacmanR.intersects(MazeHandler.getMazeTileBounds(mapTile-1))) {
                  if (MazeHandler.getMazeTileType(mapTile-1) == MazeHandler.WALL) {
                        pacmanR.x-=incX;pacmanR.y-=incY;
                        return false;
                  }
            }
            if (pacmanR.intersects(MazeHandler.getMazeTileBounds(mapTile+1))) {
                  if (MazeHandler.getMazeTileType(mapTile+1) == MazeHandler.WALL) {
                        pacmanR.x-=incX;pacmanR.y-=incY;
                        return false;
                  }
            }
            if (pacmanR.intersects(MazeHandler.getMazeTileBounds(mapTile+20))) {
                  if (MazeHandler.getMazeTileType(mapTile+20) == MazeHandler.WALL) {
                        pacmanR.x-=incX;pacmanR.y-=incY;
                        return false;
                  }
            }
            if (pacmanR.intersects(MazeHandler.getMazeTileBounds(mapTile-20))) {
                  if (MazeHandler.getMazeTileType(mapTile-20) == MazeHandler.WALL) {
                        pacmanR.x-=incX;pacmanR.y-=incY;
                        return false;
                  }
            }
            if (pacmanR.intersects(MazeHandler.getMazeTileBounds(mapTile-21))) {
                  if (MazeHandler.getMazeTileType(mapTile-21) == MazeHandler.WALL) {
                        pacmanR.x-=incX;pacmanR.y-=incY;
                        return false;
                  }
            }
            if (pacmanR.intersects(MazeHandler.getMazeTileBounds(mapTile-19))) {
                  if (MazeHandler.getMazeTileType(mapTile-19) == MazeHandler.WALL) {
                        pacmanR.x-=incX;pacmanR.y-=incY;
                        return false;
                  }
            }
            if (pacmanR.intersects(MazeHandler.getMazeTileBounds(mapTile+21))) {
                  if (MazeHandler.getMazeTileType(mapTile+21) == MazeHandler.WALL) {
                        pacmanR.x-=incX;pacmanR.y-=incY;
                        return false;
                  }
            }
            if (pacmanR.intersects(MazeHandler.getMazeTileBounds(mapTile+19))) {
                  if (MazeHandler.getMazeTileType(mapTile+19) == MazeHandler.WALL) {
                        pacmanR.x-=incX;pacmanR.y-=incY;
                        return false;
                  }
            }            
                              
            pacmanR.x-=incX;pacmanR.y-=incY;
            return true;
                        
      }


so that is what im doing right now. i know there has got to be a better less time consuming more efficient way, but i cant figure it out. can anyone give me a few pointers?

thanks. (if u need anymore of the code to see what im doing just say so Tongue)
Offline NexusOne

Junior Member




Java games rock!


« Reply #1 - Posted 2004-01-01 00:03:21 »

I don't really have time to study ur code right now, but what I would do is just have a char variable called direction, set to 'l', 'r', 'u', or 'd', since pacman can never stop there's no need for 'n', and since he can't change directions mid-tile, only at intersections, I would store another char variable called userDir that is taken from key input, and is always changeable (because you can tell pacman to turn mid-tile, he just won't do it yet) then when he reaches the intersection set direction to userDir. If userDir stays the same pacman will also therefore keep going in that direction. of course check for collision right after setting direction to userDir. Every repaint update his movement and the direction of his image based on the direction variable, not based on userDir. When checking for collision, don't write a separate test for every direction, all you need to do in a tile-based world is say something like if(tiles
  • [y] == false) movePacman();  where tile is your world, stored in boolean form (wall == true, space == false) or of course whatever form you want, and where x and y are the coordinates of pacman's goal tile (only do this right at the intersection). This is just what I would do, but I think it's pretty efficient, it shouldn't take up any time at all. Not sure if that helped or not...
Offline javatypo

Junior Member





« Reply #2 - Posted 2004-01-01 15:47:05 »

yes i think i got it much working much better right now.

i realized how to do everything after u said that he can only turn at intersections, so i just calculate whehter or not he is on only one tile, and if so then handle direction changes or dot eating actions, also before i do that i check to see if the next requested direction is opposite that of the current direction since then he can go back the way he came.

thanks.. now i have to go to getting enemies to move without them getting caught in circular loops lol..
Pages: [1]
  ignore  |  Print  
 
 
You cannot reply to this message, because it is very, very old.

 

Add your game by posting it in the WIP section,
or publish it in Showcase.

The first screenshot will be displayed as a thumbnail.

pw (17 views)
2014-07-24 01:59:36

Riven (17 views)
2014-07-23 21:16:32

Riven (14 views)
2014-07-23 21:07:15

Riven (17 views)
2014-07-23 20:56:16

ctomni231 (45 views)
2014-07-18 06:55:21

Zero Volt (40 views)
2014-07-17 23:47:54

danieldean (32 views)
2014-07-17 23:41:23

MustardPeter (36 views)
2014-07-16 23:30:00

Cero (51 views)
2014-07-16 00:42:17

Riven (50 views)
2014-07-14 18:02:53
HotSpot Options
by dleskov
2014-07-08 03:59:08

Java and Game Development Tutorials
by SwordsMiner
2014-06-14 00:58:24

Java and Game Development Tutorials
by SwordsMiner
2014-06-14 00:47:22

How do I start Java Game Development?
by ra4king
2014-05-17 11:13:37

HotSpot Options
by Roquen
2014-05-15 09:59:54

HotSpot Options
by Roquen
2014-05-06 15:03:10

Escape Analysis
by Roquen
2014-04-29 22:16:43

Experimental Toys
by Roquen
2014-04-28 13:24:22
java-gaming.org is not responsible for the content posted by its members, including references to external websites, and other references that may or may not have a relation with our primarily gaming and game production oriented community. inquiries and complaints can be sent via email to the info‑account of the company managing the website of java‑gaming.org
Powered by MySQL Powered by PHP Powered by SMF 1.1.18 | SMF © 2013, Simple Machines | Managed by Enhanced Four Valid XHTML 1.0! Valid CSS!