JAVA图像化界面小游戏(石头剪刀布)
玩着没有事情做,在寝室里突然想到了,石头剪刀布的游戏,想了一下他的算法,代码如何实现,先是完成了文字的石头剪刀布,总觉得不好,于是运用学过的java图像知识,设计了,这个图像化界面的剪刀石头布。
在游戏中,石头、剪刀或布分别用不同的数字代表: 0:石头 1:剪刀 2:布,如果是平局的话就会弹出窗口提示为平局。(是不是非常的nice哈哈) ,主要是编程实现电脑和人的出拳比较。
对于图像化,首先是考虑页面的布局,我是选择将各种部件放入一个JPanel容器里面,然后再将JPanel放入JFrame里面。
为了使其更完美一点,我添加了背景音乐,通过一个SoundPool类来实现背景音乐。
完整代码:
背景音乐类:
package Finger_guessing;
import java.applet.Applet;
import java.applet.AudioClip;
import java.net.URL;
/**
* 音乐播放的线程类
*/
public class SoundPool extends Thread{
public static final String BG_MUSIC="game_music.wav";
AudioClip ac;
public SoundPool(String path){
//音乐文件的路径
URL url = this.getClass().getResource("/Finger_guessing/"+path);
ac = Applet.newAudioClip(url);
}
public void play(){
ac.play();
ac.loop();//起到循环音乐的效果
}
@Override
public void run() {
play();
}
}
部件以及运行类:
package Finger_guessing;
import javax.swing.*;
import java.awt.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
public class fingerGuessing {
JFrame jf = new JFrame("杨氏猜拳游戏");
JPanel jp = new JPanel();
//出拳方式
JButton jButton1 = new JButton("石头");
JButton jButton2 = new JButton("剪刀");
JButton jButton3 = new JButton("布");
//玩家标签
JLabel jLabel1 = new JLabel("人类Tom VS 电脑");
JLabel jLabel2 = new JLabel("0");
JLabel jLabel3 = new JLabel(":");
JLabel jLabel4 = new JLabel("0");
JLabel jLabel5 = new JLabel();
JLabel jLabel6 = new JLabel();
//图片路径
Icon stone = new ImageIcon("src\\Finger_guessing\\stone.png");//目录下石头图片的路径
Icon scissors = new ImageIcon("src\\Finger_guessing\\scissors.png");//目录剪刀下图片的路径
Icon cloth = new ImageIcon("src\\Finger_guessing\\cloth.png");//目录下布图片的路径
//输赢的计算次数
int count_tom = 0;
int count_robot = 0;
SoundPool bgMusic;
public void init() {
//开启游戏音乐
bgMusic=new SoundPool(SoundPool.BG_MUSIC);
bgMusic.start();
//设置三个Tom出拳按钮
jButton1.setBounds(0, 50, 100, 50);
jButton2.setBounds(0, 150, 100, 50);
jButton3.setBounds(0, 250, 100, 50);
//设置默认的图片两个出拳为石头
jLabel6.setIcon(stone);
jLabel5.setIcon(stone);
//给按钮添加监听 0:石头 1:剪刀 2:布
jButton1.addActionListener(new ActionListener() {
@Override
public void actionPerformed(ActionEvent e) {
int T_hand = 0;//Tom出拳为石头
jLabel5.setIcon(stone);
robot r = new robot();
int r_hand = r.result();
//比较两个的出拳
compare(r_hand,T_hand);
}
});
jButton2.addActionListener(new ActionListener() {
@Override
public void actionPerformed(ActionEvent e) {
int T_hand = 1;//Tom出拳为剪刀
jLabel5.setIcon(scissors);
robot r = new robot();
int r_hand = r.result();
compare(r_hand,T_hand);
}
});
jButton3.addActionListener(new ActionListener() {
@Override
public void actionPerformed(ActionEvent e) {
int T_hand = 2;//Tom出拳为布
jLabel5.setIcon(cloth);
robot r = new robot();
int r_hand = r.result();
compare(r_hand,T_hand);
}
});
//标签
jLabel1.setFont(new Font("楷体", Font.BOLD, 20));
jLabel1.setForeground(Color.RED);
jLabel2.setFont(new Font("楷体", Font.BOLD, 20));
jLabel3.setFont(new Font("楷体", Font.BOLD, 20));
jLabel4.setFont(new Font("楷体", Font.BOLD, 20));
jLabel1.setBounds(150, 0, 200, 50);
jLabel2.setBounds(180, 20, 200, 50);
jLabel3.setBounds(250, 20, 200, 50);
jLabel4.setBounds(300, 20, 200, 50);
jLabel5.setIcon(stone);//添加图片,icon(图标),
jLabel5.setBounds(150, 150, 60, 60);
jLabel6.setBounds(300, 150, 60, 60);
//将组件放入Panel容器
jp.setLayout(null);
jp.add(jButton1);
jp.add(jButton2);
jp.add(jButton3);
jp.add(jLabel1);
jp.add(jLabel2);
jp.add(jLabel3);
jp.add(jLabel4);
jp.add(jLabel5);
jp.add(jLabel6);
jp.setBackground(Color.orange);//设置窗口的背景色
//将Panel容器放入到窗口中
jf.add(jp);
// Container con = jf.getContentPane();
// con.setBackground(Color.orange);
jf.setSize(400, 400);//设置窗口的大小
jf.setResizable(false);//窗口不能扩大
jf.setLocationRelativeTo(null); // 把窗口位置设置到屏幕中心
jf.setVisible(true);
jf.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);// 设置窗体关闭规则,关闭窗口时关闭程序
}
//比较两个的出拳,本质上是通过出拳的数字不同,来进行判断
public void compare(int hand1, int hand2) {
if (hand1 == hand2) JOptionPane.showMessageDialog(null, "平局!");
else if (Math.abs(hand1 - hand2) == 2) {
if (hand1 == 2) {
count_robot++;
String str2 = Integer.toString(count_robot);
jLabel4.setText(str2);
} else {
count_tom++;
String str1 = Integer.toString(count_tom);
jLabel2.setText(str1);
}
} else if (Math.abs(hand1 - hand2) == 1) {
if (hand1 < hand2) {
count_robot++;
String str2 = Integer.toString(count_robot);
jLabel4.setText(str2);
} else {
count_tom++;
String str1 = Integer.toString(count_tom);
jLabel2.setText(str1);
}
}
}
//人机类
class robot {
int OutHand;
public int result() {
int num = (int) (Math.random() * 3);
switch (num) {
case 0:
OutHand = 0;
break;
case 1:
OutHand = 1;
break;
case 2:
OutHand = 2;
break;
}
//给jLabel6标签设置对应的图片
switch (num) {
case 0:
jLabel6.setIcon(stone);
break;
case 1:
jLabel6.setIcon(scissors);
break;
case 2:
jLabel6.setIcon(cloth);
break;
}
//返回一个代表出拳的数字
return OutHand;
}
}
public static void main (String[]args){
new fingerGuessing().init();
}
}
目录文件展示:
目录展示包括三张图片和两个类以及一个音乐文件,再进行图片以及音乐文件的使用时给出合适的地址,将其访问。
运行代码展示:
运行结果1:
运行结果2:
运行结果3: