所以在制作这个小游戏的时候,我首先将它重复两次以获得服务器设置,另一次从接口类中取出常量并将它们放入客户端,我认为这是正常的。 我的主要问题是当两个客户端都启动时,播放器1(当前为atm实现)尝试点击“Spam to Win!”代码不会触发。因此所有按钮都不起作用。为什么他们不会触发。我已正确调用actionListener,甚至尝试使按钮静态和非静态。
这是未完成的服务器,但是当前将2个人连接到1个服务器的工作。
import java.io.*;
import java.net.*;
import javax.swing.*;
import java.awt.*;
import java.util.Date;
import java.util.Scanner;
public class Server extends JFrame
{
public static Socket player1;
public static Scanner player1Input;
public static PrintWriter player1Output;
public static Socket player2;
public static Scanner player2Input;
public static PrintWriter player2Output;
public static ServerSocket server;
public static void main(String[] args) throws IOException
{
final int SBAP_PORT = 8888;
server = new ServerSocket(SBAP_PORT);
//Frame
Server frame = new Server();
System.out.println("Waiting for clients to connect...");
//Player1
player1 = server.accept();
System.out.println("Player 1 has joined.");
//input/Output for player 1
player1Input = new Scanner (player1.getInputStream());
player1Output = new PrintWriter (player1.getOutputStream());
//Label player 1
player1Output.println(1);
player1Output.flush();
System.out.println("Waiting for player 2 to connect...");
//Player1
player2 = server.accept();
System.out.println("Player 2 has joined.");
//input/Output for player 1
player2Input = new Scanner (player2.getInputStream());
player2Output = new PrintWriter (player2.getOutputStream());
//Label player 1
player2Output.println(2);
player2Output.flush();
System.out.println("Both players have connected.");
}
public Server()
{
//Frame
JTextArea jtaLog = new JTextArea();
// Create a scroll pane to hold text area
JScrollPane scrollPane = new JScrollPane(jtaLog);
// Add the scroll pane to the frame
add(scrollPane, BorderLayout.CENTER);
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
setSize(450, 300);
setTitle("Server");
setVisible(true);
}
public void run()
{
while (true)
{
if(isWon() == 1)
{
}
else if(isWon() == 2)
{
}
else
{
}
}
}
/** Total clicks to win the game */
private int isWon()
{
if(player1Input.equals("WinP1"))
{
return 1;
}
if(player2Input.equals("WinP2"))
{
return 2;
}
return 3;
}
}
这是客户端,这是我为两个玩家生成的标准,它有标题栏,状态栏,缺点按钮,垃圾邮件按钮和优势按钮。
import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
import javax.swing.border.LineBorder;
import java.io.*;
import java.net.*;
import java.util.Random;
import java.util.Scanner;
public class Client extends JApplet implements Runnable
{
private static final JButton MainClick = new JButton("Spam to win!");
private static final JButton Advantage = new JButton("Spend 25 " + "clicks for " + "advantages");
private static final JButton Disadvantage = new JButton("Spend 25 " + "clicks to " + "sabotage " + "your " + "opponent");
// Starting Value of clicks for player 1
public int player1Amount = 0;
// Starting Value of clicks for player 2
public int player2Amount = 0;
// Multi-click for Player 1
public boolean multiclicksP1 = false;
// Multi-click for Player 2
public boolean multiclicksP2 = false;
// 2for1 for Player 1
public boolean player1TwoForOne = false;
// 2for1 count for Player 1
public int player1TwoForOneCount = 0;
// 2for1 timer
public Thread timer = new Thread();
// Enable Countdown for timer
public boolean countDown = false;
// Time
public int time;
// Creates a random number between 1-4
Random randomGenerator = new Random();
int randomNum1 = randomGenerator.nextInt(4) + 1;
int randomNum2 = randomGenerator.nextInt(4) + 1;
int ranNUmOfClicks = randomGenerator.nextInt(50) + 25;
// Create and initialize a title label and status label
private JLabel jlblTitle = new JLabel();
private JLabel jlblStatus = new JLabel();
// Input and output streams from/to server
private Scanner fromServer;
private PrintWriter toServer;
// Continue to play?
private boolean continueToPlay = true;
// Indicate if it runs as application
private boolean isStandAlone = true;
// Host name or ip
private String host = "localhost";
/** Initialize UI */
public void init()
{
// Panel p to hold game
JPanel p = new JPanel();
// Set properties for labels and borders for labels and panel
p.setBorder(new LineBorder(Color.black, 1));
jlblTitle.setHorizontalAlignment(JLabel.CENTER);
jlblTitle.setFont(new Font("SansSerif", Font.BOLD, 16));
jlblTitle.setBorder(new LineBorder(Color.black, 1));
jlblStatus.setBorder(new LineBorder(Color.black, 1));
// Place the panel and the labels to the applet
add(jlblTitle, BorderLayout.NORTH);
add(jlblStatus, BorderLayout.SOUTH);
add(MainClick, BorderLayout.CENTER);
add(Advantage, BorderLayout.EAST);
add(Disadvantage, BorderLayout.WEST);
MainClick.setPreferredSize(new Dimension(100,100));
Advantage.setPreferredSize(new Dimension(300,100));
Disadvantage.setPreferredSize(new Dimension(300,100));
// Connect to the server
connectToServer();
}
private void connectToServer()
{
try
{
// Create a socket to connect to the server
Socket socket;
if (isStandAlone)
socket = new Socket(host, 8888);
else
socket = new Socket(getCodeBase().getHost(), 8000);
// Create an input stream to receive data from the server
fromServer = new Scanner(socket.getInputStream());
// Create an output stream to send data to the server
toServer = new PrintWriter(socket.getOutputStream());
}
catch (Exception ex)
{
System.err.println(ex);
}
// Control the game on a separate thread
Thread thread = new Thread(this);
thread.start();
}
public void run()
{
try
{
// Get notification from the server
int player = fromServer.nextInt();
// Am I player 1 or 2?
if (player == 1)
{
jlblTitle.setText("Player 1 click for your life!" + " Your Clicks: " + player1Amount);
jlblStatus.setText("Waiting for player 2 to join.");
// Receive startup notification from the server
fromServer.nextInt(); // Whatever read is ignored
// The other player has joined
jlblStatus.setText("Player 2 has joined. Now Click!");
MainClick.addActionListener(new ActionListener()
{
public void actionPerformed(ActionEvent event)
{
//String clicked = event.toString();
if(multiclicksP1 == true)
{
//System.out.println("I'm on!"); //Debug
player1Amount = player1Amount + 2;
jlblTitle.setText("Player 1 click for your life!" + " Your Clicks: " + player1Amount);
}
else
player1Amount++;
jlblTitle.setText("Player 1 click for your life!" + " Your Clicks: " + player1Amount);
//System.out.println(player1Amount); //Debug
}
});
Advantage.addActionListener(new ActionListener()
{
public void actionPerformed(ActionEvent event)
{
System.out.println("Button Clicked");
//System.out.println("Starting random number: " + randomNum1);
if(player1Amount >= 25)
{
randomNum1 = randomGenerator.nextInt(4) + 1;
//System.out.println("New random number before choosing: " + randomNum1);
player1Amount = player1Amount - 25;
if(randomNum1 == 1)
{
multiclicksP1 = true;
jlblStatus.setText("Clicks are now worth 2 clicks!");
jlblTitle.setText("Player 1 click for your life!" + " Your Clicks: " + player1Amount);
randomNum1 = randomGenerator.nextInt(4) + 1;
//System.out.println("New random number after choosing: " + randomNum1);
}
if(randomNum1 == 2)
{
jlblStatus.setText(ranNUmOfClicks + " Clicks were added!");
jlblTitle.setText("Player 1 click for your life!" + " Your Clicks: " + player1Amount);
player1Amount = player1Amount + ranNUmOfClicks;
ranNUmOfClicks = randomGenerator.nextInt(25) + 25;
//System.out.println("New random number after adding: " + ranNUmOfClicks);
}
if(randomNum1 == 3)
{
jlblStatus.setText("Your opponents clicks wil now slightly help you for a limited time!");
player1TwoForOne = true;
}
if(randomNum1 == 4)
{
}
}
else
{
jlblStatus.setText("You don't have 25 clicks to spend!");
}
}
});
Disadvantage.addActionListener(new ActionListener()
{
public void actionPerformed(ActionEvent event)
{
System.out.println("Button Clicked");
if(player1Amount >= 25)
{
}
else
{
jlblStatus.setText("You don't have 25 clicks to spend!");
}
}
});
}
else if (player == 2)
{
jlblTitle.setText("Player 2 click for your life!" + " Your Clicks: " + player2Amount);
jlblStatus.setText("Now Click!");
JButton MainClick = new JButton("Spam to win!");
add(MainClick, BorderLayout.CENTER);
MainClick.setPreferredSize(new Dimension(100,100));
JButton Advantage = new JButton("Spend 25 " + "clicks for " + "advantages");
add(Advantage, BorderLayout.EAST);
Advantage.setPreferredSize(new Dimension(300,100));
JButton Disadvantage = new JButton("Spend 25 " + "clicks to " + "sabotage " + "your " + "opponent");
add(Disadvantage, BorderLayout.WEST);
Disadvantage.setPreferredSize(new Dimension(300,100));
// Receive startup notification from the server
fromServer.nextInt(); // Whatever read is ignored
// The other player has joined
/*if(countDown == true)
{
for(int time = 8; time >= 0; time--)
{
try {
timer.sleep(1000);
} catch (InterruptedException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
System.out.println(time);
if(time == 0)
{
player1TwoForOne = false;
}
}
}*/
MainClick.addActionListener(new ActionListener()
{
public void actionPerformed(ActionEvent event)
{
if(player1TwoForOne = true)
{
//countDown = true;
player1TwoForOneCount++;
System.out.println(player1TwoForOneCount);
if(player1TwoForOneCount == 2)
{
player1Amount = player1Amount + 1;
player1TwoForOneCount = 0;
}
if (time == 0)
{
countDown = false;
}
}
if(multiclicksP2 == true)
{
//System.out.println("I'm on!"); //Debug
player2Amount = player2Amount + 2;
jlblTitle.setText("Player 2 click for your life!" + " Your Clicks: " + player2Amount);
}
else
player2Amount++;
jlblTitle.setText("Player 2 click for your life!" + " Your Clicks: " + player2Amount);
}
});
Advantage.addActionListener(new ActionListener()
{
public void actionPerformed(ActionEvent event)
{
//System.out.println("Starting random number: " + randomNum2);
if(player2Amount >= 25)
{
randomNum2 = randomGenerator.nextInt(4) + 1;
//System.out.println("New random number before choosing: " + randomNum2);
player2Amount = player2Amount - 25;
if(randomNum2 == 1)
{
multiclicksP2 = true;
jlblStatus.setText("Clicks are now worth 2 clicks!");
jlblTitle.setText("Player 2 click for your life!" + " Your Clicks: " + player2Amount);
randomNum2 = randomGenerator.nextInt(4) + 1;
//System.out.println("New random number after choosing: " + randomNum2);
}
}
else
{
jlblStatus.setText("You don't have 25 clicks to spend!");
}
}
});
Disadvantage.addActionListener(new ActionListener()
{
public void actionPerformed(ActionEvent event)
{
if(player2Amount >= 25)
{
}
else
{
jlblStatus.setText("You don't have 25 clicks to spend!");
}
}
});
}
// Continue to play
while (continueToPlay)
{
int status = fromServer.nextInt();
continueToPlay = false;
if (player == 1)
{
if(player1Amount >= 200)
{
toServer.write(1);
}
}
else if (player == 2)
{
if(player2Amount >= 200)
{
toServer.write(2);
}
}
//else if (status == PLAYER1_WON)
{
if(player == 1)
{
jlblTitle.setText("Congrats Player 1 you've out click Player 2!");
}
if(player == 2)
{
jlblTitle.setText("Player 1 has out clicked you Player 2!");
}
}
//else if (status == PLAYER2_WON)
{
if(player == 1)
{
jlblTitle.setText("Player 2 has out clicked you Player 1!");
}
if(player == 2)
{
jlblTitle.setText("Congrats Player 2 you've out click Player 1!");
}
}
}
}
catch (Exception ex) {}
}
/** This main method enables the applet to run as an application */
public static void main(String[] args)
{
// Create a frame
JFrame frame = new JFrame("Client");
// Create an instance of the applet
Client applet = new Client();
applet.isStandAlone = true;
// Get host
if (args.length == 1) applet.host = args[0];
// Add the applet instance to the frame
frame.getContentPane().add(applet, BorderLayout.CENTER);
// Invoke init() and start()
applet.init();
applet.start();
// Display the frame
frame.setSize(800, 400);
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.setVisible(true);
}
}