導航:首頁 > 源碼編譯 > java文字小游戲源碼

java文字小游戲源碼

發布時間:2022-11-14 02:05:58

1. 求一個java的完整程序代碼 文字游戲

只要文字界面?不需要UI?這是什麼奇葩作業?
你在問題中提到了一個類,什麼類?將上面的要求寫成一個類,再用一個主類去啟動?

就你目前給的要求,而且僅是文字沒有UI的話,大概50行左右的代碼就能搞定,有意義么?

2. 求java小游戲源代碼

[最佳答案] 連連看java源代碼 import javax.swing.*; import java.awt.*; import java.awt.event.*; pu... int x0=0,y0=0,x=0,y=0,fristMsg=0,secondMsg=0,validateLV; //游戲按鈕的位...

3. 求一個簡單又有趣的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;//消除方法控制

代碼(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 拉丁字母表字元的位模式在所有的代碼頁中都相同,但用於表示重音字元(如"é"和"á")的位模式在不同的代碼頁中卻不同。如果在運行不同代碼頁的計算機間交換數據,必須將所有字元數據由發送計算機的代碼頁轉換為接收計算機的代碼頁。如果源數據中的擴展字元在接收計算機的代碼頁中未定義,那麼數據將丟失。

如果某個資料庫為來自許多不同國家的客戶端提供服務,則很難為該資料庫選擇這樣一種代碼頁,使其包括所有客戶端計算機所需的全部擴展字元。而且,在代碼頁間不停地轉換需要花費大量的處理時間。

4. 求java小游戲源代碼

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 paian = "true";

AudioClip Musci_anjian, Music_shi, Music_chenggong;

public Frame() {
try {
setDefaultCloseOperation(EXIT_ON_CLOSE);

//-----------------聲音文件---------------------
Musci_anjian = Applet.newAudioClip(new File("sounds//anjian.wav")
.toURL());
Music_shi = Applet.newAudioClip(new File("sounds//shi.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;
paian = "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 (paian.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;
paian = "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_shi.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;
}
}

5. 大神們 急求基於eclipse的java小游戲程序的源碼,程序不要多復雜啊。像坦克大戰,五子棋,掃雷之類的謝謝


import java.util.Scanner;


public class Wuziqi {

/**

* 棋盤

*/

private final int[][] qipan;

/**

* 步數

*/

private int bushu;

/**

* 構造方法,設置棋盤規格

* @param x

* @param y

*/

public Wuziqi(int x, int y) {

if (x < 1 || y < 1) {

System.out.println("棋盤規格應不小於1,使用默認規格");

qipan = new int[9][9];

} else {

qipan = new int[y][x];

}

}

/**

* 游戲開始

*/

public void play() {

int[] zuobiao = null;

//如果游戲沒有結束

while (!end(zuobiao)) {

//落子,並取得坐標

zuobiao = luozi();

//輸出棋盤

out();

}

}

/**

* 輸出棋盤和棋子

*/

private void out() {

for (int i = 0; i < qipan.length; i++) {

for (int j = 0; j < qipan[i].length; j++) {

if (qipan[i][j] == 0) {

System.out.print(" +");

}else if (qipan[i][j] == -1) {

System.out.print(" 白");

}else if (qipan[i][j] == 1) {

System.out.print(" 黑");

}

}

System.out.println(" ");

}

}

/**

* 落子

*/

private int[] luozi() {

int[] zuobiao;

bushu++;

if (bushu % 2 == 1) {

System.out.println("請黑方落子");

zuobiao = input();

qipan[zuobiao[1]][zuobiao[0]] = 1;

}else {

System.out.println("請白方落子");

zuobiao = input();

qipan[zuobiao[1]][zuobiao[0]] = -1;

}

return zuobiao;

}

/**

* 輸入坐標

* @return

*/

private int[] input() {

Scanner sc = new Scanner(System.in);

System.out.println("請輸入x軸坐標");

String x = sc.next();

System.out.println("請輸入y軸坐標");

String y = sc.next();

//如果沒有通過驗證,則再次執行input(),遞歸演算法

if (!validate(x, y)) {

return input();

}

int int_x = Integer.valueOf(x);

int int_y = Integer.valueOf(y);

return new int[] {int_x, int_y};

}

/**

* 校驗數據

* @param x

* @param y

* @return

*/

private boolean validate(String x, String y) {

Integer int_x = null;

Integer int_y = null;

//異常處理的方式判斷字元串是否是一個整數

try {

int_x = Integer.valueOf(x);

int_y = Integer.valueOf(y);

} catch (NumberFormatException e) {

System.out.println("坐標格式錯誤,坐標應為整數");

return false;

}

if (int_x < 0 || int_y < 0 || int_x >= qipan[0].length || int_y >= qipan.length) {

System.out.println("坐標越界");

return false;

}

if (qipan[int_y][int_x] == 0) {

return true;

} else {

System.out.println("坐標上已有棋子");

}

return false;

};

/**

* 結束條件

* @return

*/

private boolean end(int[] zuobiao) {

if (zuobiao == null) {

return false;

}

//計數器

//表示棋盤上經過最近落子坐標的4條線上的連續(和最近落子顏色相同的)棋子的個數

//如果某條線上連續的棋子大於等於4(加上最近落子本身,大於等於5),則游戲結束,符合五子棋規則

int[] jieguo = new int[4];

int x = zuobiao[0];

int y = zuobiao[1];

//定義八個方向

final int[][] fangxiang = {{-1, 0}, {-1, 1}, {0, 1}, {1, 1}, {1, 0}, {1, -1}, {0, -1}, {-1, -1}};

//最近落子的坐標上的棋子顏色

int number = qipan[y][x];

//搜索最近落子坐標為中心最遠4的距離

for (int i = 1; i <= 4; i++) {

//每次搜索不同的距離都搜索八個方向

for (int j = 0; j < fangxiang.length; j++) {

//約定如果某個方向為null時,不再搜索這個方向。關鍵字continue是跳過本次(一次)循環的意思

if (fangxiang[j] == null) {

continue;

}

int mubiao_x = x + i * fangxiang[j][0];

int mubiao_y = y + i * fangxiang[j][1];

//如果搜索坐標相對於棋盤越界,則不再搜索這個方向

if (mubiao_y >= qipan.length || mubiao_y < 0 || mubiao_x >= qipan[0].length || mubiao_x < 0) {

fangxiang[j] = null;

continue;

}

//如果最近落子坐標上的值等於目標坐標上的值(顏色相同),則計數器上某條線加1

//否則認為這個方向沒有棋子或有別的顏色的棋子,不再搜索這個方向

if (number == qipan[mubiao_y][mubiao_x]) {

jieguo[j % 4]++;

}else {

fangxiang[j] = null;

}

}

}

//查看計數器上是否有比3更大的數(查看是否有一方勝出)

for (int i : jieguo) {

if (i > 3) {

System.out.println("游戲結束");

if (bushu % 2 == 1) {

System.out.println("黑方勝");

} else {

System.out.println("白方勝");

}

return true;

}

}

//沒有勝出者的情況下,查看棋盤上是否還有空位置,如果有,則游戲可以繼續

for (int[] arr : qipan) {

for (int i : arr) {

if (i == 0) {

return false;

}

}

}

//如果沒有空位置,則平局

System.out.println("游戲結束,平局");

return true;

}


}

6. 跪求JAVA編寫的小游戲源代碼

import java.awt.*;
import java.applet.*;
import java.awt.event.*;
import java.awt.image.*;
import java.awt.geom.*;
import java.util.*;
class Car{
Image img;
int x, y;
Dimension dim;
Car(Image img){
this.img=img;
}
Car(Image img, int x, int y){
this.img=img; this.x=x; this.y=y;
}
Car(Image img, int x, int y, Dimension dim){
this(img, x, y);
this.dim=dim;
}
void draw(Graphics g, ImageObserver observer){
g.drawImage(img, x, y, observer);
}
int getX(){ return x;}
int getY(){ return y;}
void setX(int x){this.x=x;}
void setY(int y){this.y=y;}
void setLocation(int x, int y){
this.x=x; this.y=y;
}

int getWidth(){ return img.getWidth(null);}
int getHeight(){ return img.getHeight(null);}
Rectangle2D getRectangle(){
return new Rectangle2D.Float(x, y, getWidth(), getHeight());
}
void move(int dx, int dy){
x+=dx;
y+=dy;
if(dim!=null){
if(x<0)x=0;
if(x+getWidth()>dim.getWidth())
x=(int)dim.getWidth()-getWidth();
}
}
boolean intersects(Car car){
return getRectangle().intersects(car.getRectangle());
}
boolean intersects(int x, int y){
return getRectangle().intersects(x, y, getWidth(), getHeight());
}
}
public class CarRace extends Applet implements KeyListener, Runnable{
Image buff;
Canvas screen;
Graphics2D gs, gb;
Car redCar;
Car[] enemy=new Car[20];
Button bStart;
Thread game;
boolean loop=true;
Dimension dim=new Dimension(200, 300);
int road;
Random rnd=new Random();
public void init(){
prepareResource();
setBackground(Color.blue);
initScreen();
add(screen);
bStart=new Button("霸烙 矯累");
add(bStart);
bStart.addActionListener(new ActionListener(){
public void actionPerformed(ActionEvent ae){
screen.requestFocus();
if(!game.isAlive())
game.start();
}
});
}
void prepareResource(){
Image imgRed=getImage(getCodeBase(),"images/red_car.gif");
Image imgBlue=getImage(getCodeBase(),"images/blue_car.gif");
Image imgGreen=getImage(getCodeBase(),"images/green_car.gif");
MediaTracker mt=new MediaTracker(this);
try{
mt.addImage(imgRed, 0);
mt.addImage(imgBlue, 1);
mt.addImage(imgGreen, 2);
mt.waitForAll();
}catch(Exception e){}
buff=createImage((int)dim.getWidth(), (int)dim.getHeight());
gb=(Graphics2D)buff.getGraphics();
redCar=new Car(imgRed, 80,250, dim);

for(int i=0;i<10;i++){
enemy[i]=new Car(imgBlue, 0, 0);
}
for(int i=10;i<enemy.length;i++){
enemy[i]=new Car(imgGreen, 0, 0);
}
for(int i=0;i<enemy.length;i++){
setEnemy(i);
}
game=new Thread(this);
}
public void stop(){
loop=false;
}
public void run(){
while(loop){
drawScreen();
try{ Thread.sleep(50);}catch(Exception e){}
}
}
void initScreen(){
screen=new Canvas(){
public void paint(Graphics g){
if(gs==null){
gs=(Graphics2D)screen.getGraphics();
}
drawScreen();
}
};
screen.setSize(dim);
screen.addKeyListener(this);
}
void setEnemy(int en){
int x, y;
next:while(true){
x=rnd.nextInt((int)dim.getWidth()-enemy[en].getWidth());
y=-rnd.nextInt(5000)-200;
for(int j=0;j<enemy.length;j++){
if(j!=en && enemy[j].intersects(x, y))continue next;
}
enemy[en].setLocation(x, y);
break;
}
}
void check(Car en){
if(redCar.intersects(en)){
if(redCar.getX()>en.getX()){
en.move(-20, 0);
redCar.move(20, 0);
}
else{
en.move(20,0);
redCar.move(-20, 0);
}
}
}
synchronized void drawScreen(){
gb.clearRect(0, 0, (int)dim.getWidth(), (int)dim.getHeight());
gb.setPaint(new Color(100, 100, 100));
gb.fillRect(0, 0, (int)dim.getWidth(), (int)dim.getHeight());
drawRoad();
for(int i=0;i<enemy.length;i++){
enemy[i].move(0, 15);
enemy[i].draw(gb, screen);
if(enemy[i].getY()>dim.getHeight())
setEnemy(i);
check(enemy[i]);
}
redCar.draw(gb, screen);
gs.drawImage(buff, 0,0, screen);
}
void drawRoad(){
road+=80;
gb.setPaint(Color.yellow);
gb.fillRect((int)dim.getWidth()/2, road,10,150);
if(road>=dim.getHeight())road=-150;
}
public void keyPressed(KeyEvent ke){
if(ke.getKeyCode()==KeyEvent.VK_LEFT){
redCar.move(-10,0);
}
else if(ke.getKeyCode()==KeyEvent.VK_RIGHT){
redCar.move(10,0);
}
}
public void keyReleased(KeyEvent ke){}
public void keyTyped(KeyEvent ke){}
}
這個是一個典型的Applet,文件名應該是CarRace.java,需要圖片作為車子等等,本人也是別人給的,但是還要個.html文件來運行它,代碼為:
<Applet code="CarRace.class" width=250 height=350 >
</Applet>
請樓主採納

7. 急需基於eclipse的JAVA小游戲源代碼!!!

單人版五子棋,不用導入,直接新建一個mywindow類就行,然後把一下代碼粘貼就Ok了。或者,直接用dos就可以了。。
---------------------
import java.awt.*;
import java.awt.event.*;
import javax.swing.*;

class mypanel extends Panel implements MouseListener
{
int chess[][] = new int[11][11];
boolean Is_Black_True;
mypanel()
{
Is_Black_True = true;
for(int i = 0;i < 11;i++)
{
for(int j = 0;j < 11;j++)
{
chess[i][j] = 0;
}
}
addMouseListener(this);
setBackground(Color.BLUE);
setBounds(0, 0, 360, 360);
setVisible(true);
}
public void mousePressed(MouseEvent e)
{
int x = e.getX();
int y = e.getY();

if(x < 25 || x > 330 + 25 ||y < 25 || y > 330+25)
{
return;
}
if(chess[x/30-1][y/30-1] != 0)
{
return;
}
if(Is_Black_True == true)
{
chess[x/30-1][y/30-1] = 1;
Is_Black_True = false;
repaint();
Justisewiner();
return;
}
if(Is_Black_True == false)
{
chess[x/30-1][y/30-1] = 2;
Is_Black_True = true;
repaint();
Justisewiner();
return;
}
}
void Drawline(Graphics g)
{
for(int i = 30;i <= 330;i += 30)
{
for(int j = 30;j <= 330; j+= 30)
{
g.setColor(Color.WHITE);
g.drawLine(i, j, i, 330);
}
}

for(int j = 30;j <= 330;j += 30)
{
g.setColor(Color.WHITE);
g.drawLine(30, j, 330, j);
}

}
void Drawchess(Graphics g)
{
for(int i = 0;i < 11;i++)
{
for(int j = 0;j < 11;j++)
{
if(chess[i][j] == 1)
{
g.setColor(Color.BLACK);
g.fillOval((i + 1) * 30 - 8, (j + 1) * 30 - 8, 16, 16);
}
if(chess[i][j] == 2)
{
g.setColor(Color.WHITE);
g.fillOval((i + 1) * 30 - 8, (j + 1) * 30 - 8, 16, 16);
}
}
}
}
void Justisewiner()
{
int black_count = 0;
int white_count = 0;
int i = 0;

for(i = 0;i < 11;i++)//橫向判斷
{
for(int j = 0;j < 11;j++)
{
if(chess[i][j] == 1)
{
black_count++;
if(black_count == 5)
{
JOptionPane.showMessageDialog(this, "黑棋勝利");
Clear_Chess();
return;
}
}
else
{
black_count = 0;
}
if(chess[i][j] == 2)
{
white_count++;
if(white_count == 5)
{
JOptionPane.showMessageDialog(this, "白棋勝利");
Clear_Chess();
return;
}
}
else
{
white_count = 0;
}
}
}

for(i = 0;i < 11;i++)//豎向判斷
{
for(int j = 0;j < 11;j++)
{
if(chess[j][i] == 1)
{
black_count++;
if(black_count == 5)
{
JOptionPane.showMessageDialog(this, "黑棋勝利");
Clear_Chess();
return;
}
}
else
{
black_count = 0;
}
if(chess[j][i] == 2)
{
white_count++;
if(white_count == 5)
{
JOptionPane.showMessageDialog(this, "白棋勝利");
Clear_Chess();
return;
}
}
else
{
white_count = 0;
}
}
}

for(i = 0;i < 7;i++)//左向右斜判斷
{
for(int j = 0;j < 7;j++)
{
for(int k = 0;k < 5;k++)
{
if(chess[i + k][j + k] == 1)
{
black_count++;
if(black_count == 5)
{
JOptionPane.showMessageDialog(this, "黑棋勝利");
Clear_Chess();
return;
}
}
else
{
black_count = 0;
}
if(chess[i + k][j + k] == 2)
{
white_count++;
if(white_count == 5)
{
JOptionPane.showMessageDialog(this, "白棋勝利");
Clear_Chess();
return;
}
}
else
{
white_count = 0;
}
}
}
}

for(i = 4;i < 11;i++)//右向左斜判斷
{
for(int j = 6;j >= 0;j--)
{
for(int k = 0;k < 5;k++)
{
if(chess[i - k][j + k] == 1)
{
black_count++;
if(black_count == 5)
{
JOptionPane.showMessageDialog(this, "黑棋勝利");
Clear_Chess();
return;
}
}
else
{
black_count = 0;
}
if(chess[i - k][j + k] == 2)
{
white_count++;
if(white_count == 5)
{
JOptionPane.showMessageDialog(this, "白棋勝利");
Clear_Chess();
return;
}
}
else
{
white_count = 0;
}
}
}
}

}
void Clear_Chess()
{
for(int i=0;i<11;i++)
{
for(int j=0;j<11;j++)
{
chess[i][j]=0;
}
}
repaint();
}
public void paint(Graphics g)
{
Drawline(g);
Drawchess(g);
}
public void mouseExited(MouseEvent e){}
public void mouseEntered(MouseEvent e){}
public void mouseReleased(MouseEvent e){}
public void mouseClicked(MouseEvent e){}

}

class myframe extends Frame implements WindowListener
{
mypanel panel;
myframe()
{
setLayout(null);
panel = new mypanel();
add(panel);
panel.setBounds(0,23, 360, 360);
setTitle("單人版五子棋");
setBounds(200, 200, 360, 383);
setVisible(true);
addWindowListener(this);

}
public void windowClosing(WindowEvent e)
{
System.exit(0);
}
public void windowDeactivated(WindowEvent e){}
public void windowActivated(WindowEvent e){}
public void windowOpened(WindowEvent e){}
public void windowClosed(WindowEvent e){}
public void windowIconified(WindowEvent e){}
public void windowDeiconified(WindowEvent e){}
}
public class mywindow
{
public static void main(String argc [])
{
myframe f = new myframe();
}
}

8. java小游戲源代碼

介紹這個給你把...我空間還有很多..

http://hi..com/282919088

importjava.applet.Applet;

importjava.applet.AudioClip;

importjava.awt.Dimension;

importjava.awt.Font;

importjava.awt.Toolkit;

importjavax.sound.sampled.AudioFileFormat;

importjavax.sound.sampled.AudioSystem;

importjavax.swing.JFrame;

importjavax.swing.JPanel;

importjava.awt.Rectangle;

importjavax.swing.BorderFactory;

importjavax.swing.JButton;

importjavax.swing.JOptionPane;

importjavax.swing.JSlider;

importjavax.swing.JLabel;

importjavax.swing.SwingUtilities;

importjavax.swing.UIManager;

importjavax.swing.event.ChangeEvent;

importjavax.swing.event.ChangeListener;

importjava.awt.event.ActionEvent;

importjava.awt.event.ActionListener;

importjava.awt.event.KeyAdapter;

importjava.awt.event.KeyEvent;

importjava.io.File;

importjava.util.Vector;

{

JPanelcontentPane;

JPaneljPanel1=newJPanel();

JButtonjButton1=newJButton();

JSliderjSlider1=newJSlider();

JLabeljLabel1=newJLabel();

JButtonjButton2=newJButton();

JLabeljLabel2=newJLabel();

intcount=1,rapidity=80;//count當前進行的個數,rapidity游標的位置

intzhengque=0,cuowu=0;

intrush[]={10,20,30};//游戲每關的個數可以自由添加.列{10,20,30,40,50}

intrush_count=0;//記錄關數

charlist[]={'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'};//隨機出現的數字可以自由添加

Vectornumber=newVector();

Stringpaian="true";

AudioClipMusci_anjian,Music_shi,Music_chenggong;

publicFrame(){

try{

setDefaultCloseOperation(EXIT_ON_CLOSE);

//-----------------聲音文件---------------------

Musci_anjian=Applet.newAudioClip(newFile("sounds//anjian.wav")

.toURL());

Music_shi=Applet.newAudioClip(newFile("sounds//shi.wav")

.toURL());

Music_chenggong=Applet.newAudioClip(newFile(

"sounds//chenggong.wav").toURL());

//---------------------------------------

jbInit();

}catch(Exceptionexception){

exception.printStackTrace();

}

}

/**

*Componentinitialization.

*

*@throwsjava.lang.Exception

*/

privatevoidjbInit()throwsException{

contentPane=(JPanel)getContentPane();

contentPane.setLayout(null);

setSize(newDimension(588,530));

setTitle("FrameTitle");

jPanel1.setBorder(BorderFactory.createEtchedBorder());

jPanel1.setBounds(newRectangle(4,4,573,419));

jPanel1.setLayout(null);

jButton1.setBounds(newRectangle(277,442,89,31));

jButton1.setText("開始");

jButton1.addActionListener(newFrame1_jButton1_actionAdapter(this));

jSlider1.setBounds(newRectangle(83,448,164,21));

jSlider1.setMaximum(100);

jSlider1.setMinimum(1);

jSlider1.setValue(50);

jLabel1.setText("速度");

jLabel1.setBounds(newRectangle(35,451,39,18));

jButton2.setBounds(newRectangle(408,442,89,31));

jButton2.setText("結束");

jButton2.addActionListener(newFrame1_jButton2_actionAdapter(this));

jLabel2.setText("第一關:100個");

jLabel2.setBounds(newRectangle(414,473,171,21));

contentPane.add(jPanel1);

contentPane.add(jButton2);

contentPane.add(jButton1);

contentPane.add(jSlider1);

contentPane.add(jLabel1);

contentPane.add(jLabel2);

this.addKeyListener(newMyListener());

jButton1.addKeyListener(newMyListener());

jSlider1.addKeyListener(newMyListener());

jSlider1.addChangeListener(newChangeListener(){

publicvoidstateChanged(ChangeEvente){

rapidity=jSlider1.getValue();

}

});

}

publicvoidrun(){

number.clear();

zhengque=0;

cuowu=0;

paian="true";

while(count<=rush[rush_count]){

try{

Threadt=newThread(newTthread());

t.start();

count+=1;

Thread.sleep(1000+(int)(Math.random()*2000));//生產下組停頓時間

//最快1快.最慢2秒

}catch(InterruptedExceptione){

e.printStackTrace();

}

}

while(true){//等待最後一個字元消失

if(number.size()==0){

break;

}

}

if(zhengque==0){//為了以後相除..如果全部正確或者錯誤就會出現錯誤.所以..

zhengque=1;

}

if(cuowu==0){

cuowu=1;

}

if(paian.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);//選擇位置

}

Threadt=newThread(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;

}

}

publicvoidjButton1_actionPerformed(ActionEvente){

Threadt=newThread(this);

t.start();

}

publicvoidjButton2_actionPerformed(ActionEvente){

count=rush[rush_count]+1;

paian="flase";

}

{

publicvoidrun(){

booleanfo=true;

intY=0,X=0;

JLabelshow=newJLabel();

show.setFont(newjava.awt.Font("宋體",Font.PLAIN,33));

jPanel1.add(show);

X=10+(int)(Math.random()*400);

Stringparameter=list[(int)(Math.random()*list.length)]+"";

Beanbean=newBean();

bean.setParameter(parameter);

bean.setShow(show);

number.add(bean);

show.setText(parameter);

while(fo){

//---------------------數字下移--------------------

show.setBounds(newRectangle(X,Y+=2,33,33));

try{

Thread.sleep(rapidity);

}catch(InterruptedExceptione){

e.printStackTrace();

}

if(Y>=419){

fo=false;

for(inti=number.size()-1;i>=0;i--){

Beanbn=((Bean)number.get(i));

if(parameter.equalsIgnoreCase(bn.getParameter())){

cuowu+=1;

jLabel2.setText("正確:"+zhengque+"個,錯誤:"+cuowu

+"個");

number.removeElementAt(i);

Music_shi.play();

break;

}

}

}

}

}

}

{

publicvoidkeyPressed(KeyEvente){

Stringuu=e.getKeyChar()+"";

for(inti=0;i<number.size();i++){

Beanbean=((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();

}

}

publicstaticvoidmain(String[]args){

try{

UIManager.setLookAndFeel(UIManager.getSystemLookAndFeelClassName());

}catch(Exceptionexception){

exception.printStackTrace();

}

Frameframe=newFrame();

DimensionscreenSize=Toolkit.getDefaultToolkit().getScreenSize();

DimensionframeSize=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);

}

}

classFrame1_jButton2_{

privateFrameadaptee;

Frame1_jButton2_actionAdapter(Frameadaptee){

this.adaptee=adaptee;

}

publicvoidactionPerformed(ActionEvente){

adaptee.jButton2_actionPerformed(e);

}

}

classFrame1_jButton1_{

privateFrameadaptee;

Frame1_jButton1_actionAdapter(Frameadaptee){

this.adaptee=adaptee;

}

publicvoidactionPerformed(ActionEvente){

adaptee.jButton1_actionPerformed(e);

}

}

classBean{

Stringparameter=null;

JLabelshow=null;

publicJLabelgetShow(){

returnshow;

}

publicvoidsetShow(JLabelshow){

this.show=show;

}

publicStringgetParameter(){

returnparameter;

}

publicvoidsetParameter(Stringparameter){

this.parameter=parameter;

}

}

9. 求一個簡單的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;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[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文字小游戲源碼相關的資料

熱點內容
python字元串中符號 瀏覽:785
python正則表達式貪婪模式 瀏覽:648
愛國精神指的是什麼app 瀏覽:408
壽司解壓系列全集視頻 瀏覽:913
物體三維重建演算法 瀏覽:984
fuli直播app哪個好 瀏覽:918
租辦公室用什麼app 瀏覽:106
醫師定期考核刷題app哪個好 瀏覽:338
導出dmp文件命令 瀏覽:288
手機百度網盤怎麼解壓密碼文件 瀏覽:585
索引重新編譯 瀏覽:606
命令與征服4免cd補丁完美版 瀏覽:428
kotlin編譯為native 瀏覽:142
家用編譯機 瀏覽:552
電子加密貨幣最新政策 瀏覽:382
androidcanvas撤銷 瀏覽:272
安卓手機怎麼把圖標全部下移 瀏覽:187
飢荒被伺服器踢出怎麼進 瀏覽:173
c編譯器哪款好 瀏覽:734
快手寶哥發明什麼app 瀏覽:823