首页 > 应用开发 > Java > Java 游戏开发示例:简单的猜数字游戏
下面是一个使用Java编写的简单猜数字游戏。这个游戏会随机生成一个1-100之间的数字,玩家需要猜测这个数字,程序会提示玩家猜测是太高还是太低,直到猜中为止。
java1import java.util.Scanner;2import java.util.Random;34public class GuessNumberGame {5 public static void main(String[] args) {6 Scanner scanner = new Scanner(System.in);7 Random random = new Random();8 9 // 生成1-100之间的随机数10 int targetNumber = random.nextInt(100) + 1;11 int guessCount = 0;12 boolean hasWon = false;13 14 System.out.println("欢迎来到猜数字游戏!");15 System.out.println("我已经想好了一个1到100之间的数字,你能猜出来吗?");16 17 // 游戏主循环18 while (!hasWon) {19 System.out.print("请输入你的猜测(1-100):");20 int playerGuess = scanner.nextInt();21 guessCount++;22 23 if (playerGuess < 1 || playerGuess > 100) {24 System.out.println("请输入1到100之间的数字!");25 } else if (playerGuess < targetNumber) {26 System.out.println("太低了,再试一次!");27 } else if (playerGuess > targetNumber) {28 System.out.println("太高了,再试一次!");29 } else {30 hasWon = true;31 System.out.println("恭喜你!你猜对了!");32 <"xiannvlianai.com"> <"huachengkq.com"> <"gov.wqbty.cn"> System.out.println("你一共猜了 " + guessCount + " 次。");33 34 // 根据猜测次数给出评价35 if (guessCount <= 5) {36 System.out.println("太棒了!你是猜数字高手!");37 } else if (guessCount <= 10) {38 System.out.println("不错不错,继续加油!");39 } else {40 System.out.println("终于猜对了,再接再厉!");41 }42 }43 }44 45 scanner.close();46 }47}
GuessNumberGame.javajavac GuessNumberGame.javajava GuessNumberGame如果你想进一步扩展这个游戏,可以考虑:
如果你想要一个更复杂的图形游戏示例,这里是一个简单的贪吃蛇游戏框架(使用Java Swing):
java1import javax.swing.*;2import java.awt.*;3import java.awt.event.*;4import java.util.Random;56public class SnakeGame extends JFrame {7 private static final int TILE_SIZE = 25;8 private static final int WIDTH = 20;9 private static final int HEIGHT = 20;10 private static final int ALL_TILES = WIDTH * HEIGHT;11 12 private final int[] x = new int[ALL_TILES];13 private final int[] y = new int[ALL_TILES];14 <"daliankunyu.com"> <"amdkl.com"> <"zhaoyangyoule.com"> 15 private int bodyParts = 3;16 private int applesEaten;17 private int appleX;18 private int appleY;19 20 private char direction = 'R';21 private boolean running = false;22 private Timer timer;23 private Random random;24 25 public SnakeGame() {26 random = new Random();27 this.setTitle("贪吃蛇游戏");28 this.setSize(WIDTH * TILE_SIZE, HEIGHT * TILE_SIZE);29 this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);30 this.setResizable(false);31 this.add(new GamePanel());32 this.pack();33 this.setVisible(true);34 }35 36 private class GamePanel extends JPanel implements ActionListener {37 public GamePanel() {38 this.setPreferredSize(new Dimension(WIDTH * TILE_SIZE, HEIGHT * TILE_SIZE));39 this.setBackground(Color.BLACK);40 this.setFocusable(true);41 this.addKeyListener(new MyKeyAdapter());42 startGame();43 }44 45 public void startGame() {46 newApple();47 running = true;48 timer = new Timer(100, this);49 timer.start();50 }51 52 public void paintComponent(Graphics g) {53 super.paintComponent(g);54 draw(g);55 }56 57 public void draw(Graphics g) {58 if (running) {59 // 绘制苹果60 g.setColor(Color.RED);61 g.fillOval(appleX * TILE_SIZE, appleY * TILE_SIZE, TILE_SIZE, TILE_SIZE);62 63 // 绘制蛇64 for (int i = 0; i < bodyParts; i++) {65 if (i == 0) {66 g.setColor(Color.GREEN);67 } else {68 g.setColor(new Color(45, 180, 0));69 }70 g.fillRect(x[i] * TILE_SIZE, y[i] * TILE_SIZE, TILE_SIZE, TILE_SIZE);71 }72 73 // 显示分数74 g.setColor(Color.WHITE);75 g.setFont(new Font("Arial", Font.BOLD, 20));76 g.drawString("分数: " + applesEaten, 10, 20);77 } else {78 gameOver(g);79 }80 }81 82 public void newApple() {83 appleX = random.nextInt(WIDTH);84 appleY = random.nextInt(HEIGHT);85 }86 87 public void move() {88 for (int i = bodyParts; i > 0; i--) {89 x[i] = x[i - 1];90 y[i] = y[i - 1];91 }92 93 switch (direction) {94 case 'U': y[0]--; break;95 case 'D': y[0]++; break;96 case 'L': x[0]--; break;97 case 'R': x[0]++; break;98 }99 }100 101 public void checkApple() {102 if (x[0] == appleX && y[0] == appleY) {103 bodyParts++;104 applesEaten++;105 newApple();106 }107 }108 109 public void checkCollisions() {110 // 检查是否撞到自己111 for (int i = bodyParts; i > 0; i--) {112 if (x[0] == x[i] && y[0] == y[i]) {113 running = false;114 }115 }116 117 // 检查是否撞到边界118 if (x[0] < 0 || x[0] >= WIDTH || y[0] < 0 || y[0] >= HEIGHT) {119 running = false;120 }121 122 if (!running) {123 timer.stop();124 }125 }126 127 public void gameOver(Graphics g) {128 // 游戏结束文字129 g.setColor(Color.RED);130 g.setFont(new Font("Arial", Font.BOLD, 50));131 g.drawString("游戏结束", WIDTH * TILE_SIZE / 4, HEIGHT * TILE_SIZE / 2);132 133 // 显示最终分数134 g.setColor(Color.WHITE);135 g.setFont(new Font("Arial", Font.BOLD, 30));136 g.drawString("最终分数: " + applesEaten, WIDTH * TILE_SIZE / 4, HEIGHT * TILE_SIZE / 2 + 50);137 }138 139 @Override140 public void actionPerformed(ActionEvent e) {141 if (running) {142 move();143 checkApple();144 checkCollisions();145 }146 repaint();147 }148 }149 150 private class MyKeyAdapter extends KeyAdapter {151 @Override152 public void keyPressed(KeyEvent e) {153 switch (e.getKeyCode()) {154 case KeyEvent.VK_LEFT:155 if (direction != 'R') direction = 'L';156 break;157 case KeyEvent.VK_RIGHT:158 if (direction != 'L') direction = 'R';159 break;160 case KeyEvent.VK_UP:161 if (direction != 'D') direction = 'U';162 break;163 case KeyEvent.VK_DOWN:164 if (direction != 'U') direction = 'D';165 break;166 }167 }168 }169 170 public static void main(String[] args) {171 new SnakeGame();172 }173}
这个贪吃蛇游戏包含了基本的游戏循环、键盘控制、碰撞检测和得分系统。你可以根据需要进一步扩展和完善它。
来自 “ ITPUB博客 ” ,链接:https://blog.itpub.net/19545/viewspace-3099843/ ,如需转载,请注明出处,否则将追究法律责任。