首页 诗词 字典 板报 句子 名言 友答 励志 学校 网站地图
当前位置: 首页 > 教程频道 > JAVA > J2SE开发 >

求小游戏代码,该如何处理

2012-12-15 
求小游戏代码我是一个刚接触JAVA的新手,只懂一些简单的语句和语法,看的懂一些小程序,希望哪为好心人给点小

求小游戏代码
   我是一个刚接触JAVA的新手,只懂一些简单的语句和语法,看的懂一些小程序,希望哪为好心人给点小游戏代码可以参考参考。另外如果可以的话给点大型程序的构建的思路。不胜感激。
[解决办法]
网上有好多的,比如:俄罗斯方块、贪吃蛇、坦克大战等等的,你下下来看看
[解决办法]

import java.awt.*;
import java.awt.event.*;
import java.io.*;
import java.net.*;


public class ChatCliend{

public static void main(String[] args){
MyFrame mf=new MyFrame();
mf.launchFrame();

}
}
class MyFrame extends Frame{

TextField text;

TextArea text2;
Socket s;
DataOutputStream dos=null;
DataInputStream dis=null;
boolean on = false;

public void launchFrame(){
this.setLocation( 400, 100);
this.setSize(320, 500);
text=new TextField(10);
text2=new TextArea(100,1);
text.addActionListener(new TextMonitor());
add(text,BorderLayout.SOUTH);
add(text2,BorderLayout.CENTER);
this.addWindowListener(new WindowMonitor());
this.setVisible(true);
net();
new Thread(new Server()).start();
}

//此方法联网
public void net (){

try {
s= new Socket("10.50.140.53",8888);
dos= new DataOutputStream(s.getOutputStream());
dis=new DataInputStream(s.getInputStream());
on=true;
text2.setText("亲,您已经连接到服务器了,可以和您的朋友聊天了  "+'\n');
} catch (UnknownHostException e) {

text2.setText("亲,您没有成功连接到服务器了 ,请与杨圣博联系... ");

} catch (IOException e) {

text2.setText("亲,您没有成功连接到服务器了 ,请与杨圣博联系... ");

}
}

//把内容发送给服务器
public  void sent(String str){

try {
dos.writeUTF(str);
dos.flush();
} catch (IOException e) {

e.printStackTrace();
}
}

//这个类用于监听text的行为,给予一定的响应。然后把内容发送给服务器
public class TextMonitor implements ActionListener{

//本地处理text中的内容 然后调用sent方法  把他发送给服务器
public void actionPerformed(ActionEvent e){

String str=text.getText().trim();
text.setText("");
sent(str);

}
}

//这个类用于窗口的关毕
class WindowMonitor extends WindowAdapter{
public void windowClosing(WindowEvent e){
off();
System.exit(0);
}
}

//用于起一个多线程  
class Server implements Runnable{


public void run(){
try {
while(on){
String s1=dis.readUTF();
text2.setText(text2.getText()+'\n'+s1+'\n');
}
} catch (IOException e) {

}
}
}
public void off(){
try {
if(s!=null)s.close();
if(dis!=null)dis.close();
if(dos!=null)dos.close();
} catch (IOException e) {
e.printStackTrace();
}
}
}

[解决办法]
上面的是客户端
mport java.io.*;
  import java.net.*;
  import java.nio.CharBuffer;
  import java.util.*;


public class ChatServer {
ServerSocket ss=null;
Socket s=null;
boolean on =false;
List<Client> clients=new ArrayList<Client>();

public static void main(String[] args) {


new ChatServer().start();
}

public void start(){
try {
ss=new ServerSocket(8888);
on=true;
}catch(Exception e){
System.out.println("亲,端口已经被占用......");
}
try{
while(on){
s=ss.accept();

Client c=new Client(s);
clients.add(c);
new Thread(c).start();
System.out.println("已经连上了");
}
}catch(IOException e) {

System.out.println(" 亲,不好意思,服务器已经崩溃...");
}
}


class Client implements Runnable{
 
private Socket s=null;
private boolean on1=false;
private DataInputStream dis=null;
private DataOutputStream dos=null;
 
public Client(Socket s){
 
this.s=s;
try {
dis=new DataInputStream(s.getInputStream());
dos=new DataOutputStream(s.getOutputStream());
on1=true;
} catch (IOException e) {
try {
if(s != null) s.close();
if(dis != null)dis.close();
if(dos != null) dos.close();
} catch (IOException e1) {

System.out.print("load......");

}
}
 
public void send(String str){
try {
dos.writeUTF(str);
} catch (IOException e) {
System.out.print("load......");
}

public void run() {

try {
while(on1){
String str=dis.readUTF();
dos.writeUTF(str);
System.out.println(str);
for(int i=0;i<clients.size();i++){
Client c = clients.get(i);
c.send(str);
}

}catch (IOException e) {
System.out.print("load......");



}
}
 
 
}


这是服务器端   ip地址根据自己的该  上面写的是我的ip
[解决办法]
网上搜下,有个马里奥的视频,挺好的,适合新手
[解决办法]
建议你学GUI吧,那个挺好玩的,还简单,可以提高兴趣
[解决办法]
在本站的下载里去搜,应该有。

热点排行