0

Hello for a school exercise i need to create a game and i decided to create Pacman. Everything goes well but one thing i can't accomplish is to draw a pacman dude and his ghosts.. i made and oval but what now? i want the pacman mouth also to open and close as it moves. can someone help drawing this?

below what i have till now:

package h04PacMan;

import java.awt.*;

public class DrawPacMan {

public void drawPacMan(Graphics g, int x, int y, Color color) {

    g.setColor(color); // set color
    g.fillOval(x, y, 50, 50); // paint
    g.setColor(Color.black);
    g.drawOval(x, y, 50, 50); // outline
    // mouth?

}

public void drawGhost(Graphics g, int x, int y, Color color) {

    g.setColor(color); // color
    // here goes shape

}

}
Reshad
  • 2,570
  • 8
  • 45
  • 86
  • 6
    I suggest you use sprites/images rather than shapes. – JCOC611 Oct 08 '12 at 18:46
  • Yes i know that is much better but i have to draw it myself.. ( just started java so its for exercise purpose ) and besides that i didn't use sprites or images yet so i don't know how.. – Reshad Oct 08 '12 at 18:53

3 Answers3

2

Instead of using drawOval and fillOval, you should have a look at drawArc and fillArc.

See java.awt.Graphics.

Concerning the animation of the mouth: Given that pacman is constantly moving, you could combine pacman's position with a sine function to get a nice and smooth mouth movement, something like this:

angle = 20 * (Math.sin((x + y)*2*Math.PI/50) + 1); # alt. betw. 0 and 40
g.fillArc(x, y, 50, 50, angle/2, 360-angle);

This way, Pacman's mouth will automatically do one open-close-cycle as he moves a distance of his own size through the maze. (You may have to tweak the numbers a bit to fit your setup.)

Of course, you will still need a thread to run the game as a whole, but the animation of Pacman's mouth can be done this way, too, without extra threads.

tobias_k
  • 81,265
  • 12
  • 120
  • 179
  • i tried you way with some edits.. but it won't work as expected i did the following: int angle = (int) (20 * (sin((x + y)*2*Math.PI/50) + 1)); g.fillArc(x, y, radius, radius, angle/2, 360-angle); } public static double sin(double sin) { return sin; } – Reshad Oct 09 '12 at 13:20
  • somehow it won't work because i get an error on the sin part.. Could you give me an example on what methods you have used and the "working" code of your example? – Reshad Oct 09 '12 at 20:57
  • Nevermind it works as expected now! had to rewrite the sin to math.sin :) – Reshad Oct 09 '12 at 21:01
1

So there are a couple of things going on here that you will need to address.

1. Drawing Characters

I'll only address drawing Pacman here.

Thinking about the Pacman game, Pacman's character has two states -- mouth closed, and mouth opened (in each cardinal direction!). This will be important for when we animate Pacman in a moment, so first, lets establish our pacman shape by using the fillArc method from the Graphics library.

Example of Mouth-Open Pacman:

  g.setColor(Color.yellow);
  g.fillArc(0,0,150,150,30,300);

This will create pacman in the top-left corner of your window, with pacman's mouth facing to the right of the screen. The last two paramters of the fillArc method control this opening -- the 5th parameter is the starting angle, and the sixth parameter is the angle of the full arc. You may infer from this that the start angle 0 is the horizontal line going from the center of the arc to the right of the screen. Also remember from mathematics that a full circle is 360 degrees.

Using this information, try drawing mouth-opened Pacman facing up, down and left. After you do that, fill the entire arc to draw Pacman with his mouth closed. Also consider other methods in the Graphics library you can use to draw your ghosts!

2. Animating Pacman

For this task you will probably want to implement some sort of Thread structure to animate Pacman, which gets quite a bit more complicated than what you've demonstrated here. Since we don't know your program specifications, and you also haven't demonstrated any code relating to the animation yet, I'm hesitant to lay this out any further. However, I will direct you to some links on the matter:

Anthony Neace
  • 25,013
  • 7
  • 114
  • 129
  • i am using Swing for this project, the requirements are as followed: create some ghosts that move from a to b and from b to a ( so not random yet ) and a pacman object that can be moved by using 4 buttons (up, down, left, right) that i have created. at this moment i have created everything already up and working except the buttons for up and down won;t work as expected and i can't animate the pacman/ghosts yet the rest was not that hard. – Reshad Oct 09 '12 at 13:09
  • @Reshad Good to hear you're having some success -- it sounds like you can do what you want to do with SwingWorker. This creates a thread for the swing component your animation is in, and handles the animation there so it doesn't freeze the GUI by working in the main thread. Go ahead and give that link above a read and let us know as you progress. – Anthony Neace Oct 09 '12 at 13:12
  • Thanks for the help Anthony, I am trying to "Understand" SwingWorker but since i just started Java at school it's not very easy to understand yet :P p.s. if u want to see what i have till now see my project at github: https://github.com/reshadf/Java/tree/master/DeBasis/src/h04PacMan – Reshad Oct 09 '12 at 13:28
  • 1
    @Reshad There's a good solution [here](http://stackoverflow.com/questions/782265/how-do-i-use-swingworker-in-java) on stack overflow that elaborates more on how to use SwingWorker. – Anthony Neace Oct 09 '12 at 13:30
  • ah that is more understandable! so where do i use this SwingWorker for in my game? only for the pacman animation? ( since I assume that takes a lot of callbacks ) – Reshad Oct 09 '12 at 13:52
  • @Reshad If you elect to use SwingWorker, you'll want to use it to animate your characters. This is necessary because you'll have quite a few things going on concurrently (pacman moving, multiple ghosts moving independently, and perhaps other things like the calculations for fruit and pellets pacman collects) and trying to do them all on the same thread will freeze the GUI. By putting each character, or each operation, in its own Swingworker, you will be able to handle the animations in multiple threads. – Anthony Neace Oct 09 '12 at 13:58
0

I'd recommend taking advantage Area/Path2D API.

Have a look at Graphics 2D

You can have a look at the sample code in this answer for an example

Community
  • 1
  • 1
MadProgrammer
  • 343,457
  • 22
  • 230
  • 366