Advertisement
brilliant_moves

Canyon.java

Oct 1st, 2012
766
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 5 4.26 KB | None | 0 0
  1. import java.util.*;     // for Random function
  2. import java.awt.*;
  3. import java.awt.event.*;
  4. import javax.swing.*;   // for JFrame
  5.  
  6. public class Canyon {
  7.  
  8.     /**
  9.     *   Program:    Canyon.java by Chris Clarke
  10.     *   Created:    Nov 2006
  11.     *   Purpose:    Popular arcade game
  12.     *   Notes:      Left and right cursor keys steer pod, p = pause
  13.     *   Modified:   01.09.2012 (JFrame)
  14.     */
  15.  
  16.     public static void main(String[] args) {
  17.         CanyonJFrame frame = new CanyonJFrame();
  18.         frame.setDefaultCloseOperation( JFrame.EXIT_ON_CLOSE);
  19.         frame.setVisible(true);
  20.     }
  21. }
  22.  
  23. class CanyonJFrame extends JFrame implements KeyListener {
  24.     public CanyonJFrame() {
  25.         setTitle("Canyon by Chris Clarke");
  26.         initWall();
  27.         addKeyListener(this);
  28.         // Set to a reasonable size.
  29.         setSize(MAX_X, MAX_Y);
  30.     }
  31.  
  32.     public void initWall() {
  33.         wall = new boolean[MAX_WIDTH][MAX_HEIGHT];
  34.         start = true;
  35.     }
  36.  
  37.     public void generateRow() {
  38.         if (start) {
  39.             wall[leftWall][MAX_HEIGHT-1]=true;
  40.             wall[rightWall][MAX_HEIGHT-1]=true;
  41.             start = false;
  42.         } else {
  43.             Random r = new Random();
  44.             int addition = r.nextInt(3)-1;  // -1, 0 or 1
  45.  
  46.             if ((leftWall>0)&&(addition==-1)) {
  47.                 leftWall--;
  48.                 rightWall--;
  49.             }
  50.             if ((rightWall<MAX_WIDTH-1)&&(addition==1)) {
  51.                 leftWall++;
  52.                 rightWall++;
  53.             }
  54.             if ((r.nextInt(100)>92)&&(rightWall-leftWall>5))
  55.                 // decrease the gap
  56.                 rightWall--;
  57.                        
  58.             // shuffle up previous rows
  59.             for (int x=0; x<MAX_WIDTH; x++)
  60.                 for (int y=0; y<MAX_HEIGHT-1; y++)
  61.                     wall[x][y] = wall[x][y+1];
  62.             // create current row
  63.             for (int x=0; x<MAX_WIDTH; x++)
  64.                 if ((x==leftWall)||(x==rightWall))
  65.                     wall[x][MAX_HEIGHT-1] = true;
  66.                 else
  67.                     wall[x][MAX_HEIGHT-1] = false;
  68.         }
  69.     }
  70.  
  71.     public boolean outsideGap(int objX) {
  72.         // is pod outside the gap?
  73.         boolean[] outsideGap = new boolean[MAX_WIDTH];
  74.         boolean flag=true;
  75.         for (int x=0; x<MAX_WIDTH; x++)
  76.             outsideGap[x] = true;
  77.         for (int x=0; x<MAX_WIDTH; x++) {
  78.             if (wall[x][10])
  79.                 flag=!flag;
  80.             outsideGap[x] = flag;
  81.         }
  82.         if (outsideGap[objX])
  83.             return true;
  84.         else
  85.             return false;
  86.     }
  87.  
  88.     public void paint(Graphics g) {
  89.         generateRow();
  90.         // delete all
  91.         g.setColor(Color.WHITE);
  92.         g.fillRect(0,0,1024,768);
  93.         g.setColor(Color.BLACK);
  94.         for (int x=0; x<MAX_WIDTH; x++)
  95.             for (int y=0; y<MAX_HEIGHT-1; y++)
  96.                 if (wall[x][y])
  97.                     // draw wall row by row
  98.                     g.fillRect(100+SQR_SIZE*x,
  99.                      100+SQR_SIZE*y, SQR_SIZE, SQR_SIZE);
  100.         // draw falling object
  101.         g.setColor(Color.ORANGE);
  102.         g.fillRect(100+SQR_SIZE*objectX, 100+SQR_SIZE*10, SQR_SIZE, SQR_SIZE);
  103.         // hit the side
  104.         if (wall[objectX][10])
  105.             // game over
  106.             ends(g);
  107.         if ((score>=(MAX_HEIGHT-1)-10)&&(outsideGap(objectX)))
  108.             // game over
  109.             ends(g);
  110.         // short pause
  111.         try {
  112.             Thread.sleep(200);
  113.         }
  114.         catch (InterruptedException e) {}
  115.  
  116.         score++;
  117.         if (paused) {
  118.             g.setFont(new Font("Arial", Font.BOLD, 36));
  119.             g.drawString("Game Paused - press c to continue...", 100, 100);
  120.         } else
  121.             repaint();
  122.     }
  123.  
  124.     public void ends(Graphics g) {
  125.         g.setFont(new Font("Arial", Font.BOLD, 72));
  126.         g.drawString("Game Over", 350, 300);
  127.         g.drawString("Score = "+score, 350, 500);
  128.         // long pause
  129.         try {
  130.             Thread.sleep(4000);
  131.         }
  132.         catch (InterruptedException e) {}
  133.         // close program
  134.         System.exit(0);
  135.     }
  136.  
  137.     public void keyPressed(KeyEvent e) {
  138.         // Check if any keys have been pressed and take appropriate action.
  139.         int keyCode = e.getKeyCode();
  140.        
  141.         if (keyCode == KeyEvent.VK_LEFT)
  142.             // move object left
  143.             if (objectX>0)
  144.                 objectX--;
  145.         if (keyCode == KeyEvent.VK_RIGHT)
  146.             // move object right
  147.             if (objectX<MAX_WIDTH-1)
  148.                 objectX++;
  149.         if (keyCode == KeyEvent.VK_P)
  150.             // The letter 'p' pressed
  151.             paused = true;
  152.         if (keyCode == KeyEvent.VK_C) {
  153.             // The letter 'c' pressed to continue after a pause
  154.             paused = false;
  155.             // continue
  156.             repaint();
  157.         }
  158.     }
  159.  
  160.     public void keyReleased(KeyEvent e) {}
  161.  
  162.     public void keyTyped(KeyEvent e) {}
  163.  
  164.     boolean[][] wall;
  165.     boolean     start;
  166.     boolean     paused;
  167.  
  168.     public static final int MAX_X = 1024;
  169.     public static final int MAX_Y = 768;
  170.     public static final int MAX_WIDTH = 50;
  171.     public static final int MAX_HEIGHT = 45;
  172.     public static final int SQR_SIZE = 18;
  173.  
  174.     int leftWall = 10, rightWall = MAX_WIDTH-10;
  175.     int objectX=MAX_WIDTH/2;    // position of object
  176.     int score = 0;
  177. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement