package snake_game;
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.KeyAdapter;
import java.awt.event.KeyEvent;
import java.util.Random;
import javax.swing.ImageIcon;
import javax.swing.JPanel;
import javax.swing.Timer;
@SuppressWarnings("serial")
public class SnakePanel extends JPanel implements ActionListener {
ImageIcon title = new ImageIcon("images/title.jpg");
ImageIcon body = new ImageIcon("images/body.png");
ImageIcon down = new ImageIcon("images/down.png");
ImageIcon food = new ImageIcon("images/food.png");
ImageIcon left = new ImageIcon("images/left.png");
ImageIcon right = new ImageIcon("images/right.png");
ImageIcon up = new ImageIcon("images/up.png");
int score = 0;
int length = 3;
Random rd = new Random();
int foodx = 20 + 25*rd.nextInt(34);
int foody = 80 + 25*rd.nextInt(24);
int[] snakex = new int[750];
int[] snakey = new int[750];
String direct = "R";
boolean isStart = false ;
boolean isFail = false;
Timer timer = new Timer(100,this);
public SnakePanel() {
this.setFocusable(true);
init();
timer.start();
addKeyListener(new KeyAdapter() {
@Override
public void keyPressed(KeyEvent e) {
if(e.getKeyCode() == KeyEvent.VK_SPACE) {
if(isFail) {
isFail = false;
init();
}else{
isStart = !isStart;
}
}else if(e.getKeyCode() == KeyEvent.VK_UP && isStart) {
direct = "U";
}else if(e.getKeyCode() == KeyEvent.VK_DOWN && isStart) {
direct = "D";
}else if(e.getKeyCode() == KeyEvent.VK_LEFT && isStart) {
direct = "L";
}else if(e.getKeyCode() == KeyEvent.VK_RIGHT && isStart) {
direct = "R";
}
}
});
}
public void init(){
score = 0;
length = 3;
snakex[0] = 95;
snakey[0] = 105;
snakex[1] = 70;
snakey[1] = 105;
snakex[2] = 45;
snakey[2] = 105;
direct = "R";
isStart = false;
isFail = false;
}
public void painnt(Graphics g) {
super.paint(g);
g.drawImage(title.getImage(), 20 , 10 , this);
g.setColor(Color.WHITE);
g.fillRect(20 , 80, 850, 600);
g.setColor(Color.BLACK);
for(int i = 0; i<25; i++) {
g.drawLine(20, 80+i*25, 870, 80+i*25);
}
for(int i = 0; i < 35; i++) {
g.drawLine(20+i*25, 80, 20+i*25, 680);
}
if("R".equals(direct)) {
right.paintIcon(this, g, snakex[0], snakey[0]);
}else if("L".equals(direct)) {
left.paintIcon(this, g, snakex[0], snakey[0]);
}else if("D".equals(direct)) {
down.paintIcon(this, g, snakex[0], snakey[0]);
}else if("U".equals(direct)) {
up.paintIcon(this, g, snakex[0], snakey[0]);
}
for(int i = 1; i < length; i++) {
body.paintIcon(this, g, snakex[i], snakey[i]);
}
if(!isStart) {
g.setFont(new Font("华文行楷",Font.BOLD,40));
g.setColor(Color.blue);
}
right.paintIcon(this, g, snakex[0], snakey[0]);
body.paintIcon(this, g, snakex[1], snakey[1]);
body.paintIcon(this, g, snakex[2], snakey[2]);
g.setFont(new Font("华文行楷",Font.BOLD,40));
g.setColor(Color.BLUE);
g.drawString("按空格开始或暂停...",280,330);
g.setColor(Color.WHITE);
g.setFont(new Font("黑体",Font.BOLD,20));
g.drawString("分数:" + score, 750, 34);
g.drawString("长度" + length , 750, 60);
food.paintIcon(this, g, foodx, foody);
}
@Override
public void actionPerformed(ActionEvent e) {
if(isStart && !isFail) {
for(int i = length ; i > 0;i--) {
snakex[i] = snakex[i-1];
snakey[i] = snakey[i-1];
}
if("U".equals(direct)) {
if(snakey[0] <= 80) {
snakey[0] = 680;
}
snakey[0] = snakey[0] -25;
}else if("D".equals(direct)) {
if(snakey[0] >= 655) {
snakey[0] = 55;
}
snakey[0] = snakey[0] + 25;
}else if("L".equals(direct)) {
if(snakex[0] <= 20) {
snakex[0] = 870;
}
snakex[0] = snakex[0] -25;
}else if("R".equals(direct)) {
if(snakex[0] >= 845) {
snakex[0] = -5;
}
snakex[0] = snakex[0] + 25;
}
if(snakex[0] == foodx && snakey[0] == foody) {
length++;
score++;
foodx = 20 + 25 *rd.nextInt(34);
foody = 80 + 25 *rd.nextInt(24);
}
for(int i = 1 ; i< length ; i++) {
if(snakex[0] == snakex[i] && snakey[0] == snakey[i]) {
isFail = true;
}
}
}
repaint();
}
}
package snake_game;
import javax.swing.JFrame;
@SuppressWarnings("serial")
public class SnakeFrame extends JFrame{
public SnakeFrame() {
//SnakePanel panel = new SnakePanel();
//add(panel);
add(new SnakePanel());
this.setTitle("贪吃蛇大作战");
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
setSize(905,730);
setVisible(true);
}
public static void main (String[] args) {
new SnakeFrame();
}
}