Monday, 8 February 2021

Tetris Game: Java

  • This is a simple Tetris game in Java
  • Download the jar from here
  • Requires JRE7
  • This is the code for the program

import java.awt.Color;

import java.awt.Component;

import java.awt.Toolkit;

import java.awt.event.KeyAdapter;

import java.awt.event.KeyEvent;

 

import javax.swing.BorderFactory;

import javax.swing.JFrame;

import javax.swing.JTextArea;

 

public class Tetris {

 

       public static void main(String[] args) throws InterruptedException {

              final JFrame frm=new JFrame("Tetris");

              frm.setSize(215,400);

              frm.setLocation(Toolkit.getDefaultToolkit().getScreenSize().width/2-frm.getSize().width/2,Toolkit.getDefaultToolkit().getScreenSize().height/2-frm.getSize().height/2);

              frm.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);

              frm.setResizable(false);

              frm.setLayout(null);

              final JTextArea x = new JTextArea();

              x.setEditable(false);

              x.setVisible(true);

              x.setSize(20,20);

              x.setLocation((frm.getWidth()-15)/2-(x.getWidth()/2),0);

              x.setBackground(Color.black);

              x.setBorder(BorderFactory.createLineBorder(Color.red, 4));

              frm.add(x);

              frm.setFocusable(true);

              frm.addKeyListener(new KeyAdapter() {

                     @Override

                     public void keyTyped(KeyEvent e) {

                           loop:{

                           if (e.getKeyChar()==KeyEvent.VK_4){

                                  if (x.getLocation().x<=0) {

                                         x.setLocation(frm.getWidth()-5,x.getLocation().y);

                                         break loop;

                                  }

                                  x.setLocation(x.getLocation().x-5,x.getLocation().y);

                           }

                           if (e.getKeyChar()==KeyEvent.VK_6) {

                                  if (x.getLocation().x>=frm.getWidth()-x.getSize().width*2) {

                                         x.setLocation(5,x.getLocation().y);

                                         break loop;

                                  }

                                  x.setLocation(x.getLocation().x+5,x.getLocation().y);

                           }

                     }

                     }

              });

              frm.setVisible(true);

              for(;;) {

                     if (x.getLocation().y>frm.getHeight()-x.getHeight()-50) {

                           JTextArea y=new JTextArea();

                           y.setEditable(false);

                           y.setVisible(true);

                           y.setSize(20,20);

                           y.setBackground(Color.black);

                           y.setLocation(x.getLocation());

                           x.setLocation((frm.getWidth()-15)/2-(x.getWidth()/2),0);

                           y.setBorder(BorderFactory.createLineBorder(Color.green, 4));

                           frm.add(y);

                     }

                     Thread.sleep(60);

                     Component z[]=x.getParent().getComponents();

                     for(int i=0;i<z.length;i++) {

                           for (int w=x.getLocation().x; w<x.getLocation().x+x.getWidth(); w++) {

                                  if (x.getParent().getComponentAt(w,x.getLocation().y+x.getHeight()+2)==z[i]) {

                                         JTextArea y=new JTextArea();

                                         y.setEditable(false);

                                         y.setVisible(true);

                                         y.setSize(20,20);

                                         y.setBackground(Color.black);

                                         y.setLocation(x.getLocation());

                                         x.setLocation((frm.getWidth()-15)/2-(x.getWidth()/2),0);

                                         y.setBorder(BorderFactory.createLineBorder(Color.green, 4));

                                         frm.add(y);

                                  }

                           }

                     }

                     x.setLocation(x.getLocation().x,x.getLocation().y+4);

              }

 

       }

 

}

 

  • Run the code in your IDE

3 comments:

Algorithmic Pixel Art: The Beauty of Aliasing

Discussed  here More images in  Google drive Some outputs: Forward to anyone who says aliasing is ugly.