完整的java游戏代码 java游戏代码怎么运行

求一个简单的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; i4; i) {
btn[i] = new JButton(btn_name[i]);
btn[i].setBounds(160i * 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; i5; i) {
g.drawLine( (int) (center_xradius * Math.sin(Math.toRadians(degree72 * i))),
(int) (center_y - radius * Math.cos(Math.toRadians(degree72 * i))),
(int) (center_xradius * Math.sin(Math.toRadians(degree72 * i144))),
(int) (center_y - radius * Math.cos(Math.toRadians(degree72 * i144))));
}
}
public void actionPerformed(ActionEvent e) {
// TODO Auto-generated method stub
if(e.getActionCommand() == " ") {
if(radius200)
radius= 2;
repaint();
} else if(e.getActionCommand() == "-") {
if(radius0)
radius -= 2;
repaint();
} else if(e.getActionCommand() == "R") {
degree = (degree2) % 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 = 5num50) {
BombNum = num;
startBomb();
}
else if (num5) {
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; iBombNum; ) {
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; xi2; x) {
for (int y = j - 1; yj2; 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; xi2; x) {
for (int y = j - 1; yj2; 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.BombRoundCount0) {
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.BombFlag1) % 3;
if (bombSource.BombFlag == 1) {
if (restBomb0) {
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 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 拉丁字母表字符的位模式在所有的代码页中都相同,但用于表示重音字符(如"é"和"á")的位模式在不同的代码页中却不同 。如果在运行不同代码页的计算机间交换数据,必须将所有字符数据由发送计算机的代码页转换为接收计算机的代码页 。如果源数据中的扩展字符在接收计算机的代码页中未定义,那么数据将丢失 。
如果某个数据库为来自许多不同国家的客户端提供服务,则很难为该数据库选择这样一种代码页,使其包括所有客户端计算机所需的全部扩展字符 。而且,在代码页间不停地转换需要花费大量的处理时间 。
【完整的java游戏代码 java游戏代码怎么运行】完整的java游戏代码的介绍就聊到这里吧 , 感谢你花时间阅读本站内容,更多关于java游戏代码怎么运行、完整的java游戏代码的信息别忘了在本站进行查找喔 。

    推荐阅读