|
| 1 | +package Games.Snake; |
| 2 | +import javax.swing.*; |
| 3 | +import java.awt.*; |
| 4 | +import java.awt.event.*; |
| 5 | +import java.util.*; |
| 6 | +import java.util.List; |
| 7 | +public class snakeWin extends JPanel implements ActionListener,KeyListener,Runnable{ |
| 8 | + int fenShu=0,Speed=0; |
| 9 | + boolean start = false; |
| 10 | + int rx=0,ry=0; |
| 11 | + int eat1=0,eat2=0; |
| 12 | + JDialog dialog = new JDialog(); |
| 13 | + JLabel label = new JLabel("your score is "+fenShu+"‚ also you died!"); |
| 14 | + JButton ok = new JButton("Try again?"); |
| 15 | + Random r = new Random(); |
| 16 | + JButton newGame,stopGame; |
| 17 | + List<snakeAct> list = new ArrayList<snakeAct>(); |
| 18 | + int temp=0; |
| 19 | + Thread nThread; |
| 20 | + public snakeWin() { |
| 21 | + newGame = new JButton("new game"); |
| 22 | + stopGame = new JButton("exit"); |
| 23 | + newGame.addActionListener(this); |
| 24 | + stopGame.addActionListener(this); |
| 25 | + this.addKeyListener(this); |
| 26 | + this.setLayout(new FlowLayout(FlowLayout.LEFT)); |
| 27 | + this.add(newGame); |
| 28 | + this.add(stopGame); |
| 29 | + dialog.setLayout(new GridLayout(2, 1)); |
| 30 | + dialog.add(label); |
| 31 | + dialog.add(ok); |
| 32 | + dialog.setSize(200, 200); |
| 33 | + dialog.setLocation(200, 200); |
| 34 | + dialog.setVisible(false); |
| 35 | + ok.addActionListener(this); |
| 36 | + } |
| 37 | + public void paintComponent(Graphics g){ |
| 38 | + super.paintComponent(g); |
| 39 | + g.drawRect(10, 40, 400, 300); |
| 40 | + g.drawString("score"+fenShu, 170, 15); |
| 41 | + g.drawString("other"+Speed, 170, 35); |
| 42 | + g.setColor(new Color(0, 255, 0)); |
| 43 | + if(start){ |
| 44 | + g.fillRect(10+rx*10, 40+ry*10, 10, 10); |
| 45 | + for (int i = 0; i < list.size(); i++) { |
| 46 | + g.setColor(new Color(0, 0, 255)); |
| 47 | + g.fillRect(10+list.get(i).getX()*10, 40+list.get(i).getY()*10, 10, 10); |
| 48 | + } |
| 49 | + } |
| 50 | + } |
| 51 | + public void actionPerformed(ActionEvent e) { |
| 52 | + if(e.getSource()==newGame){ |
| 53 | + newGame.setEnabled(false); |
| 54 | + start = true; |
| 55 | + rx=r.nextInt(40);ry=r.nextInt(30); |
| 56 | + snakeAct tempAct = new snakeAct(); |
| 57 | + tempAct.setX(20); |
| 58 | + tempAct.setY(15); |
| 59 | + list.add(tempAct); |
| 60 | + this.requestFocus(); |
| 61 | + nThread = new Thread(this); |
| 62 | + nThread.start(); |
| 63 | + repaint(); |
| 64 | + } |
| 65 | + if(e.getSource()==stopGame){ |
| 66 | + System.exit(0); |
| 67 | + } |
| 68 | + if(e.getSource()==ok){ |
| 69 | + list.clear(); |
| 70 | + start=false; |
| 71 | + newGame.setEnabled(true); |
| 72 | + dialog.setVisible(false); |
| 73 | + fenShu=0; |
| 74 | + Speed=0; |
| 75 | + repaint(); |
| 76 | + } |
| 77 | + } |
| 78 | + private void eat() { |
| 79 | + if (rx==list.get(0).getX()&&ry==list.get(0).getY()) { |
| 80 | + rx = r.nextInt(40);ry = r.nextInt(30); |
| 81 | + snakeAct tempAct = new snakeAct(); |
| 82 | + tempAct.setX(list.get(list.size()-1).getX()); |
| 83 | + tempAct.setY(list.get(list.size()-1).getY()); |
| 84 | + list.add(tempAct); |
| 85 | + fenShu = fenShu+100*Speed+10; |
| 86 | + eat1++; |
| 87 | + if(eat1-eat2>=4){ |
| 88 | + eat2=eat1; |
| 89 | + Speed++; |
| 90 | + } |
| 91 | + } |
| 92 | + } |
| 93 | + public void otherMove(){ |
| 94 | + snakeAct tempAct = new snakeAct(); |
| 95 | + for (int i = 0; i < list.size(); i++) { |
| 96 | + if (i==1) { |
| 97 | + list.get(i).setX(list.get(0).getX()); |
| 98 | + list.get(i).setY(list.get(0).getY()); |
| 99 | + }else if(i>1){ |
| 100 | + tempAct=list.get(i-1); |
| 101 | + list.set(i-1, list.get(i)); |
| 102 | + list.set(i, tempAct); |
| 103 | + } |
| 104 | + |
| 105 | + } |
| 106 | + } |
| 107 | + public void move(int x,int y){ |
| 108 | + if (minYes(x, y)) { |
| 109 | + otherMove(); |
| 110 | + list.get(0).setX(list.get(0).getX()+x); |
| 111 | + list.get(0).setY(list.get(0).getY()+y); |
| 112 | + eat(); |
| 113 | + repaint(); |
| 114 | + }else { |
| 115 | + nThread = null; |
| 116 | + label.setText("your score is "+fenShu+"‚ also you died!"); |
| 117 | + dialog.setVisible(true); |
| 118 | + } |
| 119 | + |
| 120 | + } |
| 121 | + public boolean minYes(int x,int y){ |
| 122 | + if (!maxYes(list.get(0).getX()+x,list.get(0).getY()+ y)) { |
| 123 | + return false; |
| 124 | + } |
| 125 | + return true; |
| 126 | + } |
| 127 | + public boolean maxYes(int x,int y){ |
| 128 | + if (x<0||x>=40||y<0||y>=30) { |
| 129 | + return false; |
| 130 | + } |
| 131 | + for (int i = 0; i < list.size(); i++) { |
| 132 | + if (i>1&&list.get(0).getX()==list.get(i).getX()&&list.get(0).getY()==list.get(i).getY()) { |
| 133 | + return false; |
| 134 | + } |
| 135 | + } |
| 136 | + return true; |
| 137 | + } |
| 138 | + public void keyPressed(KeyEvent e) { |
| 139 | + if(start){ |
| 140 | + switch (e.getKeyCode()) { |
| 141 | + case KeyEvent.VK_UP: |
| 142 | + move(0, -1); |
| 143 | + temp=1; |
| 144 | + break; |
| 145 | + case KeyEvent.VK_DOWN: |
| 146 | + move(0, 1); |
| 147 | + temp=2; |
| 148 | + break; |
| 149 | + case KeyEvent.VK_LEFT: |
| 150 | + move(-1, 0); |
| 151 | + temp=3; |
| 152 | + break; |
| 153 | + case KeyEvent.VK_RIGHT: |
| 154 | + move(1, 0); |
| 155 | + temp=4; |
| 156 | + break; |
| 157 | + |
| 158 | + default: |
| 159 | + break; |
| 160 | + } |
| 161 | + } |
| 162 | + } |
| 163 | + public void keyReleased(KeyEvent e) {} |
| 164 | + public void keyTyped(KeyEvent e) {} |
| 165 | + public void run() { |
| 166 | + while (start) { |
| 167 | + switch (temp) { |
| 168 | + case 1: |
| 169 | + move(0, -1); |
| 170 | + break; |
| 171 | + case 2: |
| 172 | + move(0, 1); |
| 173 | + break; |
| 174 | + case 3: |
| 175 | + move(-1, 0); |
| 176 | + break; |
| 177 | + case 4: |
| 178 | + move(1, 0); |
| 179 | + break; |
| 180 | + default: |
| 181 | + break; |
| 182 | + } |
| 183 | + repaint(); |
| 184 | + try { |
| 185 | + Thread.sleep(300-30*Speed); |
| 186 | + } catch (InterruptedException e) { |
| 187 | + // TODO 自动生�的 catch � |
| 188 | + e.printStackTrace(); |
| 189 | + } |
| 190 | + } |
| 191 | + |
| 192 | + } |
| 193 | + |
| 194 | +} |
0 commit comments