AJP MicroProject
AJP MicroProject
AJP MICROPROJECT
TITLE:
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)
of Micro-Project:
2 INTERNET 10MBPS
of Micro-Project:
3
AJP MICROPROJECT
2 INTERNET 10MBPS
6.0 Outputs of Micro project:
4
AJP MICROPROJECT
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.
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;
//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
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();
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;
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();
}
}
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;
//init shape
public Shape() {
initShape();
}
11
AJP MICROPROJECT
12
AJP MICROPROJECT
}
return m;
}
//rotate shape left
public Shape rotateLeft() {
if (pieceShape == Tetrominoe.SquareShape) {
return this;
}
3.Tetris Class:
package com.tetris;
import java.awt.BorderLayout;
import java.awt.EventQueue;
import javax.swing.JFrame;
import javax.swing.JLabel;
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