上班摸鱼用Java写几个小游戏之俄罗斯方块、五子棋

基于Java swing开发的俄罗斯方块和五子棋游戏,借鉴B站视频,无聊时写的,不得不说Java在GUI这块缺失不太友好,但写几个小游戏还是够的。五子棋机器最优算法采取最简单的五元组评分算法。俄罗斯方块就是普通的逻辑,没有什么算法。
五子棋游戏支持人机模式和联机对战模式,人机模式使用五元组评分算法获取机器落子最优解。支持悔棋和提示功能。
联机对战模式使用socket进行客户端玩家通信,先启动server类,启动服务器,而后启动client客户端类,输入服务器地址进行连接。

在这里插入图片描述

在这里插入图片描述
在这里插入图片描述
在这里插入图片描述
在这里插入图片描述

在这里插入图片描述
在这里插入图片描述

  • 6
    点赞
  • 8
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
要用Java一个俄罗斯方块小游戏,可以按照以下步骤进行: 1. 创建一个Java项目,并添加必要的库文件。 2. 创建一个游戏窗口,可以使用Java Swing或JavaFX等GUI库。 3. 实现俄罗斯方块的基本逻辑,包括方块的生成、下落、旋转、移动等操作。 4. 实现游戏的计分、等级、速度等功能。 5. 添加游戏音效和背景音乐。 6. 测试游戏并进行调试。 以下是一个简单的示例代码,可以作为参考: ``` import java.awt.Color; import java.awt.Graphics; import java.awt.event.ActionEvent; import java.awt.event.ActionListener; import java.awt.event.KeyEvent; import java.awt.event.KeyListener; import java.util.Random; import javax.swing.JFrame; import javax.swing.JPanel; import javax.swing.Timer; public class Tetris extends JPanel implements ActionListener, KeyListener { private static final long serialVersionUID = 1L; private static final int WIDTH = 300; private static final int HEIGHT = 600; private static final int BLOCK_SIZE = 30; private int[][] board = new int[20][10]; private int[][][] shapes = { {{1, 1, 1, 1}}, // I {{1, 1, 0}, {0, 1, 1}}, // Z {{0, 1, 1}, {1, 1, 0}}, // S {{1, 1, 1}, {0, 1, 0}}, // T {{1, 1, 1}, {1, 0, 0}}, // L {{1, 1, 1}, {0, 0, 1}}, // J {{1, 1}, {1, 1}} // O }; private Color[] colors = { Color.CYAN, Color.RED, Color.GREEN, Color.MAGENTA, Color.ORANGE, Color.BLUE, Color.YELLOW }; private int currentShape; private int currentX; private int currentY; private Timer timer; private boolean isGameOver; public Tetris() { setPreferredSize(new Dimension(WIDTH, HEIGHT)); setBackground(Color.WHITE); addKeyListener(this); timer = new Timer(500, this); timer.start(); newShape(); } private void newShape() { currentShape = new Random().nextInt(shapes.length); currentX = 4; currentY = 0; if (!isValidMove(currentShape, currentX, currentY)) { isGameOver = true; timer.stop(); } } private boolean isValidMove(int shape, int x, int y) { int[][] current = shapes[shape]; for (int i = 0; i < current.length; i++) { for (int j = 0; j < current[i].length; j++) { if (current[i][j] != 0 && (y + i >= board.length || x + j < 0 || x + j >= board[0].length || board[y + i][x + j] != 0)) { return false; } } } return true; } private void move(int dx, int dy) { if (!isValidMove(currentShape, currentX + dx, currentY + dy)) { return; } currentX += dx; currentY += dy; repaint(); } private void rotate() { int[][] current = shapes[currentShape]; int[][] rotated = new int[current[0].length][current.length]; for (int i = 0; i < current.length; i++) { for (int j = 0; j < current[i].length; j++) { rotated[j][current.length - 1 - i] = current[i][j]; } } if (isValidMove(currentShape, currentX, currentY, rotated)) { shapes[currentShape] = rotated; repaint(); } } private boolean isValidMove(int shape, int x, int y, int[][] rotated) { for (int i = 0; i < rotated.length; i++) { for (int j = 0; j < rotated[i].length; j++) { if (rotated[i][j] != 0 && (y + i >= board.length || x + j < 0 || x + j >= board[0].length || board[y + i][x + j] != 0)) { return false; } } } return true; } private void drop() { while (isValidMove(currentShape, currentX, currentY + 1)) { currentY++; } place(); } private void place() { int[][] current = shapes[currentShape]; for (int i = 0; i < current.length; i++) { for (int j = 0; j < current[i].length; j++) { if (current[i][j] != 0) { board[currentY + i][currentX + j] = currentShape + 1; } } } checkLines(); newShape(); } private void checkLines() { int lines = 0; for (int i = board.length - 1; i >= 0; i--) { boolean isLine = true; for (int j = 0; j < board[i].length; j++) { if (board[i][j] == 0) { isLine = false; break; } } if (isLine) { lines++; for (int k = i; k > 0; k--) { System.arraycopy(board[k - 1], 0, board[k], 0, board[k].length); } for (int j = 0; j < board[0].length; j++) { board[0][j] = 0; } } } if (lines > 0) { // update score, level, speed, etc. } } @Override public void actionPerformed(ActionEvent e) { if (isGameOver) { return; } if (isValidMove(currentShape, currentX, currentY + 1)) { currentY++; } else { place(); } repaint(); } @Override public void keyPressed(KeyEvent e) { switch (e.getKeyCode()) { case KeyEvent.VK_LEFT: move(-1, 0); break; case KeyEvent.VK_RIGHT: move(1, 0); break; case KeyEvent.VK_DOWN: move(0, 1); break; case KeyEvent.VK_UP: rotate(); break; case KeyEvent.VK_SPACE: drop(); break; } } @Override public void keyTyped(KeyEvent e) { } @Override public void keyReleased(KeyEvent e) { } @Override protected void paintComponent(Graphics g) { super.paintComponent(g); for (int i = 0; i < board.length; i++) { for (int j = 0; j < board[i].length; j++) { if (board[i][j] != 0) { g.setColor(colors[board[i][j] - 1]); g.fillRect(j * BLOCK_SIZE, i * BLOCK_SIZE, BLOCK_SIZE, BLOCK_SIZE); g.setColor(Color.BLACK); g.drawRect(j * BLOCK_SIZE, i * BLOCK_SIZE, BLOCK_SIZE, BLOCK_SIZE); } } } int[][] current = shapes[currentShape]; for (int i = 0; i < current.length; i++) { for (int j = 0; j < current[i].length; j++) { if (current[i][j] != 0) { g.setColor(colors[currentShape]); g.fillRect((currentX + j) * BLOCK_SIZE, (currentY + i) * BLOCK_SIZE, BLOCK_SIZE, BLOCK_SIZE); g.setColor(Color.BLACK); g.drawRect((currentX + j) * BLOCK_SIZE, (currentY + i) * BLOCK_SIZE, BLOCK_SIZE, BLOCK_SIZE); } } } } public static void main(String[] args) { JFrame frame = new JFrame("Tetris"); frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); frame.setResizable(false); frame.add(new Tetris()); frame.pack(); frame.setLocationRelativeTo(null); frame.setVisible(true); } } ```
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值