1
$\begingroup$

We are working to create a pacman type of game and we cannot figure out for to get colored dots on the maze to represent the food the pacman eats. Here is our code so far:

style = {Background -> GrayLevel[0], 
   BaseStyle -> {Directive[White, EdgeForm[], Opacity[1]]}, 
   VertexShapeFunction -> (Rectangle[#1 + .16, #1 - .16] &), 
   EdgeShapeFunction -> (Rectangle[#1[[1]] + .16, #1[[2]] - .16] &)};
embedding = GraphEmbedding[GridGraph[{20, 30}]]; 
g = GridGraph[{20, 30}, EdgeWeight -> RandomReal[10, 1150]];
tree = FindSpanningTree[{g, 1}]; 
maze = Binarize[Image[Rasterize[Graph[tree, VertexCoordinates->embedding, style], 
       RasterSize -> 1000], ImageSize -> 1000]];
m = 5;
DynamicModule[{diskPosition = {250, 450}, diskRadius = 15, pacman = {9, 4}}, 
 EventHandler[
  Show[maze, Graphics[Dynamic[{Yellow, Disk[diskPosition, diskRadius, pacman]}],
     PlotRange -> {{0, 200}, {0, 300}}]], 
{"RightArrowKeyDown" :> 
    If[PixelValue[maze, {diskPosition[[1]] + m, diskPosition[[2]]}] == 1, 
                  pacman = {1, 6}; diskPosition[[1]] += m],
 "LeftArrowKeyDown" :> 
    If[PixelValue[maze, {diskPosition[[1]] - m, diskPosition[[2]]}] == 1, 
                  pacman = {9, 4}; diskPosition[[1]] += -m],
 "UpArrowKeyDown" :> 
    If[PixelValue[maze, {diskPosition[[1]], diskPosition[[2]] + m}] == 1, 
                  pacman = {2, 7}; diskPosition[[2]] += m],
 "DownArrowKeyDown" :> 
    If[PixelValue[maze, {diskPosition[[1]], diskPosition[[2]] - m}] == 1, 
                  pacman = {-2, -7}; diskPosition[[2]] += -m]}]]

This gives us a pacman that moves along the white part of the maze. Now we want to be able to put dots along the white that disappear when the pacman passes over them.

Code taken from another question: program a wall in which an object can not pass

$\endgroup$
1
  • $\begingroup$ Sorry about that, the question has been edited. $\endgroup$
    – Savannah S
    Dec 1, 2016 at 17:25

0

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.