4.貪吃蛇
必備知識:
- 幀,如果時間片足夠小,就是動畫,一秒30幀,連起來就是動畫,拆開就是靜態的圖片
- 鍵盤監聽
- 定時器Timer
主啓動類
package com.zr.snake;
import javax.swing.*;
import java.awt.*;
public class StartGame {
public static void main(String[] args) {
JFrame frame = new JFrame("貪吃蛇的世界");
frame.setBounds(320,100,875,750);
frame.setResizable(false);
frame.setDefaultCloseOperation(WindowConstants.EXIT_ON_CLOSE);
frame.add(new GamePanel());
frame.setVisible(true);
}
}
數據中心
package com.zr.snake;
import javax.swing.*;
import java.net.URL;
public class SnakeData {
private static URL bodyURL = SnakeData.class.getResource("/statics/body.png");
public static ImageIcon body = new ImageIcon(bodyURL);
private static URL downURL = SnakeData.class.getResource("/statics/down.png");
public static ImageIcon down = new ImageIcon(downURL);
private static URL foodURL = SnakeData.class.getResource("/statics/food.png");
public static ImageIcon food = new ImageIcon(foodURL);
private static URL headerURL = SnakeData.class.getResource("/statics/header.png");
public static ImageIcon header = new ImageIcon(headerURL);
private static URL leftURL = SnakeData.class.getResource("/statics/left.png");
public static ImageIcon left = new ImageIcon(leftURL);
private static URL rightURL = SnakeData.class.getResource("/statics/right.png");
public static ImageIcon right = new ImageIcon(rightURL);
private static URL upURL = SnakeData.class.getResource("/statics/up.png");
public static ImageIcon up = new ImageIcon(upURL);
}
遊戲面板
package com.zr.snake;
import javax.swing.*;
import java.awt.*;
import java.awt.event.*;
import java.util.Random;
public class GamePanel extends JPanel implements KeyListener,ActionListener {
private int length;
private int[] snakeX = new int[980];
private int[] snakeY = new int[980];
private String direction;
private int score;
private int foodX;
private int foodY;
private Boolean isStart;
private boolean isFail;
private Random random = new Random();
private Timer timer = new Timer(130,this);
public GamePanel() {
init();
this.setFocusable(true);
this.addKeyListener(this);
timer.start();
}
public void init() {
length = 3;
snakeX[0] = 150;snakeY[0] = 100;
snakeX[1] = 125;snakeY[1] = 100;
snakeX[2] = 100;snakeY[2] = 100;
direction = "right";
isStart = false;
isFail = false;
foodX = 25*random.nextInt(34);
foodY = 50+25*random.nextInt(26);
score = 0;
}
@Override
protected void paintComponent(Graphics g) {
super.paintComponent(g);
this.setBackground(new Color(134, 93, 75));
SnakeData.header.paintIcon(this, g, 100, 0);
g.fillRect(0, 50, 875, 700);
SnakeData.food.paintIcon(this,g,foodX,foodY);
if (direction.equals("right")) {
SnakeData.right.paintIcon(this, g, snakeX[0], snakeY[0]);
} else if (direction.equals("up")) {
SnakeData.up.paintIcon(this, g, snakeX[0], snakeY[0]);
} else if (direction.equals("down")) {
SnakeData.down.paintIcon(this, g, snakeX[0], snakeY[0]);
} else if (direction.equals("left")) {
SnakeData.left.paintIcon(this, g, snakeX[0], snakeY[0]);
}
for (int i = 1; i < length; i++) {
SnakeData.body.paintIcon(this, g, snakeX[i], snakeY[i]);
}
g.setColor(new Color(41, 45, 41, 182));
g.setFont(new Font("微軟雅黑", Font.BOLD, 15));
g.drawString("長度:"+length, 800, 20);
g.drawString("積分:"+score, 800, 40);
if (isStart == false) {
g.setColor(new Color(235, 255, 232, 182));
g.setFont(new Font("微軟雅黑", Font.BOLD, 40));
g.drawString("按下空格開始遊戲", 275, 300);
}
if (isFail){
g.setColor(new Color(109, 1, 8, 182));
g.setFont(new Font("微軟雅黑", Font.BOLD, 40));
g.drawString("你失敗了", 275, 300);
}
}
@Override
public void actionPerformed(ActionEvent e) {
if(snakeX[0] == foodX & snakeY[0] == foodY){
length++;
score = score + 10;
foodX = 25*random.nextInt(35);
foodY = 50+25*random.nextInt(30);
}
if (isStart && isFail == false) {
for (int i = length - 1; i > 0; i--) {
snakeX[i] = snakeX[i - 1];
snakeY[i] = snakeY[i - 1];
}
if(direction.equals("right")){
snakeX[0] = snakeX[0] + 25;
if(snakeX[0] > 875){ snakeX[0] = 0; }
} else if(direction.equals("left")){
snakeX[0] = snakeX[0] - 25;
if(snakeX[0] < 0) { snakeX[0] = 875; }
} else if (direction.equals("up")){
snakeY[0] = snakeY[0] - 25;
if(snakeY[0] <50) { snakeY[0] = 750; }
} else if(direction.equals("down")){
snakeY[0] = snakeY[0] + 25;
if(snakeY[0] > 750) { snakeY[0] = 50;}
}
for (int i = 1; i < length; i++) {
if(snakeX[0] == snakeX[i] & snakeY[0] == snakeY[i]){
isFail = true;
}
}
repaint();
}
}
@Override
public void keyPressed(KeyEvent e) {
int keyCode = e.getKeyCode();
if (keyCode == KeyEvent.VK_SPACE) {
if(isFail){
isFail = false;
init();
}else {
isStart = !isStart;
}
}else if(keyCode == KeyEvent.VK_UP){
if(direction.equals("down")){
return;
}else {
direction = "up";
}
}else if(keyCode == KeyEvent.VK_DOWN) {
if(direction.equals("up")){
return;
}else {
direction = "down";
}
}else if(keyCode == KeyEvent.VK_LEFT) {
if(direction.equals("right")){
return;
}else {
direction = "left";
}
}else if(keyCode == KeyEvent.VK_RIGHT) {
if(direction.equals("left")){
return;
}else {
direction = "right";
}
}
repaint();
}
public void keyTyped(KeyEvent e) {}
public void keyReleased(KeyEvent e) {}
}