Предыдущая   На главную   Содержание   Следующая
 
 
import java.awt.*;
import java.awt.event.*;
import java.io.*;

class Wolf2 extends Frame implements KeyListener
{
int score=0;
int level=0;
int max=0;
int total=0;


Label maxl=new Label("MAX");
Label sc=new Label("0");
Label levl=new Label("0");
Label vse=new Label("0");

Label maxo=new Label("5");

Label tot=new Label("Total");
//Button
Label lev=new Label("Level");
//Button
Label b2=new Label("Score");
TextField t1=new TextField();
TextField t2=new TextField();
TextField t3=new TextField();
Tama Sokka= new Tama();
Predator Firelord=new Predator(300,300,40,40);

Button res=new Button("Reset");
Button sav=new Button("Save");
//Sheep one=new Sheep(800);
//Sheep two=new Sheep(800);
//Sheep three=new Sheep(800);

Sheep[] pack=new Sheep[100];

Wolf2(String s)
{
super(s);
setLayout(null);

maxl.setBounds(640,110,50,25);
add(maxl);

maxo.setBounds(700,110,50,25);
add(maxo);

vse.setBounds(700,70,50,25);
add(vse);

levl.setBounds(700,50,50,25);
add(levl);

sc.setBounds(700,90,50,25);
add(sc);

res.setBounds(50,50,50,30);
res.addKeyListener(this);
add(res);
res.addActionListener(new Reset());

tot.setBounds(640,70,50,25);
add(tot);

lev.setBounds(640,50,50,25);
add(lev);
//b.addActionListener(new AL());
//b.addKeyListener(this);

b2.setBounds(640,90,50,25);
add(b2);
//b2.addActionListener(new AL2());
//b2.addKeyListener(this);

t1.setBounds(50,50,50,25);
//add(t1);
t1.addKeyListener(this);

t2.setBounds(170,50,50,25);
//add(t2);
t2.addKeyListener(this);

t3.setBounds(240,50,50,25);
//add(t3);
t3.addKeyListener(this);

res.setBounds(20,40,50,25);
add(res);
res.addActionListener(new AL2());
res.addKeyListener(this);

sav.setBounds(80,40,50,25);
add(sav);
sav.addActionListener(new AS());
sav.addKeyListener(this);


setBackground(new Color(16,237,187));

setSize(800, 800);
setVisible(true);

createPack();
}

public void createPack()
{
max=5+level*5;
maxo.setText(""+max);

for(int i=0;i<max;i++)
pack[i]=new Sheep(800);
}

public void paint(Graphics g)
{
//Sokka.risui(this);
Firelord.risui(this);
//one.risui(this);
//two.risui(this);
//three.risui(this);
for(int i=0;i<max;i++)
pack[i].risui(this);

}

class AS implements ActionListener
{
public void actionPerformed(ActionEvent ae)
{
try
{
// Set up the proper readers and writers

FileWriter file_writer = new FileWriter("save.txt");

PrintWriter pr_writer = new PrintWriter(file_writer);
// Get the first line of the file
String line = "level:"+level+";Total:"+total+";Score:"+score;
pr_writer.println(line);
file_writer.close();

}
catch (IOException e)
{
//print error and exit program
System.out.println(e);
System.exit(1);
}
}
}

class Reset implements ActionListener
{
public void actionPerformed(ActionEvent ae)
{
score=0;
sc.setText("0");
level=0;
levl.setText("0");
max=0;
maxo.setText("5");
total=0;
vse.setText("0");

pack=new Sheep[100];
createPack();

repaint();
}

}


class AL implements ActionListener
{
public void actionPerformed(ActionEvent ae)
{
String s=t1.getText();
int n=Integer.parseInt(s);
Sokka.eat(n);
repaint();

}
}

class AL2 implements ActionListener
{
public void actionPerformed(ActionEvent ae)
{
String s2=t2.getText();
int n2=Integer.parseInt(s2);
String s3=t3.getText();
int n3=Integer.parseInt(s3);
Sokka.go(n2,n3,800,800);
if(Firelord.follow(Sokka))
{
Sokka.w=0;
Sokka.h=0;
}
repaint();

}
}

public void keyPressed(KeyEvent k)
{
int n=k.getKeyCode();
System.out.println("n="+n);
switch(n)
{
case 37:
//Firelord.go(-Firelord.step,0);break;
Firelord.go(-Firelord.step,0,this);break;
case 38:
//Firelord.go(0,-Firelord.step);break;
Firelord.go(0,-Firelord.step,this);break;
case 39:
//Firelord.go(Firelord.step,0);break;
Firelord.go(Firelord.step,0,this);break;
case 40:
//Firelord.go(0,Firelord.step);
Firelord.go(0,Firelord.step,this);

}

//one.run(Firelord);
//two.run(Firelord);
//three.run(Firelord);

for(int i=0;i<max;i++)
{
pack[i].run(Firelord);
if(Firelord.chew(pack[i]))
score++;
}

//if(Firelord.chew(one))
//score++;
//if(Firelord.chew(two))
//score++;
//if(Firelord.chew(three))score++;
sc.setText(""+score);
levl.setText(""+levl);
vse.setText(""+total);

if(score==(max-3))
{
level++;
total=total+score;

score=0;
pack=new Sheep[100];
createPack();
}
sc.setText(""+score);
levl.setText(""+level);
vse.setText(""+total);

repaint();
}
public void keyTyped(KeyEvent k){}
public void keyReleased(KeyEvent k){}
//---------------------------------------AKAKKKAKAKAK


public static void main(String[] args)
{
Wolf2 f = new Wolf2("Kpucma say:");

f.addWindowListener (new WindowAdapter(){
public void windowClosing (WindowEvent ev){
System.exit(0);
}
});
}

}
//***************************
class Tama
{
int step=5;
int x,y,w,h;
Color c, eyeC, smileC;
int eyeW,x1,y1,x2,y2;
int smileW,smilex,smiley;

Tama()
{
x=100;
y=100;
w=20;
h=20;
c=new Color(17,58,253);
eyeC=new Color(67,171,248);
smileC=new Color(254,146,203);

setEyes();
setSmile();
}
public void setEyes()
{
eyeW=(int)(w/5);
x1=(int)(x+w/3);
x2=(int)(x+w*0.66);
y1=y2=(int)(y+h/3);

}

public void setSmile()
{
smileW=(int)(w/2);
smilex=(int)(x+w/3);
smiley=(int)(y+h/3);
}

public void eat (int eda)
{
w=(int)(w+eda*0.01);
h=w;
setEyes();
setSmile();
}

public void go (int dx, int dy)
{
x+=dx;
y+=dy;
setEyes();
setSmile();
}

public void go (int dx, int dy, int xm, int ym)
{
int a=x+dx, b=y+dy;

if(a<(xm-w)&& (a>0))x+=dx;
if(b<(ym-h)&& (b>30))y+=dy;
setEyes();
setSmile();
}

public void go (int dx, int dy, Frame f)
{
Dimension d=f.getSize();
int wf=d.width;
int hf=d.height;

int a=x+dx, b=y+dy;

if(a<(wf-w)&& (a>0))x+=dx;
if(b<(hf-h)&& (b>30))y+=dy;
setEyes();
setSmile();

}

public void risui(Wolf2 t)
{

Graphics g=t.getGraphics();
g.setColor(c);
g.fillOval(x,y,w,h);
g.setColor(eyeC);

setEyes();
setSmile();

g.fillOval(x1,y1,eyeW,eyeW);
g.fillOval(x2,y2,eyeW,eyeW);

g.setColor(smileC);
g.drawArc(smilex,smiley,smileW,smileW,0,-180);
}

}
//*****************************

class Predator extends Tama
{
int step=15;

Predator(){super();}
Predator(int a, int b, int d, int e)
{
c=new Color(253,65,2);
eyeC=new Color(162,71,4);
smileC=new Color(254,146,203);

x=a; y=b; w=d; h=e;
setEyes();
setSmile();
}
public boolean follow(Tama t)
{
if(x>=t.x) go(-step,0);
else go(step,0);
if(y>=t.y) go(0,-step);
else go(0,step);
if(Math.abs(x-t.x)<step && Math.abs(y-t.y)<step)
{
this.eat(t.w*20);
//w=w+(int)(t.w*0.2);
//h=h+(int)(t.h*0.2);
//setEyes();
//setSmile();
t.w=0;
t.h=0;
return true;
}
else return false;
}


public boolean chew(Sheep a)
{
if(Math.abs(a.x-x)<a.w/2 && Math.abs(a.y-y)<a.y/2)
{
a.w=0;
a.h=0;
return true;
}
else return false;
}
}
//*******************************************

class Sheep extends Tama
{
Sheep(int field)
{
field=field-20;
w=20;
h=20;
x=(int)(Math.random()*field);
y=(int)(Math.random()*field);
setSmile();
setEyes();

}
public void run (Predator p)
{
if(Math.abs(p.x-x)<2*w && Math.abs(p.y-y)<2*h)
if(p.x>x) go(-step,0);
else go(step,0);
if(Math.abs(p.y-y)<2*w)
if(p.y>y) go(0,-step);
else go(0,step);
}
}

JCreator
 
Этот сайт не для продажи. © http://dragonmaster.sitecity.ru/. Все права защищены.