Java小游戏源代码的分享,让人们能够轻松开展游戏开发的工作。在这篇文章中,我们将介绍一些简单的Java小游戏,以及它们的完整源代码,供大家学习和使用。
1. “Connect Four”游戏源代码
“Connect Four”是一款简单的策略游戏,又称为“四子棋”游戏。在这款游戏中,两个玩家轮流放置红色或黄色的棋子,在垂直、水平或对角线方向上取得四个相邻的棋子即可获胜。
以下是该游戏的Java源代码:
```java
import java.util.Scanner;
public class ConnectFour {
public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
char[][] board = new char[6][7];
int count = 0;
boolean gameover = false;
//初始化游戏面板
for (int i = 0; i < 6; i++) {
for (int j = 0; j < 7; j++) {
board[i][j] = ' ';
}
}
//游戏循环开始
while (!gameover) {
//打印游戏面板
System.out.println(" 1 2 3 4 5 6 7");
System.out.println("---------------");
for (int i = 0; i < 6; i++) {
for (int j = 0; j < 7; j++) {
System.out.print("|" + board[i][j]);
}
System.out.println("|");
System.out.println("---------------");
}
//提示下棋玩家的编号
int player = count % 2 + 1;
System.out.println("Player " + player + ", please enter a column number (1-7) to drop your piece:");
//输入下棋列号
int col = sc.nextInt() - 1;
//将棋子放入相应的位置
for (int i = 5; i >= 0; i--) {
if (board[i][col] == ' ') {
if (player == 1) {
board[i][col] = 'R';
} else {
board[i][col] = 'Y';
}
break;
}
}
//检查是否有玩家获胜
gameover = checkWin(board);
//如果游戏结束,打印结果
if (gameover) {
System.out.println(" 1 2 3 4 5 6 7");
System.out.println("---------------");
for (int i = 0; i < 6; i++) {
for (int j = 0; j < 7; j++) {
System.out.print("|" + board[i][j]);
}
System.out.println("|");
System.out.println("---------------");
}
System.out.println("Congratulations! Player " + player + " wins!");
}
//如果没人获胜,增加计数器
count++;
}
}
//检查是否有玩家获胜的方法
private static boolean checkWin(char[][] board) {
//水平方向
for (int i = 0; i < 6; i++) {
for (int j = 0; j < 4; j++) {
if (board[i][j] == board[i][j + 1] && board[i][j] == board[i][j + 2]
&& board[i][j] == board[i][j + 3] && board[i][j] != ' ') {
return true;
}
}
}
//垂直方向
for (int i = 0; i < 3; i++) {
for (int j = 0; j < 7; j++) {
if (board[i][j] == board[i + 1][j] && board[i][j] == board[i + 2][j]
&& board[i][j] == board[i + 3][j] && board[i][j] != ' ') {
return true;
}
}
}
//对角线方向
for (int i = 0; i < 3; i++) {
for (int j = 0; j < 4; j++) {
if (board[i][j] == board[i + 1][j + 1] && board[i][j] == board[i + 2][j + 2]
&& board[i][j] == board[i + 3][j + 3] && board[i][j] != ' ') {
return true;
}
}
}
//反对角线方向
for (int i = 0; i < 3; i++) {
for (int j = 3; j < 7; j++) {
if (board[i][j] == board[i + 1][j - 1] && board[i][j] == board[i + 2][j - 2]
&& board[i][j] == board[i + 3][j - 3] && board[i][j] != ' ') {
return true;
}
}
}
return false;
}
}
```
2. “Snake Game”游戏源代码
“Snake Game”是一款经典的贪吃蛇游戏,玩家控制蛇在屏幕上移动并吃食物,随着蛇身越来越长,难度也逐渐增加。
以下是该游戏的Java源代码:
```java
import java.awt.Color;
import java.awt.Dimension;
import java.awt.Font;
import java.awt.Graphics;
import java.awt.Point;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.KeyEvent;
import java.awt.event.KeyListener;
import java.util.ArrayList;
import java.util.Random;
import javax.swing.JFrame;
import javax.swing.JPanel;
import javax.swing.Timer;
public class SnakeGame implements ActionListener, KeyListener {
public static SnakeGame snake;
public JFrame jframe;
public JPanel jpanel;
public Timer timer = new Timer(20, this);
public ArrayList
public static final int UP = 0, DOWN = 1, LEFT = 2, RIGHT = 3, SCALE = 10;
public int ticks = 0, direction = DOWN, score = 0, tailLength = 10, time;
public Point head, cherry;
public Random random;
public boolean gameOver = false;
//游戏入口
public SnakeGame() {
Dimension dim = new Dimension(600, 600);
jframe = new JFrame("Snake");
jframe.setVisible(true);
jframe.setSize(dim);
jframe.setResizable(false);
jframe.setLocationRelativeTo(null);
jframe.add(jpanel = new JPanel());
jpanel.setBackground(Color.BLACK);
jframe.addKeyListener(this);
startGame();
}
//初始化游戏
public void startGame() {
gameOver = false;
score = 0;
tailLength = 2;
time = 0;
direction = DOWN;
head = new Point(0, -1);
random = new Random();
snakeParts.clear();
cherry = new Point(random.nextInt(59), random.nextInt(59));
timer.start();
}
//游戏绘制
public void paintComponent(Graphics g) {
super.paintComponent(g);
g.setColor(Color.RED);
g.fillOval(cherry.x * SCALE, cherry.y * SCALE, SCALE, SCALE);
for (Point point : snakeParts) {
g.setColor(Color.GREEN);
g.fillRect(point.x * SCALE, point.y * SCALE, SCALE, SCALE);
}
//游戏结束
if (gameOver) {
g.setColor(Color.RED);
g.setFont(new Font("Comic Sans MS", Font.BOLD, 40));
g.drawString("Game Over!", 200, 300);
}
//显示得分
g.setColor(Color.WHITE);
g.setFont(new Font("Comic Sans MS", Font.PLAIN, 20));
g.drawString("Score: " + score, 10, 25);
//显示时间
g.setColor(Color.WHITE);
g.setFont(new Font("Comic Sans MS", Font.PLAIN, 20));
g.drawString("Time: " + time, 500, 25);
}
@Override
public void actionPerformed(ActionEvent arg0) {
jpanel.repaint();
ticks++;
//移动蛇的每个部分
if (ticks % 2 == 0 && head != null && !gameOver) {
time++;
snakeParts.add(new Point(head.x, head.y));
if (direction == UP) {
if (head.y - 1 >= 0 && noTailAt(head.x, head.y - 1)) {
head = new Point(head.x, head.y - 1);
} else {
gameOver = true;
}
}
if (direction == DOWN) {
if (head.y + 1 < 60 && noTailAt(head.x, head.y + 1)) {
head = new Point(head.x, head.y + 1);
} else {
gameOver = true;
}
}
if (direction == LEFT) {
if (head.x - 1 >= 0 && noTailAt(head.x - 1, head.y)) {
head = new Point(head.x - 1, head.y);
} else {
gameOver = true;
}
}
if (direction == RIGHT) {
if (head.x + 1 < 60 && noTailAt(head.x + 1, head.y)) {
head = new Point(head.x + 1, head.y);
} else {
gameOver = true;
}
}
//如果蛇吃到了食物,增加得分并生成新的食物
if (snakeParts.size() > tailLength) {
snakeParts.remove(0);
}
if (cherry != null) {
if (head.equals(cherry)) {
score += 10;
tailLength += 10;
cherry.setLocation(random.nextInt(59), random.nextInt(59));
}
}
}
}
//检查蛇尾是否与新位置重叠
public boolean noTailAt(int x, int y) {
for (Point point : snakeParts) {
if (point.equals(new Point(x, y))) {
return false;
}
}
return true;
}
//监听键盘方向键的按下事件
@Override
public void keyPressed(KeyEvent e) {
int i = e.getKeyCode();
if ((i == KeyEvent.VK_LEFT || i == KeyEvent.VK_A) && direction != RIGHT) {
direction = LEFT;
}
if ((i == KeyEvent.VK_RIGHT || i == KeyEvent.VK_D) && direction != LEFT) {
direction = RIGHT;
}
if ((i == KeyEvent.VK_UP || i == KeyEvent.VK_W) && direction != DOWN) {
direction = UP;
}
if ((i == KeyEvent.VK_DOWN || i == KeyEvent.VK_S) && direction != UP) {
direction = DOWN;
}
}
//未使用的接口方法
@Override
public void keyReleased(KeyEvent e) {
}
@Override
public void keyTyped(KeyEvent e) {
}
//主程序入口
public static void main(String[] args) {
snake = new SnakeGame();
}
}
```
总之,这两个Java小游戏源代码的分享,极大地方便了Java游戏开发人员的学习和实践,也是技术共享中的一个很好的例子。无论是刚刚开始学习Java编程的初学者,还是已经有一定Java编程基础的开发人员,都可以从中获益。