package com.xs;
import java.awt.Color;
import java.awt.Font;
import java.awt.Graphics;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.KeyEvent;
import java.awt.event.KeyListener;
import java.util.Random;
import javax.swing.ImageIcon;
import javax.swing.JPanel;
import javax.swing.Timer;
public class SnakeJpanel extends JPanel implements KeyListener, ActionListener {
ImageIcon up = new ImageIcon(getClass().getResource("/pic/up.png"));
ImageIcon down = new ImageIcon(getClass().getResource("/pic/down.png"));
ImageIcon left = new ImageIcon(getClass().getResource("/pic/left.png"));
ImageIcon right = new ImageIcon(getClass().getResource("/pic/right.png"));
ImageIcon title = new ImageIcon(getClass().getResource("/pic/title.jpg"));
ImageIcon food = new ImageIcon(getClass().getResource("/pic/food.png"));
ImageIcon body = new ImageIcon(getClass().getResource("/pic/body.png"));
int[] sx = new int[200];
int[] sy = new int[200];
int len = 3;
String fx = "R";//R,L,U,D右左上下方向
boolean isStart = false;
boolean isFailed = false;
int time = 100;
Random random = new Random();
int foodx = random.nextInt(34) * 25 + 25;
int foody = random.nextInt(24) * 25 + 75;
Timer timer = new Timer(time, this);
public SnakeJpanel() {
this.setFocusable(true);
this.addKeyListener(this);
timer.start();
this.setUp();
}
public void paint(Graphics g) {
title.paintIcon(this, g, 25, 11);
g.fillRect(25, 75, 850, 600);
//蛇头
if(fx.equals("R")) {
right.paintIcon(this, g, sx[0], sy[0]);
}else if(fx.equals("L")) {
left.paintIcon(this, g, sx[0], sy[0]);
}else if(fx.equals("U")) {
up.paintIcon(this, g, sx[0], sy[0]);
}else if(fx.equals("D")) {
down.paintIcon(this, g, sx[0], sy[0]);
}
//蛇身
for (int i = 0; i < len; i++) {
body.paintIcon(this, g, sx[i], sy[i]);
}
if(!isStart) {
g.setColor(Color.RED);
g.setFont(new Font("arial", Font.BOLD, 30));
g.drawString("Press space to start/pause", 250, 350);
}
if(isFailed) {
g.setColor(Color.RED);
g.setFont(new Font("arial", Font.BOLD, 30));
g.drawString("Score:" + (len-3), 390, 250);
g.drawString("Game Over!Press space to restart", 250, 350);
}
food.paintIcon(this, g, foodx, foody);
}
public void setUp() {
isFailed = false;
isStart = false;
len = 3;
fx = "R";
sx[0] = 100;
sy[0] = 75;
sx[1] = 75;
sy[1] = 75;
sx[2] = 50;
sy[2] = 75;
}
@Override
public void keyPressed(KeyEvent e) {
if(e.getKeyCode() == KeyEvent.VK_SPACE) {
isStart = !isStart;
if(isFailed) {
this.setUp();
}
}else if(e.getKeyCode() == KeyEvent.VK_UP) {
if(sx[0] == sx[1]) {
return;
}
fx = "U";
}else if(e.getKeyCode() == KeyEvent.VK_DOWN) {
if(sx[0] == sx[1]) {
return;
}
fx = "D";
}else if(e.getKeyCode() == KeyEvent.VK_LEFT) {
if(sy[0] == sy[1]) {
return;
}
fx = "L";
}else if(e.getKeyCode() == KeyEvent.VK_RIGHT) {
if(sy[0] == sy[1]) {
return;
}
fx = "R";
}
repaint();
}
@Override
public void actionPerformed(ActionEvent e) {
//再定一个闹钟
timer.start();
if(isStart && !isFailed) {
//移动身体
for (int i = len; i > 0; i--) {
sx[i] = sx[i-1];
sy[i] = sy[i-1];
}
//移动头
if(fx.equals("R")) {
sx[0] = sx[0] + 25;
if(sx[0] > 850) {
isFailed = true;
}
}else if(fx.equals("L")) {
sx[0] = sx[0] - 25;
if(sx[0] < 25) {
isFailed = true;
}
}else if(fx.equals("U")) {
sy[0] = sy[0] - 25;
if(sy[0] < 75) {
isFailed = true;
}
}else if(fx.equals("D")) {
sy[0] = sy[0] + 25;
if(sy[0] > 650){
isFailed = true;
}
}
for (int i = 1; i < len; i++) {
if(sx[0] == sx[i] && sy[0] == sy[i]) {
isFailed = true;
}
}
//吃食物
if(sx[0] == foodx && sy[0] == foody) {
len = len + 1;
foodx = random.nextInt(34) * 25 + 25;
foody = random.nextInt(24) * 25 + 75;
}
}
//重画
repaint();
}
@Override
public void keyReleased(KeyEvent e) {
// TODO 自动生成的方法存根
}
@Override
public void keyTyped(KeyEvent e) {
// TODO 自动生成的方法存根
}
}