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

2018/7/23

Pacman Game code

Ticker175 Ticker175

2018/7/23

#
Hey guys im having tons of "fun" creating a copy of the Pacman game in greenfoot i had the great idea of trying to give the ghosts AI (its not fun...) i got 2 questions: how do i get the location of pacman from another actor or to another actor to be use like a variable (mainly to be used in the following question) so i have decided to not use nodes mainly because i don't have the time so i was planning on using the turnTowards command to try and get the ghosts to track to pacman (i don't really know how else to do it =/. It wont work great but i don't know any other way of doing it. so to get the "turnTowards" command to work im planning on getting the rotation of the ghost and rounding it to the closest direction (0,90,180,270). but I'm not sure how to code the syntax of "greaterthan 45* lessthan 135*" (45 and 135 would round to 90) if anyone has an easier idea to do what i need please lemi know i really have no idea how else to do it some important factors to take into account if your answering this is: there are walls (like in the original) i am using a different layout to normal ( ) and i would like to be able to add the different personality's to the ghosts Thanks Thomas
Super_Hippo Super_Hippo

2018/7/23

#
You can get a reference to pacman with:
1
Pacman pacman = (Pacman) getWorld().getObjects(Pacman.class).get(0);
Or you can store a reference to Pacman in your world and use that:
1
2
private Pacman pacman = new Pacman();
public Pacman getPacman() {return pacman;}
1
Pacman pacman = ((WorldClassName) getWorld()).getPacman();
This may help, too: http://www.greenfoot.org/scenarios/19573 The way the movement and intersection with walls work always depend on the way the walls are added to the world. Are they only on the background image (like I did it and my version --> using an array in the world for the map and adding of coins) or are they objects in the world with which Greenfoot intersection methods can be used on? Like always in programming, many ways lead to the target, you just have to choose one.
Ticker175 Ticker175

2018/7/23

#
thanks for the response i am using an array to create my world. i do have intersections implemented (just black tiles) for other elements. if u could have a look at my game and suggest a way to get this tracking working that would be awesome also any suggestions or improvements would be great. My game code: https://drive.google.com/file/d/1m1OJJDhzCR7irkGaYL3CdFcnD-GKf9wJ/view?usp=sharing Thanks Thomas
Ticker175 Ticker175

2018/7/23

#
i just went to impliment the solution you recommended and i realised that im not sure how to detect if there is a wall next to the ghost do you have any tips??
Bananarama Bananarama

2018/7/23

#
thanks for the response =) i am useing an array and i already have intercections for other elements im just not sure how to connect them into a network or detrect what the best route is my game files : https://drive.google.com/file/d/1m1OJJDhzCR7irkGaYL3CdFcnD-GKf9wJ/view?usp=sharing if u can check out the game and give ur suggestions or tips to improve that would be awesome Thanks Thomas
Ticker175 Ticker175

2018/7/23

#
^^^^^^^^^^^^^^^^^^^ ignore this comment please
Super_Hippo Super_Hippo

2018/7/23

#
Please upload your code here to the site or post relevant code here in the discussion. To see if there is a wall next to you, locate where the ghost is in the world's array and check the "neighbors".
You need to login to post a reply.