A股上市公司传智教育(股票代码 003032)旗下技术交流社区北京昌平校区

 找回密码
 加入黑马

QQ登录

只需一步,快速开始

© lovefmylgs 中级黑马   /  2014-5-2 08:28  /  2355 人查看  /  5 人回复  /   0 人收藏 转载请遵从CC协议 禁止商业使用本文


package Snake;
002
 
003
import java.awt.Color;
004
import java.awt.Graphics;
005
import java.awt.Image;
006
import java.awt.event.ActionEvent;
007
import java.awt.event.ActionListener;
008
import java.awt.event.KeyEvent;
009
import java.awt.event.KeyListener;
010
import java.awt.event.MouseEvent;
011
import java.awt.event.MouseListener;
012
import java.io.File;
013
import java.io.IOException;
014
import java.text.BreakIterator;
015
 
016
import javax.crypto.spec.IvParameterSpec;
017
import javax.imageio.ImageIO;
018
import javax.swing.JButton;
019
import javax.swing.JPanel;
020
import javax.swing.text.html.CSS;
021
 
022
public class ShellWin extends JPanel implements ActionListener,Runnable,KeyListener{
023
    JButton startGame=new JButton("开始");
024
    JButton stopGame=new JButton("停止");
025
    int fang;
026
    int chang;
027
    int fen=0;
028
    int[] xs=new int[400];
029
    int[] ys=new int[400];
030
    int douX;int douY;
031
    Thread nThread;
032
    public ShellWin()
033
    {
034
        nThread=new Thread(this);
035
       this.add(startGame);this.add(stopGame);
036
       this.startGame.addActionListener(this);
037
       this.addKeyListener(this);
038
       造蛇();
039
       造豆();
040
    }
041
    public void paintComponent(Graphics g)
042
    {
043
        super.paintComponent(g);
044
        g.drawString("分数:"+fen, 50, 50);
045
        g.drawRect(100,100, 600, 600);
046
        显示蛇(g);
047
        显示豆(g);
048
    }
049
    @Override
050
    public void actionPerformed(ActionEvent e) {
051
        if(e.getSource()==startGame)
052
        {
053
            this.requestFocus();
054
            nThread.start();
055
           this.repaint(); 
056
        }
057
    }
058
    //能不能吃豆,能不能死
059
    void 死()
060
    {
061
        System.out.println("死了!");
062
    }
063
    void 吃()
064
    {
065
        chang++;
066
        this.fen+=100;
067
        造豆();
068
    }
069
    boolean 能出界死吗()
070
    {
071
        if(xs[0]==70||xs[0]==700||ys[0]==70||ys[0]==700)
072
        {
073
            return true;
074
        }
075
        return false;
076
    }
077
    void 转向(int 新方向)
078
    {
079
      if(fang%2!=新方向%2)
080
      {
081
          fang=新方向;
082
      }
083
    }
084
    boolean 吃自己了吗()
085
    {
086
        for (int i = 1; i < chang; i++) {
087
            if(xs[0]==xs[i]&&ys[0]==ys[i])
088
            {
089
                return true;
090
            }
091
        }
092
        return false;
093
    }
094
    boolean 能吃豆吗()
095
    {
096
        if(xs[0]==douX&&ys[0]==douY)
097
        {
098
            return true;
099
        }
100
        return false;
101
    }
102
    void 造蛇()
103
    {
104
        fang=2;chang=5;
105
        for (int i = 0; i < chang; i++) {
106
            xs[i]=400;
107
            ys[i]=400+30*i;
108
        }
109
    }
110
    void 造豆()
111
    {
112
       douX=30*(int)(Math.random()*19)+100;
113
       douY=30*(int)(Math.random()*19)+100;
114
       for (int i = 0; i < chang; i++) {
115
           if(xs[i]==douX&&ys[i]==douY)
116
           {
117
               造豆();
118
               return;
119
           }
120
       }
121
    }
122
    void 显示豆(Graphics g)
123
    {
124
        g.setColor(Color.RED);
125
        g.fillOval(douX, douY, 30, 30);
126
    }
127
    void 爬()
128
    {
129
        for (int i = chang-1; i >0; i--) {
130
            xs[i]=xs[i-1];
131
            ys[i]=ys[i-1];
132
        }
133
        switch (fang) {
134
        case 1:
135
            xs[0]=xs[0]+30;
136
            break;
137
        case 2:
138
            ys[0]=ys[0]-30;
139
            break;
140
        case 3:
141
            xs[0]=xs[0]-30;
142
            break;
143
        case 4:
144
            ys[0]=ys[0]+30;
145
            break;
146
        default:
147
            break;
148
        }
149
    }
150
    void 显示蛇(Graphics g)
151
    {
152
        for (int i = 0; i < chang; i++) {
153
            g.setColor(Color.BLUE);
154
            g.drawRect(xs[i], ys[i], 30, 30);
155
        }
156
    }
157
    @Override
158
    public void run() {
159
        while(true)
160
        {
161
            try {              
162
                if(能吃豆吗())
163
                {
164
                   吃();
165
                }
166
                if(能出界死吗())
167
                {
168
                    死();
169
                    return;
170
                }
171
                if(吃自己了吗())
172
                {
173
                    死();
174
                    return;
175
                }
176
                爬();
177
                Thread.sleep(80);
178
                repaint();
179
            } catch (InterruptedException e) {
180
                e.printStackTrace();
181
            }
182
        }
183
    }
184
    void 转向()
185
    {
186
         
187
    }
188
    @Override
189
    public void keyPressed(KeyEvent e) {
190
        switch (e.getKeyCode()) {
191
        case KeyEvent.VK_UP:
192
            转向(2);
193
            break;
194
        case KeyEvent.VK_DOWN:
195
            转向(4);
196
            break;
197
        case KeyEvent.VK_LEFT:
198
            转向(3);
199
            break;
200
        case KeyEvent.VK_RIGHT:
201
            转向(1);
202
            break;
203
        default:
204
            break;
205
        }
206
    }
207
    @Override
208
    public void keyReleased(KeyEvent e) {
209
        // TODO Auto-generated method stub
210
         
211
    }
212
    @Override
213
    public void keyTyped(KeyEvent e) {
214
        // TODO Auto-generated method stub
215
         
216
    }
217
}

5 个回复

倒序浏览
天道酬勤_||淡腾 来自手机 中级黑马 2014-5-16 07:36:17
沙发
谢谢分享
回复 使用道具 举报
为何代码是以这种霸气的方式贴出来的  - -
回复 使用道具 举报
好东西哇:D
回复 使用道具 举报
        额,这贴代码的时候能整理一下吗
回复 使用道具 举报
这么经典的游戏源码 ,谢谢分享
回复 使用道具 举报
您需要登录后才可以回帖 登录 | 加入黑马