当我想访问 int(COORDINATES 2 ) 时,这不起作用。我该如何解决?我正在尝试访问现在已成为全球范围的 COODINATES
import socket
import os,sys
import time
from threading import Thread
HOST = '59.191.193.59'
PORT = 5555
COORDINATES = []
def connect():
globals()['client_socket'] = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
client_socket.connect((HOST,PORT))
def update_coordinates():
connect()
screen_width = 0
screen_height = 0
while True:
try:
client_socket.send("loc\n")
data = client_socket.recv(8192)
except:
connect();
continue;
globals()['COORDINATES'] = data.split()
if(not(COORDINATES[-1] == "eom" and COORDINATES[0] == "start")):
continue
if (screen_width != int(COORDINATES[2])):
screen_width = int(COORDINATES[2])
screen_height = int(COORDINATES[3])
Thread(target=update_coordinates).start()
while True:
print 'ball x' , int(COORDINATES[2])
这是我得到的错误
如果我将代码更改为:
import socket
import os,sys
import time
from threading import Thread
HOST = '59.191.193.59'
PORT = 5555
COORDINATES = []
def connect():
globals()['client_socket'] = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
client_socket.connect((HOST,PORT))
def update_coordinates():
connect()
screen_width = 0
screen_height = 0
while True:
try:
client_socket.send("loc\n")
data = client_socket.recv(8192)
except:
connect();
continue;
globals()['COORDINATES'] = data.split()
if(not(COORDINATES[-1] == "eom" and COORDINATES[0] == "start")):
continue
if (screen_width != int(COORDINATES[2])):
screen_width = int(COORDINATES[2])
screen_height = int(COORDINATES[3])
print int(COORDINATES[8])
Thread(target=update_coordinates).start()
这就是我得到的: