Tag: Object New oid length AC bullet = = Learn two static
Day101. Clash of Heroes and enemies 1) borrow the hit () collision behavior in Flyingobject to borrow the Godead () method in Flyingobject to design Hero () decrement, Subtractlife Blefire () empty fire value 4/** member Method: Decrement */public void Subtractlife () {life--; Fate minus 1}/** member method: Empty Fire value */public void Cleardoublefire () {doublefire = 0; Fire value 0} 2) because the hero machine collides with the enemy for timing, the Herobangaction () method is called in Run (): In Herobangaction (): Traverse so the enemy, get each enemy, determine whether with the hero machine hit If hit: Enemy to Die, Hero machine to reduce life, Hero machine empty Fire value/** member method: Hero Machine collision with enemy/public void herobangaction () {//Every 10 milliseconds walk once for (int i = 0; i < enemies.length; i++) {flyingobject F =enemies[i]; Get every Enemy if (Hero.islife () && f.islife () && f.hit (Hero)) {//if hit F.godead (); Enemy to Die Hero.subtractlife (); Hero machine to reduce the life hero.cleardoublefire (); Hero machine Clear Fire}}}2. Detect game end 1) borrow Hero's Getlife () to get the Kill 2) because the detection game ends for a timed occurrence,So call the Checkgameoveraction () method in Run (): In Checkgameoveraction (): Determine the hero machine's fate <= 0, if True/** member method: Detect Game End */ public void Checkgameoveraction () {//Every 10 milliseconds walk once if (Hero.getlife () <= 0) {//Game over state = Game_o VER; }}3. State 1) designed four state constants, the current status state variable designed three static images, initialized in the static Block 2) in Paint () design in different states to draw a different picture 3) a bunch of action () is designed to run In the execution will be the hero machine with the mouse movement design for only in the running state in the execution 4) rewrite mouseclicked () mouse click event Rewrite mouseclicked () mouse click event Rewrite mouseclicked () mouse click Thing /** constants: Game start, run, pause, end) */public static final int start = 0; public static final int RUNNING = 1; public static final int PAUSE = 2; public static final int game_over = 3; /** member Variable: Current state (default is start state) */private int status = start; /** member variables: Three images (start, pause, end) */private static bufferedimage start; private static bufferedimage pause; private static BufferedImage Gameover; static {//Initialize still picture start = Flyingobject.loadimage ("Start.png"); Pause = Flyingobject.loadimage ("Pause.png"); Gameover = Flyingobject.loadimage ("Gameover.png"); } switch (state) {//Draw a different picture in different states case start://Start G.drawimage (start, 0, 0, n ULL); Break Case pause://Pause g.drawimage (pause, 0, 0, NULL); Break Case Game_over://End G.drawimage (gameover, 0, 0, NULL); Break }/** rewrite mousemoved () mouse movement */public void mousemoved (MouseEvent e) {if (state = = RUNNING) {int x = E.getx (); Gets the x coordinate of the mouse int y = e.gety (); Gets the y-coordinate of the mouse Hero.moveto (x, y); Hero machine with mouse move}}/** rewrite mouseclicked () mouse click */public void mouseclicked (MouseEvent e) {switch (state) {///based on current status does not Same operation case start://start state = RUNNING; into a running state break; Case Game_over://End Status score = 0; Sky = new SkY (); Hero = new Hero (); enemies = new Flyingobject[0]; Bullets = new Bullet[0]; state = START; becomes a start state break; }}/** rewrite mouseexited () mouse out */public void mouseexited (MouseEvent e) {if (state = = RUNNING) {//Run status = PAU SE; Modified to paused state}}/** override mouseentered () mouse move */public void mouseentered (MouseEvent e) {if (state = = pause) {//Pause status state = RUNNING; Modified to run Status}}
Java Learning II (Aircraft War project) Day10