當前位置:首頁 » 編程語言 » java最簡單小游戲程序

java最簡單小游戲程序

發布時間: 2022-12-25 02:43:09

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

希望你能明白哈。。

B. 用JAVA編寫一個小游戲

前天寫的猜數字游戲,yongi控制猜測次數,有詳細解析,用黑窗口可以直接運行,

我試驗過了,沒問題

import javax.swing.Icon;
import javax.swing.JOptionPane;
public class CaiShuZi4JOptionPane {
/**
* @param args
*/
public static void main(String[] args) {
Icon icon = null;
boolean bl = false;
int put = 0;
int c = (int) (((Math.random())*100)+1); //獲取一個1-100的隨機數
System.out.println("你獲取的隨機數是:"+c); //列印你的隨機數字

String str1 = (String) JOptionPane.showInputDialog(null,"請輸入你的猜測數字(1-100): ","猜數字游戲",JOptionPane.PLAIN_MESSAGE,icon,null,"在這輸入"); //第一次輸入你的猜測數字

if(str1==null){
JOptionPane.showMessageDialog(null, "你已經取消了本次游戲"); //如果你點取消那麼本次游戲結束
}else{
bl = num(str1); //判斷是輸入的是不是數字或者是整數
if(true==bl){ //如果是數字的話進入與隨機數比較的程序
System.out.println("你輸入的數字是:"+str1); //列印你輸入的數字
put = Integer.valueOf(str1);

for(int i = 4;i > 0;i--){ //i是你可以猜測的次數
if(put==c){
JOptionPane.showMessageDialog(null, "恭喜你猜對了,正確答案是:"+c+"。"); //如果你猜對了就直接結束循環
break;
}else if(put>c){ //如果輸大了就讓你再次從新輸入
str1 = (String) JOptionPane.showInputDialog(null,"你的輸入過大。你還有"+i+"次機會,請重新輸入: ","猜數字游戲",JOptionPane.PLAIN_MESSAGE,icon,null,"在這輸入");
if(str1==null){
JOptionPane.showMessageDialog(null, "你已經取消了本次輸入");
break;
}else{
bl =num(str1);
if(true==bl){
put = Integer.valueOf(str1);
}else{
JOptionPane.showMessageDialog(null, "你的輸入不正確,請重新輸入");
}
}
}else if(put<c){ //如果你輸小了也讓你從新輸入
str1 = (String) JOptionPane.showInputDialog(null,"你的輸入過小。你還有"+i+"次機會,請重新輸入: ","猜數字游戲",JOptionPane.PLAIN_MESSAGE,icon,null,"在這輸入");
if(str1==null){
JOptionPane.showMessageDialog(null, "你已經取消了本次輸入");
break;
}else{
bl =num(str1);
if(true==bl){
put = Integer.valueOf(str1);
}else{
JOptionPane.showMessageDialog(null, "你的輸入不正確,請重新輸入");
}
}
}
}


}else if(bl==false){ //這個 是你第一次如果填寫的不是數字的話也會結束本次游戲
JOptionPane.showMessageDialog(null, "請您下次按要求填寫。本次游戲結束");
}
if(true==bl && c!=put){ //如果你i次都沒猜對,那麼就直接告訴你這個數十什麼
JOptionPane.showMessageDialog(null, "很遺憾你沒能猜對,這個數字是:"+c+".");
}

}

}

public static boolean num(String value){ //一個靜態方法,判斷你輸入的是不是數字
try {
Integer.parseInt(value);
return true;
} catch (Exception e) {
return false;
}

}
}

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

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

D. 怎麼用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;}
}
} }

E. 急求簡單的手機java小游戲代碼 能運行的 簡單的就可以

package test;

import java.util.Random;
import java.util.Scanner;

