Java AppleGame小游戏(第一版)

游戏规则:用户首先需要在提供的多种水果中挑选一种,并输入想要投入的金币个数来开始游戏。如果转盘转到的水果序号与用户输入的序号相同,则用户胜利并获得大量金币,否则用户失败,并扣除相应的金币。在一轮游戏结束后,用户可以选择是否进入下一轮游戏。

package com.yts.applegame;

import java.util.Scanner;

public class AppleGame_02 {
	public static int capmone = 100;// 本金

	public static void showMenu() {// 菜单页面
		System.out.println("*序号.名称----------倍数");
		System.out.println("*1.   苹果-------2");
		System.out.println("*2.   葡萄-------5");
		System.out.println("*3.   橘子-------10");
		System.out.println("*4.   香蕉-------15");
		System.out.println("*5.   西瓜-------20");
		System.out.println("*6.   水果拼盘----50");
		System.out.println("*当前剩余金币:" + capmone);
	}

	public static void vicOrDef(int a, int b, int c, int d) { // 用户胜负判断
		if (a == b) {
			System.out.println("恭喜,你猜对了!");
			capmone += c * (d - 1);
		} else {
			System.out.println("抱歉,你猜错了!");
			capmone -= c;
		}

	}

	public static void main(String[] args) {
		// 数据初始化
		int multi; // 翻倍倍数
		int bet; // 投入金币
		Scanner sc = new Scanner(System.in);
		int theSys;// 系统生成随机数,作为转盘转到的水果
		int theSyss; // 水果序号修正
		int theGuess; // 用户的选择
		int rflag;// 判断是否继续游戏

		System.out.println("******欢迎来到苹果机小游戏******");
		while (true) {
			theSys = (int) (Math.random() * 20 + 1);// 系统生成随机数,作为转盘转到的水果
			// System.out.println(theSys); //作弊器
			// 是否开始游戏
			System.out.println("*是否开始游戏:开始则输入1,退出则输入0");
			rflag = sc.nextInt();
			if (rflag == 0) {
				System.out.println("游戏结束,欢迎下次再来!");
				break;
			}
			showMenu();
			// 用户选择水果
			while (true) {
				System.out.print("*请输入你要挑选的水果所对应的序号(1~6之间):");
				theGuess = sc.nextInt();
				// 水果序号判定
				if (theGuess >= 1 && theGuess <= 6) {
					break;
				} else {
					System.out.println("*请输入正确的序号!");
				}
			}
			while (true) {
				// 用户投入金币
				System.out.print("请选择要投入的金币:");
				bet = sc.nextInt();
				// 投入金币数目判定
				if (bet > 0 && bet <= capmone) {
					break;
				} else {
					System.out.println("*请输入正确的金币数目:");
				}
			}
			// 苹果机结果判定
			System.out.println("-苹果机开始转动了-");
			System.out.println("-----结果是-----");
			switch (theSys) {
			case 1:
			case 2:
			case 3:
			case 4:
			case 5:
			case 6:
				System.out.println("------苹果------");
				theSyss = 1;
				multi = 2;
				vicOrDef(theGuess, theSyss, bet, multi);
				break;
			case 7:
			case 8:
			case 9:
			case 10:
			case 11:
				System.out.println("------葡萄------");
				theSyss = 2;
				multi = 5;
				vicOrDef(theGuess, theSyss, bet, multi);
				break;
			case 12:
			case 13:
			case 14:
			case 15:
				System.out.println("------橘子------");
				theSyss = 3;
				multi = 10;
				vicOrDef(theGuess, theSyss, bet, multi);
				break;
			case 16:
			case 17:
			case 18:
				System.out.println("------香蕉------");
				theSyss = 4;
				multi = 15;
				vicOrDef(theGuess, theSyss, bet, multi);
				break;
			case 19:
			case 20:
				System.out.println("------西瓜------");
				theSyss = 5;
				multi = 20;
				vicOrDef(theGuess, theSyss, bet, multi);
				break;
			case 21:
				System.out.println("-----水果拼盘-----");
				theSyss = 6;
				multi = 50;
				vicOrDef(theGuess, theSyss, bet, multi);
				break;
			}
			// 用户结果处理
			System.out.println("*当前剩余金币" + capmone);
			if (capmone <= 0) {
				System.out.println("*金币不足,游戏结束!");
				break;
			}
		}
	}

}

  • 0
    点赞
  • 9
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
好的,以下是一个简单的 Java 贪吃蛇小游戏的实现,供参考: ``` import javax.swing.*; import java.awt.*; import java.awt.event.*; import java.util.*; public class SnakeGame extends JPanel implements ActionListener { private final int WIDTH = 500; private final int HEIGHT = 500; private final int DOT_SIZE = 10; private final int ALL_DOTS = 900; private final int RAND_POS = 29; private final int DELAY = 140; private final int x[] = new int[ALL_DOTS]; private final int y[] = new int[ALL_DOTS]; private int dots; private int apple_x; private int apple_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 Image ball; private Image apple; private Image head; public SnakeGame() { addKeyListener(new TAdapter()); setBackground(Color.black); setPreferredSize(new Dimension(WIDTH, HEIGHT)); ImageIcon iid = new ImageIcon(this.getClass().getResource("dot.png")); ball = iid.getImage(); ImageIcon iia = new ImageIcon(this.getClass().getResource("apple.png")); apple = iia.getImage(); ImageIcon iih = new ImageIcon(this.getClass().getResource("head.png")); head = iih.getImage(); setFocusable(true); initGame(); } public void initGame() { dots = 3; for (int z = 0; z < dots; z++) { x[z] = 50 - z * 10; y[z] = 50; } locateApple(); timer = new Timer(DELAY, this); timer.start(); } public void paintComponent(Graphics g) { super.paintComponent(g); doDrawing(g); } private void doDrawing(Graphics g) { if (inGame) { g.drawImage(apple, apple_x, apple_y, this); for (int z = 0; z < dots; z++) { if (z == 0) { g.drawImage(head, x[z], y[z], this); } else { g.drawImage(ball, x[z], y[z], this); } } 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 - metr.stringWidth(msg)) / 2, HEIGHT / 2); } private void checkApple() { if ((x[0] == apple_x) && (y[0] == apple_y)) { dots++; locateApple(); } } private void move() { for (int z = dots; z > 0; z--) { x[z] = x[(z - 1)]; y[z] = y[(z - 1)]; } if (leftDirection) { x[0] -= DOT_SIZE; } if (rightDirection) { x[0] += DOT_SIZE; } if (upDirection) { y[0] -= DOT_SIZE; } if (downDirection) { y[0] += DOT_SIZE; } } private void checkCollision() { for (int z = dots; z > 0; z--) { if ((z > 4) && (x[0] == x[z]) && (y[0] == y[z])) { inGame = false; } } if (y[0] >= HEIGHT) { inGame = false; } if (y[0] < 0) { inGame = false; } if (x[0] >= WIDTH) { inGame = false; } if (x[0] < 0) { inGame = false; } if(!inGame) { timer.stop(); } } private void locateApple() { int r = (int) (Math.random() * RAND_POS); apple_x = ((r * DOT_SIZE)); r = (int) (Math.random() * RAND_POS); apple_y = ((r * DOT_SIZE)); } public void actionPerformed(ActionEvent e) { if (inGame) { checkApple(); checkCollision(); move(); } repaint(); } private class TAdapter extends KeyAdapter { 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; } } } } ``` 这是一个基于 Java 的面向对象的小游戏,主要包含了小蛇的移动、碰撞检测、吃苹果等基本功能。在代码中,使用了 JPanel 做画布,实现了 ActionListener 接口,用于监听定时器事件,以实现小蛇的自动运动。同时,使用了 KeyAdapter 类,实现了键盘事件监听,以控制小蛇的方向。 你可以在本地编译运行该程序,体验贪吃蛇小游戏的乐趣。

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值