Java编写完游戏代码怎么让它运行起来
JavaGUI图形化界面编程中会接触到,就是类似于一个游戏窗口的东西作为运行代码的载体,运行窗口的时候会调用贪吃蛇游戏的具体代码,比如说你点了开始以后他就会调用你的代码然后游戏窗口就给你切换到游戏开始时候的界面,然后游戏开始的界面窗口就会调用你的贪吃蛇加分啊,长度变长啊,以及死亡判断啊这些类型的代码,等你game over以后重新开始游戏就重新调用一遍游戏界面的代码来重新开始游戏,等到游戏窗口关闭了以后,载体结束运行不再调用代码,整个流程结束
求一个简单的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;
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扫雷游戏代码
import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
public class Frame
extends JFrame {
JTextField text;
JLabel nowBomb, setBomb;
int BombNum, BlockNum; // 当前雷数,当前方块数
int rightBomb, restBomb, restBlock; // 找到的地雷数,剩余雷数,剩余方块数
JButton start = new JButton(“ 开始 “);
JPanel MenuPamel = new JPanel();
JPanel bombPanel = new JPanel();
Bomb bombButton;
JPanel c;
BorderLayout borderLayout1 = new BorderLayout();
GridLayout gridLayout1 = new GridLayout();
public Frame() {
try {
setDefaultCloseOperation(EXIT_ON_CLOSE);
jbInit();
}
catch (Exception exception) {
exception.printStackTrace();
}
}
private void jbInit() throws Exception {
c = (JPanel) getContentPane();
setTitle(“扫雷“);
c.setBackground(Color.WHITE);
MenuPamel.setBackground(Color.GRAY);
c.setLayout(borderLayout1);
setSize(new Dimension(600, 600));
setResizable(false);
BlockNum = 144;
BombNum = 10;
text = new JTextField(“10 “, 3);
nowBomb = new JLabel(“当前雷数“ + “:“ + BombNum);
setBomb = new JLabel(“设置地雷数“);
start.addActionListener(new Frame1_start_actionAdapter(this));
MenuPamel.add(setBomb);
MenuPamel.add(text);
MenuPamel.add(start);
MenuPamel.add(nowBomb);
c.add(MenuPamel, java.awt.BorderLayout.SOUTH);
bombPanel.setLayout(gridLayout1);
gridLayout1.setColumns( (int) Math.sqrt(BlockNum));
gridLayout1.setRows( (int) Math.sqrt(BlockNum));
bombButton = new Bomb[ (int) Math.sqrt(BlockNum)][ (int) Math.sqrt(BlockNum)];
for (int i = 0; i 《 (int) Math.sqrt(BlockNum); i++) {
for (int j = 0; j 《 (int) Math.sqrt(BlockNum); j++) {
bombButton[i][j] = new Bomb(i, j);
//bombButton[i][j].setSize(10, 10);
bombButton[i][j].setFont(new Font(““, Font.PLAIN, 14));//设置字体大小
bombButton[i][j].setForeground(Color.white);
bombButton[i][j].addMouseListener(new Bomb_mouseAdapter(this));
bombButton[i][j].addActionListener(new Bomb_actionAdapter(this));
bombPanel.add(bombButton[i][j]);
}
}
c.add(bombPanel, java.awt.BorderLayout.CENTER);
startBomb();
}
/* 开始按钮 */
public void start_actionPerformed(ActionEvent e) {
int num=Integer.parseInt(text.getText().trim());
if (num 》= 5 && num 《 50) {
BombNum = num;
startBomb();
}
else if (num 《 5) {
JOptionPane.showMessageDialog(null, “您设置的地雷数太少了,请重设!“, “错误“,
JOptionPane.ERROR_MESSAGE);
num=10;
BombNum = num;
}
else {
JOptionPane.showMessageDialog(null, “您设置的地雷数太多了,请重设!“, “错误“,
JOptionPane.ERROR_MESSAGE);
num=10;
BombNum = num;
}
}
/* 开始,布雷 */
public void startBomb() {
nowBomb.setText(“当前雷数“ + “:“ + BombNum);
for (int i = 0; i 《 (int) Math.sqrt(BlockNum); i++) {
for (int j = 0; j 《 (int) Math.sqrt(BlockNum); j++) {
bombButton[i][j].isBomb = false;
bombButton[i][j].isClicked = false;
bombButton[i][j].isRight = false;
bombButton[i][j].BombFlag = 0;
bombButton[i][j].BombRoundCount = 9;
bombButton[i][j].setEnabled(true);
bombButton[i][j].setText(““);
bombButton[i][j].setFont(new Font(““, Font.PLAIN, 14));//设置字体大小
bombButton[i][j].setForeground(Color.BLUE);
rightBomb = 0;
restBomb = BombNum;
restBlock = BlockNum – BombNum;
}
}
for (int i = 0; i 《 BombNum; ) {
int x = (int) (Math.random() * (int) (Math.sqrt(BlockNum) – 1));
int y = (int) (Math.random() * (int) (Math.sqrt(BlockNum) – 1));
if (bombButton[x][y].isBomb != true) {
bombButton[x][y].isBomb = true;
i++;
}
}
CountRoundBomb();
}
/* 计算方块周围雷数 */
public void CountRoundBomb() {
for (int i = 0; i 《 (int) Math.sqrt(BlockNum); i++) {
for (int j = 0; j 《 (int) Math.sqrt(BlockNum); j++) {
int count = 0;
// 当需要检测的单元格本身无地雷的情况下,统计周围的地雷个数
if (bombButton[i][j].isBomb != true) {
for (int x = i – 1; x 《 i + 2; x++) {
for (int y = j – 1; y 《 j + 2; y++) {
if ( (x 》= 0) && (y 》= 0)
&& (x 《 ( (int) Math.sqrt(BlockNum)))
&& (y 《 ( (int) Math.sqrt(BlockNum)))) {
if (bombButton[x][y].isBomb == true) {
count++;
}
}
}
}
bombButton[i][j].BombRoundCount = count;
}
}
}
}
/* 是否挖完了所有的雷 */
public void isWin() {
restBlock = BlockNum – BombNum;
for (int i = 0; i 《 (int) Math.sqrt(BlockNum); i++) {
for (int j = 0; j 《 (int) Math.sqrt(BlockNum); j++) {
if (bombButton[i][j].isClicked == true) {
restBlock–;
}
}
}
if (rightBomb == BombNum || restBlock == 0) {
JOptionPane.showMessageDialog(this, “您挖完了所有的雷,您胜利了!“, “胜利“,
JOptionPane.INFORMATION_MESSAGE);
startBomb();
}
}
/** 当选中的位置为空,则翻开周围的地图* */
public void isNull(Bomb ClickedButton) {
int i, j;
i = ClickedButton.num_x;
j = ClickedButton.num_y;
for (int x = i – 1; x 《 i + 2; x++) {
for (int y = j – 1; y 《 j + 2; y++) {
if ( ( (x != i) || (y != j)) && (x 》= 0) && (y 》= 0)
&& (x 《 ( (int) Math.sqrt(BlockNum)))
&& (y 《 ( (int) Math.sqrt(BlockNum)))) {
if (bombButton[x][y].isBomb == false
&& bombButton[x][y].isClicked == false
&& bombButton[x][y].isRight == false) {
turn(bombButton[x][y]);
}
}
}
}
}
/* 翻开 */
public void turn(Bomb ClickedButton) {
ClickedButton.setEnabled(false);
ClickedButton.isClicked = true;
if (ClickedButton.BombRoundCount 》 0) {
ClickedButton.setText(ClickedButton.BombRoundCount + ““);
}
else {
isNull(ClickedButton);
}
}
/* 左键点击 */
public void actionPerformed(ActionEvent e) {
if ( ( (Bomb) e.getSource()).isClicked == false
&& ( (Bomb) e.getSource()).isRight == false) {
if ( ( (Bomb) e.getSource()).isBomb == false) {
turn( ( (Bomb) e.getSource()));
isWin();
}
else {
for (int i = 0; i 《 (int) Math.sqrt(BlockNum); i++) {
for (int j = 0; j 《 (int) Math.sqrt(BlockNum); j++) {
if (bombButton[i][j].isBomb == true) {
bombButton[i][j].setText(“b“);
}
}
}
( (Bomb) e.getSource()).setForeground(Color.RED);
( (Bomb) e.getSource()).setFont(new Font(““, Font.BOLD, 20));
( (Bomb) e.getSource()).setText(“X“);
JOptionPane.showMessageDialog(this, “你踩到地雷了,按确定重来“, “踩到地雷“, 2);
startBomb();
}
}
}
/* 右键点击 */
public void mouseClicked(MouseEvent e) {
Bomb bombSource = (Bomb) e.getSource();
boolean right = SwingUtilities.isRightMouseButton(e);
if ( (right == true) && (bombSource.isClicked == false)) {
bombSource.BombFlag = (bombSource.BombFlag + 1) % 3;
if (bombSource.BombFlag == 1) {
if (restBomb 》 0) {
bombSource.setForeground(Color.RED);
bombSource.setText(“F“);
bombSource.isRight = true;
restBomb–;
}
else {
bombSource.BombFlag = 0;
}
}
else if (bombSource.BombFlag == 2) {
restBomb++;
bombSource.setText(“Q“);
bombSource.isRight = false;
}
else {
bombSource.setText(““);
}
if (bombSource.isBomb == true) {
if (bombSource.BombFlag == 1) {
rightBomb++;
}
else if (bombSource.BombFlag == 2) {
rightBomb–;
}
}
nowBomb.setText(“当前雷数“ + “:“ + restBomb);
isWin();
}
}
public static void main(String args) {
Frame frame = new Frame();
frame.setVisible(true);
}
}
class Frame1_start_actionAdapter
implements ActionListener {
private Frame adaptee;
Frame1_start_actionAdapter(Frame adaptee) {
this.adaptee = adaptee;
}
public void actionPerformed(ActionEvent e) {
adaptee.start_actionPerformed(e);
}
}
////////////////////////////
class Bomb
extends JButton {
int num_x, num_y; // 第几号方块
int BombRoundCount; // 周围雷数
boolean isBomb; // 是否为雷
boolean isClicked; // 是否被点击
int BombFlag; // 探雷标记
boolean isRight; // 是否点击右键
public Bomb(int x, int y) {
num_x = x;
num_y = y;
BombFlag = 0;
BombRoundCount = 9;
isBomb = false;
isClicked = false;
isRight = false;
}
}
class Bomb_actionAdapter
implements ActionListener {
private Frame adaptee;
Bomb_actionAdapter(Frame adaptee) {
this.adaptee = adaptee;
}
public void actionPerformed(ActionEvent e) {
adaptee.actionPerformed(e);
}
}
class Bomb_mouseAdapter
extends MouseAdapter {
private Frame adaptee;
Bomb_mouseAdapter(Frame adaptee) {
this.adaptee = adaptee;
}
public void mouseClicked(MouseEvent e) {
adaptee.mouseClicked(e);
}
}
急求Java小游戏代码注释!!!!!
package org.zcq100.test;
import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
class TestGame {
public static void main(String args) {
App ap = new App(); //调用App()开始运行程序
ap.show();
}
}
class App extends JFrame {
MainPanel mp;
public App() {
mp = new MainPanel();
this.getContentPane().add(mp);
this.setSize(400, 450);
this.setTitle(“小游戏“);
}
}
/**
* 主面板
* 显示格子
* @author Administrator
*
*/
class MainPanel extends JPanel {
ButtonPanel bp = new ButtonPanel();
CtrlPanel rp = new CtrlPanel();
public MainPanel() {
this.setLayout(new BorderLayout());
rp.btnstart.addActionListener(new StartListener());
this.add(bp, “Center“);
this.add(rp, “South“);
}
class StartListener implements ActionListener {
/**
* 重新开始按钮的事件
* 调用按钮面板里面的颜色初始化方法
*/
public void actionPerformed(ActionEvent e) {
if (e.getActionCommand() == “重新开始“) {
bp.ColorInit();
}
}
}
}
class ButtonPanel extends JPanel {
JButton b = new JButton;
/**
* 按钮界面的构造器
* 设置布局方式为Grid布局,并生成5*5的格子,
* 在每个格子生成一个按钮,
* 为每个按钮添加一个监听事件
*/
public ButtonPanel() {
this.setLayout(new GridLayout(5, 5));
for (int i = 0; i 《 5; i++) {
for (int j = 0; j 《 5; j++) {
b[i][j] = new JButton();
b[i][j].setActionCommand(““ + (i + 1) + (j + 1));
b[i][j].addActionListener(new MyButtonListener());
this.add(b[i][j]);
}
}
this.ColorInit();
}
/**
* 面板初始化时候给所有的格子都绘上深灰色
* i.j分别是行和列
*/
public void ColorInit() {
for (int i = 0; i 《 5; i++) {
for (int j = 0; j 《 5; j++) {
b[i][j].setBackground(Color.DARK_GRAY);
}
}
}
/**
* 按钮上监听的时事件,监听点击
* @author Administrator
*
*/
class MyButtonListener implements ActionListener {
int r, c;
/**
* 需要改变颜色的行和列
* r row
* c colunm
* 调用change()来改变颜色
*/
public void actionPerformed(ActionEvent e) {
int i = Integer.parseInt(e.getActionCommand());
r = i / 10 – 1;
c = i % 10 – 1;
this.changer();
}
/**
* 传一个按钮控件进去
* 判断颜色,如果是深灰则变为粉红
* 否则义相反
* @param b
*/
public void btnChange(JButton b) {
if (b.getBackground() == Color.DARK_GRAY) {
b.setBackground(Color.pink);
} else {
b.setBackground(Color.DARK_GRAY);
}
}
/**
* 这个方法是根据点击的按钮判断周围需要
* 不能超越数组的下标
*/
public void changer() {
this.btnChange(b[r][c]);
if (r 》 0) //行号大于0
this.btnChange(b[r – 1][c]);
if (r 《 4)
this.btnChange(b[r + 1][c]);
if (c 》 0)//列号大于0
this.btnChange(b[r][c – 1]);
if (c 《 4)//列好小余0
this.btnChange(b[r][c + 1]);
}
}
}
/**
* 控制面板
* @author Administrator
*下面的开始按钮
*/
class CtrlPanel extends JPanel {
JButton btnstart;
public CtrlPanel() {
btnstart = new JButton(“重新开始“);
this.add(btnstart);
}
}
几个Java小游戏代码
package reduce;
import java.applet.Applet;
import java.applet.AudioClip;
import java.awt.Dimension;
import java.awt.Font;
import java.awt.Toolkit;
import javax.sound.sampled.AudioFileFormat;
import javax.sound.sampled.AudioSystem;
import javax.swing.JFrame;
import javax.swing.JPanel;
import java.awt.Rectangle;
import javax.swing.BorderFactory;
import javax.swing.JButton;
import javax.swing.JOptionPane;
import javax.swing.JSlider;
import javax.swing.JLabel;
import javax.swing.SwingUtilities;
import javax.swing.UIManager;
import javax.swing.event.ChangeEvent;
import javax.swing.event.ChangeListener;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.KeyAdapter;
import java.awt.event.KeyEvent;
import java.io.File;
import java.util.Vector;
public class Frame extends JFrame implements Runnable {
JPanel contentPane;
JPanel jPanel1 = new JPanel();
JButton jButton1 = new JButton();
JSlider jSlider1 = new JSlider();
JLabel jLabel1 = new JLabel();
JButton jButton2 = new JButton();
JLabel jLabel2 = new JLabel();
int count = 1, rapidity = 80; // count 当前进行的个数, rapidity 游标的位置
int zhengque = 0, cuowu = 0;
int rush = { 10 ,20 ,30 }; //游戏每关的个数 可以自由添加.列 { 10 ,20 ,30 ,40,50}
int rush_count = 0; //记录关数
char list = { ’A’, ’B’, ’C’, ’D’, ’E’, ’F’, ’G’, ’H’, ’I’, ’J’, ’K’, ’L’,
’M’, ’N’, ’O’, ’P’, ’Q’, ’R’, ’S’, ’T’, ’U’, ’V’, ’W’, ’X’, ’Y’,
’Z’, ’1’, ’2’, ’3’, ’4’, ’5’, ’6’, ’7’, ’8’, ’9’ }; //随机出现的数字 可以自由添加
Vector number = new Vector();
String paiduan = “true“;
AudioClip Musci_anjian, Music_shibai, Music_chenggong;
public Frame() {
try {
setDefaultCloseOperation(EXIT_ON_CLOSE);
//—————–声音文件———————
Musci_anjian = Applet.newAudioClip(new File(“sounds//anjian.wav“)
.toURL());
Music_shibai = Applet.newAudioClip(new File(“sounds//shibai.wav“)
.toURL());
Music_chenggong = Applet.newAudioClip(new File(
“sounds//chenggong.wav“).toURL());
//—————————————
jbInit();
} catch (Exception exception) {
exception.printStackTrace();
}
}
/**
* Component initialization.
*
* @throws java.lang.Exception
*/
private void jbInit() throws Exception {
contentPane = (JPanel) getContentPane();
contentPane.setLayout(null);
setSize(new Dimension(588, 530));
setTitle(“Frame Title“);
jPanel1.setBorder(BorderFactory.createEtchedBorder());
jPanel1.setBounds(new Rectangle(4, 4, 573, 419));
jPanel1.setLayout(null);
jButton1.setBounds(new Rectangle(277, 442, 89, 31));
jButton1.setText(“开始“);
jButton1.addActionListener(new Frame1_jButton1_actionAdapter(this));
jSlider1.setBounds(new Rectangle(83, 448, 164, 21));
jSlider1.setMaximum(100);
jSlider1.setMinimum(1);
jSlider1.setValue(50);
jLabel1.setText(“速度“);
jLabel1.setBounds(new Rectangle(35, 451, 39, 18));
jButton2.setBounds(new Rectangle(408, 442, 89, 31));
jButton2.setText(“结束“);
jButton2.addActionListener(new Frame1_jButton2_actionAdapter(this));
jLabel2.setText(“第一关:100个“);
jLabel2.setBounds(new Rectangle(414, 473, 171, 21));
contentPane.add(jPanel1);
contentPane.add(jButton2);
contentPane.add(jButton1);
contentPane.add(jSlider1);
contentPane.add(jLabel1);
contentPane.add(jLabel2);
this.addKeyListener(new MyListener());
jButton1.addKeyListener(new MyListener());
jSlider1.addKeyListener(new MyListener());
jSlider1.addChangeListener(new ChangeListener() {
public void stateChanged(ChangeEvent e) {
rapidity = jSlider1.getValue();
}
});
}
public void run() {
number.clear();
zhengque = 0;
cuowu = 0;
paiduan = “true“;
while (count 《= rush[rush_count]) {
try {
Thread t = new Thread(new Tthread());
t.start();
count += 1;
Thread.sleep(1000 + (int) (Math.random() * 2000)); // 生产下组停顿时间
// 最快1快.最慢2秒
} catch (InterruptedException e) {
e.printStackTrace();
}
}
while (true) { // 等待最后一个字符消失
if (number.size() == 0) {
break;
}
}
if (zhengque == 0) { // 为了以后相除..如果全部正确或者错误就会出现错误. 所以..
zhengque = 1;
}
if (cuowu == 0) {
cuowu = 1;
}
if (paiduan.equals(“true“)) { // 判断是否是自然结束
if (zhengque / cuowu 》= 2) {
JOptionPane.showMessageDialog(null, “恭喜你过关了“);
rush_count += 1; // 自动加1关
if (rush_count 《 rush.length) {
if (rapidity 》 10) { // 当速度大于10的时候在-5提加速度.怕速度太快
rapidity -= 5; // 速度自动减10毫秒
jSlider1.setValue(rapidity); // 选择位置
}
Thread t = new Thread(this);
t.start();
} else {
JOptionPane.showMessageDialog(null, “牛B…你通关了..“);
rush_count = 0;
count = 0;
}
} else {
JOptionPane.showMessageDialog(null, “请再接再励“);
rush_count = 0;
count = 0;
}
} else {
rush_count = 0;
count = 0;
}
}
public void jButton1_actionPerformed(ActionEvent e) {
Thread t = new Thread(this);
t.start();
}
public void jButton2_actionPerformed(ActionEvent e) {
count = rush[rush_count] + 1;
paiduan = “flase“;
}
class Tthread implements Runnable {
public void run() {
boolean fo = true;
int Y = 0, X = 0;
JLabel show = new JLabel();
show.setFont(new java.awt.Font(“宋体“, Font.PLAIN, 33));
jPanel1.add(show);
X = 10 + (int) (Math.random() * 400);
String parameter = list[(int) (Math.random() * list.length)] + ““;
Bean bean = new Bean();
bean.setParameter(parameter);
bean.setShow(show);
number.add(bean);
show.setText(parameter);
while (fo) {
// ———————数字下移——————–
show.setBounds(new Rectangle(X, Y += 2, 33, 33));
try {
Thread.sleep(rapidity);
} catch (InterruptedException e) {
e.printStackTrace();
}
if (Y 》= 419) {
fo = false;
for (int i = number.size() – 1; i 》= 0; i–) {
Bean bn = ((Bean) number.get(i));
if (parameter.equalsIgnoreCase(bn.getParameter())) {
cuowu += 1;
jLabel2.setText(“正确:“ + zhengque + “个,错误:“ + cuowu
+ “个“);
number.removeElementAt(i);
Music_shibai.play();
break;
}
}
}
}
}
}
class MyListener extends KeyAdapter {
public void keyPressed(KeyEvent e) {
String uu = e.getKeyChar() + ““;
for (int i = 0; i 《 number.size(); i++) {
Bean bean = ((Bean) number.get(i));
if (uu.equalsIgnoreCase(bean.getParameter())) {
zhengque += 1;
number.removeElementAt(i);
bean.getShow().setVisible(false);
jLabel2.setText(“正确:“ + zhengque + “个,错误:“ + cuowu + “个“);
Music_chenggong.play();
break;
}
}
Musci_anjian.play();
}
}
public static void main(String args) {
try {
UIManager.setLookAndFeel(UIManager.getSystemLookAndFeelClassName());
} catch (Exception exception) {
exception.printStackTrace();
}
Frame frame = new Frame();
Dimension screenSize = Toolkit.getDefaultToolkit().getScreenSize();
Dimension frameSize = frame.getSize();
if (frameSize.height 》 screenSize.height) {
frameSize.height = screenSize.height;
}
if (frameSize.width 》 screenSize.width) {
frameSize.width = screenSize.width;
}
frame.setLocation((screenSize.width – frameSize.width) / 2,
(screenSize.height – frameSize.height) / 2);
frame.setVisible(true);
}
}
class Frame1_jButton2_actionAdapter implements ActionListener {
private Frame adaptee;
Frame1_jButton2_actionAdapter(Frame adaptee) {
this.adaptee = adaptee;
}
public void actionPerformed(ActionEvent e) {
adaptee.jButton2_actionPerformed(e);
}
}
class Frame1_jButton1_actionAdapter implements ActionListener {
private Frame adaptee;
Frame1_jButton1_actionAdapter(Frame adaptee) {
this.adaptee = adaptee;
}
public void actionPerformed(ActionEvent e) {
adaptee.jButton1_actionPerformed(e);
}
}
class Bean {
String parameter = null;
JLabel show = null;
public JLabel getShow() {
return show;
}
public void setShow(JLabel show) {
this.show = show;
}
public String getParameter() {
return parameter;
}
public void setParameter(String parameter) {
this.parameter = parameter;
}
}
我只有一个打字母小游戏
我在网上下载了一些java游戏程序代码,请问如何在电脑上运行
把所有的文件编译成类文件.class
(用javac
文件名.java
编译)
,
然后找到游戏代码的入口文件,java
文件名
运行,就完事了当然你得确保这是一个能在命令行下运行的代码
求一个简单又有趣的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;//游戏按钮数组
JButton exitButton,resetButton,newlyButton; //退出,重列,重新开始按钮
JLabel fractionLable=new JLabel(“0“); //分数标签
JButton firstButton,secondButton; //
分别记录两次62616964757a686964616fe59b9ee7ad9431333335326239被选中的按钮
int grid = new int;//储存游戏按钮位置
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 拉丁字母表字符的位模式在所有的代码页中都相同,但用于表示重音字符(如“é“和“á“)的位模式在不同的代码页中却不同。如果在运行不同代码页的计算机间交换数据,必须将所有字符数据由发送计算机的代码页转换为接收计算机的代码页。如果源数据中的扩展字符在接收计算机的代码页中未定义,那么数据将丢失。
如果某个数据库为来自许多不同国家的客户端提供服务,则很难为该数据库选择这样一种代码页,使其包括所有客户端计算机所需的全部扩展字符。而且,在代码页间不停地转换需要花费大量的处理时间。
用java编写一个猜数字游戏,
package day06;
import java.util.Scanner;
//猜字符游戏
public class GuessingGame {
//主方法
public static void main(String args) {
Scanner scan = new Scanner(System.in);
int count = 0; //猜错的次数
char chs = generate(); //随机生成的字符数组
System.out.println(chs); //作弊
while(true){ //自造死循环
System.out.println(“猜吧!“);
String str = scan.next().toUpperCase(); //获取用户输入的字符串
if(str.equals(“EXIT“)){ //判断str是否是EXIT
System.out.println(“下次再来吧!“);
break;
}
char input = str.toCharArray(); //将字符串转换为字符数组
int result = check(chs,input); //对比
if(result==chs.length){ //位置对为5
int score = chs.length*100 – count*10; //一个字符100分,错一次减10分
System.out.println(“恭喜你猜对了,得分:“ + score);
break; //猜对时跳出循环
}else{ //没猜对
count++; //猜错次数增1
System.out.println(“字符对:“+result+“个,位置对:“+result+“个“);
}
}
}
//随机生成5个字符数组
public static char generate(){
char chs = new char;
char letters = { ’A’, ’B’, ’C’, ’D’, ’E’, ’F’, ’G’, ’H’, ’I’, ’J’,
’K’, ’L’, ’M’, ’N’, ’O’, ’P’, ’Q’, ’R’, ’S’, ’T’, ’U’, ’V’,
’W’, ’X’, ’Y’, ’Z’};
boolean flags = new boolean[letters.length]; //1.
for(int i=0;i《chs.length;i++){
int index;
do{
index = (int)(Math.random()*letters.length); //0到25
}while(flags[index]==true); //2.
chs[i] = letters[index];
flags[index] = true; //3.
}
return chs;
}
//对比随机数组与用户输入的数组
public static int check(char chs,char input){
int result = new int;
for(int i=0;i《chs.length;i++){
for(int j=0;j《input.length;j++){
if(chs[i]==input[j]){ //字符对
result++; //字符对个数增1
if(i==j){ //位置对
result++; //位置对个数增1
}
break;
}
}
}
return result;
}
}
JAVA游戏代码注释
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;//游戏按钮数组JButton exitButton,resetButton,newlyButton; //退出,重列,重新开始按钮JLabel fractionLable=new JLabel(“0“); //分数标签JButton firstButton,secondButton; //分别记录两次被选中的按钮int grid = new int;//储存游戏按钮位置static boolean pressInformation=false; //判断是否有按钮被选中int x0=0,y0=0,x=0,y=0,fristMsg=0,secondMsg=0,validateLV; //游戏按钮的位置坐标int i,j,k,n;//消除方法控制public void init(){mainFrame=new JFrame(“JKJ连连看“);thisContainer = mainFrame.getContentPane();thisContainer.setLayout(new BorderLayout());centerPanel=new JPanel();southPanel=new JPanel();northPanel=new JPanel();thisContainer.add(centerPanel,“Center“);thisContainer.add(southPanel,“South“);thisContainer.add(northPanel,“North“);centerPanel.setLayout(new GridLayout(6,5));for(int cols = 0;cols 《 6;cols++){for(int rows = 0;rows 《 5;rows++ ){diamondsButton[cols][rows]=new JButton(String.valueOf(grid[cols+1][rows+1]));diamondsButton[cols][rows].addActionListener((java.awt.event.ActionListener) this);centerPanel.add(diamondsButton[cols][rows]);}}exitButton=new JButton(“退出“);exitButton.addActionListener((java.awt.event.ActionListener) this);resetButton=new JButton(“重列“);resetButton.addActionListener((java.awt.event.ActionListener) this);newlyButton=new JButton(“再来一局“);newlyButton.addActionListener((java.awt.event.ActionListener) this);southPanel.add(exitButton);southPanel.add(resetButton);southPanel.add(newlyButton);fractionLable.setText(String.valueOf(Integer.parseInt(fractionLable.getText())));northPanel.add(fractionLable);mainFrame.setBounds(280,100,500,450);mainFrame.setVisible(true);}public void randomBuild() {int randoms,cols,rows;for(int twins=1;twins《=15;twins++) {randoms=(int)(Math.random()*25+1);for(int alike=1;alike《=2;alike++) {cols=(int)(Math.random()*6+1);rows=(int)(Math.random()*5+1);while(grid[cols][rows]!=0) {cols=(int)(Math.random()*6+1);rows=(int)(Math.random()*5+1);}this.grid[cols][rows]=randoms;}}}public void fraction(){fractionLable.setText(String.valueOf(Integer.parseInt(fractionLable.getText())+100));}public void reload() {int save = new int;int n=0,cols,rows;int grid= new int;for(int i=0;i《=6;i++) {for(int j=0;j《=5;j++) {if(this.grid[i][j]!=0) {save[n]=this.grid[i][j];n++;}}}n=n-1;this.grid=grid;while(n》=0) {cols=(int)(Math.random()*6+1);rows=(int)(Math.random()*5+1);while(grid[cols][rows]!=0) {cols=(int)(Math.random()*6+1);rows=(int)(Math.random()*5+1);}this.grid[cols][rows]=save[n];n–;}mainFrame.setVisible(false);pressInformation=false; //这里一定要将按钮点击信息归为初始init();for(int i = 0;i 《 6;i++){for(int j = 0;j 《 5;j++ ){if(grid[i+1][j+1]==0)diamondsButton[i][j].setVisible(false);}}}public void estimateEven(int placeX,int placeY,JButton bz) {if(pressInformation==false) {x=placeX;y=placeY;secondMsg=grid[x][y];secondButton=bz;pressInformation=true;}else {x0=x;y0=y;fristMsg=secondMsg;firstButton=secondButton;x=placeX;y=placeY;secondMsg=grid[x][y];secondButton=bz;if(fristMsg==secondMsg && secondButton!=firstButton){xiao();}}}public void xiao() { //相同的情况下能不能消去。仔细分析,不一条条注释if((x0==x &&(y0==y+1||y0==y-1)) || ((x0==x+1||x0==x-1)&&(y0==y))){ //判断是否相邻remove();}else{for (j=0;j《7;j++ ) {if (grid[x0][j]==0){ //判断第一个按钮同行哪个按钮为空if (y》j) { //如果第二个按钮的Y坐标大于空按钮的Y坐标说明第一按钮在第二按钮左边for (i=y-1;i》=j;i– ){ //判断第二按钮左侧直到第一按钮中间有没有按钮if (grid[x][i]!=0) {k=0;break;}else{ k=1; } //K=1说明通过了第一次验证}if (k==1) {linePassOne();}}if (y《j){ //如果第二个按钮的Y坐标小于空按钮的Y坐标说明第一按钮在第二按钮右边for (i=y+1;i《=j ;i++ ){ //判断第二按钮左侧直到第一按钮中间有没有按钮if (grid[x][i]!=0){k=0;break;}else { k=1; }}if (k==1){linePassOne();}}if (y==j ) {linePassOne();}}if (k==2) {if (x0==x) {remove();}if (x0《x) {for (n=x0;n《=x-1;n++ ) {if (grid[n][j]!=0) {k=0;break;}if(grid[n][j]==0 && n==x-1) {remove();}}}if (x0》x) {for (n=x0;n》=x+1 ;n– ) {if (grid[n][j]!=0) {k=0;break;}if(grid[n][j]==0 && n==x+1) {remove();}}}}}for (i=0;i《8;i++ ) { //列if (grid[i][y0]==0) {if (x》i) {for (j=x-1;j》=i ;j– ) {if (grid[j][y]!=0) {k=0;break;}else { k=1; }}if (k==1) {rowPassOne();}}if (x《i) {for (j=x+1;j《=i;j++ ) {if (grid[j][y]!=0) {k=0;break;}else { k=1; }}if (k==1) {rowPassOne();}}if (x==i) {rowPassOne();}}if (k==2){if (y0==y) {remove();}if (y0《y) {for (n=y0;n《=y-1 ;n++ ) {if (grid[i][n]!=0) {k=0;break;}if(grid[i][n]==0 && n==y-1) {remove();}}}if (y0》y) {for (n=y0;n》=y+1 ;n–) {if (grid[i][n]!=0) {k=0;break;}if(grid[i][n]==0 && n==y+1) {remove();}}}}}}}public void linePassOne(){if (y0》j){ //第一按钮同行空按钮在左边for (i=y0-1;i》=j ;i– ){ //判断第一按钮同左侧空按钮之间有没按钮if (grid[x0][i]!=0) {k=0;break;}else { k=2; } //K=2说明通过了第二次验证}}if (y0《j){ //第一按钮同行空按钮在与第二按钮之间for (i=y0+1;i《=j ;i++){if (grid[x0][i]!=0) {k=0;break;}else{ k=2; }}}}public void rowPassOne(){if (x0》i) {for (j=x0-1;j》=i ;j– ) {if (grid[j][y0]!=0) {k=0;break;}else { k=2; }}}if (x0《i) {for (j=x0+1;j《=i ;j++ ) {if (grid[j][y0]!=0) {k=0;break;}else { k=2; }}}}public void remove(){firstButton.setVisible(false);secondButton.setVisible(false);fraction();pressInformation=false;k=0;grid[x0][y0]=0;grid[x][y]=0;}public void actionPerformed(ActionEvent e) {if(e.getSource()==newlyButton){int grid = new int;this.grid = grid;randomBuild();mainFrame.setVisible(false);pressInformation=false;init();}if(e.getSource()==exitButton)System.exit(0);if(e.getSource()==resetButton)reload();for(int cols = 0;cols 《 6;cols++){for(int rows = 0;rows 《 5;rows++ ){if(e.getSource()==diamondsButton[cols][rows])estimateEven(cols+1,rows+1,diamondsButton[cols][rows]);}}}public static void main(String args) {lianliankan llk = new lianliankan();llk.randomBuild();llk.init();}}
请采纳。