⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 贪吃蛇.txt

📁 /* *可以本机单打,本机双打,联机双打 *用户一的键盘控制为:up= key_up down= key_down left = key_left right = key_right * pause
💻 TXT
📖 第 1 页 / 共 3 页
字号:

/* 
*可以本机单打,本机双打,联机双打 
*用户一的键盘控制为:up= key_up down= key_down left = key_left right = key_right 
* pause = key_space reset = key_r 
*用户二的键盘控制为:up = w down = s left = a right = d 
* pause = 1 reset = 2 
*@auther 张恩来 
*@e-mail:zhang_elai@cvicse.com 
*联机双打时,请先建立服务器端,然后建立客户机端 
**/ 




import java.awt.*; 
import java.awt.event.*; 
import javax.swing.*; 
import javax.swing.event.*; 
import java.util.*; 
import java.util.Random; 
import java.lang.*; 
import javax.swing.border.*; 
import java.applet.*; 
import java.net.*; 
import java.io.*; 

public class NetMultiBaby { 
public NetMultiBaby() { 

} 

public static void main(String[] args) throws Exception { 

/*com.incors.plaf.alloy.AlloyLookAndFeel.setProperty("alloy.licenseCode", "yourLicenseCode"); 
try { 
javax.swing.LookAndFeel alloyLnF = new com.incors.plaf.alloy.AlloyLookAndFeel(); 
javax.swing.UIManager.setLookAndFeel(alloyLnF); 
} catch (javax.swing.UnsupportedLookAndFeelException ex) { 
// You may handle the exception here 
}*/ 

BabyFrame frame = new BabyFrame(); 
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); 
frame.show(); 
AudioClip sound_play; 
sound_play = Applet.newAudioClip(new URL("file:" + System.getProperty("user.dir") + "/"+"ycg.mid")); 
sound_play.loop(); 
} 
} 

class AddrConfig extends JFrame { 
public boolean is_serverFlag; 
public JLabel jl_IP,jl_PORT; 
public JTextField jtf_IP,jtf_PORT; 
public JButton jb; 
public JPanel jp_IP,jp_PORT; 
public NetFrame netframe_server = null,netframe_client = null; 

public AddrConfig(boolean is_server) { 
this.is_serverFlag = is_server; 
Container c = this.getContentPane(); 

this.setLocation(10,10); 
if (is_serverFlag) { 
this.setTitle("服务器端地址配置"); 
this.setSize(300,70); 
} 
else { 
this.setTitle("客户端地址配置"); 
this.setSize(300,100); 
} 
jl_IP = new JLabel("IP:"); 
jtf_IP = new JTextField(20); 
jtf_IP.setText("192.168.51.77"); 
jp_IP = new JPanel(); 
jp_IP.add(jl_IP); 
jp_IP.add(jtf_IP); 

jl_PORT = new JLabel("PORT:"); 
jtf_PORT = new JTextField(5); 
jtf_PORT.setText("9999"); 
jp_PORT = new JPanel(); 
jp_PORT.add(jl_PORT); 
jp_PORT.add(jtf_PORT); 

jb = new JButton("Go"); 
jb.addActionListener(new MyActionListener(this)); 
jp_PORT.add(jb); 
if (!is_serverFlag) c.setLayout(new GridLayout(2,1)); 
else c.setLayout(new GridLayout(1,1)); 
if (!is_serverFlag) c.add(jp_IP); 
c.add(jp_PORT); 
this.setResizable(false); 
this.setVisible(true); 
} 

class MyActionListener implements ActionListener { 
public AddrConfig addrconfig; 
public MyActionListener(AddrConfig add) { 
this.addrconfig = add; 
} 
public void actionPerformed(ActionEvent e) { 
if (e.getSource() == addrconfig.jb) { 
if (addrconfig.is_serverFlag) { 
//if (netframe_server == null) { 
netframe_server = new NetFrame(Integer.parseInt(addrconfig.jtf_PORT.getText())); 
netframe_server.show(); 
//} 
//netframe_server.show(); 
} 
else { 
//if (netframe_client == null) { 
netframe_client = new NetFrame(addrconfig.jtf_IP.getText(),Integer.parseInt(addrconfig.jtf_PORT.getText())); 
netframe_client.show(); 
//} 
//netframe_client.show(); 
} 
} 
} 
} 
} 

class BabyFrame extends JFrame implements ActionListener { 

public static final int WIDTH = 300; 
public static final int HEIGHT = 400; 
public JMenuBar menubar; 
public JMenu fileMenu; 
public JMenuItem newSingleGameMenuItem,newLocalMultiGameMenuItem; 
public JMenu newNetMultiGameMenu; 
public JMenuItem newNetMultiGameMenuItem_server,newNetMultiGameMenuItem_client; 
public JMenuItem exitMenuItem; 
public JMenuItem pauseGameMenuItem; 
public JMenu optionMenu; 

public JCheckBoxMenuItem Border_Item; 
public JMenu aboutMenu; 
public JMenuItem mailMeMenuItem; 
public JMenuItem aboutMenuItem; 

//public BabyPanel panel; 
//public BabyDialog panel_Dialog; 
public AboutDialog about_Dialog; 
public JButton jb_server,jb_client; 
public JPanel jp; 
public AddrConfig addrconfig_server = null,addrconfig_client = null; 
public MultiFrame multiframe = null; 
public SingleFrame singleframe = null; 

public BabyFrame() { 
Container contentPane = getContentPane(); 
//panel_Dialog = new BabyDialog(panel); 
//about_Dialog = new AboutDialog(panel); 

setTitle("贪食蛇sss"); 
setSize(WIDTH, HEIGHT); 
setResizable(false); 
Dimension screenSize = Toolkit.getDefaultToolkit().getScreenSize(); 
setLocation((int)(screenSize.getWidth()/2 - WIDTH/2),(int)(screenSize.getHeight()/2 - HEIGHT/2)); 
menubar = new JMenuBar(); 
fileMenu = new JMenu("File"); 
fileMenu.setMnemonic(′F′); 

newSingleGameMenuItem = new JMenuItem("New SingleGame...",′S′); 
newSingleGameMenuItem.addActionListener(this); 
newLocalMultiGameMenuItem = new JMenuItem("new Multi-Local-Game...",′M′); 
newLocalMultiGameMenuItem.addActionListener(this); 

newNetMultiGameMenu = new JMenu("Net MultiGame"); 
newNetMultiGameMenuItem_server = new JMenuItem("As Server...",′S′); 
newNetMultiGameMenuItem_server.addActionListener(this); 
newNetMultiGameMenuItem_client = new JMenuItem("As Client...",′C′); 
newNetMultiGameMenuItem_client.addActionListener(this); 

newNetMultiGameMenu.add(newNetMultiGameMenuItem_server); 
newNetMultiGameMenu.add(newNetMultiGameMenuItem_client); 

fileMenu.add(newSingleGameMenuItem); 
fileMenu.add(newLocalMultiGameMenuItem); 
fileMenu.add(newNetMultiGameMenu); 


pauseGameMenuItem = new JMenuItem("Pause",′P′); 
pauseGameMenuItem.addActionListener( new ActionListener() { 
public void actionPerformed(ActionEvent event) { 
//panel.runFlag = !panel.runFlag; 
} 
}); 

exitMenuItem = new JMenuItem("Exit",′X′); 
exitMenuItem.addActionListener( new ActionListener() { 
public void actionPerformed(ActionEvent event) { 
System.exit(0); 
} 
}); 
fileMenu.add(pauseGameMenuItem); 
fileMenu.add(exitMenuItem); 

optionMenu = new JMenu("Option"); 
optionMenu.setMnemonic(′O′); 

Border_Item = new JCheckBoxMenuItem("Snake-Border"); 
Border_Item.addActionListener(this); 

optionMenu.add(Border_Item); 

aboutMenu = new JMenu("About"); 
aboutMenu.setMnemonic(′A′); 
aboutMenuItem = new JMenuItem("About",′A′); 
aboutMenuItem.addActionListener(this); 

aboutMenu.add(aboutMenuItem); 
menubar.add(fileMenu); 
menubar.add(optionMenu); 
menubar.add(aboutMenu); 
this.setJMenuBar(menubar); 

} 

public void actionPerformed(ActionEvent ae) { 
if (ae.getSource() == newNetMultiGameMenuItem_server) 
{ 
if (addrconfig_server == null) { 
addrconfig_server = new AddrConfig(true); 
} 
addrconfig_server.show(); 
//this.setVisible(false); 
} 
else 
if (ae.getSource() == newNetMultiGameMenuItem_client) { 
if (addrconfig_client == null) { 
addrconfig_client = new AddrConfig(false); 
} 
addrconfig_client.show(); 
} 

else 
if (ae.getSource() == newLocalMultiGameMenuItem) { 
if (multiframe == null) { 
multiframe = new MultiFrame(); 
} 
multiframe.show(); 
} 
else 
if (ae.getSource() == newSingleGameMenuItem) { 
if (singleframe == null) { 
singleframe = new SingleFrame(); 
singleframe.show(); 
} 
singleframe.show(); 
} 
} 



} 



