您的位置:

java游戏代码,java游戏代码怎么运行

本文目录一览:

java代码猜拳游戏相关代码请教

comp是电脑产生的随机数字(电脑出的拳),people 是人出的拳。 因为剪刀石头布只有 1 2 3

。如果电脑的数字比的你刚好大1,就是它比你的大。 如21,32对应就是(石头大于剪刀,布大于石头)。 但也有可能是剪刀大于布 。 那么剪刀的位子是1 ,布的位子是3. 所以当电脑数字减你的数字等于2时 就是(电脑出的布 ,你出的石头这样的情况了)。

所以是if((comp-people)==-1||(comp-people)==2) 这两者结合就是你赢的时候

运行在Eclipse环境下的java扫雷游戏的初级代码是什么?

import java.awt.Button;\x0d\x0aimport java.util.Set;\x0d\x0a// 每一个小方块类\x0d\x0apublic class Diamond extends Button {\x0d\x0aprivate Diamond[] diamonds;\x0d\x0a\x0d\x0a// 该小方块周围的八个方向上的小方块\x0d\x0aprivate Diamond east;\x0d\x0aprivate Diamond north;\x0d\x0aprivate Diamond northEast;\x0d\x0aprivate Diamond northWest;\x0d\x0aprivate Diamond south;\x0d\x0aprivate Diamond southEast;\x0d\x0aprivate Diamond southWest;\x0d\x0aprivate Diamond west;\x0d\x0a\x0d\x0aprivate boolean isBomb;// 是否是雷\x0d\x0aprivate boolean isChange;// 又没有被翻过\x0d\x0aprivate int no;// 产生的方块的编号\x0d\x0a\x0d\x0a// 持有所有小方块的引用,方便进行操作\x0d\x0apublic Diamond(Diamond[] diamonds) {\x0d\x0athis.diamonds = diamonds;\x0d\x0a}\x0d\x0a\x0d\x0a// 按键时方块发生改变\x0d\x0apublic boolean change() {\x0d\x0athis.isChange = true;// 说明已经翻过了\x0d\x0aif(isBomb) {// 触雷\x0d\x0a//this.setBackground(Color.red);\x0d\x0areturn true;\x0d\x0a} else {// 不是雷,就显示周围雷的数目\x0d\x0a//this.setLabel(this.getNearBombNo() + "");\x0d\x0athis.setLabel(this.getNearBombNo() + "");\x0d\x0a//if(this.getNearBombNo() == 0) {\x0d\x0a//this.moveon();\x0d\x0a//}\x0d\x0areturn false;\x0d\x0a}\x0d\x0a}\x0d\x0a\x0d\x0a// 获得该小方块周围雷的数量\x0d\x0apublic int getNearBombNo() {\x0d\x0aint no = 0;\x0d\x0aif(this.northWest != null this.northWest.isBomb) no++;\x0d\x0aif(this.north != null this.north.isBomb) no++;\x0d\x0aif(this.northEast != null this.northEast.isBomb) no++;\x0d\x0aif(this.east != null this.east.isBomb) no++;\x0d\x0aif(this.southEast != null this.southEast.isBomb) no++;\x0d\x0aif(this.south != null this.south.isBomb) no++;\x0d\x0aif(this.southWest != null this.southWest.isBomb) no++;\x0d\x0aif(this.west != null this.west.isBomb) no++;\x0d\x0a\x0d\x0areturn no;\x0d\x0a}\x0d\x0a\x0d\x0a// 获得该小方块周围的小方块\x0d\x0apublic Diamond getNearDimaond(int i) {\x0d\x0aint index = -1;\x0d\x0aswitch (i) {\x0d\x0acase 1:// 1表示西北,2,表示北,以此类推\x0d\x0aindex = no - 10;\x0d\x0aif(index return null;\x0d\x0a} else {\x0d\x0areturn diamonds[index];\x0d\x0a}\x0d\x0acase 2:\x0d\x0aindex = no - 9;\x0d\x0aif(index return null;\x0d\x0a} else {\x0d\x0areturn diamonds[index];\x0d\x0a}\x0d\x0acase 3:\x0d\x0aindex = no - 8;\x0d\x0aif(index return null;\x0d\x0a} else {\x0d\x0areturn diamonds[index];\x0d\x0a}\x0d\x0acase 4:\x0d\x0aindex = no + 1;\x0d\x0aif(no == 9 || no == 18 || no == 27 || no == 36 || no == 45 || no == 54 || no == 63 || no == 72 || no == 81) {\x0d\x0areturn null;\x0d\x0a} else {\x0d\x0areturn diamonds[index];\x0d\x0a}\x0d\x0acase 5:\x0d\x0aindex = no + 10;\x0d\x0aif(index = 81 ||no == 9 || no == 18 || no == 27 || no == 36 || no == 45 || no == 54 || no == 63 || no == 72 || no == 81) {\x0d\x0areturn null;\x0d\x0a} else {\x0d\x0areturn diamonds[index];\x0d\x0a}\x0d\x0acase 6:\x0d\x0aindex = no + 9;\x0d\x0aif(index 81) {\x0d\x0areturn null;\x0d\x0a} else {\x0d\x0areturn diamonds[index];\x0d\x0a}\x0d\x0acase 7:\x0d\x0aindex = no + 8;\x0d\x0aif(index = 81 || no==1 || no == 10 || no == 19 || no == 28 || no == 37 || no == 46 || no == 55 || no == 64 || no == 73) {\x0d\x0areturn null;\x0d\x0a} else {\x0d\x0areturn diamonds[index];\x0d\x0a}\x0d\x0acase 8:\x0d\x0aindex = no - 1;\x0d\x0aif(no==1 || no==10 || no == 19 || no == 28 || no == 37 || no == 46 || no == 55 || no == 64 || no == 73) {\x0d\x0areturn null;\x0d\x0a} else {\x0d\x0areturn diamonds[index];\x0d\x0a}\x0d\x0a}\x0d\x0areturn null;\x0d\x0a}\x0d\x0a\x0d\x0a// 递归,set是用来装已经翻过的小方块的,不然会死循环,为什么用set,因为set是不重复的\x0d\x0apublic void moveon(Set set) {\x0d\x0a\x0d\x0aset.add(this);// 先把自己加上\x0d\x0aif(this.getNorthWest() != null this.getNorthWest().isBomb == false) {\x0d\x0athis.getNorthWest().change();\x0d\x0a\x0d\x0aif(this.getNorthWest().getNearBombNo() == 0) {\x0d\x0aif(set.contains(this.getNorthWest()) == false)\x0d\x0athis.getNorthWest().moveon(set);\x0d\x0a}\x0d\x0a\x0d\x0aset.add(this.getNorthWest());\x0d\x0a}\x0d\x0a\x0d\x0aif(this.getNorth() != null this.getNorth().isBomb == false) {\x0d\x0athis.getNorth().change();\x0d\x0aif(this.getNorth().getNearBombNo() == 0) {\x0d\x0aif(set.contains(this.getNorth()) == false)\x0d\x0athis.getNorth().moveon(set);\x0d\x0a}\x0d\x0a\x0d\x0aset.add(this.getNorth());\x0d\x0a} \x0d\x0a\x0d\x0aif(this.getNorthEast() != null this.getNorthEast().isBomb == false) {\x0d\x0athis.getNorthEast().change();\x0d\x0aif(this.getNorthEast().getNearBombNo() == 0) {\x0d\x0aif(set.contains(this.getNorthEast()) == false)\x0d\x0athis.getNorthEast().moveon(set);\x0d\x0a}\x0d\x0a\x0d\x0aset.add(this.getNorthEast());\x0d\x0a} \x0d\x0a\x0d\x0aif(this.getEast() != null this.getEast().isBomb == false) {\x0d\x0athis.getEast().change();\x0d\x0aif(this.getEast().getNearBombNo() == 0) {\x0d\x0aif(set.contains(this.getEast()) == false)\x0d\x0athis.getEast().moveon(set);\x0d\x0a}\x0d\x0a\x0d\x0aset.add(this.getEast());\x0d\x0a} \x0d\x0a\x0d\x0aif(this.getSouthEast() != null this.getSouthEast().isBomb == false) {\x0d\x0athis.getSouthEast().change();\x0d\x0aif(this.getSouthEast().getNearBombNo() == 0) {\x0d\x0aif(set.contains(this.getSouthEast()) == false)\x0d\x0athis.getSouthEast().moveon(set);\x0d\x0a}\x0d\x0a\x0d\x0aset.add(this.getSouthEast());\x0d\x0a} \x0d\x0a\x0d\x0aif(this.getSouth() != null this.getSouth().isBomb == false) {\x0d\x0athis.getSouth().change();\x0d\x0aif(this.getSouth().getNearBombNo() == 0) {\x0d\x0aif(set.contains(this.getSouth()) == false)\x0d\x0athis.getSouth().moveon(set);\x0d\x0a}\x0d\x0a\x0d\x0aset.add(this.getSouth());\x0d\x0a} \x0d\x0a\x0d\x0aif(this.getSouthWest() != null this.getSouthWest().isBomb == false) {\x0d\x0athis.getSouthWest().change();\x0d\x0aif(this.getSouthWest().getNearBombNo() == 0) {\x0d\x0aif(set.contains(this.getSouthWest()) == false)\x0d\x0athis.getSouthWest().moveon(set);\x0d\x0a}\x0d\x0a\x0d\x0aset.add(this.getSouthWest());\x0d\x0a} \x0d\x0a\x0d\x0aif(this.getWest() != null this.getWest().isBomb == false) {\x0d\x0athis.getWest().change();\x0d\x0aif(this.getWest().getNearBombNo() == 0) {\x0d\x0aif(set.contains(this.getWest()) == false)\x0d\x0athis.getWest().moveon(set);\x0d\x0a}\x0d\x0a\x0d\x0aset.add(this.getWest());\x0d\x0a} \x0d\x0a}\x0d\x0a\x0d\x0a/*public Diamond[] getDiamonds() {\x0d\x0areturn diamonds;\x0d\x0a}*/\x0d\x0a\x0d\x0apublic Diamond getEast() {\x0d\x0areturn east;\x0d\x0a}\x0d\x0a\x0d\x0apublic int getNo() {\x0d\x0areturn no;\x0d\x0a}\x0d\x0a\x0d\x0apublic Diamond getNorth() {\x0d\x0areturn north;\x0d\x0a}\x0d\x0a\x0d\x0apublic Diamond getNorthEast() {\x0d\x0areturn northEast;\x0d\x0a}\x0d\x0a\x0d\x0apublic Diamond getNorthWest() {\x0d\x0areturn northWest;\x0d\x0a}\x0d\x0a\x0d\x0apublic Diamond getSouth() {\x0d\x0areturn south;\x0d\x0a}\x0d\x0a\x0d\x0apublic Diamond getSouthEast() {\x0d\x0areturn southEast;\x0d\x0a}\x0d\x0a\x0d\x0apublic Diamond getSouthWest() {\x0d\x0areturn southWest;\x0d\x0a}\x0d\x0a\x0d\x0apublic Diamond getWest() {\x0d\x0areturn west;\x0d\x0a}\x0d\x0a\x0d\x0apublic boolean isBomb() {\x0d\x0areturn isBomb;\x0d\x0a}\x0d\x0a\x0d\x0apublic boolean isChange() {\x0d\x0areturn isChange;\x0d\x0a}\x0d\x0a\x0d\x0apublic void setBomb(boolean isBomb) {\x0d\x0athis.isBomb = isBomb;\x0d\x0a}\x0d\x0a\x0d\x0apublic void setChange(boolean isChange) {\x0d\x0athis.isChange = isChange;\x0d\x0a}\x0d\x0a\x0d\x0apublic void setDiamonds(Diamond[] diamonds) {\x0d\x0athis.diamonds = diamonds;\x0d\x0a}\x0d\x0a\x0d\x0apublic void setEast(Diamond east) {\x0d\x0athis.east = east;\x0d\x0a}\x0d\x0a\x0d\x0apublic void setNo(int no) {\x0d\x0athis.no = no;\x0d\x0a}\x0d\x0a\x0d\x0apublic void setNorth(Diamond north) {\x0d\x0athis.north = north;\x0d\x0a}\x0d\x0a\x0d\x0apublic void setNorthEast(Diamond northEast) {\x0d\x0athis.northEast = northEast;\x0d\x0a}\x0d\x0a\x0d\x0apublic void setNorthWest(Diamond northWest) {\x0d\x0athis.northWest = northWest;\x0d\x0a}\x0d\x0a\x0d\x0apublic void setSouth(Diamond south) {\x0d\x0athis.south = south;\x0d\x0a}\x0d\x0a\x0d\x0apublic void setSouthEast(Diamond southEast) {\x0d\x0athis.southEast = southEast;\x0d\x0a}\x0d\x0a\x0d\x0apublic void setSouthWest(Diamond southWest) {\x0d\x0athis.southWest = southWest;\x0d\x0a}\x0d\x0a\x0d\x0apublic void setWest(Diamond west) {\x0d\x0athis.west = west;\x0d\x0a}\x0d\x0a\x0d\x0a}

