Games using Java
Moving Plates Game Using Java.
Source Code:
import java.awt.*;
import java.applet.*;
import java.awt.event.*;
/*
<applet code=SimplePlatesGame.class width=800 height=600>
</applet>
*/
public class SimplePlatesGame extends Applet implements MouseListener ,MouseMotionListener
{
Color c1,c2;
Label l;
int score=0;
int temp;
int x=0,y=0;
int xclick,yclick;
int xmin=0,xmax=0,ymin=0,ymax=0;
int r1,g1,b1,r2,g2,b2;
public void init()
{
l=new Label("Score is "+ score);
add(l);
r2=100;g2=b2=0;
c1 = new Color(128, 128, 255);
c2 = new Color(r2, g2,b2 );
newthread s=new newthread(this);
temp=0;
addMouseListener(this);
}
public void paint(Graphics g)
{
setBackground(c1);
l.setBackground(c1);
c2 = new Color(r2, g2,b2 );
g.setColor(c2);
g.fillRect(x, y, 30, 20);
l.setText("Score is "+ score);
showStatus(xmin+" "+xmax+" "+ymin+" "+ymax+" "+xclick+" "+yclick);
}
public void mouseClicked(MouseEvent me)
{
xclick= me.getX() ;
yclick= me.getY();
//showStatus(xclick+" "+yclick);
if((xclick>=xmin & xclick <=xmax) & (yclick>=ymin & yclick<=ymax))
{
score=score+5;
}
repaint();
}
public void mouseEntered(MouseEvent me) {}
public void mouseExited(MouseEvent me) {}
public void mousePressed(MouseEvent me) {}
public void mouseReleased(MouseEvent me) {}
public void mouseDragged(MouseEvent me) {}
public void mouseMoved(MouseEvent me) {}
}
class newthread implements Runnable
{
Thread t;
SimplePlatesGame obj;
newthread(SimplePlatesGame obj)
{
this.obj=obj;
t=new Thread(this ,"animate");
t.start();
}
public void run()
{
try
{
for(;;)
{
Thread.sleep(300);
if(obj.temp==0)
{
obj.x=obj.x+15;
if(obj.x>=690)
{
obj.temp=1;
obj.x=0;
obj.y=560;
}
obj.r2=obj.r2+5;
obj.g2=obj.g2+20;
if(obj.g2>=250)
{
obj.g2=0;
}
if(obj.r2>=250)
{
obj.r2=100;
}
}
if(obj.temp==1)
{
obj.x=obj.x+15;
obj.y=obj.y-15;
if(obj.x>=690 || obj.y<=0)
{
obj.temp=2;
obj.x=690;
obj.y=560;
}
obj.g2=obj.g2+5;
obj.b2=obj.b2+20;
if(obj.g2>=250)
{
obj.g2=0;
}
if(obj.b2>=250)
{
obj.b2=60;
}
}
if(obj.temp==2)
{
obj.x=obj.x-15;
obj.y=obj.y-15;
if(obj.x<=0 || obj.y<=0)
{
obj.temp=3;
obj.x=690;
obj.y=00;
}
obj.b2=obj.b2+5;
obj.r2=obj.r2+20;
if(obj.b2>=250)
{
obj.b2=20;
}
if(obj.r2>=250)
{
obj.r2=50;
}
}
if(obj.temp==3)
{
obj.x=obj.x-15;
obj.y=obj.y+15;
if(obj.x<=0 || obj.y>=560)
{
obj.temp=0;
obj.x=00;
obj.y=00;
}
obj.b2=obj.b2+5;
obj.r2=obj.r2+20;
obj.g2=obj.g2+10;
if(obj.b2>=250)
{
obj.b2=20;
}
if(obj.r2>=250)
{
obj.r2=50;
}
if(obj.g2>=250)
{
obj.g2=0;
}
}
obj.xmin=obj.x;
obj.xmax=obj.x+30;
obj.ymin=obj.y;
obj.ymax=obj.y+20;
obj.repaint();
}
}
catch(Exception e)
{
System.out.println(e);
}
}
}
import java.applet.*;
import java.awt.event.*;
/*
<applet code=SimplePlatesGame.class width=800 height=600>
</applet>
*/
public class SimplePlatesGame extends Applet implements MouseListener ,MouseMotionListener
{
Color c1,c2;
Label l;
int score=0;
int temp;
int x=0,y=0;
int xclick,yclick;
int xmin=0,xmax=0,ymin=0,ymax=0;
int r1,g1,b1,r2,g2,b2;
public void init()
{
l=new Label("Score is "+ score);
add(l);
r2=100;g2=b2=0;
c1 = new Color(128, 128, 255);
c2 = new Color(r2, g2,b2 );
newthread s=new newthread(this);
temp=0;
addMouseListener(this);
}
public void paint(Graphics g)
{
setBackground(c1);
l.setBackground(c1);
c2 = new Color(r2, g2,b2 );
g.setColor(c2);
g.fillRect(x, y, 30, 20);
l.setText("Score is "+ score);
showStatus(xmin+" "+xmax+" "+ymin+" "+ymax+" "+xclick+" "+yclick);
}
public void mouseClicked(MouseEvent me)
{
xclick= me.getX() ;
yclick= me.getY();
//showStatus(xclick+" "+yclick);
if((xclick>=xmin & xclick <=xmax) & (yclick>=ymin & yclick<=ymax))
{
score=score+5;
}
repaint();
}
public void mouseEntered(MouseEvent me) {}
public void mouseExited(MouseEvent me) {}
public void mousePressed(MouseEvent me) {}
public void mouseReleased(MouseEvent me) {}
public void mouseDragged(MouseEvent me) {}
public void mouseMoved(MouseEvent me) {}
}
class newthread implements Runnable
{
Thread t;
SimplePlatesGame obj;
newthread(SimplePlatesGame obj)
{
this.obj=obj;
t=new Thread(this ,"animate");
t.start();
}
public void run()
{
try
{
for(;;)
{
Thread.sleep(300);
if(obj.temp==0)
{
obj.x=obj.x+15;
if(obj.x>=690)
{
obj.temp=1;
obj.x=0;
obj.y=560;
}
obj.r2=obj.r2+5;
obj.g2=obj.g2+20;
if(obj.g2>=250)
{
obj.g2=0;
}
if(obj.r2>=250)
{
obj.r2=100;
}
}
if(obj.temp==1)
{
obj.x=obj.x+15;
obj.y=obj.y-15;
if(obj.x>=690 || obj.y<=0)
{
obj.temp=2;
obj.x=690;
obj.y=560;
}
obj.g2=obj.g2+5;
obj.b2=obj.b2+20;
if(obj.g2>=250)
{
obj.g2=0;
}
if(obj.b2>=250)
{
obj.b2=60;
}
}
if(obj.temp==2)
{
obj.x=obj.x-15;
obj.y=obj.y-15;
if(obj.x<=0 || obj.y<=0)
{
obj.temp=3;
obj.x=690;
obj.y=00;
}
obj.b2=obj.b2+5;
obj.r2=obj.r2+20;
if(obj.b2>=250)
{
obj.b2=20;
}
if(obj.r2>=250)
{
obj.r2=50;
}
}
if(obj.temp==3)
{
obj.x=obj.x-15;
obj.y=obj.y+15;
if(obj.x<=0 || obj.y>=560)
{
obj.temp=0;
obj.x=00;
obj.y=00;
}
obj.b2=obj.b2+5;
obj.r2=obj.r2+20;
obj.g2=obj.g2+10;
if(obj.b2>=250)
{
obj.b2=20;
}
if(obj.r2>=250)
{
obj.r2=50;
}
if(obj.g2>=250)
{
obj.g2=0;
}
}
obj.xmin=obj.x;
obj.xmax=obj.x+30;
obj.ymin=obj.y;
obj.ymax=obj.y+20;
obj.repaint();
}
}
catch(Exception e)
{
System.out.println(e);
}
}
}
- Save the file with name SimplePlatesGame.java
- Compile with: javac SimplePlatesGame.java
- Run with:appletviewer SimplePlatesGame.java
No comments:
Post a Comment