gpt4 book ai didi

java - Java 贪吃蛇游戏

转载 作者:行者123 更新时间:2023-12-01 23:00:03 28 4
gpt4 key购买 nike

我正在开发一个蛇程序。到目前为止,我有两个类,一个用于游戏的所有逻辑,另一个作为运行游戏的主类。这是我所拥有的

蛇类:

这是所有常量

import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
import java.util.*;

public class Snake {
// GUI components
private JPanel board;
private JButton[] snakeBodyPart;
private JButton bonusfood;
private JTextArea scoreViewer;

// Constants
private final int SNAKE_RUNNING_SPEED_FASTEST = 25;
private final int SNAKE_RUNNING_SPEED_FASTER = 50;
private final int SNAKE_RUNNING_SPEED_FAST = 100;
private final int BOARD_WIDTH = 500;
private final int BOARD_HEIGHT = 250;
private final int SCORE_BOARD_HEIGHT = 20;
private final int SNAKE_LENGTH_DEFAULT = 4;
private final int SNAKE_BODY_PART_SQURE = 10;
private final int BONUS_FOOD_SQURE = 15;
private final Point INIT_POINT = new Point(100, 150);

// Others values
private enum GAME_TYPE {NO_MAZE, BORDER, TUNNEL};
private int selectedSpeed = SNAKE_RUNNING_SPEED_FASTER;
private GAME_TYPE selectedGameType = GAME_TYPE.NO_MAZE;
private int totalBodyPart;
private int directionX;
private int directionY;
private int score;
private Point pointOfBonusFood = new Point();
private boolean isRunningLeft;
private boolean isRunningRight;
private boolean isRunningUp;
private boolean isRunningDown;
private boolean isBonusFoodAvailable;
private boolean isRunning;
private Random random = new Random();

这是游戏开始的地方

Snake() {
//initialize all variables.
resetDefaultValues();
// initialize GUI.
init();
// Create Initial body of a snake.
createInitSnake();
// Initialize Thread.
isRunning = true;
createThread();
}

这是设置董事会的地方

public void init() {
JFrame frame = new JFrame("Snake");
frame.setSize(500, 330);

//Create Menue bar with functions
setJMenueBar(frame);
// Start of UI design
JPanel scorePanel = new JPanel();
scoreViewer = new JTextArea("Score ==>" + score);
scoreViewer.setEnabled(false);
scoreViewer.setBackground(Color.BLACK);

board = new JPanel();
board.setLayout(null);
board.setBounds(0, 0, BOARD_WIDTH, BOARD_HEIGHT);
board.setBackground(Color.WHITE);
scorePanel.setLayout(new GridLayout(0, 1));
scorePanel.setBounds(0, BOARD_HEIGHT, BOARD_WIDTH, SCORE_BOARD_HEIGHT);
scorePanel.setBackground(Color.RED);
scorePanel.add(scoreViewer); // will contain score board

frame.getContentPane().setLayout(null);
frame.getContentPane().add(board);
frame.getContentPane().add(scorePanel);
frame.setVisible(true);
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.addKeyListener(new KeyAdapter() {
@Override
public void keyPressed(KeyEvent e) {
snakeKeyPressed(e);
}
});
frame.setResizable(false);
}

这是菜单

public void setJMenueBar(JFrame frame) {

JMenuBar mymbar = new JMenuBar();

JMenu game = new JMenu("Game");
JMenuItem newgame = new JMenuItem("New Game");
JMenuItem exit = new JMenuItem("Exit");
newgame.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
startNewGame();
}
});
exit.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
System.exit(0);
}
});
game.add(newgame);
game.addSeparator();
game.add(exit);
mymbar.add(game);

JMenu type = new JMenu("Type");
JMenuItem noMaze = new JMenuItem("No Maze");
noMaze.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
selectedGameType = GAME_TYPE.NO_MAZE;
startNewGame();
}
});
JMenuItem border = new JMenuItem("Border Maze");
border.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
selectedGameType = GAME_TYPE.BORDER;
startNewGame();
}
});
type.add(noMaze);
type.add(border);
mymbar.add(type);

JMenu level = new JMenu("Level");
JMenuItem level1 = new JMenuItem("Level 1");
level1.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
selectedSpeed = SNAKE_RUNNING_SPEED_FAST;
startNewGame();
}
});
JMenuItem level2 = new JMenuItem("Level 2");
level2.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
selectedSpeed = SNAKE_RUNNING_SPEED_FASTER;
startNewGame();
}
});
JMenuItem level3 = new JMenuItem("Level 3");
level3.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
selectedSpeed = SNAKE_RUNNING_SPEED_FASTEST;
startNewGame();
}
});
level.add(level1);
level.add(level2);
level.add(level3);
mymbar.add(level);

JMenu help = new JMenu("Help");
JMenuItem creator = new JMenuItem("Creator");
JMenuItem instruction = new JMenuItem("Instraction");
creator.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
JOptionPane.showMessageDialog(null, "Author: Jack Moffe");
}
});

help.add(creator);
help.add(instruction);
mymbar.add(help);

