📄 snakepanel.java
字号:
import javax.swing.*;import java.awt.*;import java.awt.event.*;import java.util.*;public class SnakePanel extends JPanel implements Runnable,KeyListener{ public SnakePanel(SnakeFrame parent){ this.parent=parent; grids=new JPanel[row][col]; others=new LinkedList(); snakeBody=new LinkedList(); snakeHead=new ArrayIndexReadOnly(0,0); tempBlock=new ArrayIndexReadOnly(0,0); direction=new ArrayIndex(0,1); setLayout(new GridLayout(row,col,1,1)); for(int i=0;i<row;i++){ for(int j=0;j<col;j++){ grids[i][j]=new JPanel(); grids[i][j].setBackground(othersColor); add(grids[i][j]); } } addKeyListener(this); } public void newGame(long speedTime){ this.speedTime=speedTime; if (gameHaveExit) { snake.initializeV(); }else{ //System.out.println("gameHaveExit=false"); snake=new SnakeModel(row,col); gameHaveExit=true; t=new Thread(this); t.start(); } requestFocus(); direction.setX(0); direction.setY(1); gameScore=0; updateTextFiled(""+gameScore); isEnd=false; } public void stopGame(){ requestFocus(); isEnd=true; } public void resumeGame(){ requestFocus(); isEnd=false; } public int getGameScore(){ return gameScore; } private void updateTextFiled(String str){ ((SnakeFrame)parent).scoreField.setText(str); } private void updateBg(){ snakeBody=snake.getSnake(); Iterator e =snakeBody.iterator(); while(e.hasNext()){ ArrayIndexReadOnly t=(ArrayIndexReadOnly)(e.next()); grids[t.getX()][t.getY()].setBackground(bodyColor); } //设定蛇头颜色 snakeHead=snake.getSnakeHead(); grids[snakeHead.getX()][snakeHead.getY()].setBackground(headColor); //设定背景颜色 others=snake.getOthers(); e =others.iterator(); while(e.hasNext()){ ArrayIndexReadOnly t=(ArrayIndexReadOnly)(e.next()); grids[t.getX()][t.getY()].setBackground(othersColor); } //设定临时块的颜色 tempBlock=snake.getTempBlock(); grids[tempBlock.getX()][tempBlock.getY()].setBackground(tempColor); } // allow panel to get input focus public boolean isFocusTraversable() { return true; } //实现Runnable接口 public void run(){ while(true){ try{ Thread.sleep(speedTime); }catch (InterruptedException e){} if(!isEnd){ isEnd=!snake.move(direction); updateBg(); if(snake.getReadyAddScore()){ gameScore+=1; updateTextFiled(""+gameScore); } } } } //实现KeyListener接口;;;;;;;;;;;;{ public void keyPressed(KeyEvent event) { int keyCode = event.getKeyCode(); if(gameHaveExit){ if (keyCode == KeyEvent.VK_LEFT) { direction.setX(0); direction.setY(-1); } else if (keyCode == KeyEvent.VK_RIGHT) { direction.setX(0); direction.setY(1); } else if (keyCode == KeyEvent.VK_UP) { direction.setX(-1); direction.setY(0); } else if (keyCode == KeyEvent.VK_DOWN) { direction.setX(1); direction.setY(0); } else if (keyCode == KeyEvent.VK_ENTER){ isEnd=!isEnd; } } } public void keyReleased(KeyEvent event){} public void keyTyped(KeyEvent event){} //实现KeyListener接口;;;;;;;;;;;;} //数据成员 JFrame parent=new JFrame(); private int row=15; private int col=25; private JPanel[][] grids; private ArrayIndex direction; private SnakeModel snake; private LinkedList snakeBody; private LinkedList others; private ArrayIndexReadOnly snakeHead; private ArrayIndexReadOnly tempBlock; private long speedTime; private Color bodyColor=Color.red; private Color headColor=Color.green; private Color tempColor=Color.orange; private Color othersColor=Color.blue; private int gameScore=0; private boolean readyAddScore; private Thread t; private boolean isEnd; private static boolean gameHaveExit;}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -