GAMES TWO: LESSON Seventeen: Truck Dodge: Code Reuse

Truck dodge reuses the cell class used in the next to last CANTRIS lesson and it uses a modified GRID class with an applet class which is pretty much the same as that used in the last two CANTRIS lessons.

The basic applet class used for this version of Truck Dodge looks very similar to the applet class used in Cantris.

import java.awt.*; import java.applet.*; import java.awt.event.*; import java.util.*; public class TRUCK extends Applet implements Runnable{ Dimension d; Thread tm; Random r = new Random(); Image offI; int speed = 33; String title[] = { "T", "R", "U", "C", "K", " ", "D", "O", "D", "G", "E"}; GRID4 grid = new GRID4(); public void init(){ d = getSize(); offI=createImage(d.width,d.height); reset(); tm = new Thread(this); tm.start(); requestFocus(); this.addKeyListener(new KeyAdapter(){ public void keyPressed(KeyEvent k) { if(grid.keepPlaying()){ if(k.getKeyCode() == KeyEvent.VK_RIGHT){ grid.move_car(1); } else if(k.getKeyCode() == KeyEvent.VK_LEFT){ grid.move_car(-1); } } else{ reset(); } repaint(); } }); } public void reset(){ grid=new GRID4(); } public void run(){ while(true){ while(grid.keepPlaying()){ grid.move_trucks(); repaint(); try{ Thread.sleep(speed); }catch(InterruptedException e){}; } } } public void update(Graphics g){ paint(g); } public void paint(Graphics g){ Graphics offG = offI.getGraphics(); offG.setColor(Color.white); offG.fillRect(0,0,d.width,d.height); grid.draw(offG); offG.setColor(Color.black); offG.setFont(new Font("sansserif", Font.BOLD, 14)); for(int i = 0; i<title.length; i++){ offG.drawString(title[i], 5, 20+20*i); } offG.setFont(new Font("sansserif", Font.PLAIN, 12)); if( grid.score==0) offG.drawString("Keep playing.",15,d.height-5); else if(grid.keepPlaying()) offG.drawString("SCORE: " + grid.score, 15,d.height-5); else{ offG.drawString("Press 'r'.", 15, d.height-2); offG.drawString("SCORE: " + grid.score,15,d.height-17); } g.drawImage(offI, 0, 0, this); } }

The following code is for the modified GRID class. Obviously, Truck Dodge has different features than Cantris and most of these differences are reflected in this class. Actually Truck Dodge is a little less complicated than Cantris. The biggest simplification is that you don't have to deal with a large two-dimensional array in Truck Dodge.

import java.awt.*; import java.util.*; public class GRID4{ CELL3[][] trucks = new CELL3[4][3]; CELL3 car = new CELL3(50,200,Color.blue); int col=2; Color sel[] = { Color.red, Color.blue, Color.cyan, Color.green, Color.magenta, Color.yellow }; Random r = new Random(); int score, next; boolean gameOn = true; GRID4(){ spawn(); } public void move_car(int m){ if(col>0 && m==-1){ col--; car.moveX(m); } else if(col<5 && m==1){ col++; car.moveX(m); } } public void move_trucks(){ for(int i=0; i<4; i++){ for(int z=0; z<3; z++){ if(trucks[i][z] != null) trucks[i][z].moveY(); } } if(trucks[next%4][0].y>80){ next++; score++; spawn(); } crash(); } public boolean keepPlaying(){ return gameOn; } public void endGame(){ gameOn=false; } public void crash(){ for(int i=0; i<4; i++){ for(int z=0; z<3; z++){ if(trucks[i][z] != null){ if(trucks[i][z].x == car.x && trucks[i][z].y == car.y){ gameOn=false; } } } } } public void spawn(){ //select color Color c = sel[Math.abs(r.nextInt()%sel.length)]; int x = 30 + 10 * Math.abs(r.nextInt()%6); trucks[next%4][0] = new CELL3(x,30,Color.yellow); trucks[next%4][1] = new CELL3(x,20,Color.red); trucks[next%4][2] = new CELL3(x,10,Color.red); } public void draw(Graphics g){ car.draw(g); for(int i = 0; i<4; i++){ for(int z=0; z<3; z++){ if(trucks[i][z]!=null) trucks[i][z].draw(g); } } g.setColor(Color.black); g.drawRect(30,10,60,210); } }
Finally the CELL class is hardly changed at all:
import java.awt.*; class CELL3{ int x, y; Color c; CELL3(int xx, int yy, Color cc){ x = xx; y = yy; c = cc; } public void moveX(int n){ if(x>30 && n == -1){ x-=10; } else if(x<80 && n == 1){ x+=10; } } public void moveY(){ y+=2; } public void draw(Graphics g){ g.setColor(c); g.fillRect(x,y,10,10); } }


ASSIGNMENT:

Randomize color assignment to trucks.

Make the freeway black with dashed lines between the lanes.

Add one more truck to the game.