1. 河豚號 > 生活百科 >

java寫游戲腳本(編程一個最簡單游戲代碼)

在 Java 中,如何用Swing實現打字游戲.需要掌握多線程,JFrame的創建方法。多線程的創建方法,一個是繼承Thread類,一個是實現Runnable接口??蚣艿膶崿F,直接創建一個類繼承JFrame.

基本思路是,1.創建一個JFrame 2.啟動十個線程,每個線程攜帶了一個JLabel,用到了隨機函數,每個JLabel的剛出現的時候,他們的y坐標都是一樣的,x坐標是隨機的(隨機范圍在JFrame的寬度內.JLabel顯示的值,是Ascii對應的拉丁字母 .3.線程控制每個JLael的y坐標增加,這樣字母就在往下掉 4.響應鍵盤事件

//載入包

import java.awt.*;

import javax.swing.*;

import java.util.*;

import java.awt.event.*;

import java.applet.*;

// 創建一個線程

class MyThread extends Thread {

int height=0;

// JLabel的x坐標

int intx=0;

// JLabel 的y坐標

int inty=0;

static int com=0;

JLabel lb1;

public MyThread(ThreadGroup g,String name,int width,int fheight){

this.setName(name);

height=fheight;

// 設置JLabel顯示的大寫字母,通過隨機值控制.65是大寫A的Ascii值

lb1=new JLabel(“”+(char)(int)(Math.random()*26+65));

// 設置JLabel的背景顏色,紅,綠,藍組成自然界所有色彩

lb1.setForeground(new Color((int)(Math.random()*250),(int)(Math.random()*250),(int)(Math.random()*250)));

intx=(int)(Math.random()*(width-20));

//線程啟動

start();

}

public void run(){

try{

while(true){

//睡眠50毫秒往下掉

sleep((int)(Math.random()*50));

lb1.setBounds(intx,inty,15,15);

//改變y坐標

inty++;

if(inty>=(height-20)){

lb1.show(false);

MyThread.com++;

break;

}

}

}catch(Exception e){}

}

}

//創建窗口

class TestKey extends JFrame

{

JButton b1,b2;

static int index=0;

Container conent=getContentPane();

static int idd=0;

static int count=0;

int ixx;

static ThreadGroup dg = new ThreadGroup(“group1”);

static ThreadGroup dg1;

static MyThread t1;

Thread list[];

public TestKey() throws ArrayStoreException

{

setSize(400,600);

b1=new JButton(“開始”);

b2=new JButton(“結束”);

conent.setLayout(null);

b1.setBounds(getWidth()/2-100,getHeight()-70,80,30);

b2.setBounds(getWidth()/2+50,getHeight()-70,80,30);

conent.add(b1);

conent.add(b2);

conent.setBackground(Color.blue);

Container cont=getContentPane();

conent.setBackground(Color.white);

show();

//響應按鈕事件

b1.addActionListener(new ActionListener()

{

public void actionPerformed(ActionEvent even)

{

for(int i=0;i<10;i++)

{

t1 =new MyThread(dg,”t”+i,getWidth(),getHeight());

getContentPane().add(t1.lb1);

}

}

});

b2.addActionListener(new ActionListener()

{

public void actionPerformed(ActionEvent even)

{

int num=conent.getComponentCount()-2;

for(int i=2;i

{

conent.remove(2);

conent.repaint();

}

if((float)num/count<=0.02)

JOptionPane.showMessageDialog(null,”共有”+count+”個字母n”+”只有”+num+”個未打掉!n您真是高手,佩服,佩服!”);

else if((float)num/count<=0.15)

JOptionPane.showMessageDialog(null,”共有”+count+”個字母n”+”有”+num+”個未打掉!n水平不錯,繼續努力吧!”);

else if((float)num/count<=0.3)

JOptionPane.showMessageDialog(null,”共有”+count+”個字母n”+”有”+num+”個未打掉!n您很有潛力,不要虛度光陰哦!”);

else if((float)num/count<=0.4)

JOptionPane.showMessageDialog(null,”共有”+count+”個字母n”+”有”+num+”個未打掉!n挺危險的,加把勁啊!”);

else

JOptionPane.showMessageDialog(null,”共有”+count+”個字母n”+”有”+num+”個未打掉!n不蠻你,你真的很差勁,抓緊練習吧!”);

}

});

//監聽窗口事件

addWindowListener(new WindowAdapter()

{

public void windowClosing(WindowEvent event)

{

if(conent.getComponentCount()==2)

JOptionPane.showMessageDialog(null,”您真的要退出嗎?n記得經常來練習啊!”);

else

JOptionPane.showMessageDialog(null,”您確定要退出嗎?n我還沒為您評分的啊!”);

System.exit(0);

}

});

b1.addKeyListener(new KeyAdapter()

{

public void keyTyped(KeyEvent event)

{

list = new Thread[dg.getParent().activeCount()];

int count = dg.getParent().enumerate(list);

for(int i=0;i

{

try

{

MyThread mt=null;

mt = (MyThread)(list[i]);// instanceof MyThread)

if(event.getKeyChar()== mt.lb1.getText().charAt(0))

{

mt.lb1.setVisible(false);

MyThread hh=new MyThread(dg,”t”+i,getWidth(),getHeight());

getContentPane().add(hh.lb1);

}

}catch(Exception e){}

}

}

});

}

public static void main(String args[]){

TestKey mf=new TestKey();

Container cont=mf.getContentPane();

cont.setBackground(Color.white);

cont.setLayout(null);

//創建十個線程,攜帶十個字母往下掉

for(int i=0;i<10;i++)

{

t1 =new MyThread(dg,”t”+i,mf.getWidth(),mf.getHeight());

cont.add(t1.lb1);

}

}

}

 

Java中實現打字游戲,新企學分享

 

大家有不同的實現方式,歡迎在下面留言

本文由網上采集發布,不代表我們立場,轉載聯系作者并注明出處:http://www.seensnowboarding.com/shbk/39367.html

聯系我們

在線咨詢:點擊這里給我發消息

微信號:15705946153

工作日:9:30-18:30,節假日休息

国产精品亚洲w码日韩中文|国产高清露脸孕妇系列|久久国语露脸国产精品|久久久777精品电影网影网|欧美高大丰满freesex