frame.setJMenuBar(mymbar);
}

这会重置所有值

public void resetDefaultValues() {
snakeBodyPart = new JButton[2000];
totalBodyPart = SNAKE_LENGTH_DEFAULT;
directionX = SNAKE_BODY_PART_SQURE;
directionY = 0;
score = 0;
isRunningLeft = false;
isRunningRight = true;
isRunningUp = true;
isRunningDown = true;
isBonusFoodAvailable = false;
}

void startNewGame() {
resetDefaultValues();
board.removeAll();
createInitSnake();
scoreViewer.setText("Score==>" + score);
isRunning = true;
}

该方法负责初始化蛇的四个 body 部位。

public void createInitSnake() {
// Location of the snake's head.
int x = (int) INIT_POINT.getX();
int y = (int) INIT_POINT.getY();

// Initially the snake has three body part.
for (int i = 0; i < totalBodyPart; i++) {
snakeBodyPart[i] = new JButton();
snakeBodyPart[i].setBounds(x, y, SNAKE_BODY_PART_SQURE, SNAKE_BODY_PART_SQURE);
snakeBodyPart[i].setBackground(Color.GRAY);
board.add(snakeBodyPart[i]);
// Set location of the next body part of the snake.
x = x - SNAKE_BODY_PART_SQURE;
}

// Create food.
createFood();
}

这个方法负责创建蛇的食物。这条蛇的最后一部分被当作了食物,还没有成为蛇的 body 部分。当且仅当蛇头接触到该食物时,该食物才是 body 的一部分。

void createFood() {
int randomX = SNAKE_BODY_PART_SQURE + (SNAKE_BODY_PART_SQURE * random.nextInt(48));
int randomY = SNAKE_BODY_PART_SQURE + (SNAKE_BODY_PART_SQURE * random.nextInt(23));

snakeBodyPart[totalBodyPart] = new JButton();
snakeBodyPart[totalBodyPart].setEnabled(false);
snakeBodyPart[totalBodyPart].setBounds(randomX, randomY, SNAKE_BODY_PART_SQURE, SNAKE_BODY_PART_SQURE);
board.add(snakeBodyPart[totalBodyPart]);

totalBodyPart++;
}

private void createBonusFood() {
bonusfood = new JButton();
bonusfood.setEnabled(false);
//Set location of the bonus food.
int bonusFoodLocX = SNAKE_BODY_PART_SQURE * random.nextInt(50);
int bonusFoodLocY = SNAKE_BODY_PART_SQURE * random.nextInt(25);

bonusfood.setBounds(bonusFoodLocX, bonusFoodLocY, BONUS_FOOD_SQURE, BONUS_FOOD_SQURE);
pointOfBonusFood = bonusfood.getLocation();
board.add(bonusfood);
isBonusFoodAvailable = true;
}

处理蛇的下一步。并决定应该做什么。即蛇在做什么以及如何响应

void processNextStep() {
boolean isBorderTouched = false;
// Generate new location of snake head.
int newHeadLocX = (int) snakeBodyPart[0].getLocation().getX() + directionX;
int newHeadLocY = (int) snakeBodyPart[0].getLocation().getY() + directionY;

// Most last part of the snake is food.
int foodLocX = (int) snakeBodyPart[totalBodyPart - 1].getLocation().getX();
int foodLocY = (int) snakeBodyPart[totalBodyPart - 1].getLocation().getY();

// Check does snake cross the border of the board?
if (newHeadLocX >= BOARD_WIDTH - SNAKE_BODY_PART_SQURE) {
newHeadLocX = 0;
isBorderTouched = true;
} else if (newHeadLocX <= 0) {
newHeadLocX = BOARD_WIDTH - SNAKE_BODY_PART_SQURE;
isBorderTouched = true;
} else if (newHeadLocY >= BOARD_HEIGHT - SNAKE_BODY_PART_SQURE) {
newHeadLocY = 0;
isBorderTouched = true;
} else if (newHeadLocY <= 0) {
newHeadLocY = BOARD_HEIGHT - SNAKE_BODY_PART_SQURE;
isBorderTouched = true;
}

// Check has snake touched the food?
if (newHeadLocX == foodLocX && newHeadLocY == foodLocY) {
// Set score.
score += 5;
scoreViewer.setText("Score==>" + score);

// Check bonus food should be given or not?
if (score % 50 == 0 && !isBonusFoodAvailable) {
createBonusFood();
}
// Create new food.
createFood();
}

// Check has snake touched the bonus food?
if (isBonusFoodAvailable &&
pointOfBonusFood.x <= newHeadLocX &&
pointOfBonusFood.y <= newHeadLocY &&
(pointOfBonusFood.x + SNAKE_BODY_PART_SQURE) >= newHeadLocX &&
(pointOfBonusFood.y + SNAKE_BODY_PART_SQURE) >= newHeadLocY) {
board.remove(bonusfood);
score += 100;
scoreViewer.setText("Score ==>" + score);
isBonusFoodAvailable = false;
}

// Check is game over?
if(isGameOver(isBorderTouched, newHeadLocX, newHeadLocY)) {
scoreViewer.setText("GAME OVER " + score);
isRunning = false;
return;
} else {
// Move the whole snake body to forword.
moveSnakeForword(newHeadLocX, newHeadLocY);
}

board.repaint();
}

