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

📄 mainpanel.java

📁 程序包括小型的飞机与导弹游戏的源代码和记事本的源代码
💻 JAVA
字号:
package com.zk1;

import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
import java.util.*;

public class MainPanel extends JPanel
{
  public static final int UP=-2;
  public static final int DOWN=2;
  public static final int LEFT=-1;
  public static final int RIGHT=1;
  private Vector<Point> body=new Vector<Point>();
  private int sign=1;
  private Point headPoint=null;
  private Point nextPoint=null;
  private boolean nextPointSign=true;
  private boolean isContinue=true;
  private boolean isPause=false;
  private MainFrame owner=null;
  private int count=0;
  private int time=100;
  private Thread move=new Thread(){
    public void run()
    {
      while(isContinue)
      {
        while(isPause)
        {
          try
          {
            this.sleep(30);
          }
          catch(Exception ex)
          {
            ex.printStackTrace();
          }
        }
        MainPanel.this.moveBody();
        MainPanel.this.repaint();
        try
        {
          this.sleep(time);
        }
        catch (Exception ex)
        {
          ex.printStackTrace();
        }
      }
    }
  };
  public MainPanel(MainFrame owner)
  {
    this.owner=owner;
    this.setBackground(Color.CYAN);//100,60
    initBody();
    initNextPoint();
    move.start();
  }
  public void setIsPause(boolean isPause)
  {
    this.isPause=isPause;
  }
  private void initBody()
  {
    body.clear();
    for(int i=0;i<10;i++)
    {
      body.add(new Point((8-i)*10,590));
    }
    headPoint=new Point(body.get(0).x,body.get(0).y);
  }
  private void initNextPoint()
  {
    do
    {
      nextPoint=new Point((int)(Math.random()*100)*10,(int)(Math.random()*60)*10);
    }while(body.indexOf(nextPoint)!=-1);
  }
  public int getSign()
  {
    return this.sign;
  }
  public void setSign(int sign)
  {
    if(Math.abs(sign)==Math.abs(this.sign)||headPoint.equals(body.get(0)))
      return;
    headPoint=new Point(body.get(0).x,body.get(0).y);
    this.sign=sign;
  }
  public void paint(Graphics g)
  {
    super.paint(g);
    g.setColor(Color.BLACK);
    paintBody(g);
    if(nextPointSign)
    {
      g.setColor(Color.RED);
      g.fillRect(nextPoint.x,nextPoint.y,10,10);
    }
    nextPointSign=!nextPointSign;
  }
  private void paintBody(Graphics g)
  {
    Iterator<Point> i=body.iterator();
    while(i.hasNext())
    {
      Point p=i.next();
      g.fillRect(p.x,p.y,10,10);
    }
  }
  private void moveBody()
  {
    Point head=new Point(body.get(0).x,body.get(0).y);
    switch(this.sign)
    {
      case MainPanel.UP:
        head.y-=10;
        break;
      case MainPanel.DOWN:
        head.y+=10;
        break;
      case MainPanel.LEFT:
        head.x-=10;
        break;
      case MainPanel.RIGHT:
        head.x+=10;
        break;
    }
    if(head.x*head.y<0||head.x>1000||head.y>600||this.isDead(head))
    {
      this.isContinue=false;
      owner.gameOver();
      JOptionPane.showMessageDialog(this,"Game Over\n 分数:"+count,"贪吃蛇",JOptionPane.ERROR_MESSAGE);
    }
    else
    {
      Point lastPoint=null;
      if(head.equals(nextPoint))
      {
        lastPoint=body.get(body.size()-1);
        initNextPoint();
      }
      for(int i=body.size()-1;i>0;i--)
      {
        body.set(i,body.get(i-1));
      }
      body.set(0, head);
      if(lastPoint!=null)
      {
        body.add(lastPoint);
        count++;
      }
      switch(count)
      {
        case 20:
          time=80;
          break;
        case 50:
          time=50;
          break;
        case 100:
          time=30;
          break;
      }
    }
  }
  private boolean isDead(Point p)
  {
    return body.indexOf(p)!=-1;
  }
}

⌨️ 快捷键说明

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