java遊戲代碼(java遊戲代碼分享)

本文目錄一覽:

用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[0]==chs.length){ //位置對為5

int score = chs.length*100 – count*10; //一個字符100分,錯一次減10分

System.out.println(“恭喜你猜對了,得分:” + score);

break; //猜對時跳出循環

}else{ //沒猜對

count++; //猜錯次數增1

System.out.println(“字符對:”+result[1]+”個,位置對:”+result[0]+”個”);

}

}

}

//隨機生成5個字符數組

public static char[] generate(){

char[] chs = new char[5];

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;ichs.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[2];

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

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

if(chs[i]==input[j]){ //字符對

result[1]++; //字符對個數增1

if(i==j){ //位置對

result[0]++; //位置對個數增1

}

break;

}

}

}

return result;

}

}

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數獨遊戲代碼

public class ShuDu {

/**存儲數字的數組*/

static int[][] n = new int[9][9];

/**生成隨機數字的源數組,隨機數字從該數組中產生*/

static int[] num = {1,2,3,4,5,6,7,8,9};

public static void main(String[] args) {

//生成數字

for(int i = 0;i 9;i++){

//嘗試填充的數字次數

int time = 0;

//填充數字

for(int j = 0;j 9;j++){

//產生數字

n[i][j] = generateNum(time);

//如果返回值為0,則代表卡住,退回處理

//退回處理的原則是:如果不是第一列,則先倒退到前一列,否則倒退到前一行的最後一列

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

//不是第一列,則倒退一列

if(j 0){

j-=2;

continue;

}else{//是第一列,則倒退到上一行的最後一列

i–;

j = 8;

continue;

}

}

//填充成功

if(isCorret(i,j)){

//初始化time,為下一次填充做準備

time = 0;

}else{ //繼續填充

//次數增加1

time++;

//繼續填充當前格

j–;

}

}

}

//輸出結果

for(int i = 0;i 9;i++){

for(int j = 0;j 9;j++){

System.out.print(n[i][j] + ” “);

}

System.out.println();

}

}

/**

* 是否滿足行、列和3X3區域不重複的要求

* @param row 行號

* @param col 列號

* @return true代表符合要求

*/

public static boolean isCorret(int row,int col){

return (checkRow(row) checkLine(col) checkNine(row,col));

}

/**

* 檢查行是否符合要求

* @param row 檢查的行號

* @return true代表符合要求

*/

public static boolean checkRow(int row){

for(int j = 0;j 8;j++){

if(n[row][j] == 0){

continue;

}

for(int k =j + 1;k 9;k++){

if(n[row][j] == n[row][k]){

return false;

}

}

}

return true;

}

/**

* 檢查列是否符合要求

* @param col 檢查的列號

* @return true代表符合要求

*/

public static boolean checkLine(int col){

for(int j = 0;j 8;j++){

if(n[j][col] == 0){

continue;

}

for(int k =j + 1;k 9;k++){

if(n[j][col] == n[k][col]){

return false;

}

}

}

return true;

}

/**

* 檢查3X3區域是否符合要求

* @param row 檢查的行號

* @param col 檢查的列號

* @return true代表符合要求

*/

public static boolean checkNine(int row,int col){

//獲得左上角的坐標

int j = row / 3 * 3;

int k = col /3 * 3;

//循環比較

for(int i = 0;i 8;i++){

if(n[j + i/3][k + i % 3] == 0){

continue;

}

for(int m = i+ 1;m 9;m++){

if(n[j + i/3][k + i % 3] == n[j + m/3][k + m % 3]){

return false;

}

}

}

return true;

}

/**

* 產生1-9之間的隨機數字

* 規則:生成的隨機數字放置在數組8-time下標的位置,隨着time的增加,已經嘗試過的數字將不會在取到

* 說明:即第一次次是從所有數字中隨機,第二次時從前八個數字中隨機,依次類推,

* 這樣既保證隨機,也不會再重複取已經不符合要求的數字,提高程序的效率

* 這個規則是本算法的核心

* @param time 填充的次數,0代表第一次填充

* @return

*/

public static int generateNum(int time){

//第一次嘗試時,初始化隨機數字源數組

if(time == 0){

for(int i = 0;i 9;i++){

num[i] = i + 1;

}

}

//第10次填充,表明該位置已經卡住,則返回0,由主程序處理退回

if(time == 9){

return 0;

}

//不是第一次填充

//生成隨機數字,該數字是數組的下標,取數組num中該下標對應的數字為隨機數字

int ranNum = (int)(Math.random() * (9 – time));

//把數字放置在數組倒數第time個位置,

int temp = num[8 – time];

num[8 – time] = num[ranNum];

num[ranNum] = temp;

//返回數字

return num[8 – time];

}

}

求一個簡單的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代碼猜拳遊戲相關代碼請教

comp是電腦產生的隨機數字(電腦出的拳),people 是人出的拳。 因為剪刀石頭布只有 1 2 3

。如果電腦的數字比的你剛好大1,就是它比你的大。 如21,32對應就是(石頭大於剪刀,布大於石頭)。 但也有可能是剪刀大於布 。 那麼剪刀的位子是1 ,布的位子是3. 所以當電腦數字減你的數字等於2時 就是(電腦出的布 ,你出的石頭這樣的情況了)。

所以是if((comp-people)==-1||(comp-people)==2) 這兩者結合就是你贏的時候

原創文章,作者:小藍,如若轉載,請註明出處:https://www.506064.com/zh-hant/n/230635.html

(0)
打賞 微信掃一掃 微信掃一掃 支付寶掃一掃 支付寶掃一掃
小藍的頭像小藍
上一篇 2024-12-10 18:18
下一篇 2024-12-10 18:18

相關推薦

  • Java JsonPath 效率優化指南

    本篇文章將深入探討Java JsonPath的效率問題,並提供一些優化方案。 一、JsonPath 簡介 JsonPath是一個可用於從JSON數據中獲取信息的庫。它提供了一種DS…

    編程 2025-04-29
  • java client.getacsresponse 編譯報錯解決方法

    java client.getacsresponse 編譯報錯是Java編程過程中常見的錯誤,常見的原因是代碼的語法錯誤、類庫依賴問題和編譯環境的配置問題。下面將從多個方面進行分析…

    編程 2025-04-29
  • Python周杰倫代碼用法介紹

    本文將從多個方面對Python周杰倫代碼進行詳細的闡述。 一、代碼介紹 from urllib.request import urlopen from bs4 import Bea…

    編程 2025-04-29
  • Java騰訊雲音視頻對接

    本文旨在從多個方面詳細闡述Java騰訊雲音視頻對接,提供完整的代碼示例。 一、騰訊雲音視頻介紹 騰訊雲音視頻服務(Cloud Tencent Real-Time Communica…

    編程 2025-04-29
  • Python字符串寬度不限制怎麼打代碼

    本文將為大家詳細介紹Python字符串寬度不限制時如何打代碼的幾個方面。 一、保持代碼風格的統一 在Python字符串寬度不限制的情況下,我們可以寫出很長很長的一行代碼。但是,為了…

    編程 2025-04-29
  • Java Bean加載過程

    Java Bean加載過程涉及到類加載器、反射機制和Java虛擬機的執行過程。在本文中,將從這三個方面詳細闡述Java Bean加載的過程。 一、類加載器 類加載器是Java虛擬機…

    編程 2025-04-29
  • Java Milvus SearchParam withoutFields用法介紹

    本文將詳細介紹Java Milvus SearchParam withoutFields的相關知識和用法。 一、什麼是Java Milvus SearchParam without…

    編程 2025-04-29
  • Python基礎代碼用法介紹

    本文將從多個方面對Python基礎代碼進行解析和詳細闡述,力求讓讀者深刻理解Python基礎代碼。通過本文的學習,相信大家對Python的學習和應用會更加輕鬆和高效。 一、變量和數…

    編程 2025-04-29
  • 為什麼不用Python開發遊戲

    Python是一種高級編程語言,擁有簡單易學、代碼簡潔等優點。同時,Python也是一種多用途的語言,可以用於Web開發、數據分析以及機器學習等領域。然而,對於遊戲開發領域,Pyt…

    編程 2025-04-29
  • Java 8中某一周的周一

    Java 8是Java語言中的一個版本,於2014年3月18日發布。本文將從多個方面對Java 8中某一周的周一進行詳細的闡述。 一、數組處理 Java 8新特性之一是Stream…

    編程 2025-04-29

發表回復

登錄後才能評論