當前位置:首頁 » 游戲種類 » java小游戲實現

java小游戲實現

發布時間: 2022-11-21 08:45:34

『壹』 求一猜謎小游戲的java實現代碼和包一起!

這是我自己編的,猜4個不重復數字的小游戲。
代碼如下:
package com.entor.one.test2;
import java.util.Random;
import java.util.Scanner;
public class Game {
public static void zhuan(int temp,int input[])
{
input[0] = temp / 1000;
temp %= 1000;
input[1] = temp /100;
temp %= 100;
input[2] = temp /10;
temp %= 10;
input[3] = temp;

}
public static boolean if_xiangtong(int num[],int input[]){
//判斷輸入的數組 與隨機生成的4個數字,是否完全相同
for(int i=0;i<4;i++){
if(num[i]!=input[i])
break;
if(i==3){
return true;

}
}
return false;
}
public static void right(int num[]){

System.out.print("\n回答正確,數字是:");
for(int i=0;i<4;i++)
System.out.print(num[i]);
}
public static void tishi(int num[],int input[]){

int numA=0; //2個數組中數字相同且位置相同的 數字數量
int numB=0; //2個數組中數字相同但位子不同的數字數量

//判斷輸入的數組 與隨機生成的4個數字中,數字相同且位置相同數字數量
for(int i=0;i<4;i++)
if(num[i]==input[i])
numA++;
//判斷輸入的數組 與隨機生成的4個數字中,數字相同且位置相同數字數量
for(int i=0;i<4;i++)
for(int j=0;j<4;j++)
if(num[i]==input[j]&&i!=j)
numB++;

System.out.println("回答錯誤,數字提示是:"+numA+"A"+numB+"B");
numA = numB = 0;
}
public static void main(String arg[]){

int num[] =new int[4];
int temp;
int input[] =new int[4];
Scanner s = new Scanner(System.in);
Random random = new Random();

boolean boo ;

while(true){
//隨機生成4個不重復的數字
for(int i=0;i<4;i++)
{
num[i]= random.nextInt(9);
for(int j=i-1;j>=0;j--)
while(num[j]==num[i])
num[i]= random.nextInt(9);

}
boo = false;

while(!boo){

System.out.print("\n請輸入4個數字:");
temp = s.nextInt();

zhuan(temp,input);//將輸入的數字轉化為數組保存

boo = if_xiangtong(num,input);//判斷輸入的數組 與隨機生成的4個數字,是否完全相同

if(boo)
right(num); //回答正確,輸出結果
else
tishi(num,input); //回答錯誤,給予提示
}

}
}
}

『貳』 用JAVA編一個小游戲或者其他程序

貪吃蛇程序:
GreedSnake.java (也是程序入口):

import java.awt.BorderLayout;
import java.awt.Canvas;
import java.awt.Color;
import java.awt.Container;
import java.awt.Graphics;
import java.awt.event.KeyEvent;
import java.awt.event.KeyListener;
import java.util.Iterator;
import java.util.LinkedList;

import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JPanel;

public class GreedSnake implements KeyListener {
JFrame mainFrame;

Canvas paintCanvas;

JLabel labelScore;// 計分牌

SnakeModel snakeModel = null;// 蛇

public static final int canvasWidth = 200;

public static final int canvasHeight = 300;

public static final int nodeWidth = 10;

public static final int nodeHeight = 10;

// ----------------------------------------------------------------------
// GreedSnake():初始化游戲界面
// ----------------------------------------------------------------------
public GreedSnake() {
// 設置界面元素
mainFrame = new JFrame("GreedSnake");
Container cp = mainFrame.getContentPane();
labelScore = new JLabel("Score:");
cp.add(labelScore, BorderLayout.NORTH);
paintCanvas = new Canvas();
paintCanvas.setSize(canvasWidth + 1, canvasHeight + 1);
paintCanvas.addKeyListener(this);
cp.add(paintCanvas, BorderLayout.CENTER);
JPanel panelButtom = new JPanel();
panelButtom.setLayout(new BorderLayout());
JLabel labelHelp;// 幫助信息
labelHelp = new JLabel("PageUp, PageDown for speed;", JLabel.CENTER);
panelButtom.add(labelHelp, BorderLayout.NORTH);
labelHelp = new JLabel("ENTER or R or S for start;", JLabel.CENTER);
panelButtom.add(labelHelp, BorderLayout.CENTER);
labelHelp = new JLabel("SPACE or P for pause", JLabel.CENTER);
panelButtom.add(labelHelp, BorderLayout.SOUTH);
cp.add(panelButtom, BorderLayout.SOUTH);
mainFrame.addKeyListener(this);
mainFrame.pack();
mainFrame.setResizable(false);
mainFrame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
mainFrame.setVisible(true);
begin();
}

// ----------------------------------------------------------------------
// keyPressed():按鍵檢測
// ----------------------------------------------------------------------
public void keyPressed(KeyEvent e) {
int keyCode = e.getKeyCode();
if (snakeModel.running)
switch (keyCode) {
case KeyEvent.VK_UP:
snakeModel.changeDirection(SnakeModel.UP);
break;
case KeyEvent.VK_DOWN:
snakeModel.changeDirection(SnakeModel.DOWN);
break;
case KeyEvent.VK_LEFT:
snakeModel.changeDirection(SnakeModel.LEFT);
break;
case KeyEvent.VK_RIGHT:
snakeModel.changeDirection(SnakeModel.RIGHT);
break;
case KeyEvent.VK_ADD:
case KeyEvent.VK_PAGE_UP:
snakeModel.speedUp();// 加速
break;
case KeyEvent.VK_SUBTRACT:
case KeyEvent.VK_PAGE_DOWN:
snakeModel.speedDown();// 減速
break;
case KeyEvent.VK_SPACE:
case KeyEvent.VK_P:
snakeModel.changePauseState();// 暫停或繼續
break;
default:
}
// 重新開始
if (keyCode == KeyEvent.VK_R || keyCode == KeyEvent.VK_S
|| keyCode == KeyEvent.VK_ENTER) {
snakeModel.running = false;
begin();
}
}

// ----------------------------------------------------------------------
// keyReleased():空函數
// ----------------------------------------------------------------------
public void keyReleased(KeyEvent e) {
}

// ----------------------------------------------------------------------
// keyTyped():空函數
// ----------------------------------------------------------------------
public void keyTyped(KeyEvent e) {
}

// ----------------------------------------------------------------------
// repaint():繪制游戲界面(包括蛇和食物)
// ----------------------------------------------------------------------
void repaint() {
Graphics g = paintCanvas.getGraphics();
// draw background
g.setColor(Color.WHITE);
g.fillRect(0, 0, canvasWidth, canvasHeight);
// draw the snake
g.setColor(Color.BLACK);
LinkedList na = snakeModel.nodeArray;
Iterator it = na.iterator();
while (it.hasNext()) {
Node n = (Node) it.next();
drawNode(g, n);
}
// draw the food
g.setColor(Color.RED);
Node n = snakeModel.food;
drawNode(g, n);
updateScore();
}

// ----------------------------------------------------------------------
// drawNode():繪畫某一結點(蛇身或食物)
// ----------------------------------------------------------------------
private void drawNode(Graphics g, Node n) {
g.fillRect(n.x * nodeWidth, n.y * nodeHeight, nodeWidth - 1,
nodeHeight - 1);
}

// ----------------------------------------------------------------------
// updateScore():改變計分牌
// ----------------------------------------------------------------------
public void updateScore() {
String s = "Score: " + snakeModel.score;
labelScore.setText(s);
}

// ----------------------------------------------------------------------
// begin():游戲開始,放置貪吃蛇
// ----------------------------------------------------------------------
void begin() {
if (snakeModel == null || !snakeModel.running) {
snakeModel = new SnakeModel(this, canvasWidth / nodeWidth,
this.canvasHeight / nodeHeight);
(new Thread(snakeModel)).start();
}
}

// ----------------------------------------------------------------------
// main():主函數
// ----------------------------------------------------------------------
public static void main(String[] args) {
GreedSnake gs = new GreedSnake();
}
}

Node.java:

public class Node {

int x;

int y;

Node(int x, int y) {
this.x = x;
this.y = y;
}

}

SnakeModel.java:

import java.util.Arrays;
import java.util.LinkedList;
import java.util.Random;

import javax.swing.JOptionPane;

