import javax.swing.*;
import java.awt.*;
import java.awt.event.*;
import java.awt.geom.*;
/**
*
* @author Administrator
*/
public class Snaker {
/**
* @param args the command line arguments
*/
public static void main(String[] args) {
Myframe frame=new Myframe();
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.setVisible(true);
// TODO code application logic here
}
}
class Myframe extends JFrame {
Mypanel mypanel=new Mypanel();
public Myframe(){//界面设置;
super("贪吃蛇游戏");
setLayout(new BorderLayout());//设置布局管理器;
JButton button=new JButton("开始游戏");
JLabel label=new JLabel("点击开始按Tab键开始游戏");//为什么要按Tab键呢?
JPanel j=new JPanel();
j.add(button);
j.add(label);
add(j,BorderLayout.NORTH);
add(mypanel,BorderLayout.CENTER);
this.setSize(400, 400);//设置窗口大小;
this.setLocation(200, 200);//设置位置;
button.addActionListener(new ActionListener(){
public void actionPerformed(ActionEvent e){
mypanel.thread.start();//button事件;
}
});
}
}
class Mypanel extends JPanel implements Runnable{
int x=100,y=100,dir=0,num=0,flag=1;
Thread thread;//定义线程;
Rectangle2D[] snake=new Rectangle2D[10];//2D矩形数组;
Rectangle2D[] food=new Rectangle2D[10];
public Mypanel(){
setLayout(null);
setFocusable(true);
this.addKeyListener(new KeyAdapter(){
@Override
public void keyPressed(KeyEvent e){
int k=e.getKeyCode();
if(k==KeyEvent.VK_DOWN){dir=0;}
if(k==KeyEvent.VK_UP){dir=1;}
if(k==KeyEvent.VK_RIGHT){dir=2;}
if(k==KeyEvent.VK_LEFT){dir=3;}
}//键盘事件;
});
thread =new Thread(this);//实例化线程;
for(int i=1;i<10;i++){
snake[i]=new Rectangle2D.Double();//实例化蛇身;
}
snake[0]=new Rectangle2D.Double(100,100,10,10);
for(int i=0;i<10;i++){
food[i]=new Rectangle2D.Double(i*40+5, i*20+5, 10 , 10);//实例化食物;
}
setBackground(Color.WHITE);
}
public void run(){
while(true){
if(dir==0){
try{
snake[0].setFrame(x, y=y+5, 10, 10); //向下移动;
repaint();//重绘;
if(x==food[num].getX()&&y==food[num].getY()){
num++;
}
Thread.sleep(100);//停止0.1秒;
}
catch(Exception e){System.out.println("抛出异常");}
}
else if(dir==1){
try{
snake[0].setFrame(x, y=y-5, 10, 10);
repaint();
if(x==food[num].getX()&&y==food[num].getY()){
num++;
}
Thread.sleep(100);
}
catch(Exception e){
System.out.println("抛出异常");
}
}
else if(dir==2){
try{
snake[0].setFrame(x=x+5, y, 10, 10);
repaint();
if(x==food[num].getX()&&y==food[num].getY()){
num++;
}
Thread.sleep(100);
}
catch(Exception e){}
}
else if(dir==3){
try{
snake[0].setFrame(x=x-5, y, 10, 10);
repaint();
if(x==food[num].getX()&&y==food[num].getY()){
num++;
}
Thread.sleep(100);
}
catch(Exception e){}
}
for(int i=(num<10?num:9);i>0;i--){
snake[i].setFrame(snake[i-1].getX(), snake[i-1].getY(), 10, 10);
}
if(num==10){
flag=0;
repaint();
break;
}
}
}
@Override
public void paintComponent(Graphics g){
super.paintComponent(g);//不明白为什么去掉此句就不能刷新了;
Graphics2D g2=(Graphics2D)g;
g2.draw(snake[0]);
g2.setColor(Color.red);
g2.fill(snake[0]);
g2.setColor(Color.yellow);
for(int i=(num<10?num:9);i>0;i--){
g2.draw(snake[i]);
g2.fill(snake[i]);
}
g2.setColor(Color.blue);
if(num<10){
g2.draw(food[num]);
g2.fill(food[num]);
}
if(flag==0){
g2.drawString("~~~你真NB!!~~~~ 通关了, 哈哈!!", 150, 200);
}
}
}