本文目录
- 请JAVA小游戏的代码
- java小游戏编程
- 求java小游戏源代码
- JAVA小游戏程序代码
- java 小游戏
- 急求简单的手机java小游戏代码 能运行的 简单的就可以
请JAVA小游戏的代码
import java.awt.*; import java.awt.event.*; import javax.swing.*; import java.util.*; public class GreedSnake implements KeyListener{ JFrame mainFrame; Canvas paintCanvas; JLabel labelScore; SnakeModel snakeModel = null; public static final int canvasWidth = 200; public static final int canvasHeight = 300; public static final int nodeWidth = 10; public static final int nodeHeight = 10; public GreedSnake() { mainFrame = new JFrame(“GreedSnake“); Container cp = mainFrame.getContentPane(); labelScore = new JLabel(“Score:“); cp.add(labelScore, BorderLayout.NORTH); paintCanvas = new Canvas(); paintCanvas.setSize(canvasWidth+1,canvasHeight+1); paintCanvas.addKeyListener(this); cp.add(paintCanvas, BorderLayout.CENTER); JPanel panelButtom = new JPanel(); panelButtom.setLayout(new BorderLayout()); JLabel labelHelp; labelHelp = new JLabel(“PageUp, PageDown for speed;“, JLabel.CENTER); panelButtom.add(labelHelp, BorderLayout.NORTH); labelHelp = new JLabel(“ENTER or R or S for start;“, JLabel.CENTER); panelButtom.add(labelHelp, BorderLayout.CENTER); labelHelp = new JLabel(“SPACE or P for pause“,JLabel.CENTER); panelButtom.add(labelHelp, BorderLayout.SOUTH); cp.add(panelButtom,BorderLayout.SOUTH);
java小游戏编程
/** * File: ControlPanel.java * User:相思无偿 * Date: 2004.12.3 * Describe: 俄罗斯方块的 Java 实现 */import javax.swing.*;import javax.swing.border.Border;import javax.swing.border.EtchedBorder;import java.awt.*;import java.awt.event.*;/** * 控制面板类,继承自JPanel. * 上边安放预显窗口、等级、得分、控制按钮 * 主要用来控制游戏进程。 */class ControlPanel extends JPanel { private JTextField tfLevel = new JTextField(““ + ErsBlocksGame.DEFAULT_LEVEL), tfScore = new JTextField(“0“); private JButton btPlay = new JButton(“Play“), btPause = new JButton(“Pause“), btStop = new JButton(“Stop“), btTurnLevelUp = new JButton(“Turn hard“), btTurnLevelDown = new JButton(“Turn easy“); private JPanel plTip = new JPanel(new BorderLayout()); private TipPanel plTipBlock = new TipPanel(); private JPanel plInfo = new JPanel(new GridLayout(4, 1)); private JPanel plButton = new JPanel(new GridLayout(5, 1)); private Timer timer; private ErsBlocksGame game; private Border border = new EtchedBorder( EtchedBorder.RAISED, Color.white, new Color(148, 145, 140)); /** * 控制面板类的构造函数 * @param game ErsBlocksGame, ErsBoxesGame类的一个实例引用, * 方便直接控制ErsBoxesGame类的行为。 */ public ControlPanel(final ErsBlocksGame game) { setLayout(new GridLayout(3, 1, 0, 4)); this.game = game; plTip.add(new JLabel(“Next block“), BorderLayout.NORTH); plTip.add(plTipBlock); plTip.setBorder(border); plInfo.add(new JLabel(“Level“)); plInfo.add(tfLevel); plInfo.add(new JLabel(“Score“)); plInfo.add(tfScore); plInfo.setBorder(border); tfLevel.setEditable(false); tfScore.setEditable(false); plButton.add(btPlay); plButton.add(btPause); plButton.add(btStop); plButton.add(btTurnLevelUp); plButton.add(btTurnLevelDown); plButton.setBorder(border); add(plTip); add(plInfo); add(plButton); addKeyListener(new ControlKeyListener()); btPlay.addActionListener(new ActionListener() { public void actionPerformed(ActionEvent ae) { game.playGame(); } }); btPause.addActionListener(new ActionListener() { public void actionPerformed(ActionEvent ae) { if (btPause.getText().equals(new String(“Pause“))) { game.pauseGame(); } else { game.resumeGame(); } } }); btStop.addActionListener(new ActionListener() { public void actionPerformed(ActionEvent ae) { game.stopGame(); } }); btTurnLevelUp.addActionListener(new ActionListener() { public void actionPerformed(ActionEvent ae) { try { int level = Integer.parseInt(tfLevel.getText()); if (level 《 ErsBlocksGame.MAX_LEVEL) tfLevel.setText(““ + (level + 1)); } catch (NumberFormatException e) { } requestFocus(); } }); btTurnLevelDown.addActionListener(new ActionListener() { public void actionPerformed(ActionEvent ae) { try { int level = Integer.parseInt(tfLevel.getText()); if (level 》 1) tfLevel.setText(““ + (level - 1)); } catch (NumberFormatException e) { } requestFocus(); } }); addComponentListener(new ComponentAdapter() { public void componentResized(ComponentEvent ce) { plTipBlock.fanning(); } }); timer = new Timer(500, new ActionListener() { public void actionPerformed(ActionEvent ae) { tfScore.setText(““ + game.getScore()); int scoreForLevelUpdate = game.getScoreForLevelUpdate(); if (scoreForLevelUpdate 》= ErsBlocksGame.PER_LEVEL_SCORE && scoreForLevelUpdate 》 0) game.levelUpdate(); } }); timer.start(); } /** * 设置预显窗口的样式, * @param style int,对应ErsBlock类的STYLES中的28个值 */ public void setTipStyle(int style) { plTipBlock.setStyle(style); } /** * 取得用户设置的游戏等级。 * @return int, 难度等级,1 - ErsBlocksGame.MAX_LEVEL */ public int getLevel() { int level = 0; try { level = Integer.parseInt(tfLevel.getText()); } catch (NumberFormatException e) { } return level; } /** * 让用户修改游戏难度等级。 * @param level 修改后的游戏难度等级 */ public void setLevel(int level) { if (level 》 0 && level 《 11) tfLevel.setText(““ + level); } /** * 设置“开始“按钮的状态。 */ public void setPlayButtonEnable(boolean enable) { btPlay.setEnabled(enable); } public void setPauseButtonLabel(boolean pause) { btPause.setText(pause ? “Pause“ : “Continue“); } /** * 重置控制面板 */ public void reset() { tfScore.setText(“0“); plTipBlock.setStyle(0); } /** * 重新计算TipPanel里的boxes里的小框的大小 */ public void fanning() { plTipBlock.fanning(); } /** * 预显窗口的实现细节类 */ private class TipPanel extends JPanel { private Color backColor = Color.darkGray, frontColor = Color.lightGray; private ErsBox boxes = new ErsBox[ErsBlock.BOXES_ROWS][ErsBlock.BOXES_COLS]; private int style, boxWidth, boxHeight; private boolean isTiled = false; /** * 预显窗口类构造函数 */ public TipPanel() { for (int i = 0; i 《 boxes.length; i++) { for (int j = 0; j 《 boxes[i].length; j++) boxes[i][j] = new ErsBox(false); } } /** * 预显窗口类构造函数 * @param backColor Color, 窗口的背景色 * @param frontColor Color, 窗口的前景色 */ public TipPanel(Color backColor, Color frontColor) { this(); this.backColor = backColor; this.frontColor = frontColor; } /** * 设置预显窗口的方块样式 * @param style int,对应ErsBlock类的STYLES中的28个值 */ public void setStyle(int style) { this.style = style; repaint(); } /** * 覆盖JComponent类的函数,画组件。 * @param g 图形设备环境 */ public void paintComponent(Graphics g) { super.paintComponent(g); if (!isTiled) fanning(); int key = 0x8000; for (int i = 0; i 《 boxes.length; i++) { for (int j = 0; j 《 boxes[i].length; j++) { Color color = (((key & style) != 0) ? frontColor : backColor); g.setColor(color); g.fill3DRect(j * boxWidth, i * boxHeight, boxWidth, boxHeight, true); key 》》= 1; } } } /** * 根据窗口的大小,自动调整方格的尺寸 */ public void fanning() { boxWidth = getSize().width / ErsBlock.BOXES_COLS; boxHeight = getSize().height / ErsBlock.BOXES_ROWS; isTiled = true; } } private class ControlKeyListener extends KeyAdapter { public void keyPressed(KeyEvent ke) { if (!game.isPlaying()) return; ErsBlock block = game.getCurBlock(); switch (ke.getKeyCode()) { case KeyEvent.VK_DOWN: block.moveDown(); break; case KeyEvent.VK_LEFT: block.moveLeft(); break; case KeyEvent.VK_RIGHT: block.moveRight(); break; case KeyEvent.VK_UP: block.turnNext(); break; default: break; } } }}
求java小游戏源代码
表1. CheckerDrag.java // CheckerDrag.javaimport java.awt.*;import java.awt.event.*;public class CheckerDrag extends java.applet.Applet{ // Dimension of checkerboard square. // 棋盘上每个小方格的尺寸 final static int SQUAREDIM = 40; // Dimension of checkerboard -- includes black outline. // 棋盘的尺寸 – 包括黑色的轮廓线 final static int BOARDDIM = 8 * SQUAREDIM + 2; // Dimension of checker -- 3/4 the dimension of a square. // 棋子的尺寸 – 方格尺寸的3/4 final static int CHECKERDIM = 3 * SQUAREDIM / 4; // Square colors are dark green or white. // 方格的颜色为深绿色或者白色 final static Color darkGreen = new Color (0, 128, 0); // Dragging flag -- set to true when user presses mouse button over checker // and cleared to false when user releases mouse button. // 拖动标记 --当用户在棋子上按下鼠标按键时设为true, // 释放鼠标按键时设为false boolean inDrag = false; // Left coordinate of checkerboard’s upper-left corner. // 棋盘左上角的左方向坐标 int boardx; // Top coordinate of checkerboard’s upper-left corner. //棋盘左上角的上方向坐标 int boardy; // Left coordinate of checker rectangle origin (upper-left corner). // 棋子矩形原点(左上角)的左方向坐标 int ox; // Top coordinate of checker rectangle origin (upper-left corner). // 棋子矩形原点(左上角)的上方向坐标 int oy; // Left displacement between mouse coordinates at time of press and checker // rectangle origin. // 在按键时的鼠标坐标与棋子矩形原点之间的左方向位移 int relx; // Top displacement between mouse coordinates at time of press and checker // rectangle origin. // 在按键时的鼠标坐标与棋子矩形原点之间的上方向位移 int rely; // Width of applet drawing area. // applet绘图区域的宽度 int width; // Height of applet drawing area. // applet绘图区域的高度 int height; // Image buffer. // 图像缓冲 Image imBuffer; // Graphics context associated with image buffer. // 图像缓冲相关联的图形背景 Graphics imG; public void init () { // Obtain the size of the applet’s drawing area. // 获取applet绘图区域的尺寸 width = getSize ().width; height = getSize ().height; // Create image buffer. // 创建图像缓冲 imBuffer = createImage (width, height); // Retrieve graphics context associated with image buffer. // 取出图像缓冲相关联的图形背景 imG = imBuffer.getGraphics (); // Initialize checkerboard’s origin, so that board is centered. // 初始化棋盘的原点,使棋盘在屏幕上居中 boardx = (width - BOARDDIM) / 2 + 1; boardy = (height - BOARDDIM) / 2 + 1; // Initialize checker’s rectangle’s starting origin so that checker is // centered in the square located in the top row and second column from // the left. // 初始化棋子矩形的起始原点,使得棋子在第一行左数第二列的方格里居中 ox = boardx + SQUAREDIM + (SQUAREDIM - CHECKERDIM) / 2 + 1; oy = boardy + (SQUAREDIM - CHECKERDIM) / 2 + 1; // Attach a mouse listener to the applet. That listener listens for // mouse-button press and mouse-button release events. // 向applet添加一个用来监听鼠标按键的按下和释放事件的鼠标监听器 addMouseListener (new MouseAdapter () { public void mousePressed (MouseEvent e) { // Obtain mouse coordinates at time of press. // 获取按键时的鼠标坐标 int x = e.getX (); int y = e.getY (); // If mouse is over draggable checker at time // of press (i.e., contains (x, y) returns // true), save distance between current mouse // coordinates and draggable checker origin // (which will always be positive) and set drag // flag to true (to indicate drag in progress). // 在按键时如果鼠标位于可拖动的棋子上方 // (也就是contains (x, y)返回true),则保存当前 // 鼠标坐标与棋子的原点之间的距离(始终为正值)并且 // 将拖动标志设为true(用来表明正处在拖动过程中) if (contains (x, y)) { relx = x - ox; rely = y - oy; inDrag = true; } } boolean contains (int x, int y) { // Calculate center of draggable checker. // 计算棋子的中心位置 int cox = ox + CHECKERDIM / 2; int coy = oy + CHECKERDIM / 2; // Return true if (x, y) locates with bounds // of draggable checker. CHECKERDIM / 2 is the // radius. // 如果(x, y)仍处于棋子范围内则返回true // CHECKERDIM / 2为半径 return (cox - x) * (cox - x) + (coy - y) * (coy - y) 《 CHECKERDIM / 2 * CHECKERDIM / 2; } public void mouseReleased (MouseEvent e) { // When mouse is released, clear inDrag (to // indicate no drag in progress) if inDrag is // already set. // 当鼠标按键被释放时,如果inDrag已经为true, // 则将其置为false(用来表明不在拖动过程中) if (inDrag) inDrag = false; } }); // Attach a mouse motion listener to the applet. That listener listens // for mouse drag events. //向applet添加一个用来监听鼠标拖动事件的鼠标运动监听器 addMouseMotionListener (new MouseMotionAdapter () { public void mouseDragged (MouseEvent e) { if (inDrag) { // Calculate draggable checker’s new // origin (the upper-left corner of // the checker rectangle). // 计算棋子新的原点(棋子矩形的左上角) int tmpox = e.getX () - relx; int tmpoy = e.getY () - rely; // If the checker is not being moved // (at least partly) off board, // assign the previously calculated // origin (tmpox, tmpoy) as the // permanent origin (ox, oy), and // redraw the display area (with the // draggable checker at the new // coordinates). // 如果棋子(至少是棋子的一部分)没有被 // 移出棋盘,则将之前计算的原点 // (tmpox, tmpoy)赋值给永久性的原点(ox, oy), // 并且刷新显示区域(此时的棋子已经位于新坐标上) if (tmpox 》 boardx && tmpoy 》 boardy && tmpox + CHECKERDIM 《 boardx + BOARDDIM && tmpoy + CHECKERDIM 《 boardy + BOARDDIM) { ox = tmpox; oy = tmpoy; repaint (); } } } }); } public void paint (Graphics g) { // Paint the checkerboard over which the checker will be dragged. // 在棋子将要被拖动的位置上绘制棋盘 paintCheckerBoard (imG, boardx, boardy); // Paint the checker that will be dragged. // 绘制即将被拖动的棋子 paintChecker (imG, ox, oy); // Draw contents of image buffer. // 绘制图像缓冲的内容 g.drawImage (imBuffer, 0, 0, this); } void paintChecker (Graphics g, int x, int y) { // Set checker shadow color. // 设置棋子阴影的颜色 g.setColor (Color.black); // Paint checker shadow. // 绘制棋子的阴影 g.fillOval (x, y, CHECKERDIM, CHECKERDIM); // Set checker color. // 设置棋子颜色 g.setColor (Color.red); // Paint checker. // 绘制棋子 g.fillOval (x, y, CHECKERDIM - CHECKERDIM / 13, CHECKERDIM - CHECKERDIM / 13); } void paintCheckerBoard (Graphics g, int x, int y) { // Paint checkerboard outline. // 绘制棋盘轮廓线 g.setColor (Color.black); g.drawRect (x, y, 8 * SQUAREDIM + 1, 8 * SQUAREDIM + 1); // Paint checkerboard. // 绘制棋盘 for (int row = 0; row 《 8; row++) { g.setColor (((row & 1) != 0) ? darkGreen : Color.white); for (int col = 0; col 《 8; col++) { g.fillRect (x + 1 + col * SQUAREDIM, y + 1 + row * SQUAREDIM, SQUAREDIM, SQUAREDIM); g.setColor ((g.getColor () == darkGreen) ? Color.white : darkGreen); } } } // The AWT invokes the update() method in response to the repaint() method // calls that are made as a checker is dragged. The default implementation // of this method, which is inherited from the Container class, clears the // applet’s drawing area to the background color prior to calling paint(). // This clearing followed by drawing causes flicker. CheckerDrag overrides // update() to prevent the background from being cleared, which eliminates // the flicker. // AWT调用了update()方法来响应拖动棋子时所调用的repaint()方法。该方法从 // Container类继承的默认实现会在调用paint()之前,将applet的绘图区域清除 // 为背景色,这种绘制之后的清除就导致了闪烁。CheckerDrag重写了update()来 // 防止背景被清除,从而消除了闪烁。 public void update (Graphics g) { paint (g); }}
JAVA小游戏程序代码
这个是比较有名的那个烟花,不知道你有没有用:建个工程,以Fireworks为类即可import java.awt.*;import java.applet.*;import java.awt.event.*;import javax.swing.*;public class Fireworks extends Applet implements MouseListener,Runnable{ int x,y; int top,point;/***对小程序进行变量和颜色的初始化。*/ public void init() { x = 0; y = 0; //设置背景色为黑色 setBackground(Color.black); addMouseListener(this); } public void paint(Graphics g) { } /** *使该程序可以作为应用程序运行。 */ public static void main(String args) { Fireworks applet = new Fireworks(); JFrame frame = new JFrame(“TextAreaNew“); frame.addWindowListener(new WindowAdapter() { public void windowClosing(WindowEvent e){ System.exit(0); } }); frame.getContentPane().add( applet, BorderLayout.CENTER); frame.setSize(800,400); applet.init(); applet.start(); frame.setVisible(true); } /***程序主线程,对一个烟花进行绘制。*/ public void run() { //变量初始化 Graphics g1; g1 = getGraphics(); int y_move,y_click,x_click; int v; x_click = x; y_click = y; y_move = 400; v = 3; int r,g,b; while(y_move 》 y_click) { g1.setColor(Color.black); g1.fillOval(x_click,y_move,5,5); y_move -= 5; r = (((int)Math.round(Math.random()*4321))%200)+55; g = (((int)Math.round(Math.random()*4321))%200)+55; b = (((int)Math.round(Math.random()*4321))%200)+55; g1.setColor(new Color(r,g,b)); g1.fillOval(x_click,y_move,5,5); for(int j = 0 ;j《=10;j++) { if(r》55) r -= 20; if(g》55) g -= 20; if(b》55) b -=20; g1.setColor(new Color(r,g,b)); g1.fillOval(x_click,y_move+j*5,5,5); } g1.setColor(Color.black); g1.fillOval(x_click,y_move+5*10,5,5); try { Thread.currentThread().sleep(v++); } catch (InterruptedException e) {} } for(int j=12;j》=0;j--) { g1.setColor(Color.black); g1.fillOval(x_click,y_move+(j*5),5,5); try { Thread.currentThread().sleep((v++)/3); } catch (InterruptedException e) {} } y_move = 400; g1.setColor(Color.black); while(y_move 》 y_click) { g1.fillOval(x_click-2,y_move,9,5); y_move -= 5; } v = 15; for(int i=0;i《=25;i++) { r = (((int)Math.round(Math.random()*4321))%200)+55; g = (((int)Math.round(Math.random()*4321))%200)+55; b = (((int)Math.round(Math.random()*4321))%200)+55; g1.setColor(new Color(r,g,b)); g1.drawOval(x_click-3*i,y_click-3*i,6*i,6*i); if(i《23) { g1.drawOval(x_click-3*(i+1),y_click-3*(i+1),6*(i+1),6*(i+1)); g1.drawOval(x_click-3*(i+2),y_click-3*(i+2),6*(i+2),6*(i+2)); } try { Thread.currentThread().sleep(v++); } catch (InterruptedException e) {} g1.setColor(Color.black); g1.drawOval(x_click-3*i,y_click-3*i,6*i,6*i); }} /***对鼠标事件进行监听。*临听其鼠标按下事件。*当按下鼠标时,产生一个新线程。*/ public void mousePressed(MouseEvent e) { x = e.getX(); y = e.getY(); Thread one; one = new Thread(this); one.start(); one = null; } /** *实现MouseListener接中的方法。为一个空方法。 */ public void mouseReleased(MouseEvent e) { } /** *实现MouseListener接中的方法。为一个空方法。 */ public void mouseEntered(MouseEvent e) { } /** *实现MouseListener接中的方法。为一个空方法。 */ public void mouseExited(MouseEvent e) { } /** *实现MouseListener接中的方法。为一个空方法。 */ public void mouseClicked(MouseEvent e) { } }
java 小游戏
import java.util.Random;import javax.swing.*;import java.awt.BorderLayout;import java.awt.Container;import java.awt.event.*;public class SmallGame extends JFrame { private Random r; private String box = { “剪刀“, “石头“, “布“ }; private JComboBox choice; private JTextArea ta; private JLabel lb; private int win = 0; private int loss = 0; private int equal = 0; public SmallGame() { initial();//调用initial方法,就是下面定义的那个.该方法主要是初始界面. pack(); setTitle(“游戏主界面“); setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); setLocation(400, 300); setVisible(true); } public static void main(String args) { new SmallGame(); } public void initial() { r = new Random(); // 生成随机数 choice = new JComboBox();//初始化choice这个下拉框.也就是你选择出剪子还是石头什么的那个下拉框.. for (int i = 0; i 《 box.length; i++) {//为那个下拉框赋值.用前面定义的private String box = { “剪刀“, “石头“, “布“ };附值.这样下拉框就有三个选项了.. choice.addItem(box[i]); } ta = new JTextArea(3, 15);//初始化那个文本域3行15列 ta.setEditable(false);//让用户不能编辑那个文本域即不能在里面写东西 JButton okBut = new JButton(“出招“);//新建一个出招的按钮 okBut.addActionListener(new ActionListener() {//给出招按钮加个监听.意思就是监听着这个按钮看用户有没有点击它..如果点击就执行下面这个方法 public void actionPerformed(ActionEvent e) {//就是这个方法 ta.setText(getResult());//给那个文本域赋值..就是显示结果 赋值的是通过getResult()这个方法得到的返回值 getResult()这个方法下面会讲 lb.setText(getTotal());//给分数那个LABEL赋值..就是显示分数..赋值的是通过getTotal()这个方法得到的返回值 } }); JButton clearBut = new JButton(“清除分数“);//新建一个清楚分数的按钮 clearBut.addActionListener(new ActionListener() {//同上给他加个监听 public void actionPerformed(ActionEvent e) {//如果用户点击了就执行这个方法 ta.setText(““);//给文本域赋值为““..其实就是清楚他的内容 win = 0;//win赋值为0 loss = 0;//同上 equal = 0;//同上 lb.setText(getTotal());//给显示分数那个赋值..因为前面已经都赋值为0了..所以这句就是让显示分数那都为0 } }); lb = new JLabel(getTotal());//初始化那个显示分数的东西 JPanel choicePanel = new JPanel();//定义一个面板..面板就相当于一个画图用的东西..可以在上面加按钮啊文本域什么的.. choicePanel.add(choice);//把下拉框加到面板里 choicePanel.add(okBut);//把出招按钮加到面板里 choicePanel.add(clearBut);//把清楚分数按钮加到面板里 JScrollPane resultPanel = new JScrollPane(ta);//把文本域加到一个可滚动的面板里面..JScrollPane就是可滚动的面板..这样如果那个文本域内容太多就会出现滚动条..而不是变大 JPanel totalPanel = new JPanel();//再定义个面板..用来显示分数的.. totalPanel.add(lb);//把那个显示分数的label加到里面去 Container contentPane = getContentPane();//下面就是布局了.. contentPane.setLayout(new BorderLayout()); contentPane.add(choicePanel, BorderLayout.NORTH); contentPane.add(resultPanel, BorderLayout.CENTER); contentPane.add(totalPanel, BorderLayout.SOUTH); } public String getResult() {//获得结果的方法 返回值是一个String..这个返回值会用来显示在文本域里面 String tmp = ““; int boxPeop = choice.getSelectedIndex();//获得你选择的那个的索引..从0开始的.. int boxComp = getBoxComp();//获得电脑出的索引..就是随机的0-2的数 tmp += “你出:\t“ + box[boxPeop];//下面你应该明白了.. tmp += “\n电脑出:\t“ + box[boxComp]; tmp += “\n结果:\t“ + check(boxPeop, boxComp); return tmp; } public int getBoxComp() {//就是产生一个0-2的随机数.. return r.nextInt(3);//Random的nextInt(int i)方法就是产生一个[0-i)的随机整数 所以nextInt(3)就是的随机数 } public String check(int boxPeop, int boxComp) {//这个就是判断你选择的和电脑选择的比较结果..是输是赢还是平..boxPeop就是你选择的..boxComp就是电脑选择的.. String result = ““; if (boxPeop == (boxComp + 1) % 3) {//(boxComp + 1) % 3 电脑选择的加上1加除以3取余..也就是如果电脑选0这个就为1..这个判断的意思就是如果电脑选0并且你选1..那么就是电脑选了 //private String box = { “剪刀“, “石头“, “布“ };这里面下标为0的..你选了下标为1的..就是电脑选剪刀你选石头..所以你赢了..如果电脑选1..(boxComp + 1) % 3就为2..意思就是 //电脑选了石头你选了布..所以你赢了..另外一种情况你明白了撒..只有三种情况你赢..所以这里都包含了..也只包含了那三种.. result = “你赢了!“; win++;//赢了就让记你赢的次数的那个变量加1 } else if (boxPeop == boxComp) {//相等当然平手了 result = “平“; equal++;//同上了 } else {//除了赢和平当然就是输了.. result = “你输了!“; loss++;//同上 } return result; } public int getPoint() { return (win - loss) * 10; } public String getTotal() { return “赢:“ + win + “ 平:“ + equal + “ 输:“ + loss + “ 得分:“ + getPoint(); }}希望你能明白哈。。
急求简单的手机java小游戏代码 能运行的 简单的就可以
package test;import java.util.Random;import java.util.Scanner;public class TestGame {/*** @param args*/public static void main(String args) {// TODO Auto-generated method stub Scanner sc = new Scanner(System.in);int r = new Random().nextInt(999999);int count =0 ;while(true){ System.out.println(“猜数字游戏,请输入一个数0到999999,输入-1结束游戏:“);int i = sc.nextInt() ;if(i==-1){break ;}count ++ ;if(i《r){System.out.print(“你猜小了。“);System.out.println(“你已经猜了“+count+“次“);}else if(i》r){System.out.println(“你猜大了。“);System.out.println(“你已经猜了“+count+“次“);}else{System.out.println(“恭喜你大对了,但是没奖励!“);}} System.out.println(“游戏结束“);}}