Black Art of Java Game Programming PHẦN 3 doc

98 432 1
Black Art of Java Game Programming PHẦN 3 doc

Đang tải... (xem toàn văn)

Tài liệu hạn chế xem trước, để xem đầy đủ mời bạn chọn Tải xuống

Thông tin tài liệu

Black Art of Java Game Programming:Building a Video Game } else { gun.setPosition(x-gun_width,gun_y); } } // fire missile from given x coordinate public void fireMissile(int x) { if (!missile.isActive()) { // if missile sprite // isn't active if (x <= min_x) { missile.init(mis_min_x); } else if (x >= max_x) { missile.init(mis_max_x); } else { missile.init(x-2); // initialize missile } } } // update all the parameters associated with the // gun. In this case, only the missile needs to move // automatically. Also the gun manager checks if the // missile hits anything public void update() { missile.update(); } // paint all sprites associated with gun public void paint(Graphics g) { gun.paint(g); missile.paint(g); } // accessor function for gun public GunSprite getGun() { return gun; } public int getGunY() { return gun_y; } } file:///D|/Downloads/Books/Computer/Java/Blac 20Java%20Game%20Programming/ch05/171-177.html (5 von 6) [13.03.2002 13:18:10] Black Art of Java Game Programming:Building a Video Game Previous Table of Contents Next file:///D|/Downloads/Books/Computer/Java/Blac 20Java%20Game%20Programming/ch05/171-177.html (6 von 6) [13.03.2002 13:18:10] Black Art of Java Game Programming:Building a Video Game Black Art of Java Game Programming by Joel Fan Sams, Macmillan Computer Publishing ISBN: 1571690433 Pub Date: 11/01/96 Previous Table of Contents Next Notice that the missile is fired by calling its init() method. This is much faster than creating a new MissileSprite object for each mouse click, and it illustrates another general rule when writing games: Avoid dynamic allocation of objects during game play. Try to allocate all the objects you will use at the very beginning, if possible, so the runtime system doesn’t need to construct one when the game is running. Now, let’s create the aliens! Defining the UFOManager The UFOManager is responsible for initializing the individual UFO sprites, and telling them when to paint and update. Let’s create the UFO class first, before defining UFOManager. The UFO Class The UFO class will animate the sequence of bitmaps shown in Figure 5-2, so it becomes a subclass derived from the BitmapLoop sprite, which we introduced in Chapter 4, Adding Interactivity. The BitmapLoop Sprite Class Listing 5-10 shows the current definition of BitmapLoop. Listing 5-10 BitmapLoop class class BitmapLoop extends BitmapSprite implements Moveable{ protected Image images[]; // sequence of bitmaps protected int currentImage; // the current bitmap protected boolean foreground; // are there foreground images? protected boolean background; // is there background image? // constructor. Assumes that background image is already // loaded. (use MediaTracker) public BitmapLoop(int x,int y,Image b,Image f[],Applet a) { super(x,y,b,a); file:///D|/Downloads/Books/Computer/Java/Blac 20Java%20Game%20Programming/ch05/177-179.html (1 von 4) [13.03.2002 13:18:10] Black Art of Java Game Programming:Building a Video Game if (image != null) { // if there's a background image background = true; } else { background = false; } images = f; currentImage = 0; if (images == null || images.length == 0) { foreground = false; // nothing in images[] } else { foreground = true; if (!background) { // if no background width = images[0].getWidth(a); // get size of images[0] height = images[0].getHeight(a); } } } // cycle currentImage if sprite is active, and there // are foreground images public void update() { if (active && foreground) { currentImage = (currentImage + 1) % images.length; } updatePosition(); } public void paint(Graphics g) { if (visible) { if (background) { g.drawImage(image,locx,locy,applet); } if (foreground) { g.drawImage(images[currentImage],locx,locy,applet); } } } // implement moveable interface public void setPosition(int x,int y) { locx = x; locy = y; } file:///D|/Downloads/Books/Computer/Java/Blac 20Java%20Game%20Programming/ch05/177-179.html (2 von 4) [13.03.2002 13:18:10] Black Art of Java Game Programming:Building a Video Game protected int vx; protected int vy; public void setVelocity(int x,int y) { vx = x; vy = y; } // update position according to velocity public void updatePosition() { locx += vx; locy += vy; } } The UFO reuses most of the code from the BitmapLoop, but it overrides update() to provide alienlike behaviors. The new update() implements a state machine that permits the alien to switch behavior at random moments. You saw a simple example of state machines in the DancingRect classes of Chapter 2, Using Objects for Animation; the UFO machine is just a bit more complex. By using state machines, you create a simple kind of machine intelligence in your enemies. The Four UFO Behavioral States The UFO has four behaviors, each represented by one of the following states : • Standby. When the UFO is in Standby mode, it moves back and forth horizontally. • Attack. An attacking UFO moves quickly downward, toward the missile launcher, and it is invulnerable to your missiles. • Retreat. The UFO can break off the attack at any moment, and retreat, which means that it moves up, toward the top of the screen. • Land. Finally, the alien can try to land, and to do this it descends vertically at a slow rate. Figure 5-13 illustrates these various UFO behaviors. Figure 5-13 UFO behaviors Now let’s describe how the UFO can make transitions from state to state. file:///D|/Downloads/Books/Computer/Java/Blac 20Java%20Game%20Programming/ch05/177-179.html (3 von 4) [13.03.2002 13:18:10] Black Art of Java Game Programming:Building a Video Game Previous Table of Contents Next file:///D|/Downloads/Books/Computer/Java/Blac 20Java%20Game%20Programming/ch05/177-179.html (4 von 4) [13.03.2002 13:18:10] Black Art of Java Game Programming:Building a Video Game Black Art of Java Game Programming by Joel Fan Sams, Macmillan Computer Publishing ISBN: 1571690433 Pub Date: 11/01/96 Previous Table of Contents Next Transitioning Between States The best way to illustrate how the UFO goes from one state, or behavior, to another is with a transition diagram, in which the circles represent the four possible states, and the arrows indicate allowable transitions, as shown in Figure 5-14. Figure 5-14 UFO transition diagram Now, the UFO moves from state to state depending on random numbers generated by the static random() method in java.lang.Math: double x = Math.random(); // x is assigned a random // double from 0.0 to 1.0 If the random number is higher than these following constants, the UFO exits the corresponding state: // probability of state transitions static final double STANDBY_EXIT = .95; static final double ATTACK_EXIT = .95; static final double RETREAT_EXIT = .95; static final double LAND_EXIT = .95; Thus, the pattern of UFO behavior is unpredictable, and you can customize it by changing the probabilities. The UFO’s update() method first checks to see if a collision has occurred with the missile gun. GunSprite implements Intersect, so it can be a target of the UFO sprite: // this implements the state machine public void update() { file:///D|/Downloads/Books/Computer/Java/Blac 20Java%20Game%20Programming/ch05/179-185.html (1 von 6) [13.03.2002 13:18:11] Black Art of Java Game Programming:Building a Video Game // if alien hits target // gun_y contains the y-coordinate of the top of // the gun. The first test is done to quickly // eliminate those cases with no chance of // intersection with the gun. if ((locy + height >= gun_y) && target.intersect(locx,locy,locx+width,locy+height)) { target.hit(); suspend(); return; } If no collision occurs with the gun, the UFO executes behaviors according to its state. Let’s examine the Standby state: double r1 = Math.random(); // pick random nums double r2 = Math.random(); switch (state) { case STANDBY: if (r1 > STANDBY_EXIT) { if (r2 > 0.5) { startAttack(); } else { startLand(); } } Depending on the random numbers, the UFO can go to the Attack state or the Land state. The methods startAttack() and startLand() set the UFO’s velocity for those states. If a state transition doesn’t occur, the UFO continues with the Standby update, which reverses the UFO’s direction if it strays too close to the edges of the screen, or if the random number is above a threshold: else if ((locx < width) || (locx > max_x - width) || (r2 > FLIP_X)) { vx = -vx; } break; Implementing the UFO Sprite Class Now take a look, in Listing 5-11, at the complete UFO sprite class, and the update() method in file:///D|/Downloads/Books/Computer/Java/Blac 20Java%20Game%20Programming/ch05/179-185.html (2 von 6) [13.03.2002 13:18:11] Black Art of Java Game Programming:Building a Video Game particular. Listing 5-11 UFO class public class UFO extends BitmapLoop implements Intersect { byte state; // UFO states static final byte STANDBY = 0; static final byte ATTACK = 1; static final byte RETREAT = 2; static final byte LAND = 3; // probability of state transitions static final double STANDBY_EXIT = .95; static final double ATTACK_EXIT = .95; static final double RETREAT_EXIT = .95; static final double LAND_EXIT = .95; static final double FLIP_X = 0.9; static final int RETREAT_Y = 17; int max_x, max_y; // max coords of this UFO static Intersect target; // refers to the gun static int gun_y; // the y-coord of gun public UFO(Image ufoImages[],int max_x,int max_y, Applet a) { super(0,0,null,ufoImages,a); this.max_x = max_x; this.max_y = max_y; currentImage = getRand(5); // start at random image startStandby(); } // finish initializing info about the player's gun static public void initialize(GunManager gm) { target = gm.getGun(); // refers to gun sprite gun_y = gm.getGunY(); // get gun y-coordinate } // implement Intersect interface: public boolean intersect(int x1,int y1,int x2,int y2) { return visible && (x2 >= locx) && (locx+width >= x1) && (y2 >= locy) && (locy+height >= y1); file:///D|/Downloads/Books/Computer/Java/Blac 20Java%20Game%20Programming/ch05/179-185.html (3 von 6) [13.03.2002 13:18:11] Black Art of Java Game Programming:Building a Video Game } public void hit() { // alien is invulnerable when it's attacking // otherwise, suspend the sprite if (state != ATTACK) { suspend(); } } // this implements the state machine public void update() { // if alien hits target if ((locy + height >= gun_y) && target.intersect(locx,locy,locx+width,locy+height)) { target.hit(); suspend(); return; } // otherwise, update alien state double r1 = Math.random(); // pick random nums double r2 = Math.random(); switch (state) { case STANDBY: if (r1 > STANDBY_EXIT) { if (r2 > 0.5) { startAttack(); } else { startLand(); } } // else change the direction by flipping // the x-velocity. Net result: ufo moves // from side to side. And if the ufo gets close to // the left or right side of screen, it always changes // direction. else if ((locx < width) || (locx > max_x - width) || (r2 > FLIP_X)) { vx = -vx; } file:///D|/Downloads/Books/Computer/Java/Blac 20Java%20Game%20Programming/ch05/179-185.html (4 von 6) [13.03.2002 13:18:11] [...]... games file:///D|/Downloads/Books/Computer /Java/ Blac 2 0Java% 2 0Game% 2 0Programming/ ch05/185-188.html (3 von 4) [ 13. 03. 2002 13: 18:12] Black Art of Java Game Programming: Building a Video Game Previous Table of Contents Next file:///D|/Downloads/Books/Computer /Java/ Blac 2 0Java% 2 0Game% 2 0Programming/ ch05/185-188.html (4 von 4) [ 13. 03. 2002 13: 18:12] Black Art of Java Game Programming: Building a Video Game Black. .. manager // turn off sprites file:///D|/Downloads/Books/Computer /Java/ Blac 2 0Java% 2 0Game% 2 0Programming/ ch06/2 03- 207.html (4 von 5) [ 13. 03. 2002 13: 18:14] Black Art of Java Game Programming: Extending Your Video Game Previous Table of Contents Next file:///D|/Downloads/Books/Computer /Java/ Blac 2 0Java% 2 0Game% 2 0Programming/ ch06/2 03- 207.html (5 von 5) [ 13. 03. 2002 13: 18:14] Black Art of Java Game Programming: Extending... file:///D|/Downloads/Books/Computer /Java/ Blac 2 0Java% 2 0Game% 2 0Programming/ ch06/1 93- 199.html (5 von 5) [ 13. 03. 2002 13: 18: 13] Black Art of Java Game Programming: Extending Your Video Game Black Art of Java Game Programming by Joel Fan Sams, Macmillan Computer Publishing ISBN: 1571690 433 Pub Date: 11/01/96 Previous Table of Contents Next Modifying GameManager and UFOManager The GameManager needs to register... file:///D|/Downloads/Books/Computer /Java/ Blac 2 0Java% 2 0Game% 2 0Programming/ ch06/199-2 03. html (4 von 4) [ 13. 03. 2002 13: 18:14] Black Art of Java Game Programming: Extending Your Video Game Black Art of Java Game Programming by Joel Fan Sams, Macmillan Computer Publishing ISBN: 1571690 433 Pub Date: 11/01/96 Previous Table of Contents Next Finally, the UFOManager methods update() and paint() will take the number of sprites for... file:///D|/Downloads/Books/Computer /Java/ Blac 2 0Java% 2 0Game% 2 0Programming/ ch05/188-192.html (4 von 4) [ 13. 03. 2002 13: 18:12] Black Art of Java Game Programming: Extending Your Video Game Black Art of Java Game Programming by Joel Fan Sams, Macmillan Computer Publishing ISBN: 1571690 433 Pub Date: 11/01/96 Previous Table of Contents Next Chapter 6 Extending Your Video Game Joel Fan Goals: Add features to... Previous Table of Contents Next file:///D|/Downloads/Books/Computer /Java/ Blac 2 0Java% 2 0Game% 2 0Programming/ ch05/179-185.html (6 von 6) [ 13. 03. 2002 13: 18:11] Black Art of Java Game Programming: Building a Video Game Black Art of Java Game Programming by Joel Fan Sams, Macmillan Computer Publishing ISBN: 1571690 433 Pub Date: 11/01/96 Previous Table of Contents Next The UFOManager Class The UFOManager’s constructor... number of aliens, defined by the constant KILL_FOR_NEXT_LEVEL The variable ufosKilled tracks the number of UFOs that the file:///D|/Downloads/Books/Computer /Java/ Blac 2 0Java% 2 0Game% 2 0Programming/ ch06/199-2 03. html (2 von 4) [ 13. 03. 2002 13: 18:14] Black Art of Java Game Programming: Extending Your Video Game player has destroyed during the game These variables are initialized in the UFOManager method newGame(),... file:///D|/Downloads/Books/Computer /Java/ Blac 2 0Java% 2 0Game% 2 0Programming/ ch06/199-2 03. html (3 von 4) [ 13. 03. 2002 13: 18:14] Black Art of Java Game Programming: Extending Your Video Game // but it gets "pushed back" if (state == ATTACK) { locy -= 17; } // otherwise explode! else if (state != EXPLODE) { startExplode(); um.killed(); // start explode state // tell UFOManager // another UFO's dead } } Previous Table of Contents... file:///D|/Downloads/Books/Computer /Java/ Blac 2 0Java% 2 0Game% 2 0Programming/ ch06/1 93- 199.html (4 von 5) [ 13. 03. 2002 13: 18: 13] Black Art of Java Game Programming: Extending Your Video Game suspend(); } You can find these changes to the UFO class in Listing 6-5 Now let’s modify the UFOManager and the GameManager so that they load and pass these new animation sequences to the UFO class Previous Table of Contents Next file:///D|/Downloads/Books/Computer /Java/ Blac... animation.stop(); animation = null; } } } file:///D|/Downloads/Books/Computer /Java/ Blac 2 0Java% 2 0Game% 2 0Programming/ ch05/188-192.html (3 von 4) [ 13. 03. 2002 13: 18:12] Black Art of Java Game Programming: Building a Video Game Recommended Applet Tag to Run the Alien Landing Game Suggestion Box • Right now, the missile launcher fires only one missile . Next file:///D|/Downloads/Books/Computer /Java/ Blac 2 0Java% 2 0Game% 2 0Programming/ ch05/171-177.html (6 von 6) [ 13. 03. 2002 13: 18:10] Black Art of Java Game Programming: Building a Video Game Black Art of Java Game Programming. Next file:///D|/Downloads/Books/Computer /Java/ Blac 2 0Java% 2 0Game% 2 0Programming/ ch05/177-179.html (4 von 4) [ 13. 03. 2002 13: 18:10] Black Art of Java Game Programming: Building a Video Game Black Art of Java Game Programming. Next file:///D|/Downloads/Books/Computer /Java/ Blac 2 0Java% 2 0Game% 2 0Programming/ ch05/179-185.html (6 von 6) [ 13. 03. 2002 13: 18:11] Black Art of Java Game Programming: Building a Video Game Black Art of Java Game Programming