class NetFrame extends JFrame { 
public static final int WIDTH = 700; 
public static final int HEIGHT = 400; 
public BabyFrame babyframe; 
public boolean is_serverFlag; 
public NetMultiBabyPanel panel; 
public String IP; 
public int PORT; 

public JMenuBar menubar; 
public JMenu fileMenu; 
public JMenuItem newGameMenuItem; 
public JMenuItem exitMenuItem; 
public JMenuItem pauseGameMenuItem; 

public NetFrame(String ip,int port) { 
this.is_serverFlag = false; 
this.IP = ip; 
this.PORT = port; 
//this.babyframe = babyframe1; 
//this.babyframe = babyframe; 
//Container c = this.getContentPane(); 
panel = new NetMultiBabyPanel(IP,PORT); 

this.setSize(700,400); 
this.setTitle("贪食蛇======网络版客户端"); 
this.setResizable(false); 
panel.thread.start(); 
Dimension screenSize = Toolkit.getDefaultToolkit().getScreenSize(); 
setLocation((int)(screenSize.getWidth()/2 - WIDTH/2),(int)(screenSize.getHeight()/2 - HEIGHT/2)); 

menubar = new JMenuBar(); 

fileMenu = new JMenu("File"); 
fileMenu.setMnemonic(′F′); 

newGameMenuItem = new JMenuItem("New",′N′); 
newGameMenuItem.addActionListener(new MyActionListener(this)); 
pauseGameMenuItem = new JMenuItem("Pause",′P′); 
pauseGameMenuItem.addActionListener(new MyActionListener(this)); 

exitMenuItem = new JMenuItem("Exit",′X′); 
exitMenuItem.addActionListener(new MyActionListener(this)); 


fileMenu.add(newGameMenuItem); 
fileMenu.add(pauseGameMenuItem); 
fileMenu.add(exitMenuItem); 
menubar.add(fileMenu); 
setJMenuBar(menubar); 
Container c = this.getContentPane(); 
c.add(panel); 

this.addWindowListener(new WindowAdapter(){ 
//close window event 
public void windowClosing(WindowEvent event) 
{ 
//babyframe. 
setVisible(false); 
try{ 
panel.thread.stop(); 
panel.socket.close(); 
}catch (IOException e){} 
}//end of window event 
} 
); 
} 

//////////////////////////////////////////////////////////// 
public NetFrame(int port) { 
this.is_serverFlag = true; 
this.PORT = port; 
panel = new NetMultiBabyPanel(PORT); 
this.setSize(700,400); 
this.setTitle("贪食蛇======网络版服务器端"); 
this.setResizable(false); 

panel.thread.start(); 
Dimension screenSize = Toolkit.getDefaultToolkit().getScreenSize(); 
setLocation((int)(screenSize.getWidth()/2 - WIDTH/2),(int)(screenSize.getHeight()/2 - HEIGHT/2)); 

menubar = new JMenuBar(); 

fileMenu = new JMenu("File"); 
fileMenu.setMnemonic(′F′); 

newGameMenuItem = new JMenuItem("New",′N′); 
newGameMenuItem.addActionListener(new MyActionListener(this)); 
pauseGameMenuItem = new JMenuItem("Pause",′P′); 
pauseGameMenuItem.addActionListener(new MyActionListener(this)); 

exitMenuItem = new JMenuItem("Exit",′X′); 
exitMenuItem.addActionListener(new MyActionListener(this)); 


fileMenu.add(newGameMenuItem); 
fileMenu.add(pauseGameMenuItem); 
fileMenu.add(exitMenuItem); 
menubar.add(fileMenu); 
setJMenuBar(menubar); 
Container c = this.getContentPane(); 
c.add(panel); 

this.addWindowListener(new WindowAdapter(){ 
//close window event 
public void windowClosing(WindowEvent event) 
{ 
//babyframe. 

//babyframe.setVisible(true); 
setVisible(false); 
try{ 
panel.thread.stop(); 
panel.serversocket.close(); 
//panel.socket_on_server.close(); 
if (!(panel.socket_on_server == null)) 
panel.socket_on_server.close(); 

}catch (IOException e){} 
} 



}//end of window event 

); 
} 


private class MyActionListener implements ActionListener { 
public NetFrame netframe; 
public MyActionListener(NetFrame nf) { 
this.netframe = nf; 
} 
public void actionPerformed(ActionEvent event) { 
if (event.getSource() == newGameMenuItem) { 
if (netframe.panel.is_serverFlag) { 
netframe.panel.user1.newGame(); 
netframe.panel.out.println(KeyEvent.VK_R); 
} 
else { 
netframe.panel.user2.newGame(); 
netframe.panel.out.println(KeyEvent.VK_2); 
} 
} 
else 
if (event.getSource() == pauseGameMenuItem) { 
if (netframe.panel.is_serverFlag) { 
netframe.panel.user1.runFlag = !netframe.panel.user1.runFlag; 
netframe.panel.out.println(KeyEvent.VK_SPACE); 
} 
else { 
netframe.panel.user2.runFlag = !netframe.panel.user2.runFlag; 
netframe.panel.out.println(KeyEvent.VK_1); 
} 
} 
else 
if (event.getSource() == exitMenuItem) { 
netframe.dispose(); 
} 
} 
} 
} 



class SingleFrame extends JFrame { 
MultiBabyPanel multibabypanel; 
public static final int WIDTH = 400; 
public static final int HEIGHT = 400; 
public JMenuBar menubar; 
public JMenu fileMenu; 
public JMenuItem newGameMenuItem; 
public JMenuItem exitMenuItem; 
public JMenuItem pauseGameMenuItem; 

public SingleFrame() { 

Container c = this.getContentPane(); 
multibabypanel = new MultiBabyPanel(1); 

//babyframe.setVisible(false); 
//this.setVisible(true); 
this.setSize(330,400); 
this.setTitle("贪食蛇=======本机单打"); 
this.setResizable(false); 
Dimension screenSize = Toolkit.getDefaultToolkit().getScreenSize(); 
setLocation((int)(screenSize.getWidth()/2 - WIDTH/2),(int)(screenSize.getHeight()/2 - HEIGHT/2)); 

menubar = new JMenuBar(); 

fileMenu = new JMenu("File"); 
fileMenu.setMnemonic(′F′); 

newGameMenuItem = new JMenuItem("New",′N′); 
newGameMenuItem.addActionListener(new MyActionListener(this)); 
pauseGameMenuItem = new JMenuItem("Pause",′P′); 
pauseGameMenuItem.addActionListener(new MyActionListener(this)); 

exitMenuItem = new JMenuItem("Exit",′X′); 
exitMenuItem.addActionListener(new MyActionListener(this)); 


fileMenu.add(newGameMenuItem); 
fileMenu.add(pauseGameMenuItem); 
fileMenu.add(exitMenuItem); 
menubar.add(fileMenu); 
setJMenuBar(menubar); 
c.add(multibabypanel); 
} 
private class MyActionListener implements ActionListener { 

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -