Spider game in java using AWT/Swing  

by ne on 2021-09-22 under Java

Following is the code to make a spider game using java-graphics- awt and swing. After successful compilation you can run the game by saving the file - spider2.java.


import javax.swing.*;
import java.awt.*;
import java.awt.event.*;
import java.io.File;
import java.applet.Applet;
import java.applet.AudioClip;
import java.net.MalformedURLException;
import java.net.URL;


public class spider2 extends KeyAdapter  implements ActionListener ,Runnable {
int n=0;
int temp;
int temp1;
  

ImageIcon icon4;
    ImageIcon icon1 =new ImageIcon("Picture2.jpg.png");
ImageIcon icon2 =new ImageIcon("Picture92.jpg.png");
ImageIcon icon7 =new ImageIcon("Picture140.jpg.png");

ImageIcon icon3 =new ImageIcon("Picture7.jpg.png");
ImageIcon icon =new ImageIcon("Picture46.jpg.png");
ImageIcon icon5=new ImageIcon("Picture20.jpg.png");
ImageIcon icon6=new ImageIcon("Picture106.jpg.png");
//int
int random1=68;

JFrame f2=new JFrame("The Killing Spider Options");
JFrame f3=new JFrame("About The Killing Spider");
JFrame f4=new JFrame("The Killing Spider  Overview");
AudioClip drag;

//private int i;
        JFrame f=new JFrame("The Killing Spider game");
JButton b=new JButton("start");
JButton b1=new JButton("option");
JButton b2=new JButton("about");
JButton b3=new JButton("help");
JPanel p13=new JPanel();
JPanel p14=new JPanel(new GridLayout(5,0));
JPanel p1=new JPanel(new GridLayout(1,0));
JPanel p2=new JPanel(new GridLayout(1,0));
JPanel p3=new JPanel(new GridLayout(1,0));
JPanel p4=new JPanel(new GridLayout(1,0));
JPanel p5=new JPanel(new GridLayout(1,0));
JPanel p6=new JPanel(new GridLayout(1,0));
JPanel p7=new JPanel(new GridLayout(1,0));
JPanel p8=new JPanel(new GridLayout(1,0));
JPanel p9=new JPanel(new GridLayout(1,0));
JPanel p10=new JPanel(new GridLayout(1,0));

JPanel p11=new JPanel(new FlowLayout());
JPanel p12=new JPanel(new GridLayout(0,2));
JLabel lb=new JLabel(" Made By:        NAVEEN  KUMAR  BAPNA \n  Email id:       ");
JLabel lb2=new JLabel(icon2);
JLabel lb1=new JLabel(icon3);
JLabel lb3=new JLabel(icon7);
JLabel lb4=new JLabel("made by   : Naveen Kumar Bapna");
JLabel lb5=new JLabel("institute : Apex Institute Of Engineering & Techology jaipur,sitapura ");

JLabel lb6=new JLabel("from      : Bhilwara");
JLabel lb7=new JLabel("phone     :9784852851");

//JProgressBar pb=new JProgressBar();
JLabel pb=new JLabel("SCORE 0");
JLabel[] labels = new JLabel[100];
     
int increase=0;
    public spider2()
    {
         
        for(int  i=0;i<100;i++)
{
if(i==1||i==9||i==12||i==20||i==25||i==31||i==44||i==48||i==52||i==60||i==72||i==75||i==94)
{
labels[i]=new JLabel(icon6);
}

else
{
labels[i]=new JLabel(icon);
}
    }
labels[random1].setIcon(icon5);
labels[65]=new JLabel(icon3);      
for(int j=0;j<10;j++)
{
         p1.add(labels[j]);
    }
     for(int j=10;j<20;j++)
{
         p2.add(labels[j]);
    }
        for(int j=20;j<30;j++)
{
         p3.add(labels[j]);
    }
        for(int j=30;j<40;j++)
{
         p4.add(labels[j]);
    }
        for(int j=40;j<50;j++)
{
         p5.add(labels[j]);
    }
        for(int j=50;j<60;j++)
{
         p6.add(labels[j]);
    }
        for(int j=60;j<70;j++)
{
         p7.add(labels[j]);
    }
        for(int j=70;j<80;j++)
{
         p8.add(labels[j]);
    }
        for(int j=80;j<90;j++)
{
         p9.add(labels[j]);
    }
        for(int j=90;j<100;j++)
{
         p10.add(labels[j]);
    }
        p11.add(b);
        p11.add(b1);
        p11.add(b2);
        p11.add(b3);   
        p11.add(pb);  
b.addKeyListener(this);
//f.add(p11);

f.add(p1);
f.add(p2);
f.add(p3);
f.add(p4);
f.add(p5);
f.add(p6);
f.add(p7);
f.add(p8);
f.add(p9);
f.add(p10);
pb.setForeground(Color.blue);
p11.setBackground(Color.DARK_GRAY);
p12.add(p11);
p12.add(lb);
f.add(p12);
f.setLayout(new GridLayout(11,0));
b.addActionListener(this);
b1.addActionListener(this);
b2.addActionListener(this);
b3.addActionListener(this);
f.setVisible(true);
f.setBounds(70, 70, 1200, 600);
f.setResizable(false);
f4.setResizable(false);

f2.setResizable(false);
p14.add(lb1);
p14.add(lb4);
p14.add(lb5);
p14.add(lb6);
p14.add(lb7);

f3.add(p14);
f4.add(lb3);
p13.add(lb2);
f2.add(p13);
p14.setBackground(Color.BLACK);
p13.setBackground(Color.BLACK);
//lb1.setForeground(Color.BLACK);
f3.setResizable(false);
//lb1.setIconImage(icon2);
f.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);


    }

    public void actionPerformed(ActionEvent e)
    {


                if(e.getActionCommand().equals("start"))
{
Thread t = new Thread(this);
                t.setDaemon(true);
                 t.start();
  b.setText("New");
                            
}
if(e.getActionCommand().equals("option"))
       {
f2.setBounds(300,125,395,325);

        f2.setVisible(true);
   
b1.addKeyListener(this);
}
if(e.getActionCommand().equals("about"))
       {
f3.setBounds(450, 150, 300, 300);


        f3.setVisible(true);
    b2.addKeyListener(this);
}
if(e.getActionCommand().equals("help"))
       {
f4.setBounds(500, 20, 522, 410);

           f4.setVisible(true);
  
b3.addKeyListener(this);
}

        }


     public void keyPressed(KeyEvent evt) {
    if (evt.getKeyChar() == KeyEvent.VK_DOWN) {

n=3;
      System.out.println("Check for key characters: " + evt.getKeyChar());
    }
    if (evt.getKeyCode() == KeyEvent.VK_UP) {
          n=2;
  System.out.println("Check for key codes: " + evt.getKeyCode());
    }
    if (evt.getKeyCode() == KeyEvent.VK_LEFT) {
          n=4;
  System.out.println("Check for key codes: " + evt.getKeyCode());
    }
    if (evt.getKeyCode() == KeyEvent.VK_RIGHT) {
          n=1;
  System.out.println("Check for key codes: " + evt.getKeyCode());
    }
    }