public class SnakeModel implements Runnable {
GreedSnake gs;

boolean[][] matrix;// 界面數據保存在數組里

LinkedList nodeArray = new LinkedList();

Node food;

int maxX;// 最大寬度

int maxY;// 最大長度

int direction = 2;// 方向

boolean running = false;

int timeInterval = 200;// 間隔時間(速度)

double speedChangeRate = 0.75;// 速度改變程度

boolean paused = false;// 游戲狀態

int score = 0;

int countMove = 0;

// UP和DOWN是偶數,RIGHT和LEFT是奇數
public static final int UP = 2;

public static final int DOWN = 4;

public static final int LEFT = 1;

public static final int RIGHT = 3;

// ----------------------------------------------------------------------
// GreedModel():初始化界面
// ----------------------------------------------------------------------
public SnakeModel(GreedSnake gs, int maxX, int maxY) {
this.gs = gs;
this.maxX = maxX;
this.maxY = maxY;
matrix = new boolean[maxX][];
for (int i = 0; i < maxX; ++i) {
matrix[i] = new boolean[maxY];
Arrays.fill(matrix[i], false);// 沒有蛇和食物的地區置false
}
// 初始化貪吃蛇
int initArrayLength = maxX > 20 ? 10 : maxX / 2;
for (int i = 0; i < initArrayLength; ++i) {
int x = maxX / 2 + i;
int y = maxY / 2;
nodeArray.addLast(new Node(x, y));
matrix[x][y] = true;// 蛇身處置true
}
food = createFood();
matrix[food.x][food.y] = true;// 食物處置true
}

// ----------------------------------------------------------------------
// changeDirection():改變運動方向
// ----------------------------------------------------------------------
public void changeDirection(int newDirection) {
if (direction % 2 != newDirection % 2)// 避免沖突
{
direction = newDirection;
}
}

// ----------------------------------------------------------------------
// moveOn():貪吃蛇運動函數
// ----------------------------------------------------------------------
public boolean moveOn() {
Node n = (Node) nodeArray.getFirst();
int x = n.x;
int y = n.y;
switch (direction) {
case UP:
y--;
break;
case DOWN:
y++;
break;
case LEFT:
x--;
break;
case RIGHT:
x++;
break;
}
if ((0 <= x && x < maxX) && (0 <= y && y < maxY)) {
if (matrix[x][y])// 吃到食物或者撞到身體
{
if (x == food.x && y == food.y)// 吃到食物
{
nodeArray.addFirst(food);// 在頭部加上一結點
// 計分規則與移動長度和速度有關
int scoreGet = (10000 - 200 * countMove) / timeInterval;
score += scoreGet > 0 ? scoreGet : 10;
countMove = 0;
food = createFood();
matrix[food.x][food.y] = true;
return true;
} else
return false;// 撞到身體
} else// 什麼都沒有碰到
{
nodeArray.addFirst(new Node(x, y));// 加上頭部
matrix[x][y] = true;
n = (Node) nodeArray.removeLast();// 去掉尾部
matrix[n.x][n.y] = false;
countMove++;
return true;
}
}
return false;// 越界(撞到牆壁)
}

// ----------------------------------------------------------------------
// run():貪吃蛇運動線程
// ----------------------------------------------------------------------
public void run() {
running = true;
while (running) {
try {
Thread.sleep(timeInterval);
} catch (Exception e) {
break;
}
if (!paused) {
if (moveOn())// 未結束
{
gs.repaint();
} else// 游戲結束
{
JOptionPane.showMessageDialog(null, "GAME OVER",
"Game Over", JOptionPane.INFORMATION_MESSAGE);
break;
}
}
}
running = false;
}

// ----------------------------------------------------------------------
// createFood():生成食物及放置地點
// ----------------------------------------------------------------------
private Node createFood() {
int x = 0;
int y = 0;
do {
Random r = new Random();
x = r.nextInt(maxX);
y = r.nextInt(maxY);
} while (matrix[x][y]);
return new Node(x, y);
}

// ----------------------------------------------------------------------
// speedUp():加快蛇運動速度
// ----------------------------------------------------------------------
public void speedUp() {
timeInterval *= speedChangeRate;
}

// ----------------------------------------------------------------------
// speedDown():放慢蛇運動速度
// ----------------------------------------------------------------------
public void speedDown() {
timeInterval /= speedChangeRate;
}

// ----------------------------------------------------------------------
// changePauseState(): 改變游戲狀態(暫停或繼續)
// ----------------------------------------------------------------------
public void changePauseState() {
paused = !paused;
}
}

