Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- import java.util.*; // for Random function
- import java.awt.*;
- import java.awt.event.*;
- import javax.swing.*; // for JFrame
- public class Canyon {
- /**
- * Program: Canyon.java by Chris Clarke
- * Created: Nov 2006
- * Purpose: Popular arcade game
- * Notes: Left and right cursor keys steer pod, p = pause
- * Modified: 01.09.2012 (JFrame)
- */
- public static void main(String[] args) {
- CanyonJFrame frame = new CanyonJFrame();
- frame.setDefaultCloseOperation( JFrame.EXIT_ON_CLOSE);
- frame.setVisible(true);
- }
- }
- class CanyonJFrame extends JFrame implements KeyListener {
- public CanyonJFrame() {
- setTitle("Canyon by Chris Clarke");
- initWall();
- addKeyListener(this);
- // Set to a reasonable size.
- setSize(MAX_X, MAX_Y);
- }
- public void initWall() {
- wall = new boolean[MAX_WIDTH][MAX_HEIGHT];
- start = true;
- }
- public void generateRow() {
- if (start) {
- wall[leftWall][MAX_HEIGHT-1]=true;
- wall[rightWall][MAX_HEIGHT-1]=true;
- start = false;
- } else {
- Random r = new Random();
- int addition = r.nextInt(3)-1; // -1, 0 or 1
- if ((leftWall>0)&&(addition==-1)) {
- leftWall--;
- rightWall--;
- }
- if ((rightWall<MAX_WIDTH-1)&&(addition==1)) {
- leftWall++;
- rightWall++;
- }
- if ((r.nextInt(100)>92)&&(rightWall-leftWall>5))
- // decrease the gap
- rightWall--;
- // shuffle up previous rows
- for (int x=0; x<MAX_WIDTH; x++)
- for (int y=0; y<MAX_HEIGHT-1; y++)
- wall[x][y] = wall[x][y+1];
- // create current row
- for (int x=0; x<MAX_WIDTH; x++)
- if ((x==leftWall)||(x==rightWall))
- wall[x][MAX_HEIGHT-1] = true;
- else
- wall[x][MAX_HEIGHT-1] = false;
- }
- }
- public boolean outsideGap(int objX) {
- // is pod outside the gap?
- boolean[] outsideGap = new boolean[MAX_WIDTH];
- boolean flag=true;
- for (int x=0; x<MAX_WIDTH; x++)
- outsideGap[x] = true;
- for (int x=0; x<MAX_WIDTH; x++) {
- if (wall[x][10])
- flag=!flag;
- outsideGap[x] = flag;
- }
- if (outsideGap[objX])
- return true;
- else
- return false;
- }
- public void paint(Graphics g) {
- generateRow();
- // delete all
- g.setColor(Color.WHITE);
- g.fillRect(0,0,1024,768);
- g.setColor(Color.BLACK);
- for (int x=0; x<MAX_WIDTH; x++)
- for (int y=0; y<MAX_HEIGHT-1; y++)
- if (wall[x][y])
- // draw wall row by row
- g.fillRect(100+SQR_SIZE*x,
- 100+SQR_SIZE*y, SQR_SIZE, SQR_SIZE);
- // draw falling object
- g.setColor(Color.ORANGE);
- g.fillRect(100+SQR_SIZE*objectX, 100+SQR_SIZE*10, SQR_SIZE, SQR_SIZE);
- // hit the side
- if (wall[objectX][10])
- // game over
- ends(g);
- if ((score>=(MAX_HEIGHT-1)-10)&&(outsideGap(objectX)))
- // game over
- ends(g);
- // short pause
- try {
- Thread.sleep(200);
- }
- catch (InterruptedException e) {}
- score++;
- if (paused) {
- g.setFont(new Font("Arial", Font.BOLD, 36));
- g.drawString("Game Paused - press c to continue...", 100, 100);
- } else
- repaint();
- }
- public void ends(Graphics g) {
- g.setFont(new Font("Arial", Font.BOLD, 72));
- g.drawString("Game Over", 350, 300);
- g.drawString("Score = "+score, 350, 500);
- // long pause
- try {
- Thread.sleep(4000);
- }
- catch (InterruptedException e) {}
- // close program
- System.exit(0);
- }
- public void keyPressed(KeyEvent e) {
- // Check if any keys have been pressed and take appropriate action.
- int keyCode = e.getKeyCode();
- if (keyCode == KeyEvent.VK_LEFT)
- // move object left
- if (objectX>0)
- objectX--;
- if (keyCode == KeyEvent.VK_RIGHT)
- // move object right
- if (objectX<MAX_WIDTH-1)
- objectX++;
- if (keyCode == KeyEvent.VK_P)
- // The letter 'p' pressed
- paused = true;
- if (keyCode == KeyEvent.VK_C) {
- // The letter 'c' pressed to continue after a pause
- paused = false;
- // continue
- repaint();
- }
- }
- public void keyReleased(KeyEvent e) {}
- public void keyTyped(KeyEvent e) {}
- boolean[][] wall;
- boolean start;
- boolean paused;
- public static final int MAX_X = 1024;
- public static final int MAX_Y = 768;
- public static final int MAX_WIDTH = 50;
- public static final int MAX_HEIGHT = 45;
- public static final int SQR_SIZE = 18;
- int leftWall = 10, rightWall = MAX_WIDTH-10;
- int objectX=MAX_WIDTH/2; // position of object
- int score = 0;
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement