会爬行的小乌龟

一 代码

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()
        {
            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);
        }
    }
}

二 运行

三 改进版——使用了双缓冲技术

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()
        {
            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
    {
        private Image image;

        public void paint(Graphics g)
        {
            drawBufferedImage();
            g.drawImage(image, 0, 0, this);
        }

        private void drawBufferedImage()
        {
            image = createImage(this.getWidth(), this.getHeight());
            Graphics g = image.getGraphics();
            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);
        }

    }
}

 

很高兴回答您的问题!让我们开始使用Python的turtle模块来画一个乌龟爬行。 首先,安装turtle模块: ``` pip install turtle ``` 然后,让我们导入turtle模块并创建一个画布: ``` import turtle turtle.setup(500,500) #设置画布大小 turtle.title("乌龟爬行示例") #设置画布标题 ``` 接下来,我们定义一个函数来绘制乌龟的轨迹: ``` def draw_turtle(): turtle.color("green") turtle.pensize(3) turtle.penup() turtle.goto(-150,0) turtle.pendown() turtle.circle(50) turtle.right(90) turtle.forward(100) turtle.right(90) turtle.forward(200) turtle.right(90) turtle.forward(100) turtle.right(90) turtle.forward(200) ``` 这个函数以50为半径绘制乌龟的头部,并向右绘制一个身体,然后向上绘制一个腿,向左绘制一个尾巴,向下绘制第二个腿,最后返回乌龟头部的位置。 最后,我们调用这个函数来绘制乌龟的轨迹: ``` draw_turtle() ``` 完整代码如下: ``` import turtle turtle.setup(500,500) #设置画布大小 turtle.title("乌龟爬行示例") #设置画布标题 def draw_turtle(): turtle.color("green") turtle.pensize(3) turtle.penup() turtle.goto(-150,0) turtle.pendown() turtle.circle(50) turtle.right(90) turtle.forward(100) turtle.right(90) turtle.forward(200) turtle.right(90) turtle.forward(100) turtle.right(90) turtle.forward(200) draw_turtle() ``` 希望这个示例可以帮助您理解如何使用turtle模块来画出乌龟爬行的轨迹!
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值