『叄』 用java寫個小游戲

這個比較麻煩了,不是一句兩句就能說清楚的

1.首先要實現東西的移動可以採用重寫paint()方法每隔一段時間刷新顯示新的坐標內容

2.底部的框杠可以採用滑鼠監聽器來實時監控同樣在重寫paint()方法的時候寫入其中

3.接住物品比較簡單,可以用上述兩者的坐標重合來表示接住,然後讓計數器自加1

4.調整下降速度就可以控制第一個說的刷新時間來控制速度

偽碼不好寫,思路給你了

『肆』 怎麼用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 (shu>num)
System.out.println("輸入的數大了,輸小點的!");
else if (shu<num)
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寫一個小游戲連連看

package mybase.programe;
/*
* lianliankan總體演算法思路:由兩個確定的按鈕。若這兩個按鈕的數字相等,就開始找它們相連的路經。這個找路經
* 分3種情況:(從下面的這三種情況,我們可以知道,需要三個檢測,這三個檢測分別檢測一條直路經。這樣就會有
* 三條路經。若這三條路經上都是空按鈕,那麼就剛好是三種直線(兩個轉彎點)把兩個按鈕連接起來了)
* 1.相鄰
*
* 2. 若不相鄰的先在第一個按鈕的同行找一個空按鈕。1).找到後看第二個按鈕橫向到這個空按鈕
* 所在的列是否有按鈕。2).沒有的話再看第一個按鈕到與它同行的那個空按鈕之間是否有按鈕。3).沒有的話,再從
* 與第一個按鈕同行的那個空按鈕豎向到與第二個按鈕的同行看是否有按鈕。沒有的話路經就通了,可以消了.
*
* 3.若2失敗後,再在第一個按鈕的同列找一個空按鈕。1).找到後看第二個按鈕豎向到這個空按鈕所在的行是否有按鈕。
* 2).沒有的話,再看第一個按鈕到與它同列的那個空按鈕之間是否有按鈕。3).沒有的話,再從與第一個按鈕同列的
* 那個空按鈕橫向到與第二個按鈕同列看是否有按鈕。沒有的話路經就通了,可以消了。
*
* 若以上三步都失敗,說明這兩個按鈕不可以消去。
*/
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; // 分別記錄兩次被選中的按鈕
// 儲存游戲按鈕位置(這里其實只要6行,5列。但是我們用了8行,7列。是等於在這個面板按鈕的周圍還圍
//了一層是0的按鈕,這樣就可以實現靠近面板邊緣的兩個按鈕可以消去)
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);
mainFrame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
}

public void randomBuild() {
int randoms, cols, rows;
for (int twins = 1; twins <= 15; twins++) {//一共15對button,30個
randoms = (int) (Math.random() * 25 + 1);//button上的數字
for (int alike = 1; alike <= 2; alike++) {
cols = (int) (Math.random() * 6 + 1);
rows = (int) (Math.random() * 5 + 1);
while (grid[cols][rows] != 0) {//等於0說明這個空格有了button
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];//記下每個button的數字
n++;//有幾個沒有消去的button
}
}
}
n = n - 1;
this.grid = grid;
while (n >= 0) {//把沒有消去的button重新放一次
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) {//第二個按鈕在空按鈕右邊
for (i = y - 1; i >= j; i--) { //檢測從第二個按鈕橫向左邊到空格所在列為止是否全是空格
if (grid[x][i] != 0) {
k = 0;
break;//存在非空格的就退出,這一退出就不可能k==2了,所以就會到下而215行出同理的判斷列
} else {
k = 1;
} // K=1說明全是空格通過了第一次驗證,也就是從第二個按鈕橫向左邊到空格所在列為止全是空格
}
if (k == 1) {
linePassOne();//進入第二次驗證,也就是從第一個按鈕到它同行的空格之間的空格判斷
}
}

if (y < j) { // 第二個按鈕在空按鈕左邊
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();
}
}

//第三次檢測,檢測確定為空的第j列的那個按鈕豎向到第二個按鈕,看是不是有按鈕
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

//當上面的檢測與第一個按鈕同行的空格按鈕失敗後(不能找到與第二個按鈕的相連路經),下面就執行
//檢測與第一個按鈕同列的空格按鈕
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();
}
}
}
}
}//--------------------------------for
}//-------------else
}//------------xiao

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();
}
}

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

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

『柒』 Java各種小游戲的編程思路

Java小游戲主要的是使用java swing,通過組件化示例一個模型,滑鼠監聽移動,刷新界面,進行交互。

『捌』 java 小游戲

import java.util.Random;
import javax.swing.*;
import java.awt.BorderLayout;
import java.awt.Container;
import java.awt.event.*;

public class SmallGame extends JFrame {
private Random r;

private String[] box = { "剪刀", "石頭", "布" };

private JComboBox choice;

private JTextArea ta;

private JLabel lb;

private int win = 0;

private int loss = 0;

private int equal = 0;

public SmallGame() {
initial();//調用initial方法,就是下面定義的那個.該方法主要是初始界面.
pack();
setTitle("游戲主界面");
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
setLocation(400, 300);
setVisible(true);
}

public static void main(String[] args) {
new SmallGame();
}

public void initial() {
r = new Random(); // 生成隨機數
choice = new JComboBox();//初始化choice這個下拉框.也就是你選擇出剪子還是石頭什麼的那個下拉框..
for (int i = 0; i < box.length; i++) {//為那個下拉框賦值.用前面定義的private String[] box = { "剪刀", "石頭", "布" };附值.這樣下拉框就有三個選項了..
choice.addItem(box[i]);
}
ta = new JTextArea(3, 15);//初始化那個文本域3行15列
ta.setEditable(false);//讓用戶不能編輯那個文本域即不能在裡面寫東西
JButton okBut = new JButton("出招");//新建一個出招的按鈕
okBut.addActionListener(new ActionListener() {//給出招按鈕加個監聽.意思就是監聽著這個按鈕看用戶有沒有點擊它..如果點擊就執行下面這個方法
public void actionPerformed(ActionEvent e) {//就是這個方法
ta.setText(getResult());//給那個文本域賦值..就是顯示結果 賦值的是通過getResult()這個方法得到的返回值 getResult()這個方法下面會講
lb.setText(getTotal());//給分數那個LABEL賦值..就是顯示分數..賦值的是通過getTotal()這個方法得到的返回值
}
});
JButton clearBut = new JButton("清除分數");//新建一個清楚分數的按鈕
clearBut.addActionListener(new ActionListener() {//同上給他加個監聽
public void actionPerformed(ActionEvent e) {//如果用戶點擊了就執行這個方法
ta.setText("");//給文本域賦值為""..其實就是清楚他的內容
win = 0;//win賦值為0
loss = 0;//同上
equal = 0;//同上
lb.setText(getTotal());//給顯示分數那個賦值..因為前面已經都賦值為0了..所以這句就是讓顯示分數那都為0
}
});
lb = new JLabel(getTotal());//初始化那個顯示分數的東西
JPanel choicePanel = new JPanel();//定義一個面板..面板就相當於一個畫圖用的東西..可以在上面加按鈕啊文本域什麼的..
choicePanel.add(choice);//把下拉框加到面板里
choicePanel.add(okBut);//把出招按鈕加到面板里
choicePanel.add(clearBut);//把清楚分數按鈕加到面板里
JScrollPane resultPanel = new JScrollPane(ta);//把文本域加到一個可滾動的面板裡面..JScrollPane就是可滾動的面板..這樣如果那個文本域內容太多就會出現滾動條..而不是變大
JPanel totalPanel = new JPanel();//再定義個面板..用來顯示分數的..
totalPanel.add(lb);//把那個顯示分數的label加到裡面去
Container contentPane = getContentPane();//下面就是布局了..
contentPane.setLayout(new BorderLayout());
contentPane.add(choicePanel, BorderLayout.NORTH);
contentPane.add(resultPanel, BorderLayout.CENTER);
contentPane.add(totalPanel, BorderLayout.SOUTH);
}

public String getResult() {//獲得結果的方法 返回值是一個String..這個返回值會用來顯示在文本域裡面
String tmp = "";
int boxPeop = choice.getSelectedIndex();//獲得你選擇的那個的索引..從0開始的..
int boxComp = getBoxComp();//獲得電腦出的索引..就是隨機的0-2的數
tmp += "你出:\t" + box[boxPeop];//下面你應該明白了..
tmp += "\n電腦出:\t" + box[boxComp];
tmp += "\n結果:\t" + check(boxPeop, boxComp);
return tmp;
}

public int getBoxComp() {//就是產生一個0-2的隨機數..
return r.nextInt(3);//Random的nextInt(int i)方法就是產生一個[0-i)的隨機整數 所以nextInt(3)就是[0-2]的隨機數
}

public String check(int boxPeop, int boxComp) {//這個就是判斷你選擇的和電腦選擇的比較結果..是輸是贏還是平..boxPeop就是你選擇的..boxComp就是電腦選擇的..
String result = "";
if (boxPeop == (boxComp + 1) % 3) {//(boxComp + 1) % 3 電腦選擇的加上1加除以3取余..也就是如果電腦選0這個就為1..這個判斷的意思就是如果電腦選0並且你選1..那麼就是電腦選了
//private String[] box = { "剪刀", "石頭", "布" };這裡面下標為0的..你選了下標為1的..就是電腦選剪刀你選石頭..所以你贏了..如果電腦選1..(boxComp + 1) % 3就為2..意思就是
//電腦選了石頭你選了布..所以你贏了..另外一種情況你明白了撒..只有三種情況你贏..所以這里都包含了..也只包含了那三種..
result = "你贏了!";
win++;//贏了就讓記你贏的次數的那個變數加1
} else if (boxPeop == boxComp) {//相等當然平手了
result = "平";
equal++;//同上了
} else {//除了贏和平當然就是輸了..
result = "你輸了!";
loss++;//同上
}
return result;
}

public int getPoint() {
return (win - loss) * 10;
}

public String getTotal() {
return "贏:" + win + " 平:" + equal + " 輸:" + loss + " 得分:"
+ getPoint();
}
}