基于Java语言的打地鼠的小游戏源代码是什么?

 public void mouseClicked(MouseEvent e){\x0d\x0aObject source=e.getSource(); //获取事件源,即地鼠标签\x0d\x0aif(source instanceof JLabel){ //如果事件是标签组件\x0d\x0aJLabel mouse=(JLabel)source; //强制转换为JLabel标签\x0d\x0amouse.setIcon(null); //取消标签图标\x0d\x0a}\x0d\x0a}\x0d\x0a});\x0d\x0athis.getContentPane().add(mouses[i]); //添加显示地鼠的标签到窗体\x0d\x0a}\x0d\x0a\x0d\x0amouses[0].setLocation(253, 300); //设置每个标签的位置\x0d\x0amouses[1].setLocation(333, 250);\x0d\x0amouses[2].setLocation(388, 296);\x0d\x0amouses[3].setLocation(362, 364);\x0d\x0amouses[4].setLocation(189, 353);\x0d\x0amouses[5].setLocation(240, 409);\x0d\x0a\x0d\x0afinal JLabel backLabel=new JLabel(); //创建显示背景的标签\x0d\x0abackLabel.setBounds(0, 0, img.getIconWidth(), img.getIconHeight());\x0d\x0athis.setBounds(100,100,img.getIconWidth(),img.getIconHeight());\x0d\x0abackLabel.setIcon(img); //添加背景到标签\x0d\x0athis.getContentPane().add(backLabel); //添加背景标签到窗体\x0d\x0a}\x0d\x0a/**\x0d\x0a* 线程的核心方法\x0d\x0a*/\x0d\x0a\x0d\x0apublic void run(){\x0d\x0awhile(true){ //使用无限循环\x0d\x0atry{\x0d\x0aThread.sleep(3000); //使线程休眠3秒\x0d\x0aint index=(int)(Math.random()*6); //生成随机的地鼠索引\x0d\x0aif(mouses[index].getIcon()==null){ //如果地鼠标签没有设置图片\x0d\x0amouses[index].setIcon(imgMouse); //为该标签添加地鼠图片\x0d\x0a}\x0d\x0a}catch(InterruptedException e){\x0d\x0ae.printStackTrace();\x0d\x0a}\x0d\x0a}\x0d\x0a}\x0d\x0a\x0d\x0a}

求一份抽奖游戏(Java写的代码)

import java.util.Scanner;

/**

 *

 */

public class f {

    public static void main(String args[]){

        Scanner scan = new Scanner(System.in);

        System.out.print("请输入抽奖号码上限:");

        int max = scan.nextInt();

        System.out.print("请输入抽奖次数:");

        int n = scan.nextInt();

        System.out.print("中奖号码依次为:");

        for(int i=0;in;i++){

            System.out.print((int)(Math.random()*max+1)+" ");

        }

    }

}

求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); }}