Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- package zavrsni;
- import java.awt.*;
- import java.awt.event.*;
- import java.io.*;
- import java.applet.*;
- import java.util.Random;
- import java.net.URL;
- import java.net.MalformedURLException;
- /**
- * a helper class
- */
- class Square {
- int x, y, c;
- Square(int x, int y, int c) {
- this.x = x;
- this.y = y;
- this.c = c;
- }
- int X() {
- return x;
- }
- int Y() {
- return y;
- }
- int Color() {
- return c;
- }
- boolean InBounds() {
- return (x >= 0 && x < tetris.cols &&
- y >= 0 && y < tetris.rows+4);
- }
- boolean IsEqual(Square s) {
- return x == s.x && y == s.y && c == s.c;
- }
- }
- /**
- * Godmar Back
- * University of Utah, Computer Systems Lab, 1995
- *
- * my favorite tetris in Java
- */
- public class tetris extends Applet implements Runnable, ActionListener{
- static int sqlength; // length of tetris square
- static final int xoffset = 200; // distance from left
- static int cols; // columns
- static int rows; // rows
- int f[][]; // field
- int of[][]; // old field
- int what; // type of current piece
- Square curpiece[] = new Square[4]; // current piece
- boolean lost; // lost
- boolean neednewpiece; // need new piece
- Thread killme = null; // thread that does gravity
- Color colors[];
- int curval, score=0;
- int level; // current level of difficulty
- int pieces; // nr of pieces dropped
- String highscorehost;
- short highscoreport;
- boolean blackwhite; // BW mode
- boolean suspended = false; // the suggested off-switch
- boolean justupdating = false;
- final Button start_newgame_butt = new TetrisButton("Start");
- final Button pause_resume_butt = new TetrisButton("Pause");
- private GridCanvas game_grid = new GridCanvas(grid, true);
- private class TetrisButton extends Button {
- public TetrisButton(String label) {
- super(label);
- }
- public Dimension getPreferredSize() {
- return new Dimension(120, super.getPreferredSize().height);
- }
- }
- // move a bunch of squares
- private boolean movepiece(Square from[], Square to[])
- {
- outerlabel:
- for (int i=0; i<to.length; i++) {
- if (!to[i].InBounds()) {
- return false;
- }
- if (f[to[i].X()][to[i].Y()] != 0) { // it's taken
- for (int j=0; j<from.length; j++)
- if(to[i].IsEqual(from[j])) {
- continue outerlabel; // but by a from
- }
- return false; // no can do
- }
- }
- // blank old piece
- for (int i=0; i<from.length; i++)
- if(from[i].InBounds()) {
- f[from[i].X()][from[i].Y()] = 0;
- }
- // mark new piece
- for (int i=0; i<to.length; i++)
- f[to[i].X()][to[i].Y()] = to[i].Color();
- return true;
- }
- // throw new piece in the game
- private void newpiece()
- {
- Square old[] = new Square[4];
- old[0] = old[1] = old[2] = old[3] = new Square(-1, -1, 0);
- int m = cols/2;
- what = (int) (Math.random()*7);
- switch(what) {
- case 0:
- // XXXX red
- curval = 100;
- curpiece[0] = new Square(m-1, rows-1, 1);
- curpiece[1] = new Square(m-2, rows-1, 1);
- curpiece[2] = new Square(m, rows-1, 1);
- curpiece[3] = new Square(m+1, rows-1, 1);
- break;
- case 1:
- // X orange
- // XXX
- curval = 120;
- curpiece[0] = new Square(m , rows-2, 5);
- curpiece[1] = new Square(m , rows-1, 5);
- curpiece[2] = new Square(m-1, rows-2, 5);
- curpiece[3] = new Square(m+1, rows-2, 5);
- break;
- case 2:
- // XX ???
- // XX
- curval = 180;
- curpiece[0] = new Square(m , rows-2, 2);
- curpiece[1] = new Square(m-1, rows-1, 2);
- curpiece[2] = new Square(m , rows-1, 2);
- curpiece[3] = new Square(m+1, rows-2, 2);
- break;
- case 3:
- // XX ???
- // XX
- curval = 180;
- curpiece[0] = new Square(m , rows-2, 7);
- curpiece[1] = new Square(m+1, rows-1, 7);
- curpiece[2] = new Square(m , rows-1, 7);
- curpiece[3] = new Square(m-1, rows-2, 7);
- break;
- case 4:
- // XX light blue
- // XX
- curval = 100;
- curpiece[0] = new Square(m-1, rows-1, 3);
- curpiece[1] = new Square(m , rows-1, 3);
- curpiece[2] = new Square(m-1, rows-2, 3);
- curpiece[3] = new Square(m , rows-2, 3);
- break;
- case 5:
- // XXX
- // X
- curval = 120;
- curpiece[0] = new Square(m , rows-1, 6);
- curpiece[1] = new Square(m-1, rows-1, 6);
- curpiece[2] = new Square(m+1, rows-1, 6);
- curpiece[3] = new Square(m+1, rows-2, 6);
- break;
- case 6:
- // XXX yellow
- // X
- curval = 120;
- curpiece[0] = new Square(m , rows-1, 4);
- curpiece[1] = new Square(m+1, rows-1, 4);
- curpiece[2] = new Square(m-1, rows-1, 4);
- curpiece[3] = new Square(m-1, rows-2, 4);
- break;
- }
- lost = !movepiece(old, curpiece);
- }
- // move current piece
- private synchronized boolean movecurpiece(int byx, int byy, boolean rotate)
- {
- Square newpos[] = new Square[4];
- for(int i=0; i<4; i++) {
- if(rotate) {
- int dx = curpiece[i].X() - curpiece[0].X();
- int dy = curpiece[i].Y() - curpiece[0].Y();
- // 90 degree rotation: [ 0 -1]
- // [ 1 0]
- newpos[i] = new Square( curpiece[0].X() - dy,
- curpiece[0].Y() + dx,
- curpiece[i].Color());
- } else
- newpos[i] = new Square( curpiece[i].X() + byx,
- curpiece[i].Y() + byy,
- curpiece[i].Color());
- }
- if (!movepiece(curpiece, newpos))
- return false;
- curpiece = newpos;
- return true;
- }
- // remove full lines
- private void removelines()
- {
- outerlabel:
- for (int j=0; j<rows; j++) {
- // check jth line
- for (int i=0; i<cols; i++)
- if (f[i][j] == 0)
- continue outerlabel;
- // remove jth line
- for (int k=j; k<rows-1; k++)
- for (int i=0; i<cols; i++)
- f[i][k] = f[i][k+1];
- // recheck jth line
- j--;
- }
- }
- private static class TetrisLabel extends Label {
- private final static Font LABEL_FONT = new Font("Serif", Font.BOLD, 18);
- private TetrisLabel(String text) {
- super(text);
- setFont(LABEL_FONT);
- }
- private void addValue(int val) {
- setText(Integer.toString((Integer.parseInt(getText())) + val ));
- }
- }
- // applet is being loaded the first time
- public void init()
- { // applet is loaded the first time
- pause_resume_butt.setEnabled(false);
- start_newgame_butt.addActionListener(new ActionListener() {
- public void actionPerformed(ActionEvent ae) {
- if(start_newgame_butt.getLabel().equals("Start"))
- startGame();
- else
- start();
- }
- });
- pause_resume_butt.addActionListener(new ActionListener() {
- public void actionPerformed(ActionEvent ae) {
- if(pause_resume_butt.getLabel().equals("Pause"))
- pauseGame();
- else
- resumeGame();
- }
- });
- String s = getParameter("squaresize");
- sqlength = s == null ? 20 : (new Integer(s)).intValue();
- s = getParameter("columns");
- cols = s == null ? 10 : (new Integer(s)).intValue();
- s = getParameter("rows");
- rows = s == null ? 20 : (new Integer(s)).intValue();
- s = getParameter("blackwhite");
- blackwhite = s == null ? false : (new Boolean(s)).booleanValue();
- highscorehost = getParameter("highscorehost");
- s = getParameter("highscoreport");
- highscoreport = (short)(s == null ? 4711 : (new Integer(s)).intValue());
- f = new int[cols][rows+4];
- of = new int[cols][rows+4];
- resize(sqlength*cols+xoffset*2, sqlength*(rows+3));
- colors = new Color[8];
- colors[0] = new Color(40,40,40); // off color
- if (blackwhite) {
- for(int i=1; i<8; i++)
- colors[i] = new Color(255, 255, 255);
- } else {
- colors[1] = new Color(255,0,0); // Default red - ok
- colors[2] = new Color(0,200,0); // green ?
- colors[3] = new Color(0,200,255); // light blue - ok
- colors[4] = new Color(255,255,0); // yellow - ok
- colors[5] = new Color(255,150,0); // orange - ok
- colors[6] = new Color(210,0,240); // purple ?
- colors[7] = new Color(40,0,240); // dark blue ?
- // add the key listener to all components that might get focus
- // so that it'll work regardless of which has focus
- start_newgame_butt.addActionListener(this);
- pause_resume_butt.addActionListener(this);
- Panel right_panel = new Panel(new GridLayout(3, 1));
- right_panel.setBackground(Color.white);
- Panel control_panel = new Panel();
- control_panel.add(start_newgame_butt);
- control_panel.add(pause_resume_butt);
- control_panel.setBackground(Color.white);
- right_panel.add(control_panel);
- Panel tmp = new Panel(new BorderLayout());
- tmp.setBackground(Color.white);
- right_panel.add(tmp);
- Panel stats_panel = new Panel(new GridLayout(4, 2));
- stats_panel.add(new TetrisLabel(" Level: " + level));
- stats_panel.add(new TetrisLabel(" Score: " + score));
- tmp = new Panel(new BorderLayout());
- tmp.setBackground(Color.white);
- tmp.add("Center", stats_panel);
- right_panel.add(tmp);
- // finaly, add all the main panels to the applet panel
- this.setLayout(new GridLayout(1, 2));
- this.add(game_grid);
- this.add(right_panel);
- this.setBackground(Color.black);
- this.validate();
- }
- }
- public void actionPerformed(ActionEvent e)
- {
- String commandStr = e.getActionCommand();
- if (commandStr.equals("Start Game"))
- {
- start_newgame_butt.setLabel("End Game");
- startGame();
- }
- else if (commandStr.equals("End Game"))
- {
- start_newgame_butt.setLabel("Start Game");
- stop();
- }
- else if (commandStr.equals("Pause Game"))
- {
- pause_resume_butt.setLabel("Pause Game");
- stop();
- }
- }
- // applet is started
- public void start()
- {
- for(int i=0; i<cols; i++)
- for(int j=0; j<rows+4; j++) {
- f[i][j] = 0;
- of[i][j] = -1;
- }
- level = 0;
- score = 0;
- pieces= 0;
- curval = -1;
- neednewpiece = true;
- lost = false;
- // spawn gravity thread
- (killme = new Thread(this)).start();
- requestFocus();
- }
- // applet is stopped
- public synchronized void stop()
- {
- if (killme != null)
- killme.stop();
- killme = null;
- }
- private void startGame() {
- start_newgame_butt.setLabel("Start New Game");
- pause_resume_butt.setEnabled(true); // stays enabled from here on
- pause_resume_butt.setLabel("Pause");
- pause_resume_butt.validate();
- }
- private void pauseGame() {
- pause_resume_butt.setLabel("Resume");
- }
- private void resumeGame() {
- pause_resume_butt.setLabel("Pause");
- }
- // main loop for launched thread
- public void run()
- {
- while (!lost) {
- try {
- int t =
- level == 0 ? 900 :
- level == 1 ? 800 :
- level == 2 ? 700 :
- level == 3 ? 600 :
- level == 4 ? 500 :
- level == 5 ? 400 :
- level == 6 ? 300 :
- level == 7 ? 250 :
- level == 8 ? 200 :
- level == 9 ? 150 :
- level == 10 ? 100 : 75;
- ;
- if(t == 900){
- int m = 1;
- }
- Thread.sleep(t);
- } catch (InterruptedException e){}
- if (neednewpiece) {
- if(curval > 0) {
- score += curval;
- pieces++;
- level = 0 + pieces/30;
- }
- removelines();
- newpiece();
- neednewpiece = false;
- } else {
- neednewpiece = !movecurpiece(0, -1, false);
- if(!neednewpiece)
- curval -= 5;
- }
- repaint();
- }
- killme = null;
- if (highscorehost == null)
- return;
- System.out.println("switching to highscore...");
- try {
- /* send mail to gback@cs.utah.edu if you want to get
- * the highscore class and the server
- HighScore hs = new HighScore(this, highscorehost,
- highscoreport, score);
- */
- } catch(Exception e) {
- getAppletContext().showStatus("highscore failed: "+e.getMessage());
- }
- // this terminates second thread
- }
- // key is pressed
- public boolean keyDown(java.awt.Event evt, int key)
- {
- if (key != 's' && lost || suspended)
- return true;
- switch(key) {
- case 's':
- stop(); // stop old game
- start(); // start new game
- break;
- case 'h': // left
- case java.awt.Event.LEFT:
- curval -= 5;
- movecurpiece(-1, 0, false);
- neednewpiece = false;
- repaint();
- break;
- case 'k': // right
- case 'l':
- case java.awt.Event.RIGHT:
- curval -= 5;
- movecurpiece(1, 0, false);
- neednewpiece = false;
- repaint();
- break;
- case 'j': // rotate
- case java.awt.Event.UP:
- if(what!=4) {
- curval -= 5;
- movecurpiece(0, 0, true);
- repaint();
- }
- break;
- case ' ': // drop
- case java.awt.Event.DOWN:
- while(movecurpiece(0, -1, false))
- ;
- repaint();
- break;
- }
- return true;
- }
- // we need to override this method, otherwise repaint()
- // will erase the whole screen, causing tremendous flickering
- public void update(Graphics g)
- {
- justupdating = true;
- // tell paint to draw only stuff that has changed
- paint(g);
- }
- // paint graphics on screen
- public synchronized void paint(Graphics g)
- {
- g.setFont(new java.awt.Font("Helvetica", 0, 18));
- int gx = sqlength;
- int gy = sqlength*rows/4;
- g.clearRect(gx, gy-25, xoffset-gx, 25);
- g.drawString("Score: " + score, gx, gy);
- gy += 30;
- g.clearRect(gx, gy-25, xoffset-gx, 25);
- g.drawString("Level: " + level, gx, gy);
- for (int i=0; i<cols; i++)
- for (int j=0; j<rows; j++) {
- if (!justupdating ||
- of[i][rows-1-j] == -1 ||
- of[i][rows-1-j] != f[i][rows-1-j]) {
- g.setColor(colors[f[i][rows-1-j]]);
- g.fill3DRect(
- xoffset+sqlength*i,
- sqlength+sqlength*j,
- sqlength, sqlength, true);
- }
- of[i][rows-1-j] = f[i][rows-1-j];
- }
- justupdating = false;
- }
- public boolean mouseDown(java.awt.Event evt, int x, int y)
- {
- if (killme == null)
- return true;
- if (suspended)
- killme.resume();
- else
- killme.suspend();
- suspended = !suspended;
- return true;
- }
- } // end Tetris applet
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement