Article Directory
brief introduction
This article will simply use the socket and pygame modules to implement games in the LAN. Under the same network, the client can enter the correct ip address to play online games.
operating environment
Compiler: PyCharm 2021.2.1
Interpreter: Anaconda 3.8
install module
pip install pygame==2.1.0
Socket introduction and usage
socket introduction
A socket is an abstraction of an endpoint for two-way communication between application processes on different hosts in the network. A socket is one end of process communication on the network, providing a mechanism for application layer processes to exchange data using network protocols. From the perspective of its position, the socket connects to the application process and connects to the network protocol stack, which is the interface for the application program to communicate through the network protocol, and the interface for the application program to interact with the network protocol stack——Baidu Encyclopedia
To put it simply, it can realize communication between different hosts, for example, we browse the web every day, chat on QQ, send and receive emails
socket usage
Server
import socket
# socket.AF_INET 用于服务器之间的通信
# socket.SOCK_STREAM tcp通信
# 创建套接字
server = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
# ip 表示本机ip地址
# port 表示端口号
address = ('ip', 'port')
# 连接地址
server.bind(address)
# 监听数量
server.listen(5)
print('正在等待连接...')
# client 连接对象
# client_address 连接对象地址
client, client_address = server.accept()
# 发送数据
client.send('hahah'.encode('gbk'))
# 接受1024个字节数据
data = client.recv(1024)
print('服务器接受的数据', data.decode('gbk'))
# 关闭连接
client.close()
client
import socket
# 创建socket
client_socket = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
# 连接服务器
client_socket.connect(('ip', 'port'))
# 发送的数据
client_socket.send('收到'.encode("gbk"))
# 接收对方发送过来的数据,最大接收1024个字节
recv = client_socket.recv(1024)
print('接收到的数据为:', recv.decode('gbk'))
# 关闭套接字
client_socket.close()
Some people here don’t know how to check the ip address of this machine. Here I will teach you how to check the ip address.
First, win+r will pop up a window, enter cmd and click OK
After confirming, a black window will pop up, enter ipconfig , and it will come out
Socket realizes the principle of LAN game
The essence of online games is actually to send and receive data, and how to send, receive, and define data is what we have to consider now. The following mainly explains the sending and receiving data of the client. The server is similar, and it is almost the same. Let's start!
send
Send data, when the user clicks or performs a certain action, the client sends data to the server, and the server returns information to respond to the client after receiving the data.
def send_data():
global all_coordinate, sending
sending['checkerboard'] = all_coordinate
socket_client.send(json.dumps(sending).encode('gbk'))
sending['active'] = False
print('服务器发送的数据', sending)
accept
To accept data, whether it is a client or a server, when the client is connected to the server, it must accept data all the time. An infinite loop is defined here to receive data, of course a thread must be opened.
def accept_data():
global sending, all_coordinate
while True:
recv = socket_client.recv(4096)
sending = json.loads(recv.decode('gbk'))
all_coordinate = sending['checkerboard']
print('线程客户端接受的数据', sending)
Data Format
Here, the data format is defined in the form of a dictionary. When the client sends data, a certain item in the data will be changed, and the same is true for the server.
sending = {
'checkerboard': all_coordinate,
'active': True,
'start': [False, False],
'time': False,
'flag': [False, 0, 0],
}
end
Please click here for the complete code , everyone is welcome, the mountains are high and the road is far away, goodbye! ! !