Java 小游戏源代码,尽享编码乐趣14


作为一名熟练的程序员,我热衷于通过编写小游戏来探索 Java 编程语言的强大功能。在我多年的编码经验中,我积累了丰富的 Java 小游戏源代码,涵盖各种类型和复杂程度。本文将与大家分享我精心挑选的 1500 行 Java 小游戏源代码,供您学习、享受和创造属于您自己的交互式杰作。

1. 贪吃蛇

重温经典!源代码:
```java
public class Snake {
// 贪吃蛇的身体
private List body;
// 食物的位置
private Coordinate food;
// 移动方向
private Direction direction;
// 游戏结束标识
private boolean gameOver;
// 初始化游戏
public void init() {
body = new ArrayList();
food = new Coordinate();
direction = ;
gameOver = false;
}
// 游戏循环
public void run() {
while (!gameOver) {
// 更新蛇的位置
updateBody();
// 检查是否吃到食物
if (().equals(food)) {
eatFood();
}
// 检查是否撞到自己或边界
if (isCollided()) {
gameOver = true;
}
}
}
// 更新蛇的身体
private void updateBody() {
Coordinate head = ();
(0, new Coordinate(() + (), () + ()));
(() - 1);
}
// 吃到食物
private void eatFood() {
food = new Coordinate();
// 增加蛇的身长
(new Coordinate(().getX(), ().getY()));
}
// 判断是否撞到自己或边界
private boolean isCollided() {
Coordinate head = ();
return ().skip(1).anyMatch(c -> (head)) ||
() < 0 || () >= WIDTH || () < 0 || () >= HEIGHT;
}
}
```

2. 井字棋

挑战你的策略思维!源代码:
```java
public class TicTacToe {
// 格子状态
private char[][] board;
// 当前玩家
private char player;
// 游戏结束标识
private boolean gameOver;
// 胜者
private char winner;
// 初始化游戏
public void init() {
board = new char[3][3];
player = 'X';
gameOver = false;
winner = ' ';
}
// 游戏循环
public void run() {
while (!gameOver) {
// 玩家下棋
makeMove();
// 检查是否分出胜负
if (checkWin()) {
gameOver = true;
winner = player;
}
// 切换玩家
player = player == 'X' ? 'O' : 'X';
}
}
// 玩家下棋
private void makeMove() {
int row, col;
do {
("玩家" + player + ",请输入行号和列号(0-2):");
Scanner scanner = new Scanner();
row = ();
col = ();
} while (!isValidMove(row, col));
board[row][col] = player;
}
// 检查是否分出胜负
private boolean checkWin() {
// 检查行
for (int i = 0; i < 3; i++) {
if (board[i][0] == board[i][1] && board[i][1] == board[i][2] && board[i][0] != ' ') {
return true;
}
}
// 检查列
for (int j = 0; j < 3; j++) {
if (board[0][j] == board[1][j] && board[1][j] == board[2][j] && board[0][j] != ' ') {
return true;
}
}
// 检查对角线
if (board[0][0] == board[1][1] && board[1][1] == board[2][2] && board[0][0] != ' ') {
return true;
}
if (board[0][2] == board[1][1] && board[1][1] == board[2][0] && board[0][2] != ' ') {
return true;
}
return false;
}
// 判断是否为合法走法
private boolean isValidMove(int row, int col) {
return row >= 0 && row < 3 && col >= 0 && col < 3 && board[row][col] == ' ';
}
}
```

3. 打砖块

考验你的手眼协调!源代码:
```java
public class Breakout {
// 球的位置和速度
private Coordinate ball;
private Coordinate ballVelocity;
// 砖块的位置
private List bricks;
// 球拍的位置和大小
private Coordinate paddle;
private int paddleWidth;
// 游戏结束标识
private boolean gameOver;
// 分数
private int score;
// 初始化游戏
public void init() {
ball = new Coordinate();
ballVelocity = new Coordinate();
bricks = new ArrayList();
paddle = new Coordinate();
paddleWidth = 100;
gameOver = false;
score = 0;
}
// 游戏循环
public void run() {
while (!gameOver) {
// 更新球的位置
updateBall();
// 检查球是否与砖块或球拍碰撞
checkCollision();
// 检查是否游戏结束
if (isGameOver()) {
gameOver = true;
}
}
}
// 更新球的位置
private void updateBall() {
(ballVelocity);
}
// 检查球是否与砖块或球拍碰撞
private void checkCollision() {
// 检查球是否与边界碰撞
if (() < 0 || () > WIDTH) {
(-());
}
if (() < 0) {
(-());
}
// 检查球是否与球拍碰撞
if (() >= HEIGHT - paddleWidth && () >= () && () = () && () = () && () HEIGHT || ();
}
}
```

4. 俄罗斯方块

经典永不落幕!源代码:
```java
public class Tetris {
// 方块的形状和坐标
private List blocks;
private Coordinate blockPosition;
private BlockType blockType;
// 游戏区域
private char[][] grid;
// 游戏结束标识
private boolean gameOver;
// 初始化游戏
public void init() {
blocks = new ArrayList();
blockPosition = new Coordinate();
blockType = ();
grid = new char[HEIGHT][WIDTH];
gameOver = false;
}
// 游戏循环
public void run() {
while (!gameOver) {
// 更新方块的位置
updateBlock();
// 检查方块是否着陆
if (isLanded()) {
fixBlock();
// 检查是否游戏结束
if (isGameOver()) {
gameOver = true;
} else {
// 生成新方块
blockType = ();
blockPosition = new Coordinate();
}

2024-10-14


上一篇:Java 数据结构与算法:全面的指南

下一篇:Java 代码编辑器的终极指南