JAVA小游戏程序代码
这个是比较有名的那个烟花,不知道你有没有用:
建个工程,以Fireworks为类即可
import java.awt.*;
import java.applet.*;
import java.awt.event.*;
import javax.swing.*;
public class Fireworks extends Applet implements MouseListener,Runnable
{
int x,y;
int top,point;
/**
*对小程序进行变量和颜色的初始化。
*/
public void init()
{
x = 0;
y = 0;
//设置背景色为黑色
setBackground(Color.black);
addMouseListener(this);
}
public void paint(Graphics g)
{
}
/**
*使该程序可以作为应用程序运行。
*/
public static void main(String args[]) {
Fireworks applet = new Fireworks();
JFrame frame = new JFrame("TextAreaNew");
frame.addWindowListener(new WindowAdapter() {
public void windowClosing(WindowEvent e){
System.exit(0);
}
});
frame.getContentPane().add(
applet, BorderLayout.CENTER);
frame.setSize(800,400);
applet.init();
applet.start();
frame.setVisible(true);
}
/**
*程序主线程,对一个烟花进行绘制。
*/
public void run()
{
//变量初始化
Graphics g1;
g1 = getGraphics();
int y_move,y_click,x_click;
int v;
x_click = x;
y_click = y;
y_move = 400;
v = 3;
int r,g,b;
while(y_move y_click)
{
g1.setColor(Color.black);
g1.fillOval(x_click,y_move,5,5);
y_move -= 5;
r = (((int)Math.round(Math.random()*4321))%200)+55;
g = (((int)Math.round(Math.random()*4321))%200)+55;
b = (((int)Math.round(Math.random()*4321))%200)+55;
g1.setColor(new Color(r,g,b));
g1.fillOval(x_click,y_move,5,5);
for(int j = 0 ;j=10;j++)
{
if(r55) r -= 20;
if(g55) g -= 20;
if(b55) b -=20;
g1.setColor(new Color(r,g,b));
g1.fillOval(x_click,y_move+j*5,5,5);
}
g1.setColor(Color.black);
g1.fillOval(x_click,y_move+5*10,5,5);
try
{
Thread.currentThread().sleep(v++);
} catch (InterruptedException e) {}
}
for(int j=12;j=0;j--)
{
g1.setColor(Color.black);
g1.fillOval(x_click,y_move+(j*5),5,5);
try
{
Thread.currentThread().sleep((v++)/3);
} catch (InterruptedException e) {}
}
y_move = 400;
g1.setColor(Color.black);
while(y_move y_click)
{
g1.fillOval(x_click-2,y_move,9,5);
y_move -= 5;
}
v = 15;
for(int i=0;i=25;i++)
{
r = (((int)Math.round(Math.random()*4321))%200)+55;
g = (((int)Math.round(Math.random()*4321))%200)+55;
b = (((int)Math.round(Math.random()*4321))%200)+55;
g1.setColor(new Color(r,g,b));
g1.drawOval(x_click-3*i,y_click-3*i,6*i,6*i);
if(i23)
{
g1.drawOval(x_click-3*(i+1),y_click-3*(i+1),6*(i+1),6*(i+1));
g1.drawOval(x_click-3*(i+2),y_click-3*(i+2),6*(i+2),6*(i+2));
}
try
{
Thread.currentThread().sleep(v++);
} catch (InterruptedException e) {}
g1.setColor(Color.black);
g1.drawOval(x_click-3*i,y_click-3*i,6*i,6*i);
}
}
/**
*对鼠标事件进行监听。
*临听其鼠标按下事件。
*当按下鼠标时,产生一个新线程。
*/
public void mousePressed(MouseEvent e)
{
x = e.getX();
y = e.getY();
Thread one;
one = new Thread(this);
one.start();
one = null;
}
/**
*实现MouseListener接中的方法。为一个空方法。
*/
public void mouseReleased(MouseEvent e)
{
}
/**
*实现MouseListener接中的方法。为一个空方法。
*/
public void mouseEntered(MouseEvent e)
{
}
/**
*实现MouseListener接中的方法。为一个空方法。
*/
public void mouseExited(MouseEvent e)
{
}
/**
*实现MouseListener接中的方法。为一个空方法。
*/
public void mouseClicked(MouseEvent 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; //分别记录两次被选中的按钮
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;//消除方法控制
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(this);
centerPanel.add(diamondsButton[cols][rows]);
}
}
exitButton=new JButton("退出");
exitButton.addActionListener(this);
resetButton=new JButton("重列");
resetButton.addActionListener(this);
newlyButton=new JButton("再来一局");
newlyButton.addActionListener(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[30];
int n=0,cols,rows;
int grid[][]= new int[8][7];
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;j7;j++ ) {
if (grid[x0][j]==0){ //判断第一个按钮同行哪个按钮为空
if (yj) { //如果第二个按钮的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 (yj){ //如果第二个按钮的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 (x0x) {
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 (x0x) {
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;i8;i++ ) { //列
if (grid[i][y0]==0) {
if (xi) {
for (j=x-1;j=i ;j-- ) {
if (grid[j][y]!=0) {
k=0;
break;
}
else { k=1; }
}
if (k==1) {
rowPassOne();
}
}
if (xi) {
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 (y0y) {
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 (y0y) {
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 (y0j){ //第一按钮同行空按钮在左边
for (i=y0-1;i=j ;i-- ){ //判断第一按钮同左侧空按钮之间有没按钮
if (grid[x0][i]!=0) {
k=0;
break;
}
else { k=2; } //K=2说明通过了第二次验证
}
}
if (y0j){ //第一按钮同行空按钮在与第二按钮之间
for (i=y0+1;i=j ;i++){
if (grid[x0][i]!=0) {
k=0;
break;
}
else{ k=2; }
}
}
}
public void rowPassOne(){
if (x0i) {
for (j=x0-1;j=i ;j-- ) {
if (grid[j][y0]!=0) {
k=0;
break;
}
else { k=2; }
}
}
if (x0i) {
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[8][7];
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();
}
}
//old 998 lines
//new 318 lines
java小游戏编程
/**
* File: ControlPanel.java
* User:相思无偿
* Date: 2004.12.3
* Describe: 俄罗斯方块的 Java 实现
*/
import javax.swing.*;
import javax.swing.border.Border;
import javax.swing.border.EtchedBorder;
import java.awt.*;
import java.awt.event.*;
/**
* 控制面板类,继承自JPanel.
* 上边安放预显窗口、等级、得分、控制按钮
* 主要用来控制游戏进程。
*/
class ControlPanel extends JPanel {
private JTextField
tfLevel = new JTextField("" + ErsBlocksGame.DEFAULT_LEVEL),
tfScore = new JTextField("0");
private JButton
btPlay = new JButton("Play"),
btPause = new JButton("Pause"),
btStop = new JButton("Stop"),
btTurnLevelUp = new JButton("Turn hard"),
btTurnLevelDown = new JButton("Turn easy");
private JPanel plTip = new JPanel(new BorderLayout());
private TipPanel plTipBlock = new TipPanel();
private JPanel plInfo = new JPanel(new GridLayout(4, 1));
private JPanel plButton = new JPanel(new GridLayout(5, 1));
private Timer timer;
private ErsBlocksGame game;
private Border border = new EtchedBorder(
EtchedBorder.RAISED, Color.white, new Color(148, 145, 140));
/**
* 控制面板类的构造函数
* @param game ErsBlocksGame, ErsBoxesGame类的一个实例引用,
* 方便直接控制ErsBoxesGame类的行为。
*/
public ControlPanel(final ErsBlocksGame game) {
setLayout(new GridLayout(3, 1, 0, 4));
this.game = game;
plTip.add(new JLabel("Next block"), BorderLayout.NORTH);
plTip.add(plTipBlock);
plTip.setBorder(border);
plInfo.add(new JLabel("Level"));
plInfo.add(tfLevel);
plInfo.add(new JLabel("Score"));
plInfo.add(tfScore);
plInfo.setBorder(border);
tfLevel.setEditable(false);
tfScore.setEditable(false);
plButton.add(btPlay);
plButton.add(btPause);
plButton.add(btStop);
plButton.add(btTurnLevelUp);
plButton.add(btTurnLevelDown);
plButton.setBorder(border);
add(plTip);
add(plInfo);
add(plButton);
addKeyListener(new ControlKeyListener());
btPlay.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent ae) {
game.playGame();
}
});
btPause.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent ae) {
if (btPause.getText().equals(new String("Pause"))) {
game.pauseGame();
} else {
game.resumeGame();
}
}
});
btStop.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent ae) {
game.stopGame();
}
});
btTurnLevelUp.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent ae) {
try {
int level = Integer.parseInt(tfLevel.getText());
if (level ErsBlocksGame.MAX_LEVEL)
tfLevel.setText("" + (level + 1));
} catch (NumberFormatException e) {
}
requestFocus();
}
});
btTurnLevelDown.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent ae) {
try {
int level = Integer.parseInt(tfLevel.getText());
if (level 1)
tfLevel.setText("" + (level - 1));
} catch (NumberFormatException e) {
}
requestFocus();
}
});
addComponentListener(new ComponentAdapter() {
public void componentResized(ComponentEvent ce) {
plTipBlock.fanning();
}
});
timer = new Timer(500, new ActionListener() {
public void actionPerformed(ActionEvent ae) {
tfScore.setText("" + game.getScore());
int scoreForLevelUpdate =
game.getScoreForLevelUpdate();
if (scoreForLevelUpdate = ErsBlocksGame.PER_LEVEL_SCORE
scoreForLevelUpdate 0)
game.levelUpdate();
}
});
timer.start();
}
/**
* 设置预显窗口的样式,
* @param style int,对应ErsBlock类的STYLES中的28个值
*/
public void setTipStyle(int style) {
plTipBlock.setStyle(style);
}
/**
* 取得用户设置的游戏等级。
* @return int, 难度等级,1 - ErsBlocksGame.MAX_LEVEL
*/
public int getLevel() {
int level = 0;
try {
level = Integer.parseInt(tfLevel.getText());
} catch (NumberFormatException e) {
}
return level;
}
/**
* 让用户修改游戏难度等级。
* @param level 修改后的游戏难度等级
*/
public void setLevel(int level) {
if (level 0 level 11) tfLevel.setText("" + level);
}
/**
* 设置"开始"按钮的状态。
*/
public void setPlayButtonEnable(boolean enable) {
btPlay.setEnabled(enable);
}
public void setPauseButtonLabel(boolean pause) {
btPause.setText(pause ? "Pause" : "Continue");
}
/**
* 重置控制面板
*/
public void reset() {
tfScore.setText("0");
plTipBlock.setStyle(0);
}
/**
* 重新计算TipPanel里的boxes[][]里的小框的大小
*/
public void fanning() {
plTipBlock.fanning();
}
/**
* 预显窗口的实现细节类
*/
private class TipPanel extends JPanel {
private Color backColor = Color.darkGray, frontColor = Color.lightGray;
private ErsBox[][] boxes =
new ErsBox[ErsBlock.BOXES_ROWS][ErsBlock.BOXES_COLS];
private int style, boxWidth, boxHeight;
private boolean isTiled = false;
/**
* 预显窗口类构造函数
*/
public TipPanel() {
for (int i = 0; i boxes.length; i++) {
for (int j = 0; j boxes[i].length; j++)
boxes[i][j] = new ErsBox(false);
}
}
/**
* 预显窗口类构造函数
* @param backColor Color, 窗口的背景色
* @param frontColor Color, 窗口的前景色
*/
public TipPanel(Color backColor, Color frontColor) {
this();
this.backColor = backColor;
this.frontColor = frontColor;
}
/**
* 设置预显窗口的方块样式
* @param style int,对应ErsBlock类的STYLES中的28个值
*/
public void setStyle(int style) {
this.style = style;
repaint();
}
/**
* 覆盖JComponent类的函数,画组件。
* @param g 图形设备环境
*/
public void paintComponent(Graphics g) {
super.paintComponent(g);
if (!isTiled) fanning();
int key = 0x8000;
for (int i = 0; i boxes.length; i++) {
for (int j = 0; j boxes[i].length; j++) {
Color color = (((key style) != 0) ? frontColor : backColor);
g.setColor(color);
g.fill3DRect(j * boxWidth, i * boxHeight,
boxWidth, boxHeight, true);
key = 1;
}
}
}
/**
* 根据窗口的大小,自动调整方格的尺寸
*/
public void fanning() {
boxWidth = getSize().width / ErsBlock.BOXES_COLS;
boxHeight = getSize().height / ErsBlock.BOXES_ROWS;
isTiled = true;
}
}
private class ControlKeyListener extends KeyAdapter {
public void keyPressed(KeyEvent ke) {
if (!game.isPlaying()) return;
ErsBlock block = game.getCurBlock();
switch (ke.getKeyCode()) {
case KeyEvent.VK_DOWN:
block.moveDown();
break;
case KeyEvent.VK_LEFT:
block.moveLeft();
break;
case KeyEvent.VK_RIGHT:
block.moveRight();
break;
case KeyEvent.VK_UP:
block.turnNext();
break;
default:
break;
}
}
}
}
求一个简单又有趣的JAVA小游戏代码
那你就自己做个猜数字好了
import java.util.*;
import java.io.*;
public class CaiShu{
public static void main(String[] args) throws IOException{
Random a=new Random();
int num=a.nextInt(100);
System.out.println("请输入一个100以内的整数:");
for (int i=0;i=9;i++){
BufferedReader bf=new BufferedReader(new InputStreamReader(System.in));
String str=bf.readLine();
int shu=Integer.parseInt(str);
if (shunum)
System.out.println("输入的数大了,输小点的!");
else if (shunum)
System.out.println("输入的数小了,输大点的!");
else {
System.out.println("恭喜你,猜对了!");
if (i=2)
System.out.println("你真是个天才!");
else if (i=6)
System.out.println("还将就,你过关了!");
else if (i=8)
System.out.println("但是你还……真笨!");
else
System.out.println("你和猪没有两样了!");
break;}
}
}
}
怎么用JAVA来写一个小游戏程序
import java.util.*;
import java.io.*;
public class CaiShu{
public static void main(String[] args) throws IOException{
Random a=new Random();
int num=a.nextInt(100);
System.out.println("请输入一个100以内的整数:");
for (int i=0;i=9;i++){
BufferedReader bf=new BufferedReader(new InputStreamReader(System.in));
String str=bf.readLine();
int shu=Integer.parseInt(str);
if (shunum)
System.out.println("输入的数大了,输小点的!");
else if (shunum)
System.out.println("输入的数小了,输大点的!");
else {
System.out.println("恭喜你,猜对了!");
if (i=2)
System.out.println("你真是个天才!");
else if (i=6)
System.out.println("还将就,你过关了!");
else if (i=8)
System.out.println("但是你还……真笨!");
else
System.out.println("你和猪没有两样了!"); break;}
}
} }