【Java】GUI实现贪吃蛇
前言
我们在做这个小游戏之前,得确保自己的AWT和Swing有一定的基础,并且会写一些简单的逻辑操作。这些都会在后面写的时候体现出来。
狂神老师从这里开始讲贪吃蛇的
我们在看视频的时候都知道,视频是一帧一帧播放的,一般我们看动画的帧率是每秒24帧。
我们在GUI中实现帧率的,通过不断刷新实现
此外,我们要通过键盘监听来监听键盘的上下左右操作来控制小蛇的移动
还需要通过定时器Timer来实现
文章中的贪吃蛇代码源码已经给出了,这里还有一份我自己构建的jar包,下载地址
1、构建静态页面
效果如下:
我们创建一个游戏启动类,一个图片资源类,一个面板类
其中游戏启动类用于启动这个JFrame,图片资源就是用来加载图片资源的,面板就是用来放顶部的背景和小蛇存在的区域的
-
游戏启动类 package top.woodwhale.snake;
import javax.swing.*;
public class StartGame {
public static void main(String[] args) {
JFrame frame = new JFrame();
frame.setBounds(10,10,900,720);
frame.setResizable(false);
frame.setDefaultCloseOperation(WindowConstants.EXIT_ON_CLOSE);
frame.add(new GamePanel());
frame.setVisible(true);
}
}
-
资源类 package top.woodwhale.snake;
import javax.swing.*;
import java.net.URL;
public class Data {
public static URL headerURL = Data.class.getResource("statics/img/header.png");
public static ImageIcon header = new ImageIcon(headerURL);
public static URL upRUL = Data.class.getResource("statics/img/up.png");
public static ImageIcon up = new ImageIcon(upRUL);
public static URL downURL = Data.class.getResource("statics/img/down.png");
public static ImageIcon down = new ImageIcon(downURL);
public static URL leftURL = Data.class.getResource("statics/img/left.png");
public static ImageIcon left = new ImageIcon(leftURL);
public static URL rightURL = Data.class.getResource("statics/img/right.png");
public static ImageIcon right = new ImageIcon(rightURL);
public static URL bodyURL = Data.class.getResource("statics/img/body.png");
public static ImageIcon body = new ImageIcon(bodyURL);
public static URL foodURL = Data.class.getResource("statics/img/food.png");
public static ImageIcon food = new ImageIcon(foodURL);
}
-
游戏面板类 package top.woodwhale.snake;
import javax.swing.*;
import java.awt.*;
public class GamePanel extends JPanel {
@Override
public void paintComponent(Graphics g) {
super.paintComponent(g);
this.setBackground(Color.white);
Data.header.paintIcon(this,g,25,11);
g.fillRect(25,75,850,600);
}
}
2、构建小蛇位置
这一部分,我们在GamePanel类中添加了一些小蛇的信息,来绘制小蛇,并且对小蛇的属性进行定义。
效果如下:
代码:
package top.woodwhale.snake;
import javax.swing.*;
import java.awt.*;
public class GamePanel extends JPanel {
int length;
int[] snakeX = new int[600];
int[] snakeY = new int[600];
String dictionary;
boolean isStart;
public GamePanel() {
init();
}
public void init() {
length = 3;
snakeX[0] = 100;snakeY[0] = 100;
snakeX[1] = 75;snakeY[1] = 100;
snakeX[2] = 50;snakeY[2] = 100;
dictionary = "R";
isStart = false;
}
@Override
public void paintComponent(Graphics g) {
super.paintComponent(g);
this.setBackground(Color.white);
Data.header.paintIcon(this,g,25,11);
g.fillRect(25,75,850,600);
if (dictionary.equals("R")) {
Data.right.paintIcon(this,g,snakeX[0],snakeY[0]);
} else if (dictionary.equals("L")) {
Data.left.paintIcon(this,g,snakeX[0],snakeY[0]);
} else if (dictionary.equals("U")) {
Data.up.paintIcon(this,g,snakeX[0],snakeY[0]);
} else {
Data.down.paintIcon(this,g,snakeX[0],snakeY[0]);
}
for (int i = 1; i < length; i++) {
Data.body.paintIcon(this,g,snakeX[i],snakeY[i]);
}
if (!isStart) {
g.setColor(Color.white);
g.setFont(new Font("Chilanka",Font.BOLD,40));
g.drawString("Press Space To Start!",250,400);
}
}
}
3、监听键盘操作
我们仍然是在GamePanel类中实现键盘监听操作,我们直接让这个类使用KeyListener接口,重写三个键盘监听方法就可以实现键盘监听了
我们需要第一步做的就是按下空格开始/暂停,使用键盘监听空格键,如果按下了空格,那么我们就让游戏状态取反
package top.woodwhale.snake;
import javax.swing.*;
import java.awt.*;
import java.awt.event.KeyEvent;
import java.awt.event.KeyListener;
public class GamePanel extends JPanel implements KeyListener {
int length;
int[] snakeX = new int[600];
int[] snakeY = new int[600];
String dictionary;
boolean isStart;
public GamePanel() {
init();
this.setFocusable(true);
this.addKeyListener(this);
}
public void init() {
length = 3;
snakeX[0] = 100;snakeY[0] = 100;
snakeX[1] = 75;snakeY[1] = 100;
snakeX[2] = 50;snakeY[2] = 100;
dictionary = "R";
isStart = false;
}
@Override
public void paintComponent(Graphics g) {
super.paintComponent(g);
this.setBackground(Color.white);
Data.header.paintIcon(this,g,25,11);
g.fillRect(25,75,850,600);
if (dictionary.equals("R")) {
Data.right.paintIcon(this,g,snakeX[0],snakeY[0]);
} else if (dictionary.equals("L")) {
Data.left.paintIcon(this,g,snakeX[0],snakeY[0]);
} else if (dictionary.equals("U")) {
Data.up.paintIcon(this,g,snakeX[0],snakeY[0]);
} else {
Data.down.paintIcon(this,g,snakeX[0],snakeY[0]);
}
for (int i = 1; i < length; i++) {
Data.body.paintIcon(this,g,snakeX[i],snakeY[i]);
}
if (!isStart) {
g.setColor(Color.white);
g.setFont(new Font("Chilanka",Font.BOLD,40));
g.drawString("Press Space To Start!",250,400);
}
}
@Override
public void keyTyped(KeyEvent keyEvent) {
}
@Override
public void keyPressed(KeyEvent keyEvent) {
int keyCode = keyEvent.getKeyCode();
if (keyCode == KeyEvent.VK_SPACE) {
isStart = !isStart;
repaint();
}
}
@Override
public void keyReleased(KeyEvent keyEvent) {
}
}
4、定时器使小蛇移动
在这一节中,我们要通过Timer类实现定时器,不断刷新、重画,让小蛇动起来,并且给小蛇监听键盘的上下左右
package top.woodwhale.snake;
import javax.swing.*;
import java.awt.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.KeyEvent;
import java.awt.event.KeyListener;
public class GamePanel extends JPanel implements KeyListener, ActionListener {
int length;
int[] snakeX = new int[600];
int[] snakeY = new int[600];
String dictionary;
boolean isStart;
int preX;
int preY;
Timer timer;
public GamePanel() {
init();
this.setFocusable(true);
this.addKeyListener(this);
}
public void init() {
length = 3;
snakeX[0] = 100;snakeY[0] = 100;
snakeX[1] = 75;snakeY[1] = 100;
snakeX[2] = 50;snakeY[2] = 100;
preX = snakeX[0];preY = snakeY[0];
dictionary = "R";
isStart = false;
timer = new Timer(150,this);
timer.start();
}
@Override
public void paintComponent(Graphics g) {
super.paintComponent(g);
this.setBackground(Color.white);
Data.header.paintIcon(this,g,25,11);
g.fillRect(25,75,850,600);
switch (dictionary) {
case "R":
Data.right.paintIcon(this, g, snakeX[0], snakeY[0]);
break;
case "L":
Data.left.paintIcon(this, g, snakeX[0], snakeY[0]);
break;
case "U":
Data.up.paintIcon(this, g, snakeX[0], snakeY[0]);
break;
case "D":
Data.down.paintIcon(this, g, snakeX[0], snakeY[0]);
break;
}
for (int i = 1; i < length; i++) {
Data.body.paintIcon(this,g,snakeX[i],snakeY[i]);
}
if (!isStart) {
g.setColor(Color.white);
g.setFont(new Font("Chilanka",Font.BOLD,40));
g.drawString("Press Space To Start!",250,400);
}
}
@Override
public void keyTyped(KeyEvent keyEvent) {
}
@Override
public void keyPressed(KeyEvent keyEvent) {
int keyCode = keyEvent.getKeyCode();
if (keyCode == KeyEvent.VK_SPACE) {
isStart = !isStart;
repaint();
}
if (Math.abs(preX - snakeX[0]) >= 25 || Math.abs(preY - snakeY[0]) >= 25) {
switch (keyCode) {
case KeyEvent.VK_UP:
if (!dictionary.equals("D")) {
preX = snakeX[0];
preY = snakeY[0];
dictionary = "U";
}
break;
case KeyEvent.VK_DOWN:
if (!dictionary.equals("U")) {
preX = snakeX[0];
preY = snakeY[0];
dictionary = "D";
}
break;
case KeyEvent.VK_LEFT:
if (!dictionary.equals("R")) {
preX = snakeX[0];
preY = snakeY[0];
dictionary = "L";
}
break;
case KeyEvent.VK_RIGHT:
if (!dictionary.equals("L")) {
preX = snakeX[0];
preY = snakeY[0];
dictionary = "R";
}
break;
}
}
}
@Override
public void keyReleased(KeyEvent keyEvent) {
}
@Override
public void actionPerformed(ActionEvent actionEvent) {
if (isStart) {
for (int i = length-1; i > 0; i--) {
snakeY[i] = snakeY[i-1];
snakeX[i] = snakeX[i-1];
}
switch (dictionary) {
case "R":
snakeX[0] += 25;
break;
case "L":
snakeX[0] -= 25;
break;
case "U":
snakeY[0] -= 25;
break;
case "D":
snakeY[0] += 25;
break;
}
if (snakeX[0] > 850) {
snakeX[0] = 25;
} else if (snakeX[0] < 25) {
snakeX[0] = 850;
}
if (snakeY[0] > 650) {
snakeY[0] = 75;
} else if (snakeY[0] < 75) {
snakeY[0] = 650;
}
repaint();
timer.start();
}
}
}
5、判断小蛇吃果子
如何判断小蛇吃到果子呢?果子应该在哪里生成呢?
- 小蛇头和果子坐标重合就是吃到果子了
- 果子用随机数生成
- 如果果子生成在小蛇身体里,就重新生成
- 每次吃完一次果子,果子没消失,而是坐标转移
package top.woodwhale.snake;
import javax.swing.*;
import java.awt.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.KeyEvent;
import java.awt.event.KeyListener;
import java.util.Arrays;
import java.util.Random;
import java.util.stream.Collectors;
public class GamePanel extends JPanel implements KeyListener, ActionListener {
int length;
int[] snakeX = new int[600];
int[] snakeY = new int[600];
String dictionary;
boolean isStart;
int preX;
int preY;
int foodX;
int foodY;
Random random = new Random();
Timer timer = new Timer(100,this);
public GamePanel() {
init();
this.setFocusable(true);
this.addKeyListener(this);
}
public void init() {
length = 3;
snakeX[0] = 100;snakeY[0] = 100;
snakeX[1] = 75;snakeY[1] = 100;
snakeX[2] = 50;snakeY[2] = 100;
preX = snakeX[0];preY = snakeY[0];
randomFood();
dictionary = "R";
isStart = false;
timer.start();
}
public void randomFood() {
foodX = 25 + 25 * random.nextInt(34);
foodY = 75 + 25 * random.nextInt(24);
java.util.List<Integer> tmpX = Arrays.stream(snakeX).boxed().collect(Collectors.toList());
java.util.List<Integer> tmpY = Arrays.stream(snakeY).boxed().collect(Collectors.toList());
if (tmpX.contains(foodX) || tmpY.contains(foodY)) {
randomFood();
}
}
@Override
public void paintComponent(Graphics g) {
super.paintComponent(g);
this.setBackground(Color.white);
Data.header.paintIcon(this,g,25,11);
g.fillRect(25,75,850,600);
switch (dictionary) {
case "R":
Data.right.paintIcon(this, g, snakeX[0], snakeY[0]);
break;
case "L":
Data.left.paintIcon(this, g, snakeX[0], snakeY[0]);
break;
case "U":
Data.up.paintIcon(this, g, snakeX[0], snakeY[0]);
break;
case "D":
Data.down.paintIcon(this, g, snakeX[0], snakeY[0]);
break;
}
for (int i = 1; i < length; i++) {
Data.body.paintIcon(this,g,snakeX[i],snakeY[i]);
}
Data.food.paintIcon(this,g,foodX,foodY);
if (!isStart) {
g.setColor(Color.white);
g.setFont(new Font("Chilanka",Font.BOLD,40));
g.drawString("Press Space To Start!",250,400);
}
}
@Override
public void keyTyped(KeyEvent keyEvent) {
}
@Override
public void keyPressed(KeyEvent keyEvent) {
int keyCode = keyEvent.getKeyCode();
if (keyCode == KeyEvent.VK_SPACE) {
isStart = !isStart;
repaint();
}
if (Math.abs(preX - snakeX[0]) >= 25 || Math.abs(preY - snakeY[0]) >= 25) {
switch (keyCode) {
case KeyEvent.VK_UP:
if (!dictionary.equals("D")) {
preX = snakeX[0];
preY = snakeY[0];
dictionary = "U";
}
break;
case KeyEvent.VK_DOWN:
if (!dictionary.equals("U")) {
preX = snakeX[0];
preY = snakeY[0];
dictionary = "D";
}
break;
case KeyEvent.VK_LEFT:
if (!dictionary.equals("R")) {
preX = snakeX[0];
preY = snakeY[0];
dictionary = "L";
}
break;
case KeyEvent.VK_RIGHT:
if (!dictionary.equals("L")) {
preX = snakeX[0];
preY = snakeY[0];
dictionary = "R";
}
break;
}
}
}
@Override
public void keyReleased(KeyEvent keyEvent) {
}
@Override
public void actionPerformed(ActionEvent actionEvent) {
if (isStart) {
for (int i = length-1; i > 0; i--) {
snakeY[i] = snakeY[i-1];
snakeX[i] = snakeX[i-1];
}
switch (dictionary) {
case "R":
snakeX[0] += 25;
break;
case "L":
snakeX[0] -= 25;
break;
case "U":
snakeY[0] -= 25;
break;
case "D":
snakeY[0] += 25;
break;
}
if (snakeY[0] == foodY && snakeX[0] == foodX) {
length++;
snakeX[length-1] = snakeX[length-2];
snakeY[length-1] = snakeY[length-2];
randomFood();
}
if (snakeX[0] > 850) {
snakeX[0] = 25;
} else if (snakeX[0] < 25) {
snakeX[0] = 850;
}
if (snakeY[0] > 650) {
snakeY[0] = 75;
} else if (snakeY[0] < 75) {
snakeY[0] = 650;
}
repaint();
timer.start();
}
}
}
6、游戏失败判断
小蛇的头与身体任意部分的坐标重合,就是失败
package top.woodwhale.snake;
import javax.swing.*;
import java.awt.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.KeyEvent;
import java.awt.event.KeyListener;
import java.util.Arrays;
import java.util.Random;
import java.util.stream.Collectors;
public class GamePanel extends JPanel implements KeyListener, ActionListener {
int length;
int[] snakeX = new int[600];
int[] snakeY = new int[600];
String dictionary;
boolean isStart;
boolean isFail;
int preX;
int preY;
int foodX;
int foodY;
int score;
Random random = new Random();
Timer timer = new Timer(150,this);
public GamePanel() {
init();
this.setFocusable(true);
this.addKeyListener(this);
}
public void init() {
length = 3;
score = 0;
snakeX[0] = 100;snakeY[0] = 100;
snakeX[1] = 75;snakeY[1] = 100;
snakeX[2] = 50;snakeY[2] = 100;
preX = snakeX[0];preY = snakeY[0];
randomFood();
dictionary = "R";
isStart = false;
timer.start();
}
public void randomFood() {
foodX = 25 + 25 * random.nextInt(34);
foodY = 75 + 25 * random.nextInt(24);
java.util.List<Integer> tmpX = Arrays.stream(snakeX).boxed().collect(Collectors.toList());
java.util.List<Integer> tmpY = Arrays.stream(snakeY).boxed().collect(Collectors.toList());
if (tmpX.contains(foodX) || tmpY.contains(foodY)) {
randomFood();
}
}
public void isGameOver() {
for (int i = 1; i < length; i++) {
if (snakeY[0] == snakeY[i] && snakeX[0] == snakeX[i]) {
isFail = true;
break;
}
}
}
@Override
public void paintComponent(Graphics g) {
super.paintComponent(g);
this.setBackground(Color.white);
Data.header.paintIcon(this,g,25,11);
g.fillRect(25,75,850,600);
g.setColor(Color.orange);
g.setFont(new Font("Chilanka",Font.BOLD,18));
g.drawString("Length : " + length,750,30);
g.drawString("Score : " + score,750,55);
Data.food.paintIcon(this,g,foodX,foodY);
switch (dictionary) {
case "R":
Data.right.paintIcon(this, g, snakeX[0], snakeY[0]);
break;
case "L":
Data.left.paintIcon(this, g, snakeX[0], snakeY[0]);
break;
case "U":
Data.up.paintIcon(this, g, snakeX[0], snakeY[0]);
break;
case "D":
Data.down.paintIcon(this, g, snakeX[0], snakeY[0]);
break;
}
for (int i = 1; i < length; i++) {
Data.body.paintIcon(this,g,snakeX[i],snakeY[i]);
}
if (!isStart) {
g.setColor(Color.white);
g.setFont(new Font("Chilanka",Font.BOLD,40));
g.drawString("Press Space To Start !",250,400);
}
if (isFail) {
g.setColor(Color.red);
g.setFont(new Font("Chilanka",Font.BOLD,40));
g.drawString("Failed ! Space Again ?!",250,400);
}
}
@Override
public void keyTyped(KeyEvent keyEvent) {
}
@Override
public void keyPressed(KeyEvent keyEvent) {
int keyCode = keyEvent.getKeyCode();
if (keyCode == KeyEvent.VK_SPACE) {
if (isFail) {
isFail = false;
init();
} else {
isStart = !isStart;
}
repaint();
}
if (Math.abs(preX - snakeX[0]) >= 25 || Math.abs(preY - snakeY[0]) >= 25) {
switch (keyCode) {
case KeyEvent.VK_UP:
if (!dictionary.equals("D")) {
preX = snakeX[0];
preY = snakeY[0];
dictionary = "U";
}
break;
case KeyEvent.VK_DOWN:
if (!dictionary.equals("U")) {
preX = snakeX[0];
preY = snakeY[0];
dictionary = "D";
}
break;
case KeyEvent.VK_LEFT:
if (!dictionary.equals("R")) {
preX = snakeX[0];
preY = snakeY[0];
dictionary = "L";
}
break;
case KeyEvent.VK_RIGHT:
if (!dictionary.equals("L")) {
preX = snakeX[0];
preY = snakeY[0];
dictionary = "R";
}
break;
}
}
}
@Override
public void keyReleased(KeyEvent keyEvent) {
}
@Override
public void actionPerformed(ActionEvent actionEvent) {
if (isStart && !isFail) {
for (int i = length-1; i > 0; i--) {
snakeY[i] = snakeY[i-1];
snakeX[i] = snakeX[i-1];
}
switch (dictionary) {
case "R":
snakeX[0] += 25;
break;
case "L":
snakeX[0] -= 25;
break;
case "U":
snakeY[0] -= 25;
break;
case "D":
snakeY[0] += 25;
break;
}
if (snakeY[0] == foodY && snakeX[0] == foodX) {
score += 10;
length++;
snakeX[length-1] = snakeX[length-2];
snakeY[length-1] = snakeY[length-2];
randomFood();
}
if (snakeX[0] > 850) {
snakeX[0] = 25;
} else if (snakeX[0] < 25) {
snakeX[0] = 850;
}
if (snakeY[0] > 650) {
snakeY[0] = 75;
} else if (snakeY[0] < 75) {
snakeY[0] = 650;
}
isGameOver();
repaint();
timer.start();
}
}
}
总结
所有源码:
Data类
package top.woodwhale.snake;
import javax.swing.*;
import java.net.URL;
public class Data {
public static URL headerURL = Data.class.getResource("statics/img/header.png");
public static ImageIcon header = new ImageIcon(headerURL);
public static URL upRUL = Data.class.getResource("statics/img/up.png");
public static ImageIcon up = new ImageIcon(upRUL);
public static URL downURL = Data.class.getResource("statics/img/down.png");
public static ImageIcon down = new ImageIcon(downURL);
public static URL leftURL = Data.class.getResource("statics/img/left.png");
public static ImageIcon left = new ImageIcon(leftURL);
public static URL rightURL = Data.class.getResource("statics/img/right.png");
public static ImageIcon right = new ImageIcon(rightURL);
public static URL bodyURL = Data.class.getResource("statics/img/body.png");
public static ImageIcon body = new ImageIcon(bodyURL);
public static URL foodURL = Data.class.getResource("statics/img/food.png");
public static ImageIcon food = new ImageIcon(foodURL);
}
StartGame类
package top.woodwhale.snake;
import javax.swing.*;
public class StartGame {
public static void main(String[] args) {
JFrame frame = new JFrame();
frame.setBounds(10,10,900,720);
frame.setResizable(false);
frame.setDefaultCloseOperation(WindowConstants.EXIT_ON_CLOSE);
frame.add(new GamePanel());
frame.setVisible(true);
}
}
GamePanel类
package top.woodwhale.snake;
import javax.swing.*;
import java.awt.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.KeyEvent;
import java.awt.event.KeyListener;
import java.util.Arrays;
import java.util.Random;
import java.util.stream.Collectors;
public class GamePanel extends JPanel implements KeyListener, ActionListener {
int length;
int[] snakeX = new int[600];
int[] snakeY = new int[600];
String dictionary;
boolean isStart;
boolean isFail;
int preX;
int preY;
int foodX;
int foodY;
int score;
Random random = new Random();
Timer timer = new Timer(150,this);
public GamePanel() {
init();
this.setFocusable(true);
this.addKeyListener(this);
}
public void init() {
length = 3;
score = 0;
snakeX[0] = 100;snakeY[0] = 100;
snakeX[1] = 75;snakeY[1] = 100;
snakeX[2] = 50;snakeY[2] = 100;
preX = snakeX[0];preY = snakeY[0];
randomFood();
dictionary = "R";
isStart = false;
timer.start();
}
public void randomFood() {
foodX = 25 + 25 * random.nextInt(34);
foodY = 75 + 25 * random.nextInt(24);
java.util.List<Integer> tmpX = Arrays.stream(snakeX).boxed().collect(Collectors.toList());
java.util.List<Integer> tmpY = Arrays.stream(snakeY).boxed().collect(Collectors.toList());
if (tmpX.contains(foodX) || tmpY.contains(foodY)) {
randomFood();
}
}
public void isGameOver() {
for (int i = 1; i < length; i++) {
if (snakeY[0] == snakeY[i] && snakeX[0] == snakeX[i]) {
isFail = true;
break;
}
}
}
@Override
public void paintComponent(Graphics g) {
super.paintComponent(g);
this.setBackground(Color.white);
Data.header.paintIcon(this,g,25,11);
g.fillRect(25,75,850,600);
g.setColor(Color.orange);
g.setFont(new Font("Chilanka",Font.BOLD,18));
g.drawString("Length : " + length,750,30);
g.drawString("Score : " + score,750,55);
Data.food.paintIcon(this,g,foodX,foodY);
switch (dictionary) {
case "R":
Data.right.paintIcon(this, g, snakeX[0], snakeY[0]);
break;
case "L":
Data.left.paintIcon(this, g, snakeX[0], snakeY[0]);
break;
case "U":
Data.up.paintIcon(this, g, snakeX[0], snakeY[0]);
break;
case "D":
Data.down.paintIcon(this, g, snakeX[0], snakeY[0]);
break;
}
for (int i = 1; i < length; i++) {
Data.body.paintIcon(this,g,snakeX[i],snakeY[i]);
}
if (!isStart) {
g.setColor(Color.white);
g.setFont(new Font("Chilanka",Font.BOLD,40));
g.drawString("Press Space To Start !",250,400);
}
if (isFail) {
g.setColor(Color.red);
g.setFont(new Font("Chilanka",Font.BOLD,40));
g.drawString("Failed ! Space Again ?!",250,400);
}
}
@Override
public void keyTyped(KeyEvent keyEvent) {
}
@Override
public void keyPressed(KeyEvent keyEvent) {
int keyCode = keyEvent.getKeyCode();
if (keyCode == KeyEvent.VK_SPACE) {
if (isFail) {
isFail = false;
init();
} else {
isStart = !isStart;
}
repaint();
}
if (Math.abs(preX - snakeX[0]) >= 25 || Math.abs(preY - snakeY[0]) >= 25) {
switch (keyCode) {
case KeyEvent.VK_UP:
if (!dictionary.equals("D")) {
preX = snakeX[0];
preY = snakeY[0];
dictionary = "U";
}
break;
case KeyEvent.VK_DOWN:
if (!dictionary.equals("U")) {
preX = snakeX[0];
preY = snakeY[0];
dictionary = "D";
}
break;
case KeyEvent.VK_LEFT:
if (!dictionary.equals("R")) {
preX = snakeX[0];
preY = snakeY[0];
dictionary = "L";
}
break;
case KeyEvent.VK_RIGHT:
if (!dictionary.equals("L")) {
preX = snakeX[0];
preY = snakeY[0];
dictionary = "R";
}
break;
}
}
}
@Override
public void keyReleased(KeyEvent keyEvent) {
}
@Override
public void actionPerformed(ActionEvent actionEvent) {
if (isStart && !isFail) {
for (int i = length-1; i > 0; i--) {
snakeY[i] = snakeY[i-1];
snakeX[i] = snakeX[i-1];
}
switch (dictionary) {
case "R":
snakeX[0] += 25;
break;
case "L":
snakeX[0] -= 25;
break;
case "U":
snakeY[0] -= 25;
break;
case "D":
snakeY[0] += 25;
break;
}
if (snakeY[0] == foodY && snakeX[0] == foodX) {
score += 10;
length++;
snakeX[length-1] = snakeX[length-2];
snakeY[length-1] = snakeY[length-2];
randomFood();
}
if (snakeX[0] > 850) {
snakeX[0] = 25;
} else if (snakeX[0] < 25) {
snakeX[0] = 850;
}
if (snakeY[0] > 650) {
snakeY[0] = 75;
} else if (snakeY[0] < 75) {
snakeY[0] = 650;
}
isGameOver();
repaint();
timer.start();
}
}
}
|