希望你能明白哈。。

『玖』 JAVA小游戲的線程簡單代碼 (兩個武士A、B對打)

首先寫個類命名people
public class people
{
//定義人有的屬性
int blood;//血
int force;//攻擊力
//定義構造方法對人進行初始化
public people(int blood,int force)
{
this.blood=blood;
this.force=force;
}
//定義人的方法,比如攻擊
public void attrack(people x)
{
x.blood-=this.force;

}
}

主體中代碼:

people a=new people(100,20);
people b=new people(100,30);
//建一個線程a攻擊b
xiancheng one=new xiancheng(a,b);
thread t1= new Thread(one);
t1.start();
//800毫秒後建一個線程b攻擊a;
thread.sleep(800);
xiancheng two=new xiancheng(b,a);
thread t2= new Thread(two);
t2.start();

class xiancheng implements runnable //繼承runnable介面
{
people x;
people y;
public xiancheng(people x,people y )
{
this.x=x;
this.y=y;
}

pulbic vid run()
{
while(y.blood>0)
{
x.attrack(y);
thread.sleep(800);
}
}
}
//很久沒用java了線程有點忘了,,汗

熱點內容
絕地求生未來之役比賽為什麼進不去 發布:2023-08-31 22:07:08 瀏覽:1391
dota2位置什麼意思 發布:2023-08-31 22:00:04 瀏覽:832
lol電競是什麼樣子 發布:2023-08-31 21:58:40 瀏覽:1291
絕地求生八倍鏡的那個圓圈怎麼弄 發布:2023-08-31 21:58:31 瀏覽:1377
lol龍龜一個多少金幣 發布:2023-08-31 21:55:07 瀏覽:741
王者如何改游戲內名稱 發布:2023-08-31 21:55:06 瀏覽:1032
游戲主播打廣告是什麼意思 發布:2023-08-31 21:55:06 瀏覽:1713
絕地求生如何免費拿到ss7賽季手冊 發布:2023-08-31 21:52:13 瀏覽:908
pgg是哪個國家的戰隊lol 發布:2023-08-31 21:52:07 瀏覽:789
一個人的時候才發現游戲很沒意思 發布:2023-08-31 21:49:24 瀏覽:1424