Ameba Ownd

アプリで簡単、無料ホームページ作成

loronbapum1987's Ownd

How to make a pacman game in java

2022.01.16 00:55




















About the code: I have not started the graphics yet. I have just done a little work using a two-dimenstional array to store the state of the Pacman, and at the bottom there is a method which randomizes the motion of the Pacman. The large array in the beginning represents a 5 x 5 grid with vertices in which the Pacman can move through.


If you copy the code into a compiler, you can see what I mean. When Pacman is at a vertex , he will make a decision and can thus move to another one. If the value is negative, then there is a vertical distance in which the Pacman can move through, but if the value is positive, then this distance is horizontal. Please give me any suggestions you have on what my next steps should be and how I should clean up the code. The more specific, the better.


But basically, anything you say will be fine. This is a great code review opportunity because your program 1 works, and 2 has tons of room for improvement. Almost every line of your code could be refactored, but don't take that negatively.


Every day I come out of code reviews with changes suggested to my awesome code. It's just how the business works and we should always enjoy the opportunity to learn and write even better code in the future. You define the graph in your code, which is okay for early development and quick debugging, but you should really load it from a file.


Google will lead you to some Stack Overflow answers on how to do this , and I highly reccommend it. It's quite easy, and it's never to early to have some experience with file IO. But was does -4 in your graph mean? As you'll see, it's a bad idea to use these Magic Numbers. That way you don't have to worry about the problems that can come along with arrays.


Notice also that I declared them private. Without a modifier, they have default access, which means any other class in the same package as it can modify it's values. Rarely a good idea. You should replace them with constants declared near the top of the class so you can easily modify. And with the previous suggestion, this could be rewritten as. I'll keep bringing up magic numbers because they are evil. Or, if not evil, unnecessary, as the following code shows:.


Whitespace between things. Getting length of the array means you can change the size of your grid without having to change everything. Seriously, anywhere you have 25 in your code should be replaced with grid. Avoid read, never use an if -statement without curly brackets. It'll only cause bugs when you come back and change things. For example, what if I intended for System. Speaking of, the traditional way to output an array is to use Arrays. This got me thinking, why is graph an int array anyways?


Can you not make it a char array and just print that out, instead of doing all these calculations? I got confused here. What is renderPlane and how is it different from graph? That being said, you can do some neat improvements to this method. Again, magic numbers. Replace with constants so you can change easily.


What does this code really mean? If the ghost enters a tunnel, he will continue in the same direction until he is out of the tunnel. Moving of ghosts is partly random. We do not apply this randomness inside long tunnels. The ghost might get stuck there.


Next we are going to examine the movePacman method. The reqdx and reqdy variables are determined in the TAdapter inner class. These variables are controlled with cursor keys. If the pacman moves to a position, where there is a point, we remove it from the maze and increase the score value. There are four possible recommendations for a pacman.


There are four images to all recommendations. The images are used to turn the pacman begin his mouth area shut. The drawMaze strategy draw a network out of numbers in the wide range screendata. Variety 1 is the staying boundary, 2 is the greater boundary, 4 is the real boundary, 8 and 16 is the decreased limit is the aspect.


We just went through all int box network. For example we have 9 in the wide range screendata. We had a bit first 1 and 4th items 8 locations.


So we draw down and staying limitations in this particular rectangular shape. You are commenting using your WordPress. You are commenting using your Google account. You are commenting using your Twitter account. You are commenting using your Facebook account. Notify me of new comments via email.


Notify me of new posts via email. Menu Skip to content. Home Java Coding Theory About. Pacman has three lives. We also count the score. The Pacman is controlled with the cursor keys. The Esc key finishes the game, the Pause key pauses it. The first two variables store the x and y coordinates of the Pacman sprite. The last two variables are the delta changes in horizontal and vertical directions.


These numbers make up the maze. They provide information out of which we create the corners and the points. Number 1 is a left corner. Numbers 2, 4 and 8 represent top, right, and bottom corners respectively. Number 16 is a point. The doAnim counts the pacmanAnimPos variable which determines what pacman image is drawn. There are four pacman images. Otherwise the pacman would open his mouth too fast. This code is part of the checkMaze method. It checks if there are any points left for the Pacman to eat.


Number 16 stands for a point.