public void run()
    {
try{

    File drag_f=new File("Arrival.wav");        URL drag_url = drag_f.toURL();
        drag=Applet.newAudioClip(drag_url);

}
catch(Exception e)
{
}

        while(true)
         {
                while (n==2)
            {
                try
            {
//for up direction
         for(int i=0;i<100;i++)
        {
            if(labels[i].getIcon()==icon3)
            {
              temp=i;

             if(temp==0)
            {
                temp1=90;
            }
             if(temp==1)
            {
                temp1=91;
                }
             if(temp==2)
            {
                temp1=92;
               }
             if(temp==3)
            {
                temp1=93;
            }
              if(temp==4)
            {
                temp1=94;
            }
              if(temp==5)
            {
                temp1=95;
            }
              if(temp==6)
            {
                temp1=96;
            }
              if(temp==7)
            {
                temp1=97;
            }
              if(temp==8)
            {
                temp1=98;
            }
              if(temp==9)
            {
                temp1=99;
            }
               if(temp>9)
             {
               temp1=temp-10;
              }
            }

        }
       if(temp1==random1){
          random1=(int)(Math.random()*100);
if(random1==1||random1==9||random1==12||random1==20||random1==25||random1==31||random1==44||random1==48||random1==52||random1==60||random1==72||random1==75||random1==94)
{
random1=random1+2;
}
          labels[random1].setIcon(icon5);
           System.out.println("You are wrong");
       increase=increase+1;
       pb.setText("SCORE " + increase);
if(increase%10==0){
drag.play();
}
       }
if(temp==1||temp==9||temp==12||temp==20||temp==25||temp==31||temp==44||temp==48||temp==52||temp==60||temp==72||temp==75||temp==94)
{
JOptionPane.showMessageDialog(null,
"GAME OVER \n PLEASE TRY AGAIN!", "Hey!",2,icon3);
//Thread.wait();
System.exit(9);

//,JOptionPane.ERROR_MESSAGE
}
      labels[temp].setIcon(icon);
      labels[temp1].setIcon(icon3);

b.addKeyListener(this);

Thread.sleep(550);

        }
     catch(Exception e)
            {

     }

        }


while(n==3)
            {
                try
            {
//for down direction
        for(int i=0;i<100;i++)
        {
            if(labels[i].getIcon()==icon3)
            {
              temp=i;

             if(temp==90)
            {
                temp1=0;
            }
             if(temp==91)
            {
                temp1=1;
                }
             if(temp==92)
            {
                temp1=2;
               }
             if(temp==93)
            {
                temp1=3;
            }
              if(temp==94)
            {
                temp1=4;
            }
              if(temp==95)
            {
                temp1=5;
            }
              if(temp==96)
            {
                temp1=6;
            }
              if(temp==97)
            {
                temp1=7;
            }
              if(temp==98)
            {
                temp1=8;
            }
              if(temp==99)
            {
                temp1=9;
            }
               if(temp<90)
             {
               temp1=temp+10;
              }
            }

        }
       if(temp1==random1){
          random1=(int)(Math.random()*99);
if(random1==1||random1==9||random1==12||random1==20||random1==25||random1==31||random1==44||random1==48||random1==52||random1==60||random1==72||random1==75||random1==94)
{
random1=random1+2;
}
         
labels[random1].setIcon(icon5);
           System.out.println("You are wrong");
       increase=increase+1;
        pb.setText("SCORE " + increase);
if(increase%10==0){
drag.play();

}      }
if(temp==1||temp==9||temp==12||temp==20