【问题标题】:PyGame: Stop moving a polygon once a stop message is received from SocketPyGame:从套接字收到停止消息后停止移动多边形
【发布时间】:2023-02-22 19:31:31
【问题描述】:

我有一个用于 PyGame 的 python 脚本,它在屏幕上移动一个多边形。一旦通过套接字收到消息“停止”,它就应该停止移动多边形。当前,整个 pyGame 脚本在收到停止消息时结束。我希望仍显示多边形的当前位置。

这是移动多边形的代码:

import pygame
import select
import socket

# Initialize pygame and create a window
pygame.init()
screen = pygame.display.set_mode((640, 480))

# Set up the socket connection
server_socket = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
server_socket.bind(('192.168.178.49', 5000))
server_socket.listen(1)

# Set up the triangle
triangle_pos = [0, 10]
triangle_speed = 1

# Initialize the stop_movement variable
stop_movement = False

# Start the main loop
exiting = False
while not exiting:
    # Check for any incoming messages from the socket connection
    inputs, _, _ = select.select([server_socket], [], [], 0.1)
    for sock in inputs:
        data = sock.recv(1024)
        if data == b'stop':
            stop_movement = True
        else:
            stop_movement = False

    # Update the triangle position if it has not been stopped
    if not stop_movement:
        triangle_pos[0] += triangle_speed
        if triangle_pos[0] > 640:
            triangle_pos[0] = 0

    # Draw the triangle
    pygame.draw.polygon(screen, (255, 0, 0), [(triangle_pos[0], triangle_pos[1]), (triangle_pos[0]+5, triangle_pos[1]+10), (triangle_pos[0]-5, triangle_pos[1]+10)])

    # Update the display
    pygame.display.flip()

    # Check for quit event
    for event in pygame.event.get():
        if event.type == pygame.QUIT:
            exiting = True

# Quit Pygame
pygame.quit()

这是套接字另一端的发送者代码:

import socket

# Set up the socket connection
client_socket = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
client_socket.connect(('192.168.178.49', 5000))

# Send the "stop" message
client_socket.send(b'stop')

我究竟做错了什么?

先感谢您!

【问题讨论】:

  • 当它通过套接字收到“停止”消息时,您正在退出循环并退出 Pygame。您需要修改代码以继续显示多边形的当前位置,即使收到“停止”消息也是如此。
  • @Michael 这当然不是这里发生的事情。问题是直接在套接字上调用recv,而不是调用accept返回的连接。

标签: python sockets pygame


【解决方案1】:

select.select 返回输入套接字时,您应该在该套接字上调用accept,然后使用返回的连接对象接收数据。

此外,套接字应该是非阻塞的,因此游戏循环可以在检查该连接上的数据时运行。

这是一个快速的'n'dirty示例:

第1部分:

import pygame
import select
import socket

# Initialize pygame and create a window
pygame.init()
screen = pygame.display.set_mode((640, 480))

# Set up the socket connection
server_socket = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
server_socket.bind(('127.0.0.1', 5000))
server_socket.listen(1)
server_socket.setblocking(False) # <========= the socket should not block

# Set up the triangle
triangle_pos = [0, 10]
triangle_speed = 5

# Initialize the stop_movement variable
stop_movement = False

# Start the main loop
exiting = False
connection = None
while not exiting:
    # Check for any incoming messages from the socket connection
    inputs, _, _ = select.select([server_socket], [], [], 0.1)
    for sock in inputs:
        connection, addr = sock.accept() # <========= keep track of the connection
        print('Connected')
        
    if connection:
        try:
            data = connection.recv(1024) # <========= call recv on the connection
            print(data)
            if data == b'stop':
                stop_movement = True
            else:
                stop_movement = False
        except: # <========= if there's no data, ignore it
            pass

    # Update the triangle position if it has not been stopped
    if not stop_movement:
        triangle_pos[0] += triangle_speed
        if triangle_pos[0] > 640:
            triangle_pos[0] = 0

    screen.fill('black') # <========= clear the screen 
    # Draw the triangle
    pygame.draw.polygon(screen, (255, 0, 0), [(triangle_pos[0], triangle_pos[1]), (triangle_pos[0]+5, triangle_pos[1]+10), (triangle_pos[0]-5, triangle_pos[1]+10)])

    # Update the display
    pygame.display.flip()

    # Check for quit event
    for event in pygame.event.get():
        if event.type == pygame.QUIT:
            exiting = True

# Quit Pygame
pygame.quit()

第2部分:

import socket, time

# Set up the socket connection
client_socket = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
client_socket.connect(('127.0.0.1', 5000))

# Send the "stop" message
while True:
    client_socket.send(b'stop')
    time.sleep(1)
    client_socket.send(b'start')
    time.sleep(2)

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2019-10-19
    • 1970-01-01
    • 1970-01-01
    • 2016-05-24
    • 2021-07-17
    • 1970-01-01
    • 1970-01-01
    • 2018-07-22
    相关资源
    最近更新 更多