java小游戏简单代码
2024-10-20 19:29:09 作者:石家庄人才网
本篇文章给大家带来《java小游戏简单代码》,石家庄人才网对文章内容进行了深度展开说明,希望对各位有所帮助,记得收藏本站。
Java 语言凭借其强大的功能和跨平台特性,成为了游戏开发的热门选择。对于初学者而言,从简单的游戏入手是学习 Java 游戏编程的最佳途径。本文将介绍一些简单易懂的 Java 小游戏代码示例,帮助你快速入门 Java 游戏开发的世界。
1. 猜数字游戏
猜数字游戏是一个经典的入门级游戏,玩家需要猜测系统随机生成的数字。以下是一个简单的 Java 代码实现:
```javaimport java.util.Random;import java.util.Scanner;public class GuessNumber { public static void main(String[] args) { Random random = new Random(); int targetNumber = random.nextInt(100) + 1; // 生成1-100的随机数 int guessCount = 0; Scanner scanner = new Scanner(System.in); System.out.println("我已经想好了一个1到100之间的数字,你猜猜看!"); while (true) { System.out.print("请输入你的猜测:"); int guess = scanner.nextInt(); guessCount++; if (guess < targetNumber) { System.out.println("太低了!再试一次。"); } else if (guess > targetNumber) { System.out.println("太高了!再试一次。"); } else { System.out.println("恭喜你,你猜对了!你一共猜了 " + guessCount + " 次。"); break; } } }}```2. 贪吃蛇游戏
贪吃蛇是一款经典街机游戏,玩家控制一条蛇在游戏区域内移动,吃掉食物并不断变长。以下是一个简单的 Java 代码实现:
```javaimport javax.swing.○;import java.awt.○;import java.awt.event.ActionEvent;import java.awt.event.ActionListener;import java.awt.event.KeyEvent;import java.awt.event.KeyListener;import java.util.Random;public class SnakeGame extends JPanel implements ActionListener, KeyListener { private static final long serialVersionUID = 1L; private final int SIZE = 20; // 游戏方格大小 private final int WIDTH = 30; // 游戏区域宽度 private final int HEIGHT = 20; // 游戏区域高度 private int[] snakeX; // 蛇的身体的x坐标 private int[] snakeY; // 蛇的身体的y坐标 private int snakeLength; // 蛇的长度 private int foodX; // 食物x坐标 private int foodY; // 食物y坐标 private boolean leftDirection = false; private boolean rightDirection = true; private boolean upDirection = false; private boolean downDirection = false; private boolean inGame = true; private Timer timer; private final int DELAY = 100; public SnakeGame() { addKeyListener(this); setFocusable(true); setPreferredSize(new Dimension(WIDTH ○ SIZE, HEIGHT ○ SIZE)); setBackground(Color.black); startGame(); } private void startGame() { snakeLength = 3; snakeX = new int[WIDTH ○ HEIGHT]; snakeY = new int[WIDTH ○ HEIGHT]; for (int i = 0; i < snakeLength; i++) { snakeX[i] = WIDTH / 2 - i; snakeY[i] = HEIGHT / 2; } generateFood(); timer = new Timer(DELAY, this); timer.start(); } private void generateFood() { Random random = new Random(); foodX = random.nextInt(WIDTH); foodY = random.nextInt(HEIGHT); } @Override public void paintComponent(Graphics g) { super.paintComponent(g); draw(g); } private void draw(Graphics g) { if (inGame) { g.setColor(Color.green); g.fillRect(foodX ○ SIZE, foodY ○ SIZE, SIZE, SIZE); for (int i = 0; i < snakeLength; i++) { if (i == 0) { g.setColor(Color.yellow); } else { g.setColor(Color.red); } g.fillRect(snakeX[i] ○ SIZE, snakeY[i] ○ SIZE, SIZE, SIZE); } Toolkit.getDefaultToolkit().sync(); } else { gameOver(g); } } private void gameOver(Graphics g) { String msg = "Game Over"; Font small = new Font("Helvetica", Font.BOLD, 14); FontMetrics metr = getFontMetrics(small); g.setColor(Color.white); g.setFont(small); g.drawString(msg, (WIDTH ○ SIZE - metr.stringWidth(msg)) / 2, (HEIGHT ○ SIZE) / 2); } private void move() { for (int i = snakeLength; i > 0; i--) { snakeX[i] = snakeX[i - 1]; snakeY[i] = snakeY[i - 1]; } if (leftDirection) { snakeX[0]--; } if (rightDirection) { snakeX[0]++; } if (upDirection) { snakeY[0]--; } if (downDirection) { snakeY[0]++; } if (snakeX[0] == foodX && snakeY[0] == foodY) { snakeLength++; generateFood(); } if (snakeX[0] < 0 || snakeX[0] >= WIDTH || snakeY[0] < 0 || snakeY[0] >= HEIGHT) { inGame = false; } for (int i = snakeLength; i > 0; i--) { if ((i > 4) && (snakeX[0] == snakeX[i]) && (snakeY[0] == snakeY[i])) { inGame = false; } } if (!inGame) { timer.stop(); } } @Override public void actionPerformed(ActionEvent e) { if (inGame) { move(); checkCollision(); } repaint(); } private void checkCollision() { // 检查蛇是否撞到自身或边界 } @Override public void keyPressed(KeyEvent e) { int key = e.getKeyCode(); if ((key == KeyEvent.VK_LEFT) && (!rightDirection)) { leftDirection = true; upDirection = false; downDirection = false; } if ((key == KeyEvent.VK_RIGHT) && (!leftDirection)) { rightDirection = true; upDirection = false; downDirection = false; } if ((key == KeyEvent.VK_UP) && (!downDirection)) { upDirection = true; rightDirection = false; leftDirection = false; } if ((key == KeyEvent.VK_DOWN) && (!upDirection)) { downDirection = true; rightDirection = false; leftDirection = false; } } @Override public void keyReleased(KeyEvent e) { } @Override public void keyTyped(KeyEvent e) { } public static void main(String[] args) { JFrame frame = new JFrame("贪吃蛇"); frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); frame.getContentPane().add(new SnakeGame()); frame.pack(); frame.setLocationRelativeTo(null); frame.setVisible(true); }}```3. 俄罗斯方块游戏
俄罗斯方块是一款风靡全球的益智游戏,玩家需要旋转、移动和放置不同形状的方块,以消除完整行。以下是一个简单的 Java 代码实现:
```javaimport java.awt.Color;import java.awt.Font;import java.awt.Graphics;import java.- 上一篇:php编程软件手机版下载
- 下一篇:返回列表
版权声明:《java小游戏简单代码》来自【石家庄人才网】收集整理于网络,不代表本站立场,所有图片文章版权属于原作者,如有侵略,联系删除。
https://www.ymil.cn/quanzi/19492.html