⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 frog.java

📁 一个用JAVA编写的"青蛙吃虫"Applet游戏
💻 JAVA
字号:
import java.applet.Applet;
import java.awt.*;
import java.awt.image.*;

public final class frog extends Applet
    implements Runnable
{

    int i;
    int j;
    int k;
    int counter;
    int flyX[];
    int flyY[];
    int flyOldX[];
    int flyOldY[];
    int flyDx[];
    int flyDy[];
    int nearX;
    int nearY;
    int nearNum;
    int eatDx;
    int eatDy;
    int eatState;
    int flyBaseX[];
    int flyBaseY[];
    int tongueX[] = {
        18, 22, 0, 0
    };
    int tongueY[] = {
        27, 28, 0, 0
    };
    double tongue;
    boolean flyOn[];
    int currHead;
    int count;
    final int headList[] = {
        0, 1, 2, 3, 2, 1
    };
    double headcount;
    Image collection;
    Image offImage;
    Image frog;
    Image frogheads[];
    Image legs;
    Image flies[];
    Graphics offGraphics;
    MediaTracker tracker;
    ImageFilter filter;
    Thread updateThread;
    Math m;
    Font disp1;
    Font disp2;
    String str1;
    String str2;
    Color lightBlue;
    Color deepGreen;

    public void init()
    {
        disp1 = new Font("\u5B8B\u4F53", 1, 30);
        disp2 = new Font("\u6977\u4F53", 2, 20);
        offImage = createImage(96, 60);
        offGraphics = offImage.getGraphics();
        tracker = new MediaTracker(this);
        collection = getImage(getCodeBase(), "froggies.gif");
        tracker.addImage(collection, 0);
        try
        {
            tracker.waitForID(0);
        }
        catch(InterruptedException _ex) { }
        frogheads = new Image[8];
        flies = new Image[3];
        filter = new CropImageFilter(0, 0, 96, 60);
        frog = createImage(new FilteredImageSource(collection.getSource(), filter));
        tracker.addImage(frog, 1);
        for(i = 0; i < 4; i++)
        {
            for(j = 0; j < 2; j++)
            {
                filter = new CropImageFilter(j * 48, 60 + i * 40, 48, 40);
                frogheads[i * 2 + j] = createImage(new FilteredImageSource(collection.getSource(), filter));
                tracker.addImage(frogheads[i * 2 + j], 1);
            }

        }

        filter = new CropImageFilter(48, 40, 48, 20);
        legs = createImage(new FilteredImageSource(collection.getSource(), filter));
        tracker.addImage(legs, 1);
        for(j = 0; j < 3; j++)
        {
            filter = new CropImageFilter(j * 13, 220, 13, 8);
            flies[j] = createImage(new FilteredImageSource(collection.getSource(), filter));
            tracker.addImage(flies[j], 1);
        }

        flyX = new int[5];
        flyY = new int[5];
        flyOldX = new int[5];
        flyOldY = new int[5];
        flyDx = new int[5];
        flyDy = new int[5];
        flyBaseX = new int[5];
        flyBaseY = new int[5];
        try
        {
            tracker.waitForID(1);
        }
        catch(InterruptedException _ex) { }
        resize(500, 60);
    }

    public void run()
    {
        while(updateThread != null) 
        {
            try
            {
                Thread.sleep(75L);
            }
            catch(InterruptedException _ex) { }
            counter = (counter + 1) % 30;
            nearX = 500;
            nearY = 0;
            nearNum = -1;
            for(j = 0; j < 5; j++)
            {
                if(flyOn[j])
                {
                    flyOldX[j] = flyX[j];
                    flyOldY[j] = flyY[j];
                    if(counter == j)
                    {
                        flyBaseX[j] = (int)(100D + Math.random() * 400D);
                    }
                    if(counter == j + 10)
                    {
                        flyBaseY[j] = (int)(16D + Math.random() * 19D);
                    }
                    if(flyX[j] < flyBaseX[j] && flyDx[j] < 11)
                    {
                        flyDx[j] += 2;
                    } else
                    if(flyX[j] > flyBaseX[j] && flyDx[j] > -11)
                    {
                        flyDx[j] -= 2;
                    }
                    if(flyY[j] < flyBaseY[j] && flyDy[j] < 7)
                    {
                        flyDy[j] += 2;
                    } else
                    if(flyY[j] > flyBaseY[j] && flyDy[j] > -7)
                    {
                        flyDy[j] -= 2;
                    }
                    flyX[j] += flyDx[j];
                    flyY[j] += flyDy[j];
                    if(flyX[j] < nearX)
                    {
                        nearX = flyX[j];
                        nearY = flyY[j];
                        nearNum = j;
                    }
                } else
                if(Math.random() < 0.01D)
                {
                    flyX[j] = 500;
                    flyY[j] = 25;
                    flyBaseY[j] = 25;
                    flyDx[j] = 0;
                    flyDy[j] = 0;
                    flyOn[j] = true;
                }
            }

            switch(eatState)
            {
            default:
                break;

            case 0: // '\0'
                headcount += 0.0030000000000000001D * (double)(700 - nearX);
                if(headcount > 12D)
                {
                    headcount -= 6D;
                }
                currHead = headList[(int)headcount % 6];
                if(nearX > 96 && nearX < 130 && nearY < 40 && nearY > 6)
                {
                    eatState = 1;
                    eatDx = nearX - 59;
                    eatDy = nearY - 22;
                    tongue = 1.0D;
                    flyOn[nearNum] = false;
                    currHead = 4;
                }
                break;

            case 1: // '\001'
                tongue = tongue - 0.29999999999999999D;
                if(tongue < 0.0D)
                {
                    eatState = 2;
                    headcount = 0.0D;
                    count++;
                    currHead = 5;
                }
                break;

            case 2: // '\002'
                headcount += 0.10000000000000001D;
                if(headcount < 0.59999999999999998D)
                {
                    currHead = 5;
                    break;
                }
                if(headcount < 3.2999999999999998D)
                {
                    currHead = 6 + ((int)(headcount * 2D) & 1);
                    break;
                }
                if(headcount < 4.4000000000000004D)
                {
                    currHead = 5;
                } else
                {
                    eatState = 0;
                }
                break;
            }
            repaint();
        }
    }

    public void start()
    {
        if(updateThread == null)
        {
            updateThread = new Thread(this, "Flies");
            updateThread.start();
        }
    }

    public void stop()
    {
        if(updateThread != null && updateThread.isAlive())
        {
            updateThread.stop();
        }
        updateThread = null;
    }

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

    public void update(Graphics g)
    {
        offGraphics.setColor(lightBlue);
        offGraphics.fillRect(0, 0, 96, 60);
        offGraphics.drawImage(legs, 0, 40, this);
        offGraphics.drawImage(frogheads[currHead], 0, 0, this);
        if(eatState == 1)
        {
            offGraphics.setColor(Color.pink);
            tongueX[2] = (int)(18D + tongue * (double)eatDx);
            tongueY[2] = (int)(30D + tongue * (double)eatDy);
            tongueX[3] = tongueX[2];
            tongueY[3] = (int)(28D + tongue * (double)eatDy);
            offGraphics.fillPolygon(tongueX, tongueY, 4);
            offGraphics.drawPolygon(tongueX, tongueY, 4);
            offGraphics.drawImage(flies[2], (int)(11D + tongue * (double)eatDx), (int)(25D + tongue * (double)eatDy), this);
        }
        g.setColor(deepGreen);
        g.fillRect(0, 60, 600, 60);
        g.setColor(lightBlue);
        g.fillRect(144, 0, 456, 60);
        g.drawImage(offImage, 48, 0, this);
        for(k = 0; k < 5; k++)
        {
            if(flyOn[k])
            {
                g.drawImage(flies[counter & 1], flyX[k], flyY[k], this);
            }
        }

        if((eatState == 2) & (headcount == 0.0D))
        {
            if(count % 5 == 0)
            {
                play(getCodeBase(), "spacemusic.au");
            } else
            {
                play(getCodeBase(), "drip.au");
            }
        }
        g.setColor(Color.red);
        if(count % 5 == 0)
        {
            g.setFont(disp1);
            g.drawString(str1, 100, 30);
        }
        g.setFont(disp2);
        g.drawString(str2 + count, 400, 100);
    }

    public frog()
    {
        flyOn = new boolean[5];
        str1 = "\u54C8\u54C8!";
        str2 = "\u8BA1\u6570:";
        lightBlue = new Color(0, 50, 255);
        deepGreen = new Color(0, 200, 0);
    }
}

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -