
正文
记忆游戏代码java,记忆游戏名称
提示:扫一扫查出行【扫一扫了解最新限行尾号】
复制提示
要做一个记忆翻牌游戏 应该用Java还是Flash做比较好?
在eclipse里,这个也比较好学,新建一个java工程,
默认java程序都是自动编译编写好的java程序(*,从你的java文件上,右键选择行就行了.exe编译成为字节码文件(*.class),首先要用javac。
如果还有什么不明白的,给我发消息。
如果想做动画,最好用flash,然后使用java.exe来执行。
建议你一个eclipse,用集成开发环境,这个比较方便,对于做动画.java)
相关问答
Q1: 求一个简单的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();
}
}
}
Q2: 求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); }}
Q3: java记忆拼图游戏问题
你的代码没有注释,实在不想看.
按钮和小图片的匹配不会写? 把小图片和按钮按里面的编号都取出来,分别组成2个字符串,然后用equals()方法来实现匹配.
建议把小图片和按钮都放在table里面,这样比较好操作.
我以前做的时候,在每个"拼图部分"里面都是有一个字符串作为编号的,鼠标点击单元格的图片的时候(比如该单元格在表格坐标是(m,n) ),则取(m+1,n),(m-1,n),(m,n+1),(m,n-1),四个单元格,检查里面是不是有"拼图部分",如果全都有,就return,如果有空格,就传点击的这个"拼图部分"进去,删除当前单元格的"拼图部分",然后把所有"拼图部分"的编号拿出来匹配.
这样做的话好像比较容易
PS:代码拿出来给别人看的时候要写注释,这个是很必须的习惯!!!
楼上说的纯属胡说八道,你见过9个拼图部分中只要A块回到原来的位置就成功的游戏么?
另外,站长团上有产品团购,便宜有保证
Q4: 请高手帮我写一个java的记忆翻转游戏(400分)
import javax.swing.*;
import javax.swing.border.Border;
import javax.swing.border.EtchedBorder;
import java.awt.*;
import java.awt.event.*;
import java.util.HashMap;
class jButton extends JButton {
}
class mine implements MouseListener, ActionListener, ItemListener, Runnable {
HashMap imgmap; String lastsrc="";int rightcount=1,wincount=3;
jButton b_mine[][];
jButton b_ls;
JPanel p1
,
p2;
JLabel l2;
JFrame f;
Thread thread1;
MenuBar mb;
Menu m_games;
CheckboxMenuItem item2
,
item3
,
item4;
Container con;
Icon icon[]
,
mine_1
,
mine_2
,
mine_3
,
mine_if
,
mine_is
,
mine_scan
,
b_click1
,
b_click2
,
b_click3
,
b_click4;
int x
,
y
,
n;
Font font1;
boolean isBreak = false;
int count = 0;
int icon_size = 40
,
Difficulty = 1
,
Width = 5
,
Height = 5
,
Xpos = 300
,
Ypos = 100
,
Time1 = 999
,
Time2 = 999
,
Time3 = 999;
dialog1 a;
public mine() {
f = new JFrame("记忆力");
con = f.getContentPane();
con.setLayout(null);
menuCreate(); //生成菜单
p1 = new JPanel();
p1.setLayout(null);
p2 = new JPanel();
l2 = new JLabel("", Label.RIGHT);
font1 = new Font("Helvetica", Font.BOLD, 20);
l2.setFont(font1);
Border border2 = new EtchedBorder(EtchedBorder.RAISED, new Color(144, 80, 44), new Color(144, 80, 44));
l2.setBorder(BorderFactory.createLoweredBevelBorder());
l2.setBackground(new Color(100, 50, 100));
l2.setForeground(Color.red);
l2.setOpaque(true);
p2.setBorder(border2);
p1.add(l2);
con.add(p1);
con.add(p2);
f.setLocation(Xpos, Ypos);
layout_init(Width, Height, icon_size);
button_init(Width, Height, 2);
f.show();
f.setResizable(false);
f.addWindowListener(new WindowAdapter() //适配器:关闭窗口
{
public void windowClosing(WindowEvent e) {
f.setVisible(false);
System.exit(0);
}
});
try {
Thread.sleep(3000);
}
catch (Exception e) {
}
}
//****************窗口添加控件和控件初始化**************************//\
public void layout_init(int width, int height, int icon_size) //生成最初的版面
{
this.icon_size = icon_size;
l2.setBounds(width * icon_size - 42, 3, 40, 25);
p1.setBounds(0, 0, width * icon_size + 4, 30);
p2.setBounds(0, 32, width * icon_size + 4, height * icon_size + 4);
p2.setLayout(new GridLayout(height, width));
f.setSize(width * icon_size + 10, height * icon_size + 100);
}
public void button_init(int width, int height, int typecount) //添加按钮
{
l2.setText("000");
imgmap=new HashMap();
wincount= width-2;
this.Width = width;
this.Height = height;
b_mine = new jButton[height][width];
for (int i = 0; i height; i++)
for (int j = 0; j width; j++) {
b_mine[i][j] = new jButton();
p2.add(b_mine[i][j]);
b_mine[i][j].addMouseListener(this);
imgmap.put(i+"_"+j,(int)(Math.random()*typecount)+".gif");
}
}
public void button_remove(int width, int height) //清除按扭
{
for (int i = 0; i height; i++)
for (int j = 0; j width; j++) {
b_mine[i][j].removeMouseListener(this);
p2.remove(b_mine[i][j]);
}
}
public void itemStateChanged(ItemEvent e) {
if (e.getSource() == item2) {
if (thread1 != null) {
thread1.yield();
thread1 = null;
}
item2.setState(true);
item3.setState(false);
item4.setState(false);
Difficulty = 1;
button_remove(Width, Height);
button_init(5, 5, 2);
layout_init(5, 5, icon_size);
f.show();
}
if (e.getSource() == item3) {
if (thread1 != null) {
thread1.yield();
thread1 = null;
}
item2.setState(false);
item3.setState(true);
item4.setState(false);
Difficulty = 2;
button_remove(Width, Height);
button_init(6,6, 3);
layout_init(6, 6, icon_size);
f.show();
}
if (e.getSource() == item4) {
if (thread1 != null) {
thread1.yield();
thread1 = null;
}
item2.setState(false);
item3.setState(false);
item4.setState(true);
Difficulty = 3;
button_remove(Width, Height);
button_init(7, 7, 4);
layout_init(7, 7, icon_size);
f.show();
}
layout_init(Width, Height, 40);
icon_size = 40;
f.show();
}
public void mousePressed(MouseEvent e) {
if (thread1 == null) {
thread1 = new Thread(this);
thread1.start();
}
//***************找出所点击的按钮的两个下标值*****************//
b_ls = (jButton) e.getSource();
for (int i = 0; i Height; i++)
for (int j = 0; j Width; j++) {
if (b_mine[i][j] == e.getSource()) {
x = j;
y = i;
break;
}
}
}
public void mouseReleased(MouseEvent e) {
if (e.getModifiers() == 16) //左键功能完成:
{
String thissrc = (String)imgmap.get(y+"_"+x);
b_mine[y][x].setIcon(new ImageIcon("D:\\java\\source\\baidujava\\classes\\"+thissrc));
if (!thissrc .equals(lastsrc) ) {
lastsrc = thissrc;
for (int ti = 0; ti Width; ti++) {
for (int tj = 0; tj Height; tj++) {
if (ti == y tj == x) continue;
b_mine[ti][tj].setIcon(null);
}
}
rightcount = 1;
}else{
rightcount++;
}
if(rightcount==wincount){
JLabel t=new JLabel();
t.setText("ok,You win.用了"+l2.getText()+"秒");
thread1=null;
button_remove(Width, Height);
button_init(Width, Height, Width-2);
layout_init(Width, Width, icon_size);
JOptionPane.showConfirmDialog(f,t,"你赢",JOptionPane.OK_CANCEL_OPTION ,JOptionPane.QUESTION_MESSAGE ) ;
}
}
}
public void moveMouseListener() {
for (int i = 0; i Height; i++)
for (int j = 0; j Width; j++)
b_mine[i][j].removeMouseListener(this);
}
public void mouseClicked(MouseEvent e) {
}
public void mouseEntered(MouseEvent e) {
}
public void mouseExited(MouseEvent e) {
}
//*****************菜单定义部分********************//
public void menuCreate() {
mb = new MenuBar();
m_games = new Menu("游戏");
item2 = new CheckboxMenuItem("初级");
item3 = new CheckboxMenuItem("中级");
item4 = new CheckboxMenuItem("高级");
m_games.add(item2);
m_games.add(item3);
m_games.add(item4);
mb.add(m_games);
f.setMenuBar(mb);
if (Difficulty == 1)
item2.setState(true);
else if (Difficulty == 2)
item3.setState(true);
else if (Difficulty == 3)
item4.setState(true);
item2.addItemListener(this);
item3.addItemListener(this);
item4.addItemListener(this);
}
public void run() {
n = 0;
String s;
while (thread1 != null) {
if (n 10)
s = "00";
else if (n 100)
s = "0";
else
s = "";
l2.setText(s + String.valueOf(++n));
try {
thread1.sleep(1000);
}
catch (Exception e) {
}
}
}
public void actionPerformed(ActionEvent e) {
//To change body of implemented methods use File | Settings | File Templates.
}
}
public class clearmine {
public static void main(String args[]) {
new mine();
}
}
class dialog1 extends Dialog implements ActionListener //自定义对话框
{
int width
,
height
,
mine;
TextField t1
,
t2
,
t3;
Label l1
,
l2
,
l3;
Button b1
,
b2;
public dialog1(JFrame f, int width, int height, int mine) {
super(f, "你赢了", true);
this.width = width;
this.height = height;
this.mine = mine;
}
public void actionPerformed(ActionEvent e) {
if (e.getSource() == b1) {
try {
width = Integer.parseInt(t1.getText());
}
catch (Exception ew) {
width = 6;
}
try {
height = Integer.parseInt(t2.getText());
}
catch (Exception eh) {
height = 6;
}
try {
mine = Integer.parseInt(t3.getText());
}
catch (Exception em) {
mine = 6;
}
if (width 6) width = 6;
else if (width 35) width = 34;
if (height 6) height = 6;
else if (height 25) height = 24;
if (mine width * height * 2 / 3) mine = width * height * 2 / 3;
setVisible(false);
} else if (e.getSource() == b2) {
setVisible(false);
}
}
}
class logo extends Dialog {
Icon logo1;
JLabel jl;
public logo(JFrame f) {
super(f, "记忆力V1.0", false);
logo1 = new ImageIcon("./images/finenesssoft.gif");
jl = new JLabel(logo1);
add(jl);
setSize(406, 308);
setResizable(false);
addWindowListener(new WindowAdapter() {
public void windowClosing(WindowEvent aa) {
setVisible(false);
}
});
}
public void str() {
setVisible(true);
}
public void exit() {
System.exit(0);
}
}
写好了.4个图片要放在D:\java\source\baidujava\classes下,名字分别是0.gif一直到3.gif.如果要改路径,去看238行.
Q5: 求一个简单又有趣的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 个不同的位模式中的一个模式。
在计算机中,字符由不同的位模式(ON 或 OFF)表示。每个字节有 8 位,这 8 位可以有 256 种不同的 ON 和 OFF 组合模式。对于使用 1 个字节存储每个字符的程序,通过给每个位模式指派字符可表示最多 256 个不同的字符。2 个字节有 16 位,这 16 位可以有 65,536 种唯一的 ON 和 OFF 组合模式。使用 2 个字节表示每个字符的程序可表示最多 65,536 个字符。
单字节代码页是字符定义,这些字符映射到每个字节可能有的 256 种位模式中的每一种。代码页定义大小写字符、数字、符号以及 !、@、#、% 等特殊字符的位模式。每种欧洲语言(如德语和西班牙语)都有各自的单字节代码页。
虽然用于表示 A 到 Z 拉丁字母表字符的位模式在所有的代码页中都相同,但用于表示重音字符(如"é"和"á")的位模式在不同的代码页中却不同。如果在运行不同代码页的计算机间交换数据,必须将所有字符数据由发送计算机的代码页转换为接收计算机的代码页。如果源数据中的扩展字符在接收计算机的代码页中未定义,那么数据将丢失。
如果某个数据库为来自许多不同国家的客户端提供服务,则很难为该数据库选择这样一种代码页,使其包括所有客户端计算机所需的全部扩展字符。而且,在代码页间不停地转换需要花费大量的处理时间。






