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

📄 nancheng2.java

📁 这是个用JAVA编写的贪吃蛇小游戏.希望大家喜欢.
💻 JAVA
字号:
import java.applet.*;
import java.awt.*;
import java.awt.event.*;
import java.net.*;
public class nancheng2 extends Applet 
{

	
	
	
	
	
	
	int m=18,n=22,j=0;URL url1;
	Image image0,image1,image2,image3;
	GridBagLayout g=new GridBagLayout();
	
    GridBagConstraints c=new GridBagConstraints();
	Font font1=new Font(" TimesRoman",Font.BOLD,12);
	Font font2=new Font(" TimesRoman",Font.PLAIN,14);
	Font font3=new Font(" TimesRoman",Font.BOLD,26);
	Label lb=new Label(" 欢迎进入贪吃蛇世界");
	
	Panel p1,p2;Graphics g1,g2;
	
	
	Image image;URL codeBase,theURL;
String url="nancheng.htm";
public void init()
{ setBackground(Color.white);
  this.setLayout(g);
		p1=new Panel();p2=new Panel();
		addlabel(m,n,this,"*");
		url1=getCodeBase();
		image1=getImage(url1,"welcom.gif");
		image0=getImage(url1,"shengdan.gif");
		location(6,6,11,4,p1);
		
		
		
		
		
		
		p1.setLayout(g);p2.setLayout(g);
		addlabel(4,13,p1," ");
		addlabel(10,17,p2," ");
		lb.setFont(font3);lb.setForeground(Color.blue);
	    location2(2,1,8,2,lb,p1);
		g1=p1.getGraphics();g2=p2.getGraphics();

codeBase=getCodeBase();
try{
theURL=new URL(getDocumentBase(),url);
}catch(MalformedURLException e){}; //捕获URL异常
}
public void paint(Graphics g)
{ 
image=getImage(codeBase,"01s.jpg");
g.drawImage(image,100,50,150,150,this);

try{
Thread.sleep(1000);
}catch(InterruptedException e){}
getAppletContext().showDocument(theURL);
}

public void addlabel(int m,int n,Panel p,String str)
	{   int i,j;
		 
		 
		
		for (i=0;i<m;i++)
		 for(j=0;j<n;j++)
		 {   
		   Label label=new Label(str);
		   label.setFont(font1);
		   label.setForeground(Color.pink);
			if (j==n-1)c.gridwidth=GridBagConstraints.REMAINDER;
	       location2(j,i,1,1,label,p);
		   if (i>=1&&i<m-1)j=j+n-2;
	     }
	}
	public void location2(int x,int y,int gx,int gy,Component b,Panel p)
	{
		c.gridx=x;c.gridy=y;
		c.gridwidth=gx;c.gridheight=gy;
		p.add(b);g.setConstraints(b,c);
	}

	public void location(int x,int y,int gx,int gy,Component b)
	{
		c.gridx=x;c.gridy=y;
		c.gridwidth=gx;c.gridheight=gy;
		add(b);g.setConstraints(b,c);
	}





}

⌨️ 快捷键说明

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