会爬行的小乌龟

Posted hyhy904

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了会爬行的小乌龟相关的知识,希望对你有一定的参考价值。

一 代码

import java.awt.*;
import java.awt.event.*;
public class DrawTurtle
{
private int x, y;

public static void main(String[] args)
{
new DrawTurtle();
}

public DrawTurtle()
{
x = 100;
y = 10;
Frame frame = new Frame("DrawTurtle");
DrawLittleTurtle turtle = new DrawLittleTurtle();
frame.add(turtle);
frame.setSize(500, 500);
frame.setVisible(true);
frame.addWindowListener(new WindowAdapter()
{
public void windowClosing(WindowEvent e)
{
System.exit(0);
}
});
turtle.requestFocus();
turtle.addKeyListener(new KeyAdapter(http://www.amjmh.com/v/BIBRGZ_558768/)
{
public void keyPressed(KeyEvent e)
{
if (e.getKeyCode() == KeyEvent.VK_UP)
{
y -= 10;
}
if (e.getKeyCode() == KeyEvent.VK_LEFT)
{
x -= 10;
}
if (e.getKeyCode() == KeyEvent.VK_RIGHT)
{
x += 10;
}
if (e.getKeyCode() == KeyEvent.VK_DOWN)
{
y += 10;
}
turtle.repaint();
}
});
}

class DrawLittleTurtle extends Canvas
{
public void paint(Graphics g)
{
g.setColor(Color.YELLOW); // 乌龟四条腿
g.fillOval(x + 0, y + 40, 30, 30);
g.fillOval(x + 90, y + 40, 30, 30);
g.fillOval(x + 0, y + 110, 30, 30);
g.fillOval(x + 90, y + 110, 30, 30);
g.fillOval(x + 50, y + 130, 20, 50); // 乌龟尾巴
g.fillOval(x + 40, y + 0, 40, 70); // 乌龟头
g.setColor(Color.BLACK);
g.fillOval(x + 50, y + 15, 5, 5);
g.fillOval(x + 65, y + 15, 5, 5);
g.setColor(Color.GREEN); // 乌龟壳
g.fillOval(x + 10, y + 30, 100, 120);
g.setColor(Color.BLACK);
g.drawLine(x + 24, y + 50, x + 40, y + 67);
g.drawLine(x + 97, y + 50, x + 80, y + 67);
g.drawLine(x + 24, y + 130, x + 40, y + 113);
g.drawLine(x + 97, y + 130, x + 80, y + 113);
g.drawLine(x + 40, y + 67, x + 80, y + 67);
g.drawLine(x + 40, y + 113, x + 80, y + 113);
g.drawLine(x + 10, y + 90, x + 110, y + 90);
g.drawLine(x + 60, y + 30, x + 60, y + 150);
}
}
}

以上是关于会爬行的小乌龟的主要内容,如果未能解决你的问题,请参考以下文章

NOIP2013 乌龟棋

[Noip2010]乌龟棋

洛谷 P1541 乌龟棋

题解 P1541 乌龟棋总结

luogu P1541 乌龟棋

NOIP2010乌龟棋[DP 多维状态]