<!DOCTYPE tutorial SYSTEM "../progzoo.dtd">
<tutorial title="Animation" level="*">
 <blurb>
 </blurb>
 <question className="Sprite" title="Lunar Lander I"
  applet="A1" width='400' height='400'>
  <blurb>
  <p>Try the applet generated by this program. Press the up arrow to slow the
  descent.</p>
  <p>Now try the following changes:
  </p>
  <ul>
    <li>Show how much fuel is left by adding the following line to the
        <code>paintFrame</code> method.
    <pre>g.drawString("Fuel: "+fuel,50,10);</pre></li>
    <li>In the <code>tick</code> method decrement fuel inside the
      <code>if</code> statement. Use <code>fuel--</code></li>
  </ul>
  <hint tease='How it works'>
   <p>
   The applet <a href='http://progzoo.net/22/~andrew/A1.java'
   target='doc'>A1.java</a> creates a single
   Sprite object. The Sprite method <code>tick</code> gets called 10 times
   per second and this is where the game state is updated. The parameter
   <code>kid</code> holds the "key is down" list: it is an array of boolean
   values - a true value for <code>kid[KeyEvent.VK_UP]</code> indicates that
   the up key is currently being held down.</p>
   The list of key codes is at
   <a target='doc' href='http://java.sun.com/j2se/1.5.0/docs/api/java/awt/event/KeyEvent.html#field_summary'>
   KeyEvent</a>.
  </hint>
  </blurb>
<prog lang="java"><![CDATA[
import java.awt.Graphics2D;
import java.awt.event.KeyEvent;
public class Sprite{
 int yv=0;
 int y=0;
 int fuel = 50;

 public void tick(boolean[] kid){
   yv++;
   if (kid[KeyEvent.VK_UP] && fuel>0){
      yv-=2;
   }
   y+=yv;
 }

 public void paintFrame(Graphics2D g,boolean[]kid) {
   g.drawRect(10,y,10,10);
 }
}





]]></prog>

 </question>
 <question className="Sprite" title="Lunar Lander II"
  applet="A1" width='400' height='400'>
  <blurb>
  </blurb>
<prog lang="java"><![CDATA[
import java.awt.Graphics2D;
import java.awt.event.KeyEvent;
public class Sprite{
 int yv=0;
 int y=0;
 int fuel = 50;
 boolean halt=false;

 public void tick(boolean[] kid){
   if (halt) return;
   yv++;
   if (kid[KeyEvent.VK_UP] && fuel>0){
      yv-=2;
      fuel--;
   }
   y+=yv;
   if (y>380){
     halt = true;
   }
 }

 public void paintFrame(Graphics2D g,boolean[]kid) {
   g.drawString("Fuel: "+fuel,50,20);
   g.drawString("Speed: "+yv,50,30);
   if (!halt){
     g.drawRect(10,y,10,10);
   }else{  
     if (yv>2)
       g.drawString("You crashed :(",50,40);
     else
       g.drawString("Good landing!",50,40);
   }
 }
}





]]></prog>

 </question>

 <question className="Sprite" title="Lunar Lander II"
  applet="A1" width='400' height='400'>
  <blurb>
  </blurb>
<prog lang="java"><![CDATA[
import java.awt.*;
import java.awt.event.KeyEvent;
public class Sprite{
Polygon ship = new Polygon(
   new int[]{-20,-20,50},
   new int[]{-20,20,0},
   3);
 double accn= 0.0;
 int y=50;
 int x=50;
 double xv = 0.0;
 double yv = 0.0;
 double angle=0.0;
 public void tick(boolean[] kid){
   accn=0.0;
   if (kid[KeyEvent.VK_UP]) accn=1.0;
   if (kid[KeyEvent.VK_LEFT]) angle-=0.2;
   if (kid[KeyEvent.VK_RIGHT]) angle+=0.2;
   if (kid[KeyEvent.VK_DOWN]) accn=-1.0;
   xv+=accn*Math.cos(angle);
   yv+=accn*Math.sin(angle);
   yv+=0.5;
   x+=xv;
   y+=yv;
   if (x>400) x-=400;
   if (y>400) y-=400;
   if (x<0) x+=400;
   if (y<0) y+=400;
 }

 public void paintFrame(Graphics2D g,boolean[]kid) {
   g.setRenderingHint(RenderingHints.KEY_ANTIALIASING,
                      RenderingHints.VALUE_ANTIALIAS_ON);
   g.translate(x,y);
   g.rotate(angle);
   g.drawPolygon(ship);
   g.rotate(-angle);
   g.translate(-x,-y);
 }
}
]]></prog>

 </question>

</tutorial>

