import java.awt.*;
import java.awt.event.KeyAdapter;
import java.awt.event.KeyEvent;
import java.awt.event.WindowAdapter;
import java.awt.event.WindowEvent;
class TortoiseCrawl extends Frame {
final int width=500;
final int height=500;
Tortoise t=new Tortoise(0,0,10);
public void launchFrame(){
setTitle("乌龟爬行");
setVisible(true);//显示窗口
setSize(width,height);//窗口大小
setLocation(100,100);//窗口位置
addWindowListener(new WindowAdapter() {
@Override
public void windowClosing(WindowEvent e) {//关闭窗口
System.exit(0);
}
});
new PointThread().start();
this.addKeyListener(new KeyMonitor());//启动键盘监听
}
@Override
public void paint(Graphics g) {
Color c=g.getColor();//记录画笔原始颜色
g.setColor(Color.gray);
g.fillRect(0,0,width,height);//绘制灰色矩形窗口
t.drawMyself(g);
}
class PointThread extends Thread{
@Override
public void run() {
while (true){
repaint();
try {
Thread.sleep(100);
} catch (InterruptedException e) {
e.printStackTrace();
}
}
}
}
class KeyMonitor extends KeyAdapter{
@Override
public void keyPressed(KeyEvent e) {
t.addDirection(e);
}
@Override
public void keyReleased(KeyEvent e) {
t.minusDirection(e);
}
}
public static void main(String[] args) {
TortoiseCrawl tort=new TortoiseCrawl();
tort.launchFrame();
}
}
乌龟类
import java.awt.*;
import java.awt.event.KeyEvent;
class Tortoise{
int x,y;//定义乌龟原始坐标(其他部位)
int speed;//定义乌龟移动速度
boolean left,right,up,down;
int count=0;
int x1=120,y1=100;//左上脚
int x2=290,y2=100;//右上脚
int x3=120,y3=310;//左下角
int x4=290,y4=310;//右下角
int f=0;//移动变量
public Tortoise(int x, int y, int speed) {
this.x = x;
this.y = y;
this.speed = speed;
}
public void addDirection(KeyEvent e){//监听键盘1
switch(e.getKeyCode()){
case KeyEvent.VK_LEFT:
left=true;
System.out.println(left);
move();
break;
case KeyEvent.VK_RIGHT:
right=true;
move();
break;
case KeyEvent.VK_UP:
up=true;
move();
break;
case KeyEvent.VK_DOWN:
down=true;
move();
break;
}
}
public void minusDirection(KeyEvent e){//监听键盘2
switch(e.getKeyCode()){
case KeyEvent.VK_LEFT:
left=false;
System.out.println(left);
break;
case KeyEvent.VK_RIGHT:
right=false;
break;
case KeyEvent.VK_UP:
up=false;
break;
case KeyEvent.VK_DOWN:
down=false;
break;
}
}
public void move() {//移动方法
count++;
if (left) {
if(x1>x3) {
f = 1;
}else {
f = 0;
}
x-=speed;
if(f == 1) {
x1-=(2*speed);
x4-=(2*speed);
}
if(f == 0) {
x2-=(2*speed);
x3-=(2*speed);
}
}
if (right) {
if(x1<x3) {
f = 1;
}else {
f = 0;
}
x+=speed;
if(f == 1) {
x1+=(2*speed);
x4+=(2*speed);
}
if(f == 0) {
x2+=(2*speed);
x3+=(2*speed);
}
}
if (up) {
if(y1<y2) {
f = 1;
}else {
f = 0;
}
y-=speed;
if(f==0) {
y1-=(2*speed);
y4-=(2*speed);
}if(f==1) {
y2-=(2*speed);
y3-=(2*speed);
}
}
if (down) {
if(y1<y2) {
f = 1;
}else {
f = 0;
}
y+=speed;
if(f == 1) {
y1+=(2*speed);
y4+=(2*speed);
}
if(f == 0) {
y2+=(2*speed);
y3+=(2*speed);
}
}
}
public void drawMyself(Graphics g){
//this.count++;
g.setColor(Color.green);
g.fillOval(x1,y1,70,70);//左上脚
g.fillOval(x2,y2,70,70);//右上脚
g.fillOval(x3,y3,70,70);//左下脚
g.fillOval(x4,y4,70,70);//右下脚
try {
Thread.sleep(10);
} catch (InterruptedException e) {
e.printStackTrace();
}
g.fillOval(205+x,60+y,70,100);//头
if(count%2==0){
g.setColor(Color.getHSBColor(169, 40, 3));//左尾巴
g.fillOval(205+x, y+350, 60, 80);
g.setColor(Color.gray);
g.fillOval(x+225, y+370, 60, 80);
}else{
g.setColor(Color.getHSBColor(169, 40, 3));//右尾巴
g.fillOval(215+x, y+350, 60, 80);
g.setColor(Color.gray);
g.fillOval(x+195, y+370, 60, 80);
}
g.setColor(new Color(45, 215, 166));
g.fillOval(135+x,100+y,210,280);//龟壳1
g.setColor(new Color(88, 187, 53));
g.fillOval(160+x,120+y,165,240);//龟壳2
g.setColor(Color.red);
g.fillOval(230+x,80+y,20,20);//头发
g.setColor(Color.white);
g.fillOval(220+x,70+y,12,12);//左眼
g.fillOval(245+x,70+y,12,12);//右眼
}
}