Java-Gaming.org    
Featured games (81)
games approved by the League of Dukes
Games in Showcase (495)
Games in Android Showcase (114)
games submitted by our members
Games in WIP (563)
games currently in development
News: Read the Java Gaming Resources, or peek at the official Java tutorials
 
   Home   Help   Search   Login   Register   
  Show Posts
Pages: [1]
1  Java Game APIs & Engines / Java 2D / Flickering problems(!) Again(!) on: 2003-02-01 02:23:19
Hello, I'm rather new to Java but that didn't stop me from trying to make my first game. I thought I'd start with Pacman since I was given some of the source code from someone, I'm having problems with the buffer (I think). There seems to be flickering in the applet. If someone can help me out, I'd appreciate it. I'm not sure where the entire problem lies, but I'll paste my source code that I've done so far:

(applet is set at width=380 height=480)
Test.gif is a pretty coloured maze, a bit like the original pacman then.

----------------------------------


import java.awt.Color;
import java.awt.Event;
import java.awt.Graphics;
import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
import java.applet.Applet;
import java.awt.image.*;
import java.awt.geom.AffineTransform;
import java.awt.event.WindowEvent;
import java.awt.event.WindowListener;
import java.awt.event.WindowAdapter;

public class Pacman extends java.applet.Applet implements Runnable
{
     Thread CurrentThread; // Deals with the thread in which the game will run.
     long startTime;                  // used to keep track of timing and to prevent applet from running too fast
     
     int open = 0; // Angle of mouth, actually does the animation.
     static final int PacSize = 20; // Size of Pacman.
     int mouthStartAngle = 90; // Mouth direction movement is dealt with this.
     
     // Moving Pacman in different directions.
     int dx[] = {0,0}; // amount x position will change
     int dy[] = {0,0}; // amount y will change
     int x = 180;
     int y = 360;
     
     int nextX = 0;
     int nextY = 0;

     int row;
     int col;
     boolean hitWall = false;
     
     BufferedImage Tester;
     Image MazePic;
     Graphics GraphicBuffer;      // used for double-buffered graphics
     Image ImageBuffer;      // also used for double-buffered graphics

     int i;
     
     final static int MazeSize = 24;
     // This array describes each "cell" of the screen
     // Cells labeled 0 are open and those with a 1 are
     // walls.
     int[][] mazeArray =
           {       
                 {1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,0,0,0,0,0},
                 {1,0,0,0,0,0,0,0,0,1,0,0,0,0,0,0,0,0,1,0,0,0,0,0},
                 {1,0,1,1,0,1,1,1,0,1,0,1,1,1,0,1,1,0,1,0,0,0,0,0},
                 {1,0,1,1,0,1,1,1,0,1,0,1,1,1,0,1,1,0,1,0,0,0,0,0},
                 {1,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,1,0,0,0,0,0},
                 {1,0,1,1,0,1,0,1,1,1,1,1,0,1,0,1,1,0,1,0,0,0,0,0},
                 {1,0,0,0,0,1,0,0,0,1,0,0,0,1,0,0,0,0,1,0,0,0,0,0},
                 {1,1,1,1,0,1,1,1,0,1,0,1,1,1,0,1,1,1,1,0,0,0,0,0},
                 {0,0,0,1,0,1,0,0,0,0,0,0,0,1,0,1,0,0,0,0,0,0,0,0},
                 {0,0,0,1,0,1,0,1,1,0,1,1,0,1,0,1,0,0,0,0,0,0,0,0},
                 {1,1,1,1,0,1,0,1,0,0,0,1,0,1,0,1,1,1,1,0,0,0,0,0},
                 {0,0,0,0,0,0,0,1,0,0,0,1,0,0,0,0,0,0,0,0,0,0,0,0},
                 {1,1,1,1,0,1,0,1,0,0,0,1,0,1,0,1,1,1,1,0,0,0,0,0},
                 {0,0,0,1,0,1,0,1,1,1,1,1,0,1,0,1,0,0,0,0,0,0,0,0},
                 {0,0,0,1,0,1,0,0,0,0,0,0,0,1,0,1,0,0,0,0,0,0,0,0},
                 {1,1,1,1,0,1,0,1,1,1,1,1,0,1,0,1,1,1,1,0,0,0,0,0},
                 {1,0,0,0,0,0,0,0,0,1,0,0,0,0,0,0,0,0,1,0,0,0,0,0},
                 {1,0,1,1,0,1,1,1,0,1,0,1,1,1,0,1,1,0,1,0,0,0,0,0},
                 {1,0,0,1,0,0,0,0,0,0,0,0,0,0,0,1,0,0,1,0,0,0,0,0},
                 {1,1,0,1,0,1,0,1,1,1,1,1,0,1,0,1,0,1,1,0,0,0,0,0},
                 {1,0,0,0,0,1,0,0,0,1,0,0,0,1,0,0,0,0,1,0,0,0,0,0},
                 {1,0,1,1,1,1,1,1,0,1,0,1,1,1,1,1,1,0,1,0,0,0,0,0},
                 {1,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,1,0,0,0,0,0},
                 {1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,0,0,0,0,0}
           };


     // Start applet.
     // This method is called when the applet is run.
     // It initiallizes the thread and gets things going.      
  public void start ()
  {
     if (CurrentThread == null)
     {
        CurrentThread = new Thread (this);
        CurrentThread.start ();
        startTime = System.currentTimeMillis();
     }
  }
 
     // Stop applet.  
     // This method is called when the applet is stopped.
  public void stop ()
  {
     if (CurrentThread != null)
     {
        CurrentThread.stop ();
        CurrentThread = null;
     }
  }
 
  public void init(){
         //setBackground (Color.black);
           ImageBuffer = createImage(size().width, size().height);
         GraphicBuffer = ImageBuffer.getGraphics();
         MazePic = getToolkit().getImage("Test.gif");

}
 
     // Run applet and initialise.
     // This is the most important method.  It loops over and
     // over again as the game is running.  It makes the calls
     // that move things and then draw them.
  public void run ()
  {    
           
      // Duh, set background to any colour.
     while (true)
     {
        open = (open + 10) % 80; // Open and close mouth update.
       
       //Move PacMan
       
           //x = x + dx; // Key presses change this to move Pacman left or right.
           //y = y + dy; // Key presses change this to move Pacman up or down.
           
                       for (i = 1; i >= 0; i--)
           {
                 nextX = x + dx;
                 nextY = y + dy;
                 hitWall = false;

                 // Process each "cell" of the maze and paint it correctly
                 for (int xCorner = 0; xCorner < PacSize; xCorner += PacSize - 1)
                 {
                       for (int yCorner = 0; yCorner < PacSize; yCorner += PacSize - 1)
                       {
                             col = (nextX + xCorner) / PacSize;
                             row = (nextY + yCorner) / PacSize;
                             if (row < MazeSize && col < MazeSize)
                             {
                                   if (mazeArray[row][col] == 1)
                                   {
                                         hitWall = true;
                                         break;
                                   }
                             }
                       }
                 }
                 if (!hitWall)
                 {
                       if (i == 1)
                       {
                             dx[0] = dx[1];
                             dy[0] = dy[1];
                       }
                       break;
                 }

           }
           
           if (dx[0] > 0)
           {
                 mouthStartAngle = 0;
           }
           else if (dx[0] < 0)
           {
                 mouthStartAngle = 180;
           }
           else if (dy[0] > 0)
           {
                 mouthStartAngle = 270;
           }
           else if (dy[0] < 0)
           {
                 mouthStartAngle = 90;
           }
           
                       //Move PacMan
           if (!hitWall)
           {
                 x = nextX;
                 y = nextY;
           }
           else
           {
                 dx[0] = 0;
                 dx[1] = 0;
                 dy[0] = 0;
                 dy[1] = 0;
           }


           // Don't let Pacman go off the sides of the screen.
           if (x > size().width - PacSize)
           {
                 x = size().width - PacSize;
                 dx[0] = 0;
                 dx[1] = 0;
           }
           if (x < 0)
           {
                 x = 0;
                 dx[0] = 0;
                 dx[1] = 0;
           }

           // Don't let Pacman go off the top or bottom of the screen.
           if (y > size().height - PacSize)
           {
                 y = size().height - PacSize;
                 dy = 0;
                 dy[1] = 0;
           }
           if (y < 0)
           {
                 y = 0;
                 dy = 0;
                 dy[1] = 0;
           }
           
                       // clear what we drew last time.
                       
           GraphicBuffer.clearRect(0, 0, size().width, size().height);
           
                 GraphicBuffer.setColor (Color.yellow);
     
     //Pac.drawRect (10, 20, size().width - 20, size().height - 40);
     // Point(x1), Point(y1), Point(x2), lengthLine(y2). //size().height assigned in HTML.
     //Pac.drawLine (10, 20, size().width - 10, 20); //size().width assigned in HTML.
     // Starting Point(x), lengthLine(y), Stop Point(endx), lengthLine(endy).
     
     // Drawing the Arc.
     GraphicBuffer.fillArc(x, y, PacSize, PacSize,mouthStartAngle + open/2,360-open);
     // The arc is closed and opened with the settings, no need to have a arc initialised.
     
     
     //Draw Maze Borders
/*                  GraphicBuffer.setColor(Color.blue);

           for (row = 0; row < MazeSize; row++)
           {
                 for (col = 0; col < MazeSize; col++)
                 {
                       if (mazeArray[row][col] == 1)
                       {
                             GraphicBuffer.fillRect(col * PacSize, row * PacSize, PacSize, PacSize);
                       }
                 }
           }*/
           
           
        repaint (); // Run??
        try
        {
              // this code slows the applet down if it is on a really fast machine
                 long sleepTime = Math.max(startTime - System.currentTimeMillis(), 20);
                 CurrentThread.sleep(sleepTime);
           //Thread.sleep (60); //Frames per second (Read: SPEED).
        }
        // nothing really to be caught
        catch (InterruptedException e)
        {
        }
        startTime = System.currentTimeMillis() + 40;
     }
  }
 


     // Draw Pacman with graphics.
     
  public void paint (Graphics Pac)
  {

update(Pac)      ;      
  }
 
 
    public void update (Graphics Pac)
  {


Pac.drawImage(MazePic, 0, 0, this);
Pac.drawImage(ImageBuffer, 0, 0, this);
           
  }

 
public boolean keyDown(java.awt.Event e, int key)
{

     // This method handles key presses.

     if (key == 1006) // Left arrow (Can also use EVENT.LEFT instead of key).
     {
           if (dx[0] == 0 && dy[0] == 0)
           {
                 dx[0] = -5;
                 dx[1] = -5;
                 dy[0] = 0;
                 dy[1] = 0;
           }
           else
           {
                 dx[1] = -5;
                 dy[1] = 0;
           }
           return false;
     }
     if (key == 1007) // Right arrow.
     {
           if (dx[0] == 0 && dy[0] == 0)
           {
                 dx[0] = 5;
                 dx[1] = 5;
                 dy[0] = 0;
                 dy[1] = 0;
           }
           else
           {
                 dx[1] = 5;
                 dy[1] = 0;
           }
           return false;
     }
     if (key == 1004) // Up arrow.
     {
           if (dx[0] == 0 && dy[0] == 0)
           {
                 dx[0] = 0;
                 dx[1] = 0;
                 dy[0] = -5;
                 dy[1] = -5;
           }
           else
           {
                 dx[1] = 0;
                 dy[1] = -5;
           }
           
           return false;
     }
     if (key == 1005) // Down arrow.
     {
           if (dx[0] == 0 && dy[0] == 0)
           {
                 dx[0] = 0;
                 dx[1] = 0;
                 dy[0] = 5;
                 dy[1] = 5;
           }
           else
           {
                 dx[1] = 0;
                 dy[1] = 5;
           }
           return false;
     }
     if (key == 112) // 'P'ause key.
     {
           // Will pause the game somehow!
           return false;
     }

     return false;
}
     
public boolean keyUp(java.awt.Event e, int key)
{
     // This method is called when a key is released.
           
     if (key == 1006 || key == 1007)  // Left or right key released.
     {
           return false;
     }

     if (key == 1004 || key == 1005) // Up or down key released.
     {
           return false;
     }

     return false;
}

// To ensure Java 1.1 compatibility, request focus on mouseDown
// Need this, click in applet to use keys, hence mouse event needed!
// READ: NEED THIS TO USE THE KEYS! SHOCKER!
public boolean mouseDown(java.awt.Event e, int x, int y)
{
     requestFocus();
     return false;
}

}// End Pacman class.

Pages: [1]
 

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

The first screenshot will be displayed as a thumbnail.

Dwinin (25 views)
2014-09-12 09:08:26

Norakomi (56 views)
2014-09-10 13:57:51

TehJavaDev (69 views)
2014-09-10 06:39:09

Tekkerue (35 views)
2014-09-09 02:24:56

mitcheeb (56 views)
2014-09-08 06:06:29

BurntPizza (40 views)
2014-09-07 01:13:42

Longarmx (26 views)
2014-09-07 01:12:14

Longarmx (33 views)
2014-09-07 01:11:22

Longarmx (31 views)
2014-09-07 01:10:19

mitcheeb (39 views)
2014-09-04 23:08:59
List of Learning Resources
by Longor1996
2014-08-16 10:40:00

List of Learning Resources
by SilverTiger
2014-08-05 19:33:27

Resources for WIP games
by CogWheelz
2014-08-01 16:20:17

Resources for WIP games
by CogWheelz
2014-08-01 16:19:50

List of Learning Resources
by SilverTiger
2014-07-31 16:29:50

List of Learning Resources
by SilverTiger
2014-07-31 16:26:06

List of Learning Resources
by SilverTiger
2014-07-31 11:54:12

HotSpot Options
by dleskov
2014-07-08 01:59:08
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!