这个方法负责检测游戏是否结束?当蛇被任何迷宫或自身触及时,游戏应该结束。

private boolean isGameOver(boolean isBorderTouched, int headLocX, int headLocY) {
switch(selectedGameType) {
case BORDER:
if(isBorderTouched) {
return true;
}
break;
case TUNNEL:
// TODO put logic here...
throw new UnsupportedOperationException();
default:
break;
}

for (int i = SNAKE_LENGTH_DEFAULT; i < totalBodyPart - 2; i++) {
Point partLoc = snakeBodyPart[i].getLocation();
System.out.println("("+partLoc.x +", "+partLoc.y+") ("+headLocX+", "+headLocY+")");
if (partLoc.equals(new Point(headLocX, headLocY))) {
return true;
}
}

return false;
}

/**
* Every body part should be placed to location of the front part.
* For example if part:0(100,150) , part: 1(90, 150), part:2(80,150) and new head location (110,150) then,
Location of part:2 should be (80,150) to (90,150), part:1 will be (90,150) to (100,150) and part:3 will be (100,150) to (110,150)
* This movement process should be start from the last part to first part.
* We must avoid the food that means most last body part of the snake.
* Notice that we write (totalBodyPart - 2) instead of (totalBodyPart - 1).
* (totalBodyPart - 1) means food and (totalBodyPart - 2) means tail.
* @param headLocX
* @param headLocY
*/
public void moveSnakeForword(int headLocX, int headLocY) {
for (int i = totalBodyPart - 2; i > 0; i--) {
Point frontBodyPartPoint = snakeBodyPart[i - 1].getLocation();
snakeBodyPart[i].setLocation(frontBodyPartPoint);
}
snakeBodyPart[0].setBounds(headLocX, headLocY, SNAKE_BODY_PART_SQURE, SNAKE_BODY_PART_SQURE);
}

public void snakeKeyPressed(KeyEvent e) {
// snake should move to left when player pressed left arrow
if (isRunningLeft == true && e.getKeyCode() == 37) {
directionX = -SNAKE_BODY_PART_SQURE; // means snake move right to left by 10 pixel
directionY = 0;
isRunningRight = false; // means snake cant move from left to right
isRunningUp = true; // means snake can move from down to up
isRunningDown = true; // means snake can move from up to down
}
// snake should move to up when player pressed up arrow
if (isRunningUp == true && e.getKeyCode() == 38) {
directionX = 0;
directionY = -SNAKE_BODY_PART_SQURE; // means snake move from down to up by 10 pixel
isRunningDown = false; // means snake can move from up to down
isRunningRight = true; // means snake can move from left to right
isRunningLeft = true; // means snake can move from right to left
}
// snake should move to right when player pressed right arrow
if (isRunningRight == true && e.getKeyCode() == 39) {
directionX = +SNAKE_BODY_PART_SQURE; // means snake move from left to right by 10 pixel
directionY = 0;
isRunningLeft = false;
isRunningUp = true;
isRunningDown = true;
}
// snake should move to down when player pressed down arrow
if (isRunningDown == true && e.getKeyCode() == 40) {
directionX = 0;
directionY = +SNAKE_BODY_PART_SQURE; // means snake move from left to right by 10 pixel
isRunningUp = false;
isRunningRight = true;
isRunningLeft = true;
}
}

private void createThread() {
// start thread
Thread thread = new Thread(new Runnable() {

public void run() {
runIt();
}
});
thread.start(); // go to runIt() method
}

public void runIt() {
while (true) {
if(isRunning) {
// Process what should be next step of the snake.
processNextStep();
try {
Thread.sleep(selectedSpeed);
} catch (InterruptedException ie) {
ie.printStackTrace();
}
}
}
}
}

主类:

public class Main {
/**
* @param args the command line arguments
*/
public static void main(String[] args) {
new Snake();
}
}

我的问题是,当我运行游戏时,指示不起作用。蛇只是自动地朝一个方向移动,没有其他任何 Action 。

你觉得怎么样?

最佳答案

不要使用KeyListener,尤其是在像JFrame这样的顶级容器上。

KeyListener 仅当其注册的组件可聚焦且具有焦点时才会引发按键事件。 JFrame 有一个 JRootPane、内容和可能的玻璃 Pane ,它们都会妨碍框架每次接收键盘焦点(这是在您添加任何其他组件之前)它)。

相反,请使用 key bindings API这将允许您定义将引发关键事件的焦点级别

不要使用null布局。像素完美布局是现代 UI 设计中的一种幻觉,您无法控制字体、DPI、渲染管道或其他会改变组件在屏幕上渲染方式的因素。

Swing 旨在与布局管理器配合来克服这些问题。如果您坚持忽略这些功能并违背 API 设计,请做好迎接许多头痛和永无休止的艰苦工作的准备......

关于java - Java 贪吃蛇游戏,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/23509500/

28 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com