本文目录一览:
运行在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游戏代码,100行左右,谢谢!
import java.awt.Dimension;
import java.awt.Graphics;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JPanel;
public class Painter extends JFrame{
/**
*
*/
private static final long serialVersionUID = 8160427604782702376L;
CanvasPanel canvas = new CanvasPanel();;
public Painter() {
super("Star");
this.add(canvas);
this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
this.pack();
this.setResizable(false);
this.setLocationRelativeTo(null);
this.setVisible(true);
}
public static void main(String[] args) {
new Painter();
}
}
class CanvasPanel extends JPanel implements ActionListener{
/**
*
*/
private static final long serialVersionUID = -4642528854538741028L;
private JButton[] btn = new JButton[4];
private String[] btn_name = {"+", "-", "R", "L"};
private int center_x = 200, center_y = 200, radius = 100, degree = 0;
public CanvasPanel() {
this.setPreferredSize(new Dimension(400, 500));
this.setLayout(null);
for(int i = 0; i 4; i++) {
btn[i] = new JButton(btn_name[i]);
btn[i].setBounds(160 + i * 60, 425, 50, 50);
btn[i].addActionListener(this);
this.add(btn[i]);
}
}
@Override
public void paintComponent(Graphics g) {
super.paintComponent(g);
for(int i = 0; i 5; i++) {
g.drawLine( (int) (center_x + radius * Math.sin(Math.toRadians(degree + 72 * i))),
(int) (center_y - radius * Math.cos(Math.toRadians(degree + 72 * i))),
(int) (center_x + radius * Math.sin(Math.toRadians(degree + 72 * i + 144))),
(int) (center_y - radius * Math.cos(Math.toRadians(degree + 72 * i + 144))));
}
}
public void actionPerformed(ActionEvent e) {
/知派/ TODO Auto-generated method stub
if(e.getActionCommand() == "+") {
if(radius 答猛吵 200)
radius += 2;
repaint();
} else if(e.getActionCommand() == "-") {
if(radius 0)
radius -= 2;
repaint();
} else if(e.getActionCommand() == "清侍R") {
degree = (degree + 2) % 360;
repaint();
} else if(e.getActionCommand() == "L") {
degree = (degree - 2) % 360;
repaint();
}
}
}
求一个简单又有趣的JAVA小游戏代码
具体如下:
连连看的小源码
package Lianliankan;
import javax.swing.*;
import java.awt.*;
import java.awt.event.*;
public class lianliankan implements ActionListener
{
JFrame mainFrame; //主面板
Container thisContainer;
JPanel centerPanel,southPanel,northPanel; //子面板
JButton diamondsButton[][] = new JButton[6][5];//游戏按钮数组
JButton exitButton,resetButton,newlyButton; //退出,重列,重新开始按钮
JLabel fractionLable=new JLabel("0"); //分数标签
JButton firstButton,secondButton; //
分别记录两次62616964757a686964616fe59b9ee7ad9431333335326239被选中的按钮
int grid[][] = new int[8][7];//储存游戏按钮位置
static boolean pressInformation=false; //判断是否有按钮被选中
int x0=0,y0=0,x=0,y=0,fristMsg=0,secondMsg=0,validateLV; //游戏按钮的位置坐标
int i,j,k,n;//消除方法控制
代码(code)是程序员用开发工具所支持的语言写出来的源文件,是一组由字符、蔽岩扮符号或信号码元以离散形式表示信息的明确的规则体系。
对于字符和Unicode数据的位模式的定义,此模式代表特定字母、数字或符号(例如 0x20 代表一个空格,而 0x74 代表字符“t”)。一些数据类型每个字符使用一个字节;每个字节可以具有 256 个不同的位模式中的一个模式。-java游戏代码
在计算机中,字符由不同的位模式(ON 或 OFF)表示。每个字节有 8 位,这 8 位可以有 256 种不同的 ON 和 OFF 组合模式。对于使用 1 个字节存储每个字符的程序,通过给宏灶每个位模式指派字符可表示最多 256 个不同的字符。2 个字节有 16 位,这 16 位可以有 65,536 种唯一的 ON 和 OFF 组合模式。使用 2 个字节表示每个字符的程序可表示最多 65,536 个字符。-java游戏代码
单字节代码页是字符定义,这些字符映射到每个字节可能有的 256 种位模式中的每一种。代码页定义大小写字符、数字、符号以及 !、@、#、% 等特殊字符的位模式。每种欧洲语言(如德语和西班牙语)都有各自的单字节代码页。-java游戏代码
虽然用于表示 A 到 Z 拉丁字母表字符的位模式在所有的代码页中都相同,但用于表示重音字符(如"é"和"枣隐á")的位模式在不同的代码页中却不同。如果在运行不同代码页的计算机间交换数据,必须将所有字符数据由发送计算机的代码页转换为接收计算机的代码页。如果源数据中的扩展字符在接收计算机的代码页中未定义,那么数据将丢失。-java游戏代码
如果某个数据库为来自许多不同国家的客户端提供服务,则很难为该数据库选择这样一种代码页,使其包括所有客户端计算机所需的全部扩展字符。而且,在代码页间不停地转换需要花费大量的处理时间。