public class TestGame {

/**
* @param args
*/
public static void main(String[] args) {
// TODO Auto-generated method stub

Scanner sc = new Scanner(System.in);
int r = new Random().nextInt(999999);
int count =0 ;
while(true){

System.out.println("猜數字游戲,請輸入一個數0到999999,輸入-1結束游戲:");
int i = sc.nextInt() ;
if(i==-1){
break ;
}
count ++ ;
if(i<r){
System.out.print("你猜小了。");
System.out.println("你已經猜了"+count+"次");
}else if(i>r){
System.out.println("你猜大了。");
System.out.println("你已經猜了"+count+"次");
}else{
System.out.println("恭喜你大對了,但是沒獎勵!");
}

}

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

}

F. 用JAVA編一個小游戲或者其他程序

import java.util.Random;
import java.util.Scanner;

public class Game {

private static int win=0;
private static int fail=0;
private static int pi=0;
private static void check(int cpu,int pe){
int t=0;
if(pe-cpu==2) t= -1;
else if(pe-cpu==-2) t= 1;
else t=pe-cpu;
if(t>0) {System.out.println("你贏了!");win++;}
else if(t==0) {System.out.println("咱們平了!");pi++;}
else {System.out.println("你輸了!");fail++;}
}
public static void main(String[] args) {
String input="";
String cpuStr="";
Random rand=new Random();
int cpu=0;
int pe=0;
while(true){
System.out.println("*************************小游戲一個 輸e/E可以退出*****************");
System.out.println("請選擇你要出什麼?F--剪刀(forfex),S--石頭(stone),C--布(cloth)");
Scanner scan=new Scanner(System.in);
input=scan.nextLine();
cpu=rand.nextInt(3);
if(cpu==0)cpuStr="剪刀";
else if(cpu==1)cpuStr="石頭";
else cpuStr="布";

if(input.equals("F")||input.equals("f")){
pe=0;
System.out.println("你出的是,剪刀");
System.out.println("我出"+cpuStr);
check(cpu,pe);
}else if(input.equals("S")||input.equals("s")){
pe=1;
System.out.println("你出的是,石頭");
System.out.println("我出"+cpuStr);
check(cpu,pe);
}else if(input.equals("C")||input.equals("c")){
pe=2;
System.out.println("你出的是,布");
System.out.println("我出"+cpuStr);
check(cpu,pe);
}else if(input.equals("E")||input.equals("e")){
System.out.println("結束游戲。。");
System.out.println("結果統計:");
System.out.println("勝:"+win+"局");
System.out.println("負:"+fail+"局");
System.out.println("平:"+pi+"局");
System.exit(0);
}
}

}

}

以上回答參考:
http://..com/question/39899654.html

G. java程序編寫小游戲 要求:程序隨機產生20—50根火柴

為什麼用AWT不用 swing?

演算法思想很簡單
是取勝原理
用反推法:欲拿最後一根,必須留2根在那裡,欲留2根,必須上一輪留2+3+1=6給對方,(它拿一,你拿三,它拿二,你拿二,它拿三,你拿一。都是留2根)。再向上一輪,就是6+4=10。
取勝原理:把隨機產生的火柴數,分解成:2+4的n次方+m,(m≤3),當m=0的時候,後取者勝,當m=1、2、3的時候,先取者勝。先取者取完m,留2+4的n次方給對方,對方不管取多少,你取的數和對方相加等於4,一直到最後,留2根給對方,根據規則,對方只能取一根,留一根給你取勝。

另:取完者勝(含最後一根):最後留4根給對方,不管對方取多少,你都可以一次取完。上一輪同樣加4。
取勝原理:把隨機產生的火柴數,分解成:4的n次方+m,(m≤3),當m=0的時候,後取者勝,當m=1、2、3的時候,先取者勝。先取者取完m,留4的n次方給對方,對方不管取多少,你取的數和對方相加等於4,一直到最後,留4根給對方。

代碼調試可用

自己用GUI搭個界面 二十分鍾的事

import java.util.Scanner;

public class MatchGame {

private static Scanner scanner = new Scanner(System.in);;
private int total;
private Computer com;
private static int exit = 1;

public MatchGame(int from, int to, String level) {
if (from >= to) {
throw new IllegalArgumentException();
}
total = (int)( Math.random() * (to - from)) + from;
com = new Computer(level);
}

public void start() {
System.out.println("0 means endGame, 4 means restartGame!");
System.out.println("The number of matches is " + total);
System.out.println("~Start~");
System.out.println("----------------------------------------");
while (true) {
int u = scanner.nextInt();
if (u > 0 && u < 4) {
System.out.println("You entered " + u);
if (total - u <= 0) {
exit = 2;
endGame();
}
total = total - u;
System.out.println("Total : " + total);
int c = com.play(u, total);
System.out.println("Computer selected " + c + " matches~");
if (total - c <= 0) {
exit = 0;
endGame();
}
total = total - c;
System.out.println("Total : " + total);

}else if (u == 0) {
endGame();
}else if (u > 4 || u < 0) {
System.out.println("You entered Wrong number~");
} else {
restart();
}
}
}

public static void restart() {
MatchGame game;
System.out
.println("Please select Computer Level: 1:HARD 2:NORMAL 3:EASY");
int level = scanner.nextInt();
if (level == 1) {
game = new MatchGame(20, 50, Computer.HARD);
} else if (level == 2) {
game = new MatchGame(20, 50, Computer.NORMAL);
} else {
game = new MatchGame(20, 50, Computer.EASY);
}
game.start();
}

public static void endGame() {
if (exit == 0) {
System.out.println("YOU WIN!!!");
} else if (exit == 2) {
System.out.println("YOU LOSE!!!");
}
System.exit(exit);
}

public static class Computer {
private static String EASY = "EASY";
private static String NORMAL = "NORMAL";
private static String HARD = "HARD";
private static String LEVEL;
private int com;

public Computer(String level) {
LEVEL = level;
}

public int play(int user, int total) {
if (LEVEL.equals(EASY)) {
com = 1;
} else if (LEVEL.equals(NORMAL)) {
com = (int) (Math.random() * 2) + 1;
} else {
int i;
if (total % 4 == 0) {
i = total / 4 - 1;
} else {
i = total / 4;
}
com = total - 4 * i - 1;
if (com == 0) {
com = 4 - user;
}
}
return com;
}
}

public static void main(String[] args) {
MatchGame game;
System.out
.println("Please select Computer Level: 1:HARD 2:NORMAL 3:EASY");
int level = scanner.nextInt();
if (level == 1) {
game = new MatchGame(20, 50, Computer.HARD);
} else if (level == 2) {
game = new MatchGame(20, 50, Computer.NORMAL);
} else {
game = new MatchGame(20, 50, Computer.EASY);
}
game.start();
}
}

熱點內容
什麼配置單反拍視頻最好 發布:2024-05-05 00:30:56 瀏覽:477
sql敏感 發布:2024-05-05 00:28:20 瀏覽:62
android工程師筆試 發布:2024-05-05 00:10:52 瀏覽:948
python調試pycharm 發布:2024-05-05 00:10:51 瀏覽:707
索尼電腦vaio忘了密碼如何恢復出廠設置 發布:2024-05-05 00:09:56 瀏覽:895
安卓系統的用戶管理在哪裡 發布:2024-05-04 23:12:27 瀏覽:430
我的世界伺服器推薦電腦版免費 發布:2024-05-04 23:04:46 瀏覽:395
c程序如何編譯 發布:2024-05-04 22:58:05 瀏覽:932
蘋果手機怎麼查看id密碼 發布:2024-05-04 22:54:49 瀏覽:658
家有三相電如何配置音響設備 發布:2024-05-04 22:53:42 瀏覽:56