- html - 出于某种原因,IE8 对我的 Sass 文件中继承的 html5 CSS 不友好?
- JMeter 在响应断言中使用 span 标签的问题
- html - 在 :hover and :active? 上具有不同效果的 CSS 动画
- html - 相对于居中的 html 内容固定的 CSS 重复背景?
所以我正在开发一款非常简单的多人 Java 游戏,只需单击屏幕,您的玩家就会沿直线移动到该位置,并向所有其他连接的玩家显示您的玩家的移动,以下是三个类:
机器人:
import java.applet.Applet;
import java.awt.Color;
import java.awt.Dimension;
import java.awt.Graphics;
import java.awt.Image;
import java.awt.event.MouseEvent;
import java.awt.event.MouseListener;
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.io.PrintWriter;
import java.net.Socket;
import java.net.UnknownHostException;
import java.util.ArrayList;
public class Bots extends Applet implements MouseListener, Runnable{
Thread gameLoop;
ArrayList<Player> players = new ArrayList<Player>();
//Socket stuff for multiplayer client
final static String SERVER_ADDR = "localhost";
final static int SERVER_PORT = 1025;
static Socket socket;
static volatile BufferedReader br;
static PrintWriter pw;
//Double buffer stuff
Graphics g;
Image offscreen;
Dimension dim;
@Override
public void run() {
//Starts the main game loop
Thread t = Thread.currentThread();
//Checks to see if the thread is alive
while (t == gameLoop){
try{
Thread.sleep(1000/60);
//Repaints the screen
players.get(0).move();
repaint();
if (socket != null){
sendMove();
}
} catch (InterruptedException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
}
public void start(){
gameLoop = new Thread(this);
gameLoop.start();
}
public void stop(){
gameLoop = null;
}
public void init(){
this.setSize(640,480);
dim = this.getSize();
addMouseListener(this);
//More double buffer stuff
offscreen = createImage(this.getSize().width,this.getSize().height);
g = offscreen.getGraphics();
//Adds the initial player
players.add(new Player(320,240,8,8,"Jeremy"));
multiplayer();
}
public void paint(Graphics bufferGraphics){
g = offscreen.getGraphics();
g.clearRect(0,0,dim.width,dim.height);
g.setColor(Color.BLACK);
for (Player player: players){
g.fillOval(player.x, player.y, player.width, player.height);
}
bufferGraphics.drawImage(offscreen,0,0,this);
}
public void multiplayer(){
try {
socket = new Socket(SERVER_ADDR, SERVER_PORT);
InputStreamReader isr;
isr = new InputStreamReader(socket.getInputStream());
br = new BufferedReader(isr);
pw = new PrintWriter(socket.getOutputStream(), true);
} catch (UnknownHostException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
multiplayerThread();
}
public void sendMove(){
//pw.println(players.get(0).name+":X"+players.get(0).x+":Y"+players.get(0).y);
}
public void multiplayerThread(){
new Thread(){
public void run() {
String line;
try {
while ((line = br.readLine()) != null) {
boolean newClient = true;
for (Player player: players){
if (line.startsWith(player.name)){
if (line.startsWith(players.get(0).name)){
//Do nothing
newClient = false;
} else {
String[] tempLine = line.split(":");
player.x = Integer.parseInt(tempLine[1].substring(1));
player.y = Integer.parseInt(tempLine[2].substring(1));
newClient = false;
}
}
System.out.println(line);
}
if (newClient && !line.contains("Welcome")){
String[] tempLine = line.split(":");
players.add(new Player (Integer.parseInt(tempLine[1].substring(1)),Integer.parseInt(tempLine[2].substring(1)),8,8,tempLine[0].substring(0)));
}
}
}
catch (IOException ioe) {
}
}
}.start();
}
@Override
public void mouseClicked(MouseEvent me) {
// TODO Auto-generated method stub
}
@Override
public void mouseEntered(MouseEvent arg0) {
// TODO Auto-generated method stub
}
@Override
public void mouseExited(MouseEvent arg0) {
// TODO Auto-generated method stub
}
@Override
public void mousePressed(MouseEvent me) {
// TODO Auto-generated method stub
players.get(0).x2 = me.getX();
players.get(0).y2 = me.getY();
players.get(0).calculateMove();
}
@Override
public void mouseReleased(MouseEvent arg0) {
// TODO Auto-generated method stub
}
public void update(Graphics g)
{
paint(g);
}
}
玩家等级:
import java.awt.Rectangle;
public class Player {
public String name;
public int x;
public int y;
public int width;
public int height;
public int x2;
public int y2;
public int longest;
public int numerator;
public int shortest;
public int dx1,dy1,dx2,dy2;
public boolean moving;
public Player(int x, int y, int width, int height, String name) {
this.x = x;
this.y = y;
this.x2 = x;
this.y2 =y;
this.width = width;
this.height = height;
this.name = name;
}
public Rectangle getBounds(){
Rectangle r;
r = new Rectangle(x - width/2, y-height/2,width,height);
return r;
}
public void move(){
if (this.x2 != this.x && this.y2 != this.y){
this.numerator += this.shortest ;
if (!(this.numerator<this.longest)) {
this.numerator -= this.longest ;
this.x += this.dx1 ;
this.y += this.dy1 ;
} else {
this.x += this.dx2 ;
this.y += this.dy2 ;
}
this.moving = false;
}
}
public void calculateMove(){
int w = x2 - x ;
int h = y2 - y ;
int dx1 = 0, dy1 = 0, dx2 = 0, dy2 = 0 ;
if (w<0) dx1 = -1 ; else if (w>0) dx1 = 1 ;
if (h<0) dy1 = -1 ; else if (h>0) dy1 = 1 ;
if (w<0) dx2 = -1 ; else if (w>0) dx2 = 1 ;
int longest = Math.abs(w) ;
int shortest = Math.abs(h) ;
if (!(longest>shortest)) {
longest = Math.abs(h) ;
shortest = Math.abs(w) ;
if (h<0) dy2 = -1 ; else if (h>0) dy2 = 1 ;
dx2 = 0 ;
}
this.shortest = shortest;
this.longest = longest;
this.numerator = longest >> 1 ;
this.dx1 = dx1;
this.dy1 = dy1;
this.dx2 = dx2;
this.dy2 = dy2;
this.moving = true;
}
}
最后是我的 BotsServer 类:
import java.io.BufferedReader;
import java.io.InputStreamReader;
import java.io.IOException;
import java.io.PrintWriter;
import java.net.ServerSocket;
import java.net.Socket;
import java.util.ArrayList;
import java.util.List;
class BotsServer
{
private final static int PORT_NO = 1025;
private ServerSocket listener;
private List<Connection> clients;
BotsServer() throws IOException
{
listener = new ServerSocket(PORT_NO);
clients = new ArrayList<Connection>();
System.out.println("listening on port "+PORT_NO);
}
void runServer()
{
try
{
while (true)
{
Socket socket = listener.accept();
System.out.println("accepted connection");
Connection con = new Connection(socket);
synchronized(clients)
{
clients.add(con);
con.start();
con.send("Welcome!");
}
}
}
catch (IOException ioe)
{
System.err.println("I/O error: "+ioe.getMessage());
return;
}
}
private class Connection extends Thread
{
private volatile BufferedReader br;
private volatile PrintWriter pw;
private String clientName;
Connection(Socket s) throws IOException
{
br = new BufferedReader(new InputStreamReader(s.getInputStream()));
pw = new PrintWriter(s.getOutputStream(), true);
}
@Override
public void run()
{
System.out.println("test");
String line;
try
{
while ((line = br.readLine()) != null){
System.out.println(line);
send(line);
}
}
catch (IOException ioe)
{
System.err.println("I/O error: "+ioe.getMessage());
}
finally
{
synchronized(clients)
{
clients.remove(this);
}
}
}
private void send(String message)
{
pw.println(message);
}
}
public static void main(String[] args)
{
try
{
System.out.println("ChatServer starting");
new BotsServer().runServer();
}
catch (IOException ioe)
{
System.err.println("unable to create server socket");
}
}
}
我认为问题出在 BotsServer 类中,我把:
System.out.println("test");
在运行中,但只有在客户端首次连接时才会被调用,它应该循环吗?任何帮助都很棒!谢谢!
最佳答案
BotServer.Connection
是一个线程
。 Thread
的 run
方法仅被调用一次。您的测试打印位于 run()
内,但不在任何循环内,因此每次 BotServer.Connection
您 start()
只会打印一次。所以你所看到的行为对我来说是有意义的。你为什么认为它应该循环?
关于Java套接字,简单的多人游戏,发送数据,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/12224827/
我正在维护一些 Java 代码,我目前正在将它们转换为 C#。 Java 代码是这样做的: sendString(somedata + '\000'); 在 C# 中,我正在尝试做同样的事情: sen
如何确定函数中传递的参数是字符串还是字符(不确定如何正确调用它)文字? 我的函数(不正确): void check(const char* str) { // some code here }
我真的不知道如何准确地提出这个问题,但我希望标题已经说明了这一点。 我正在寻找一种方法(一个框架/库),它提供了执行 String.contains() 函数的能力,该函数告诉我给定的字符串是否与搜索
我正在尝试编写一些读取 Lambda 表达式并输出 beta 缩减版本的东西。 Lambda 的类型如下:\variable -> expression,应用程序的形式为 (表达式) (表达式)。因此
StackOverflow 上的第 1 篇文章,如果我没能把它做好,我深表歉意。我陷入了一个愚蠢的练习,我需要制作一个“刽子手游戏”,我尝试从“.txt”文件中读取单词,然后我得到了我的加密函数,它将
我想在 Groovy 中测试我的 Java 自定义注释,但由于字符问题而未能成功。 Groovyc: Expected 'a' to be an inline constant of type cha
当我尝试在单击按钮期间运行 javascript location.href 时,出现以下错误“字 rune 字中的字符过多”。 最佳答案 这应该使用 OnClientClick相反? 您可能还想停
我想要类似的东西: let a = ["v".utf8[0], 1, 2] 我想到的最接近的是: let a = [0x76, 1, 2] 和 "v".data(using: String.Encod
有没有办法在 MySQL 中指定 Unicode 字 rune 字? 我想用 Ascii 字符替换 Unicode 字符,如下所示: Update MyTbl Set MyFld = Replace(
阅读 PNG 规范后,我有点惊讶。我读过字 rune 字应该用像 0x41 这样的二进制值进行硬编码,而不是在(程序员友好的)'A' 中。问题似乎是在具有不同底层字符集的不同系统上编译期间字 rune
考虑一个具有 UTF-8 执行字符集的 C++11 编译器(并且符合要求 char 类型为有符号 8 位字节的 x86-64 ABI) . 字母 Ä(元音变音)具有 0xC4 的 unicode 代码
为什么即使有 UTF-8 字符串文字,C11 或 C++11 中也没有 UTF-8 字 rune 字?我知道,一般来说,字 rune 字表示单个 ASCII 字符,它与单字节 UTF-8 代码点相同,
我怎样才能用 Jade 做到这一点? how would I do this 我几乎可以做任何事情,除了引入一个 span 中间句子。 最佳答案 h3.blur. how would I do t
这似乎是一个非常简单的问题,但我只是想澄清我的疑问。我正在查看其他开发人员编写的代码。有一些涉及 float 的计算。 示例:Float fNotAvlbl = new Float(-99); 他为什
我想知道第 3 行“if dec:”中的“dec”是什么意思 1 def dec2bin(dec): 2 result='' 3 if dec:
我试图在字符串中查找不包含任何“a”字符的单词。我写了下面的代码,但它不起作用。我怎么能对正则表达式说“不包括”?我不能用“^”符号表示“不是”吗? import re string2 = "asfd
这个问题在这里已经有了答案: Is floating point math broken? (31 个答案) Is floating point arbitrary precision availa
我正在创建一个时尚的文本应用程序,但在某些地方出现错误(“字 rune 字中的字符太多”)。我只写了一个字母,但是当我粘贴它时,它会转换成许多这样的字母:“\uD83C\uDD89”,原始字母是“🆉
我正在尝试检查用户是否在文本框中输入了一个数字值,是否接受了小数位。非常感谢任何帮助。 Private Sub textbox1_AfterUpdate() If IsNumeric(textbox1
我知道一个 Byte 是 8 位,但其他的代表什么?我正在参加一个使用摩托罗拉 68k 架构的汇编类(class),我对目前的词汇感到困惑。 最佳答案 如 operator's manual for
我是一名优秀的程序员,十分优秀!