Ngày đăng: 12/08/2014, 09:21

Từ khóa liên quan

Mục lục

  • Black Art Of Java Game Programming

    • Black Art of Java Game Programming:Building a Video Game

    • Black Art of Java Game Programming:Building a Video Game

    • Black Art of Java Game Programming:Building a Video Game

    • Black Art of Java Game Programming:Building a Video Game

    • Black Art of Java Game Programming:Extending Your Video Game

    • Black Art of Java Game Programming:Extending Your Video Game

    • Black Art of Java Game Programming:Extending Your Video Game

    • Black Art of Java Game Programming:Extending Your Video Game

    • Black Art of Java Game Programming:Extending Your Video Game

    • Black Art of Java Game Programming:Extending Your Video Game

    • Black Art of Java Game Programming:Extending Your Video Game

    • Black Art of Java Game Programming:Extending Your Video Game

    • Black Art of Java Game Programming:Creating Customizable Games with the AWT

    • Black Art of Java Game Programming:Creating Customizable Games with the AWT

    • Black Art of Java Game Programming:Creating Customizable Games with the AWT

    • Black Art of Java Game Programming:Creating Customizable Games with the AWT

    • Black Art of Java Game Programming:Creating Customizable Games with the AWT

    • Black Art of Java Game Programming:Creating Customizable Games with the AWT

    • Black Art of Java Game Programming:Creating Customizable Games with the AWT

    • Black Art of Java Game Programming:Creating Customizable Games with the AWT

Tài liệu cùng người dùng

  • Đang cập nhật ...

Tài liệu liên quan