各種經(jīng)典java小游戲源代碼(java 游戲源代碼)
今天給各位分享各種經(jīng)典java小游戲源代碼的知識(shí),其中也會(huì)對(duì)java 游戲源代碼進(jìn)行解釋,如果能碰巧解決你現(xiàn)在面臨的問(wèn)題,別忘了關(guān)注本站,現(xiàn)在開(kāi)始吧!
本文目錄一覽:
- 1、求java小游戲源代碼
- 2、求一個(gè)簡(jiǎn)單的JAVA游戲代碼,100行左右,謝謝!
- 3、求"貪吃蛇"小游戲JAVA源代碼一份
- 4、java 小游戲
- 5、求Java課程設(shè)計(jì)—小游戲(含源代碼)
- 6、求一個(gè)簡(jiǎn)單又有趣的JAVA小游戲代碼
求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 當(dāng)前進(jìn)行的個(gè)數(shù), rapidity 游標(biāo)的位置
int zhengque = 0, cuowu = 0;
int rush[] = { 10 ,20 ,30 }; //游戲每關(guān)的個(gè)數(shù) 可以自由添加.列 { 10 ,20 ,30 ,40,50}
int rush_count = 0; //記錄關(guān)數(shù)
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' }; //隨機(jī)出現(xiàn)的數(shù)字 可以自由添加
Vector number = new Vector();
String paiduan = "true";
AudioClip Musci_anjian, Music_shibai, Music_chenggong;
public Frame() {
try {
setDefaultCloseOperation(EXIT_ON_CLOSE);
//-----------------聲音文件---------------------
Musci_anjian = Applet.newAudioClip(new File("sounds//anjian.wav")
.toURL());
Music_shibai = Applet.newAudioClip(new File("sounds//shibai.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("開(kāi)始");
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("結(jié)束");
jButton2.addActionListener(new Frame1_jButton2_actionAdapter(this));
jLabel2.setText("第一關(guān):100個(gè)");
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;
paiduan = "true";
while (count = rush[rush_count]) {
try {
Thread t = new Thread(new Tthread());
t.start();
count += 1;
Thread.sleep(1000 + (int) (Math.random() * 2000)); // 生產(chǎn)下組停頓時(shí)間
// 最快1快.最慢2秒
} catch (InterruptedException e) {
e.printStackTrace();
}
}
while (true) { // 等待最后一個(gè)字符消失
if (number.size() == 0) {
break;
}
}
if (zhengque == 0) { // 為了以后相除..如果全部正確或者錯(cuò)誤就會(huì)出現(xiàn)錯(cuò)誤. 所以..
zhengque = 1;
}
if (cuowu == 0) {
cuowu = 1;
}
if (paiduan.equals("true")) { // 判斷是否是自然結(jié)束
if (zhengque / cuowu = 2) {
JOptionPane.showMessageDialog(null, "恭喜你過(guò)關(guān)了");
rush_count += 1; // 自動(dòng)加1關(guān)
if (rush_count rush.length) {
if (rapidity 10) { // 當(dāng)速度大于10的時(shí)候在-5提加速度.怕速度太快
rapidity -= 5; // 速度自動(dòng)減10毫秒
jSlider1.setValue(rapidity); // 選擇位置
}
Thread t = new Thread(this);
t.start();
} else {
JOptionPane.showMessageDialog(null, "牛B...你通關(guān)了..");
rush_count = 0;
count = 0;
}
} else {
JOptionPane.showMessageDialog(null, "請(qǐng)?jiān)俳釉賱?lì)");
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;
paiduan = "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) {
// ---------------------數(shù)字下移--------------------
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 + "個(gè),錯(cuò)誤:" + cuowu
+ "個(gè)");
number.removeElementAt(i);
Music_shibai.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 + "個(gè),錯(cuò)誤:" + cuowu + "個(gè)");
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;
}
}
求一個(gè)簡(jiǎn)單的JAVA游戲代碼,100行左右,謝謝!
import java.awt.Dimension;
import java.awt.Graphics;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JPanel;
public class Painter extends JFrame{
/**
*
*/
private static final long serialVersionUID = 8160427604782702376L;
CanvasPanel canvas = new CanvasPanel()
public Painter() {
super("Star");
this.add(canvas);
this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
this.pack();
this.setResizable(false);
this.setLocationRelativeTo(null);
this.setVisible(true);
}
public static void main(String[] args) {
new Painter();
}
}
class CanvasPanel extends JPanel implements ActionListener{
/**
*
*/
private static final long serialVersionUID = -4642528854538741028L;
private JButton[] btn = new JButton[4];
private String[] btn_name = {"+", "-", "R", "L"};
private int center_x = 200, center_y = 200, radius = 100, degree = 0;
public CanvasPanel() {
this.setPreferredSize(new Dimension(400, 500));
this.setLayout(null);
for(int i = 0; i 4; i++) {
btn[i] = new JButton(btn_name[i]);
btn[i].setBounds(160 + i * 60, 425, 50, 50);
btn[i].addActionListener(this);
this.add(btn[i]);
}
}
@Override
public void paintComponent(Graphics g) {
super.paintComponent(g);
for(int i = 0; i 5; i++) {
g.drawLine( (int) (center_x + radius * Math.sin(Math.toRadians(degree + 72 * i))),
(int) (center_y - radius * Math.cos(Math.toRadians(degree + 72 * i))),
(int) (center_x + radius * Math.sin(Math.toRadians(degree + 72 * i + 144))),
(int) (center_y - radius * Math.cos(Math.toRadians(degree + 72 * i + 144))));
}
}
public void actionPerformed(ActionEvent e) {
// TODO Auto-generated method stub
if(e.getActionCommand() == "+") {
if(radius 200)
radius += 2;
repaint();
} else if(e.getActionCommand() == "-") {
if(radius 0)
radius -= 2;
repaint();
} else if(e.getActionCommand() == "R") {
degree = (degree + 2) % 360;
repaint();
} else if(e.getActionCommand() == "L") {
degree = (degree - 2) % 360;
repaint();
}
}
}
求"貪吃蛇"小游戲JAVA源代碼一份
貪吃蛇
import java.awt.*;
import java.awt.event.*;
public class GreedSnake //主類
{
/**
* @param args
*/
public static void main(String[] args) {
// TODO Auto-generated method stub
new MyWindow();
}
}
class MyPanel extends Panel implements KeyListener,Runnable//自定義面板類,繼承了鍵盤(pán)和線程接口
{
Button snake[]; //定義蛇按鈕
int shu=0; //蛇的節(jié)數(shù)
int food[]; //食物數(shù)組
boolean result=true; //判定結(jié)果是輸 還是贏
Thread thread; //定義線程
static int weix,weiy; //食物位置
boolean t=true; //判定游戲是否結(jié)束
int fangxiang=0; //蛇移動(dòng)方向
int x=0,y=0; //蛇頭位置
MyPanel()
{
setLayout(null);
snake=new Button[20];
food=new int [20];
thread=new Thread(this);
for(int j=0;j20;j++)
{
food[j]=(int)(Math.random()*99);//定義20個(gè)隨機(jī)食物
}
weix=(int)(food[0]*0.1)*60; //十位*60為橫坐標(biāo)
weiy=(int)(food[0]%10)*40; //個(gè)位*40為縱坐標(biāo)
for(int i=0;i20;i++)
{
snake[i]=new Button();
}
add(snake[0]);
snake[0].setBackground(Color.black);
snake[0].addKeyListener(this); //為蛇頭添加鍵盤(pán)監(jiān)視器
snake[0].setBounds(0,0,10,10);
setBackground(Color.cyan);
}
public void run() //接收線程
{
while(t)
{
if(fangxiang==0)//向右
{
try
{
x+=10;
snake[0].setLocation(x, y);//設(shè)置蛇頭位置
if(x==weixy==weiy) //吃到食物
{
shu++;
weix=(int)(food[shu]*0.1)*60;
weiy=(int)(food[shu]%10)*40;
repaint(); //重繪下一個(gè)食物
add(snake[shu]); //增加蛇節(jié)數(shù)和位置
snake[shu].setBounds(snake[shu-1].getBounds());
}
thread.sleep(100); //睡眠100ms
}
catch(Exception e){}
}
else if(fangxiang==1)//向左
{
try
{
x-=10;
snake[0].setLocation(x, y);
if(x==weixy==weiy)
{
shu++;
weix=(int)(food[shu]*0.1)*60;
weiy=(int)(food[shu]%10)*40;
repaint();
add(snake[shu]);
snake[shu].setBounds(snake[shu-1].getBounds());
}
thread.sleep(100);
}
catch(Exception e){}
}
else if(fangxiang==2)//向上
{
try
{
y-=10;
snake[0].setLocation(x, y);
if(x==weixy==weiy)
{
shu++;
weix=(int)(food[shu]*0.1)*60;
weiy=(int)(food[shu]%10)*40;
repaint();
add(snake[shu]);
snake[shu].setBounds(snake[shu-1].getBounds());
}
thread.sleep(100);
}
catch(Exception e){}
}
else if(fangxiang==3)//向下
{
try
{
y+=10;
snake[0].setLocation(x, y);
if(x==weixy==weiy)
{
shu++;
weix=(int)(food[shu]*0.1)*60;
weiy=(int)(food[shu]%10)*40;
repaint();
add(snake[shu]);
snake[shu].setBounds(snake[shu-1].getBounds());
}
thread.sleep(100);
}
catch(Exception e){}
}
int num1=shu;
while(num11)//判斷是否咬自己的尾巴
{
if(snake[num1].getBounds().x==snake[0].getBounds().xsnake[num1].getBounds().y==snake[0].getBounds().y)
{
t=false;
result=false;
repaint();
}
num1--;
}
if(x0||x=this.getWidth()||y0||y=this.getHeight())//判斷是否撞墻
{
t=false;
result=false;
repaint();
}
int num=shu;
while(num0) //設(shè)置蛇節(jié)位置
{
snake[num].setBounds(snake[num-1].getBounds());
num--;
}
if(shu==15) //如果蛇節(jié)數(shù)等于15則勝利
{
t=false;
result=true;
repaint();
}
}
}
public void keyPressed(KeyEvent e) //按下鍵盤(pán)方向鍵
{
if(e.getKeyCode()==KeyEvent.VK_RIGHT)//右鍵
{
if(fangxiang!=1)//如果先前方向不為左
fangxiang=0;
}
else if(e.getKeyCode()==KeyEvent.VK_LEFT)
{ if(fangxiang!=0)
fangxiang=1;
}
else if(e.getKeyCode()==KeyEvent.VK_UP)
{ if(fangxiang!=3)
fangxiang=2;
}
else if(e.getKeyCode()==KeyEvent.VK_DOWN)
{ if(fangxiang!=2)
fangxiang=3;
}
}
public void keyTyped(KeyEvent e)
{
}
public void keyReleased(KeyEvent e)
{
}
public void paint(Graphics g) //在面板上繪圖
{
int x1=this.getWidth()-1;
int y1=this.getHeight()-1;
g.setColor(Color.red);
g.fillOval(weix, weiy, 10, 10);//食物
g.drawRect(0, 0, x1, y1); //墻
if(t==falseresult==false)
g.drawString("GAME OVER!", 250, 200);//輸出游戲失敗
else if(t==falseresult==true)
g.drawString("YOU WIN!", 250, 200);//輸出游戲成功
}
}
class MyWindow extends Frame implements ActionListener//自定義窗口類
{
MyPanel my;
Button btn;
Panel panel;
MyWindow()
{
super("GreedSnake");
my=new MyPanel();
btn=new Button("begin");
panel=new Panel();
btn.addActionListener(this);
panel.add(new Label("begin后請(qǐng)按Tab鍵選定蛇"));
panel.add(btn);
panel.add(new Label("按上下左右鍵控制蛇行動(dòng)"));
add(panel,BorderLayout.NORTH);
add(my,BorderLayout.CENTER);
setBounds(100,100,610,500);
setVisible(true);
validate();
addWindowListener(new WindowAdapter()
{
public void windowClosing(WindowEvent e)
{
System.exit(0);
}
});
}
public void actionPerformed(ActionEvent e)//按下begin按鈕
{
if(e.getSource()==btn)
{
try
{
my.thread.start(); //開(kāi)始線程
my.validate();
}
catch(Exception ee){}
}
}
}
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();//調(diào)用initial方法,就是下面定義的那個(gè).該方法主要是初始界面.
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(); // 生成隨機(jī)數(shù)
choice = new JComboBox();//初始化choice這個(gè)下拉框.也就是你選擇出剪子還是石頭什么的那個(gè)下拉框..
for (int i = 0; i box.length; i++) {//為那個(gè)下拉框賦值.用前面定義的private String[] box = { "剪刀", "石頭", "布" };附值.這樣下拉框就有三個(gè)選項(xiàng)了..
choice.addItem(box[i]);
}
ta = new JTextArea(3, 15);//初始化那個(gè)文本域3行15列
ta.setEditable(false);//讓用戶不能編輯那個(gè)文本域即不能在里面寫(xiě)東西
JButton okBut = new JButton("出招");//新建一個(gè)出招的按鈕
okBut.addActionListener(new ActionListener() {//給出招按鈕加個(gè)監(jiān)聽(tīng).意思就是監(jiān)聽(tīng)著這個(gè)按鈕看用戶有沒(méi)有點(diǎn)擊它..如果點(diǎn)擊就執(zhí)行下面這個(gè)方法
public void actionPerformed(ActionEvent e) {//就是這個(gè)方法
ta.setText(getResult());//給那個(gè)文本域賦值..就是顯示結(jié)果 賦值的是通過(guò)getResult()這個(gè)方法得到的返回值 getResult()這個(gè)方法下面會(huì)講
lb.setText(getTotal());//給分?jǐn)?shù)那個(gè)LABEL賦值..就是顯示分?jǐn)?shù)..賦值的是通過(guò)getTotal()這個(gè)方法得到的返回值
}
});
JButton clearBut = new JButton("清除分?jǐn)?shù)");//新建一個(gè)清楚分?jǐn)?shù)的按鈕
clearBut.addActionListener(new ActionListener() {//同上給他加個(gè)監(jiān)聽(tīng)
public void actionPerformed(ActionEvent e) {//如果用戶點(diǎn)擊了就執(zhí)行這個(gè)方法
ta.setText("");//給文本域賦值為""..其實(shí)就是清楚他的內(nèi)容
win = 0;//win賦值為0
loss = 0;//同上
equal = 0;//同上
lb.setText(getTotal());//給顯示分?jǐn)?shù)那個(gè)賦值..因?yàn)榍懊嬉呀?jīng)都賦值為0了..所以這句就是讓顯示分?jǐn)?shù)那都為0
}
});
lb = new JLabel(getTotal());//初始化那個(gè)顯示分?jǐn)?shù)的東西
JPanel choicePanel = new JPanel();//定義一個(gè)面板..面板就相當(dāng)于一個(gè)畫(huà)圖用的東西..可以在上面加按鈕啊文本域什么的..
choicePanel.add(choice);//把下拉框加到面板里
choicePanel.add(okBut);//把出招按鈕加到面板里
choicePanel.add(clearBut);//把清楚分?jǐn)?shù)按鈕加到面板里
JScrollPane resultPanel = new JScrollPane(ta);//把文本域加到一個(gè)可滾動(dòng)的面板里面..JScrollPane就是可滾動(dòng)的面板..這樣如果那個(gè)文本域內(nèi)容太多就會(huì)出現(xiàn)滾動(dòng)條..而不是變大
JPanel totalPanel = new JPanel();//再定義個(gè)面板..用來(lái)顯示分?jǐn)?shù)的..
totalPanel.add(lb);//把那個(gè)顯示分?jǐn)?shù)的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() {//獲得結(jié)果的方法 返回值是一個(gè)String..這個(gè)返回值會(huì)用來(lái)顯示在文本域里面
String tmp = "";
int boxPeop = choice.getSelectedIndex();//獲得你選擇的那個(gè)的索引..從0開(kāi)始的..
int boxComp = getBoxComp();//獲得電腦出的索引..就是隨機(jī)的0-2的數(shù)
tmp += "你出:\t" + box[boxPeop];//下面你應(yīng)該明白了..
tmp += "\n電腦出:\t" + box[boxComp];
tmp += "\n結(jié)果:\t" + check(boxPeop, boxComp);
return tmp;
}
public int getBoxComp() {//就是產(chǎn)生一個(gè)0-2的隨機(jī)數(shù)..
return r.nextInt(3);//Random的nextInt(int i)方法就是產(chǎn)生一個(gè)[0-i)的隨機(jī)整數(shù) 所以nextInt(3)就是[0-2]的隨機(jī)數(shù)
}
public String check(int boxPeop, int boxComp) {//這個(gè)就是判斷你選擇的和電腦選擇的比較結(jié)果..是輸是贏還是平..boxPeop就是你選擇的..boxComp就是電腦選擇的..
String result = "";
if (boxPeop == (boxComp + 1) % 3) {//(boxComp + 1) % 3 電腦選擇的加上1加除以3取余..也就是如果電腦選0這個(gè)就為1..這個(gè)判斷的意思就是如果電腦選0并且你選1..那么就是電腦選了
//private String[] box = { "剪刀", "石頭", "布" };這里面下標(biāo)為0的..你選了下標(biāo)為1的..就是電腦選剪刀你選石頭..所以你贏了..如果電腦選1..(boxComp + 1) % 3就為2..意思就是
//電腦選了石頭你選了布..所以你贏了..另外一種情況你明白了撒..只有三種情況你贏..所以這里都包含了..也只包含了那三種..
result = "你贏了!";
win++;//贏了就讓記你贏的次數(shù)的那個(gè)變量加1
} else if (boxPeop == boxComp) {//相等當(dāng)然平手了
result = "平";
equal++;//同上了
} else {//除了贏和平當(dāng)然就是輸了..
result = "你輸了!";
loss++;//同上
}
return result;
}
public int getPoint() {
return (win - loss) * 10;
}
public String getTotal() {
return "贏:" + win + " 平:" + equal + " 輸:" + loss + " 得分:"
+ getPoint();
}
}
希望你能明白哈。。
求Java課程設(shè)計(jì)—小游戲(含源代碼)
Tank——坦克大戰(zhàn)(簡(jiǎn)潔版)源代碼-------(此文檔是自己在韓順平教程總結(jié)而來(lái))
*????功能:?1.防止敵人的坦克重疊運(yùn)動(dòng)
*?????????????(決定把判斷是否碰撞的函數(shù)寫(xiě)到EnemyTank類)
*??????????2.可以分關(guān)
*??????????????2.1?(做一個(gè)開(kāi)始的Panel,它是一個(gè)空的)
*??????????????2.2?開(kāi)始字體閃爍
*??????????3.可以在玩游戲的時(shí)候,暫停和繼續(xù)
*??????????????3.1當(dāng)用戶點(diǎn)擊暫停時(shí),子彈的速度和坦克速度設(shè)為0,并讓坦克的方向
*?????????????????不要發(fā)生變化。
*??????????4.可以記錄玩家的成績(jī)
*??????????????4.1用文件流的方式(小游戲)[大游戲是用的數(shù)據(jù)庫(kù)cs,bs結(jié)構(gòu),三國(guó)]
*??????????????4.2單寫(xiě)一個(gè)記錄類,完成對(duì)玩家的記錄
*??????????????4.3先完成保存共擊毀了多少輛敵人坦克的功能
*??????????????4.4存盤(pán)退出游戲,可以記錄當(dāng)時(shí)的敵人的坦克坐標(biāo),并可以恢復(fù)
*??????????5.java如何操作聲音文件
*/
求一個(gè)簡(jiǎn)單又有趣的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];//游戲按鈕數(shù)組
JButton exitButton,resetButton,newlyButton; //退出,重列,重新開(kāi)始按鈕
JLabel fractionLable=new JLabel("0"); //分?jǐn)?shù)標(biāo)簽
JButton firstButton,secondButton; //
分別記錄兩次62616964757a686964616fe59b9ee7ad9431333335326239被選中的按鈕
int grid[][] = new int[8][7];//儲(chǔ)存游戲按鈕位置
static boolean pressInformation=false; //判斷是否有按鈕被選中
int x0=0,y0=0,x=0,y=0,fristMsg=0,secondMsg=0,validateLV; //游戲按鈕的位置坐標(biāo)
int i,j,k,n;//消除方法控制
代碼(code)是程序員用開(kāi)發(fā)工具所支持的語(yǔ)言寫(xiě)出來(lái)的源文件,是一組由字符、符號(hào)或信號(hào)碼元以離散形式表示信息的明確的規(guī)則體系。
對(duì)于字符和Unicode數(shù)據(jù)的位模式的定義,此模式代表特定字母、數(shù)字或符號(hào)(例如 0x20 代表一個(gè)空格,而 0x74 代表字符“t”)。一些數(shù)據(jù)類型每個(gè)字符使用一個(gè)字節(jié);每個(gè)字節(jié)可以具有 256 個(gè)不同的位模式中的一個(gè)模式。
在計(jì)算機(jī)中,字符由不同的位模式(ON 或 OFF)表示。每個(gè)字節(jié)有 8 位,這 8 位可以有 256 種不同的 ON 和 OFF 組合模式。對(duì)于使用 1 個(gè)字節(jié)存儲(chǔ)每個(gè)字符的程序,通過(guò)給每個(gè)位模式指派字符可表示最多 256 個(gè)不同的字符。2 個(gè)字節(jié)有 16 位,這 16 位可以有 65,536 種唯一的 ON 和 OFF 組合模式。使用 2 個(gè)字節(jié)表示每個(gè)字符的程序可表示最多 65,536 個(gè)字符。
單字節(jié)代碼頁(yè)是字符定義,這些字符映射到每個(gè)字節(jié)可能有的 256 種位模式中的每一種。代碼頁(yè)定義大小寫(xiě)字符、數(shù)字、符號(hào)以及 !、@、#、% 等特殊字符的位模式。每種歐洲語(yǔ)言(如德語(yǔ)和西班牙語(yǔ))都有各自的單字節(jié)代碼頁(yè)。
雖然用于表示 A 到 Z 拉丁字母表字符的位模式在所有的代碼頁(yè)中都相同,但用于表示重音字符(如"é"和"á")的位模式在不同的代碼頁(yè)中卻不同。如果在運(yùn)行不同代碼頁(yè)的計(jì)算機(jī)間交換數(shù)據(jù),必須將所有字符數(shù)據(jù)由發(fā)送計(jì)算機(jī)的代碼頁(yè)轉(zhuǎn)換為接收計(jì)算機(jī)的代碼頁(yè)。如果源數(shù)據(jù)中的擴(kuò)展字符在接收計(jì)算機(jī)的代碼頁(yè)中未定義,那么數(shù)據(jù)將丟失。
如果某個(gè)數(shù)據(jù)庫(kù)為來(lái)自許多不同國(guó)家的客戶端提供服務(wù),則很難為該數(shù)據(jù)庫(kù)選擇這樣一種代碼頁(yè),使其包括所有客戶端計(jì)算機(jī)所需的全部擴(kuò)展字符。而且,在代碼頁(yè)間不停地轉(zhuǎn)換需要花費(fèi)大量的處理時(shí)間。
關(guān)于各種經(jīng)典java小游戲源代碼和java 游戲源代碼的介紹到此就結(jié)束了,不知道你從中找到你需要的信息了嗎 ?如果你還想了解更多這方面的信息,記得收藏關(guān)注本站。
掃描二維碼推送至手機(jī)訪問(wèn)。
版權(quán)聲明:本文由飛速云SEO網(wǎng)絡(luò)優(yōu)化推廣發(fā)布,如需轉(zhuǎn)載請(qǐng)注明出處。