贪吃蛇练习
public class MyData {
public static URL headerUrl = MyData.class.getResource("statics/header.png");
public static URL bodyUrl = MyData.class.getResource("statics/body.png");
public static URL foodUrl = MyData.class.getResource("statics/food.png");
public static URL upUrl = MyData.class.getResource("statics/up.png");
public static URL downUrl = MyData.class.getResource("statics/down.png");
public static URL leftUrl = MyData.class.getResource("statics/left.png");
public static URL rightUrl = MyData.class.getResource("statics/right.png");
public static ImageIcon header = new ImageIcon(headerUrl);
public static ImageIcon body = new ImageIcon(bodyUrl);
public static ImageIcon food = new ImageIcon(foodUrl);
public static ImageIcon up = new ImageIcon(upUrl);
public static ImageIcon down = new ImageIcon(downUrl);
public static ImageIcon left = new ImageIcon(leftUrl);
public static ImageIcon right = new ImageIcon(rightUrl);
}
public class GamePanel extends JPanel implements KeyListener, ActionListener {
int length;
int[] snakeX = new int[600];
int[] snakeY = new int[600];
String direction;
boolean isStart;
int foodX;
int foodY;
Random random = new Random();
boolean isFail;
int score;
Timer timer = new Timer(100,this);
public GamePanel(){
init();
this.setFocusable(true);
this.addKeyListener(this);
}
public void init(){
length = 3;
direction = "R";
isStart = false;
snakeX[0] = 100;snakeY[0] = 75;
snakeX[1] = 75;snakeY[1] = 75;
snakeX[2] = 50;snakeY[2] = 75;
timer.start();
isFail = false;
score = 0;
foodX = 25+25*random.nextInt(34);
foodY = 25+25*random.nextInt(30);
}
@Override
protected void paintComponent(Graphics g) {
super.paintComponent(g);
this.setBackground(Color.white);
g.fillRect(25,50,850,750);
MyData.food.paintIcon(this,g,foodX,foodY);
g.setColor(Color.RED);
g.setFont(new Font("微软雅黑",Font.BOLD,25));
g.drawString("长度:"+length,100,33);
g.drawString("成绩:"+score,670,33);
if (direction.equals("R")){
MyData.right.paintIcon(this,g,snakeX[0],snakeY[0]);
}else if (direction.equals("L")){
MyData.left.paintIcon(this,g,snakeX[0],snakeY[0]);
}else if (direction.equals("U")){
MyData.up.paintIcon(this,g,snakeX[0],snakeY[0]);
}else {
MyData.down.paintIcon(this,g,snakeX[0],snakeY[0]);
}
for (int i = 1; i < length; i++) {
MyData.body.paintIcon(this,g,snakeX[i],snakeY[i]);
}
if (isStart==false){
g.setColor(Color.white);
g.setFont(new Font("微软雅黑",Font.BOLD,40));
g.drawString("按下空格开始游戏!",300,380);
}
if (isFail==true){
g.setColor(Color.red);
g.setFont(new Font("微软雅黑",Font.BOLD,40));
g.drawString("失败!按下空格重新开始游戏!",200,380);
}
}
@Override
public void keyPressed(KeyEvent e) {
int keyCode = e.getKeyCode();
if (keyCode == KeyEvent.VK_SPACE){
if (isFail){
isFail=false;
init();
}else {
isStart = !isStart;
}
repaint();
}
if (keyCode == KeyEvent.VK_UP){
direction = "U";
}else if (keyCode == KeyEvent.VK_DOWN){
direction = "D";
}else if (keyCode == KeyEvent.VK_LEFT){
direction = "L";
}else if (keyCode == KeyEvent.VK_RIGHT){
direction = "R";
}
}
@Override
public void actionPerformed(ActionEvent e) {
if (isStart && isFail==false){
if (snakeX[0] == foodX && snakeY[0] == foodY){
length++;
score = score+10;
foodX = 25+25*random.nextInt(34);
foodY = 25+25*random.nextInt(30);
}
for (int i = length-1; i > 0; i--) {
snakeX[i] = snakeX[i-1];
snakeY[i] = snakeY[i-1];
}
if (direction.equals("R")){
snakeX[0] = snakeX[0]+25;
}else if (direction.equals("L")){
snakeX[0] = snakeX[0]-25;
}else if (direction.equals("U")){
snakeY[0] = snakeY[0]-25;
}else if (direction.equals("D")){
snakeY[0] = snakeY[0]+25;
}
if (snakeX[0] > 850){
isFail = true;
}else if (snakeX[0] < 25){
isFail = true;
}
if (snakeY[0] > 775){
isFail = true;
}else if (snakeY[0] < 50){
isFail = true;
}
for (int i = 1; i < length; i++) {
if (snakeX[0] == snakeX[i] && snakeY[0] == snakeY[i]){
isFail = true;
}
}
repaint();
}
timer.start();
}
@Override
public void keyTyped(KeyEvent e) {}
@Override
public void keyReleased(KeyEvent e) {}
}
public class StartGame {
public static void main(String[] args) {
JFrame jFrame = new JFrame();
GamePanel gamePanel = new GamePanel();
jFrame.setTitle("小小贪吃蛇");
jFrame.add(gamePanel);
jFrame.setResizable(false);
jFrame.setBounds(0,0,900,850);
jFrame.setDefaultCloseOperation(WindowConstants.EXIT_ON_CLOSE);
jFrame.setVisible(true);
}
}