0% found this document useful (0 votes)
82 views14 pages

AJP MicroProject

This document contains details about a Tetris game project created using Java GUI. It includes the names of the team members working on the project and an index of the document contents. It also includes annexures that contain a project proposal template, teacher evaluation sheet, and details of the Tetris game classes and code. The aim of the project is to program a Tetris game using Java Swing and AWT.
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
82 views14 pages

AJP MicroProject

This document contains details about a Tetris game project created using Java GUI. It includes the names of the team members working on the project and an index of the document contents. It also includes annexures that contain a project proposal template, teacher evaluation sheet, and details of the Tetris game classes and code. The aim of the project is to program a Tetris game using Java Swing and AWT.
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 14

AJP MICROPROJECT

AJP MICROPROJECT

TITLE:

TETRIS GAME USING JAVA GUI


UNDER GUIDANCE OF: PRADNYA MAM
CLASS: CO5IA

TEAM:
SR NAME ENROLL
1 KHAN ARMAN 1905690298
2 KHAN SOHAIL 1905690309
3 KHAN RIZWAN 1905690288
4 THANWAAL ALAMGEER 1905690305

1
AJP MICROPROJECT

INDEX:
SR TITLE PG NO.
1 INTRODUCTION 1
2 INDEX 2
3 ANNEXURE 3
4 PROJECT 7

2
AJP MICROPROJECT

ANNEXURE 1 A
PART A - Plan (About 2-3 pages)

Format for Micro-Project Proposal For 1st To 6th Semester Title

of Micro-Project:

TETRIS GAME USING JAVA GUI

1.0 Brief Introduction(Importance of the project, in about 4 to 5 Sentences):


Tetris game using java swing and awt.

2.0 Aim of Micro project:


To successfully program a game using java.

3.0 Action Plan


SR NO NAME OF ACTIVITY START DATE FINISH DATE RESPONSIBLE
TEAM MEMBER

4.0 Resources Required


SR NO NAME OF SPECIFICATION QUANTITY REMARK
RESOURCE
1 LAPTOP 1

2 INTERNET 10MBPS

3 ECLIPSE FOR JAVA 2021


ANNEXURE 2 A
PART B - Plan (About 2-3 pages)
Format for Micro-Project Proposal For 1st To 6th Semester Title

of Micro-Project:

3
AJP MICROPROJECT

TETRIS GAME USING JAVA GUI

1.0 Brief Introduction(Importance of the project, in about 4 to 5 Sentences):


Tetris game using java swing and awt.

2.0 Aim of Micro project:


To successfully program a game using java.

3.0 Course Outcomes:


To be able to program any game.

4.0 Actual Procedure Followed:

5.0 Resources Required:


SR NAME OF RESOURCE SPECIFICATION QUANTITY REMARK
NO
1 LAPTOP 1

2 INTERNET 10MBPS
6.0 Outputs of Micro project:

7.0 Skills Developed:


Learnt to test software.
Teacher Evaluation Sheet

Name of Student: …………………………………………………………


Enrolment No.: ……………………………………………… Name of Program:
…………………………………………………
Semester: .........................................................
Course Title: ……………………………………………………………….
Code: ……………………………………………………………

4
AJP MICROPROJECT

Title of the Micro-Project:


…………………………………………………………………………………………………….
Course Outcomes Achieved:
………………………………………………………………………………………………………

Evaluation as per Suggested Rubric for Assessment of Micro Project
SR Characteristic to be Poor (Marks Average Good Excellent
NO assessed 1-3) (Marks4-5) (Marks6-8) (Marks9-
10)
1 Relevance to the
course
2 Literature Survey /
Information Collection

3
Project Proposal

4 Completion of the
Target as per project
proposal
5 Analysis of Data and
representation
6 Quality of Model

7 Report Presentation

8 Presentation

9 Defence
Micro-Project Evaluation Sheet
Project Process Total
Assessment Assessment Marks(10)
Part A Project Part B Individual
Proposal (2 M) Methodology Project Report Presentation
(2 M) (2 M) (4 M)

5
AJP MICROPROJECT

Note:Every course teacher is expected to assign marks for group evolution in first 3 columns &
individual evaluation in 4TH columns for each group of students as per rubrics.

Comments/Suggestions about team work/leadership/inter-personal communication (if any):


………………………………………………………………………………………………………
…….
Any Other Comment:
………………………………………………………………………………………………………
…….
Name and designation of the Faculty Member:……………………………………………………
Signature:…………………………………………………………….

TETRIS
NAME: TETRIS GAME
PACKAGE: com.tetris CLASSES:
• Board.java
• Shape.java
• Tetris.java
JVM: JDK 11 CODE:
1. Board Class: package com.tetris;

import com.tetris.Shape.Tetrominoe;

import javax.swing.JLabel; import


javax.swing.JPanel; import
javax.swing.Timer; import
java.awt.Color; import
java.awt.Graphics; import
java.awt.event.ActionEvent; import
java.awt.event.ActionListener; import
java.awt.event.KeyAdapter; import
java.awt.event.KeyEvent;

//board class
public class Board extends JPanel {
//constants
private final int BOARD_WIDTH = 10;
private final int BOARD_HEIGHT = 22;
private final int PERIOD_INTERVAL = 300;

6
AJP MICROPROJECT

//variables private Timer timer;


private boolean isFallingFinished = false;
private boolean isPaused = false; private
int numLinesRemoved = 0; private int curX
= 0; private int curY = 0; private
JLabel statusbar; private Shape curPiece;
private Tetrominoe[] board; //initialize
board public Board(Tetris parent) {
initBoard(parent);
}
//initialize board function
private void initBoard(Tetris parent) {
setFocusable(true);
statusbar = parent.getStatusBar();
addKeyListener(new Tadapter());
setBackground(Color.BLACK);
}
//determine width of each square making shapes on basis of window size
private int squareWidth() {
return (int) getSize().getWidth() / BOARD_WIDTH;
}
//determine height of each square making shapes on basis of window size
private int squareHeight() {
return (int) getSize().getHeight() / BOARD_HEIGHT;
}

private Tetrominoe shapeAt(int x, int y) {


return board[(y * BOARD_WIDTH) + x];
}
//start the game void
start() { curPiece = new
Shape();
board = new Tetrominoe[BOARD_WIDTH * BOARD_HEIGHT];

clearBoard();
newPiece();
//start new game
timer = new Timer(PERIOD_INTERVAL, new GameCycle());
timer.start();
}
//on pause game stop the game, saving the instance
private void pause() { isPaused = !isPaused;

if (isPaused) {
statusbar.setText("paused");
} else {
statusbar.setText(String.valueOf(numLinesRemoved));
}
repaint();
}
//initialize the shapes
@Override
public void paintComponent(Graphics g) {

7
AJP MICROPROJECT

super.paintComponent(g);
doDrawing(g);
}
//randomly draw the shapes using squares drawn by drawSquare func
private void doDrawing(Graphics g) { var size = getSize();
int boardTop = (int) size.getHeight() - BOARD_HEIGHT * squareHeight();
for (int i = 0; i < BOARD_HEIGHT; i++) { for (int j = 0; j <
BOARD_WIDTH; j++) {
Tetrominoe shape = shapeAt(j, BOARD_HEIGHT - i - 1);
if (shape != Tetrominoe.NoShape) { drawSquare(g, j *
squareWidth(),
boardTop + i * squareHeight(), shape);
}
}
}
if (curPiece.getShape() != Tetrominoe.NoShape) {
for (int i = 0; i < 4; i++) { int x = curX +
curPiece.x(i);
int y = curY - curPiece.y(i);

drawSquare(g, x * squareWidth(),
boardTop + (BOARD_HEIGHT - y - 1) * squareHeight(),
curPiece.getShape());
}
}
}
//on clicking space button fully drop down the shape
private void dropDown() { int newY = curY; while
(newY > 0) {
if (!tryMove(curPiece, curX, newY - 1)) {
break;
}
newY--;
}
pieceDropped();
}
//on clicking D button drop the shape by on line
private void oneLineDown() {
if (!tryMove(curPiece, curX, curY - 1)) {
pieceDropped();
}
}
//on game over clear board
private void clearBoard() {
for (int i = 0; i < BOARD_HEIGHT * BOARD_WIDTH; i++) {
board[i] = Tetrominoe.NoShape;
}
}
//check the instance and position of shape
private void pieceDropped() { for (int i =
0; i < 4; i++) { int x = curX +
curPiece.x(i); int y = curY -
curPiece.y(i);
board[(y * BOARD_WIDTH) + x] = curPiece.getShape();

8
AJP MICROPROJECT

removeFullLines();

if (!isFallingFinished) {
newPiece();
}
}
//spawn new shape private
void newPiece() {
curPiece.setRandomShape();
curX = BOARD_WIDTH / 2 + 1;
curY = BOARD_HEIGHT - 1 + curPiece.minY();
if (!tryMove(curPiece, curX, curY)) {
curPiece.setShape(Tetrominoe.NoShape);
timer.stop();

var msg = String.format("Game over. Score: %d", numLinesRemoved);


statusbar.setText(msg);
}
}
//move the shape on button click event
private boolean tryMove(Shape newPiece, int newX, int newY) {
for (int i = 0; i < 4; i++) { int x = newX + newPiece.x(i);
int y = newY - newPiece.y(i);

if (x < 0 || x >= BOARD_WIDTH || y < 0 || y >= BOARD_HEIGHT) {


return false;
}

if (shapeAt(x, y) != Tetrominoe.NoShape) {
return false;
}
}

curPiece = newPiece;
curX = newX; curY =
newY; repaint();

return true;
}
//delete the line if completely formed
private void removeFullLines() { int
numFullLines = 0;

for (int i = BOARD_HEIGHT - 1; i >= 0; i--) {


boolean lineIsFull = true;

for (int j = 0; j < BOARD_WIDTH; j++) {


if (shapeAt(j, i) == Tetrominoe.NoShape) {
lineIsFull = false;
break;
}
}

9
AJP MICROPROJECT

if (lineIsFull) {
numFullLines++;
for (int k = i; k < BOARD_HEIGHT - 1; k++) { for
(int j = 0; j < BOARD_WIDTH; j++) { board[(k * BOARD_WIDTH) + j] =
shapeAt(j, k
+ 1);
}
}
}
}
if (numFullLines > 0) {
numLinesRemoved += numFullLines;

statusbar.setText(String.valueOf(numLinesRemoved));
isFallingFinished = true;
curPiece.setShape(Tetrominoe.NoShape);
}
}
//draw square to construct the shape
private void drawSquare(Graphics g, int x, int y, Tetrominoe shape) {
Color colors[] = {new Color(0, 0, 0), new Color(204, 102, 102),
new Color(102, 204, 102), new Color(102, 102, 204), new
Color(204, 204, 102), new Color(204, 102, 204), new
Color(102, 204, 204), new Color(218, 170, 0)
}; var color =

colors[shape.ordinal()];

g.setColor(color);
g.fillRect(x + 1, y + 1, squareWidth() - 2, squareHeight() - 2);

g.setColor(color.brighter());
g.drawLine(x, y + squareHeight() - 1, x, y);
g.drawLine(x, y, x + squareWidth() - 1, y);

g.setColor(color.darker());
g.drawLine(x + 1, y + squareHeight() - 1,
x + squareWidth() - 1, y + squareHeight() - 1);
g.drawLine(x + squareWidth() - 1, y + squareHeight() - 1,
x + squareWidth() - 1, y + 1);
}
//game cycle
private class GameCycle implements ActionListener {
@Override
public void actionPerformed(ActionEvent e) {
doGameCycle();
}
}

private void doGameCycle() {


update(); repaint();
}

private void update() {

10
AJP MICROPROJECT

if (isPaused) {
return;
}

if (isFallingFinished) {
isFallingFinished = false;
newPiece(); } else {
oneLineDown();
}
}
//key adapter
class TAdapter extends KeyAdapter {
@Override
public void keyPressed(KeyEvent e) {
if (curPiece.getShape() == Tetrominoe.NoShape) {
return;
} int keycode =

e.getKeyCode();

//key events
switch (keycode) {
case KeyEvent.VK_P -> pause();
case KeyEvent.VK_LEFT -> tryMove(curPiece, curX - 1, curY);
case KeyEvent.VK_RIGHT -> tryMove(curPiece, curX + 1, curY); case
KeyEvent.VK_DOWN -> tryMove(curPiece.rotateRight(), curX, curY);
case KeyEvent.VK_UP -> tryMove(curPiece.rotateLeft(), curX,
curY);
case KeyEvent.VK_SPACE -> dropDown();
case KeyEvent.VK_D -> oneLineDown();
}
}
}
}

2.Shape Class:
package com.tetris;

import java.util.Random;

public class Shape { protected enum Tetrominoe { NoShape, ZShape,


SShape, LineShape,
TShape, SquareShape, LShape, MirroredLShape }

private Tetrominoe pieceShape;


private int coords[][];
private int[][][] coordsTable;

//init shape
public Shape() {
initShape();
}

11
AJP MICROPROJECT

//init shape func


private void initShape() {
coords = new int[4][2];

coordsTable = new int[][][] {


{ { 0, 0 }, { 0, 0 }, { 0, 0 }, { 0, 0 } },
{ { 0, -1 }, { 0, 0 }, { -1, 0 }, { -1, 1 } },
{ { 0, -1 }, { 0, 0 }, { 1, 0 }, { 1, 1 } },
{ { 0, -1 }, { 0, 0 }, { 0, 1 }, { 0, 2 } },
{ { -1, 0 }, { 0, 0 }, { 1, 0 }, { 0, 1 } },
{ { 0, 0 }, { 1, 0 }, { 0, 1 }, { 1, 1 } },
{ { -1, -1 }, { 0, -1 }, { 0, 0 }, { 0, 1 } },
{ { 1, -1 }, { 0, -1 }, { 0, 0 }, { 0, 1 } }
};
setShape(Tetrominoe.NoShape);
} //set
shape
protected void setShape(Tetrominoe shape) {
for (int i = 0; i < 4 ; i++) { for (int j
= 0; j < 2; ++j) {
coords[i][j] = coordsTable[shape.ordinal()][i][j];
}
}
pieceShape = shape;
}

private void setX(int index, int x) { coords[index][0] = x; }


private void setY(int index, int y) { coords[index][1] = y; }
public int x(int index) { return coords[index][0]; } public
int y(int index) { return coords[index][1]; } public
Tetrominoe getShape() { return pieceShape; }
//init random shape public void
setRandomShape() { var r = new Random();
int x = Math.abs(r.nextInt()) % 7 + 1;

Tetrominoe[] values = Tetrominoe.values();


setShape(values[x]);
}

public int minX() {


int m = coords[0][0];

for (int i=0; i < 4; i++) {


m = Math.min(m, coords[i][0]);
}
return m;
}

public int minY() {


int m = coords[0][1];

for (int i=0; i < 4; i++) {


m = Math.min(m, coords[i][1]);

12
AJP MICROPROJECT

}
return m;
}
//rotate shape left
public Shape rotateLeft() {
if (pieceShape == Tetrominoe.SquareShape) {
return this;
}

var result = new Shape();


result.pieceShape = pieceShape; for (int i
= 0; i < 4; ++i) { result.setX(i,
y(i)); result.setY(i, -x(i));
}
return result;
}
//rotate shape right public Shape
rotateRight() { if (pieceShape ==
Tetrominoe.SquareShape) {
return this;
}

var result = new Shape();


result.pieceShape = pieceShape;

for (int i = 0; i < 4; ++i) {


result.setX(i, -y(i));
result.setY(i, x(i));
}
return result;
}
}

3.Tetris Class:
package com.tetris;

import java.awt.BorderLayout;
import java.awt.EventQueue;
import javax.swing.JFrame;
import javax.swing.JLabel;

public class Tetris extends JFrame {


private JLabel statusbar;
//initialize the window public
Tetris() { initUI();
}
//initialize window func private
void initUI() { statusbar = new
JLabel(" 0"); add(statusbar,
BorderLayout.SOUTH);

var board = new Board(this);


add(board); board.start();

13
AJP MICROPROJECT

setTitle("Tetris");
setSize(250, 500);
setDefaultCloseOperation(EXIT_ON_CLOSE);
setLocationRelativeTo(null);
this.getContentPane().setBackground(Color.WHITE);
}

JLabel getStatusBar() {
return statusbar;
}
//main func
public static void main(String[] args) {
EventQueue.invokeLater(() -> { var game
= new Tetris();
game.setVisible(true);
});
}
}

OUTPUT:

THE END.
14

You might also like