100% found this document useful (1 vote)
912 views

Source Code Pacman

This document contains the code for a Pacman game applet written in Java. It initializes graphics buffers and loads images for the game. The main run() method contains the game loop that calls methods to update the game state and redraw it, preventing flickering. Keypress handling methods are included but not fully implemented. The code sets up double buffering and threading to run the game loop continuously in the background.

Uploaded by

JakaItuJack
Copyright
© Attribution Non-Commercial (BY-NC)
Available Formats
Download as TXT, PDF, TXT or read online on Scribd
100% found this document useful (1 vote)
912 views

Source Code Pacman

This document contains the code for a Pacman game applet written in Java. It initializes graphics buffers and loads images for the game. The main run() method contains the game loop that calls methods to update the game state and redraw it, preventing flickering. Keypress handling methods are included but not fully implemented. The code sets up double buffering and threading to run the game loop continuously in the background.

Uploaded by

JakaItuJack
Copyright
© Attribution Non-Commercial (BY-NC)
Available Formats
Download as TXT, PDF, TXT or read online on Scribd
You are on page 1/ 4

// Pacman by X5K MaStEr X5K and John // Started September 10, 2001 // // // // We used some code from: Warp

By Karl Hornell, June 10, 1996 to get us going. Thanks Karl! java.awt.*; java.awt.image.*; java.applet.AudioClip; java.net.*; java.awt.Font;

import import import import import

import java.util.Random; public final class Pacman extends java.applet.Applet implements Runnable { Thread updateThread; // thread in which the game will run long startTime; // used to keep track of timing and to p revent applet from running too fast Graphics gBuf; // used for double-buffered graphics Image imgBuf; // also used for double-buffered graphics static final int MAX_X = 400; // widest the playing screen can be static final int MAX_Y = 400; // tallest the playing screen can be public void init() { // Make the applet window the size we want it to be resize(MAX_X, MAX_Y); // Load the images we will use from the web getMainGraphics(); // Garbage collection call. Not really needed. System.gc(); // Make a black background setBackground(Color.black); // Set up double-buffered graphics. // This allows us to draw without flickering. imgBuf = createImage(MAX_X, MAX_Y); gBuf = imgBuf.getGraphics(); // try to grab the keyboard focus. requestFocus(); }

public void getMainGraphics() // Load and process the most common graphics { MediaTracker tracker; int i = 0; tracker = new MediaTracker(this);

// this code doesn't load any graphics yet! try { tracker.waitForAll(); } catch (InterruptedException e) { } }

public void run() { // 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. while (updateThread != null) { try { // this code slows the applet down if it is on a really fast machine long sleepTime = Math.max(startTime - System.currentTime Millis(), 20); updateThread.sleep(sleepTime); } catch (InterruptedException e) { } startTime = System.currentTimeMillis() + 40; // DRAW STUFF HERE: // clear what we drew last time. gBuf.clearRect(0, 0, MAX_X, MAX_Y); // set the drawing color gBuf.setColor(Color.yellow); // draw a rectangle for now. gBuf.drawRect(10, 10, 50, 50); // repaint() will call paint(Graphics) which will call update(Gr aphics) repaint(); } } // This method is called when the applet is run. // It initiallizes the thread and gets things going. public void start()

{ if (updateThread == null) { updateThread = new Thread(this, "Game"); updateThread.start(); startTime = System.currentTimeMillis(); } } // This method is called when the applet is stopped. public void stop() { updateThread = null; } public boolean keyDown(java.awt.Event e, int key) { // This method handles key presses. // For now all the statements are placeholders. // // // // it is nice to have a print statement here. it can be quickly uncommented and the output used to get keycodes since I am too lazy to look them up.

//System.out.println(key); if (key == 1006) // right arrow { return false; } if (key == 1007) // left arrow { return false; } if (key == 1004) // up arrow { return false; } if (key == 1005) // down arrow { return false; } if (key == 32) // space bar { return false; } if (key == 112) // 'P' key {

return false; } return false; } public boolean keyUp(java.awt.Event e, int key) { // This method is called when a key is released. // right now it just has place holders. if (key == 1006 { } if (key == 1004 { } if (key == 32) // space bar released { return false; } return false; } // To ensure Java 1.1 compatibility, request focus on mouseDown public boolean mouseDown(java.awt.Event e, int x, int y) { requestFocus(); return false; } public void paint(Graphics g) // Draw the control panel and stuff { // Since there are no borders or anything // static to draw yet we only need to call // the update method. update(g); } public void update(Graphics g) { // draw the offscreen buffer to the screen! // This buffer was drawn on by the run() method // and by any methods run() might have called. g.drawImage(imgBuf, 0, 0, this); } } key == 1005) // up or down key released. key == 1007) // left or right key released

return false;

return false;

You might also like