我正在尝试使用简单的服务器/客户端套接字来控制游戏中的桨。这个想法是让客户端能够通过向服务器发送指令来控制桨,服务器会将指令传递给游戏。所以我创建了一个线程来监听客户的方向并将它们存储在一个变量中,另一个线程用于运行游戏。问题是当客户端发送字母“z”(将桨向上移动)时,游戏开始,但他发送另一个字母,游戏就崩溃了。
我不知道我是否已经对问题进行了足够的解释,我是 python 新手,我会感谢您的帮助,告诉我如何解决问题!
这是我的代码:
import socket
import threading
from threading import RLock
import pygame
from pygame.locals import *
class Pong(object):
def __init__(self, screensize):
self.screensize = screensize
self.centerx = int(screensize[0]*0.5)
self.centery = int(screensize[1]*0.5)
self.radius = 8
self.rect = pygame.Rect(self.centerx-self.radius,
self.centery-self.radius,
self.radius*2, self.radius*2)
self.color = (100,100,255)
self.direction = [1,1]
self.speedx = 2
self.speedy = 5
self.hit_edge_left = False
self.hit_edge_right = False
def update(self, player_paddle, ai_paddle):
self.centerx += self.direction[0]*self.speedx
self.centery += self.direction[1]*self.speedy
self.rect.center = (self.centerx, self.centery)
if self.rect.top <= 0:
self.direction[1] = 1
elif self.rect.bottom >= self.screensize[1]-1:
self.direction[1] = -1
if self.rect.right >= self.screensize[0]-1:
self.hit_edge_right = True
elif self.rect.left <= 0:
self.hit_edge_left = True
if self.rect.colliderect(player_paddle.rect):
self.direction[0] = -1
if self.rect.colliderect(ai_paddle.rect):
self.direction[0] = 1
def render(self, screen):
pygame.draw.circle(screen, self.color, self.rect.center, self.radius, 0)
pygame.draw.circle(screen, (0,0,0), self.rect.center, self.radius, 1)
class AIPaddle(object):
def __init__(self, screensize):
self.screensize = screensize
self.centerx = 5
self.centery = int(screensize[1]*0.5)
self.height = 100
self.width = 10
self.rect = pygame.Rect(0, self.centery-int(self.height*0.5), self.width, self.height)
self.color = (255,100,100)
self.speed = 3
def update(self, pong):
if pong.rect.top < self.rect.top:
self.centery -= self.speed
elif pong.rect.bottom > self.rect.bottom:
self.centery += self.speed
self.rect.center = (self.centerx, self.centery)
def render(self, screen):
pygame.draw.rect(screen, self.color, self.rect, 0)
pygame.draw.rect(screen, (0,0,0), self.rect, 1)
class PlayerPaddle(object):
def __init__(self, screensize):
self.screensize = screensize
self.centerx = screensize[0]-5
self.centery = int(screensize[1]*0.5)
self.height = 100
self.width = 10
self.rect = pygame.Rect(0, self.centery-int(self.height*0.5), self.width, self.height)
self.color = (100,255,100)
self.speed = 3
self.direction = 0
def update(self):
self.centery += self.direction*self.speed
self.rect.center = (self.centerx, self.centery)
if self.rect.top < 0:
self.rect.top = 0
if self.rect.bottom > self.screensize[1]-1:
self.rect.bottom = self.screensize[1]-1
def render(self, screen):
pygame.draw.rect(screen, self.color, self.rect, 0)
pygame.draw.rect(screen, (0,0,0), self.rect, 1)
class _Directions(object):
data = ""
def __init__(self):
_Directions.data = ""
def set_Data(self, data_Val):
_Directions.data = data_Val
def get_Data(self):
return _Directions.data
def pongGame(Direct_Data):
pygame.init()
screensize = (640,480)
screen = pygame.display.set_mode(screensize)
clock = pygame.time.Clock()
pong = Pong(screensize)
ai_paddle = AIPaddle(screensize)
player_paddle = PlayerPaddle(screensize)
running = True
while running:
#fps limiting/reporting phase
clock.tick(64)
#event handling phase
for event in pygame.event.get():
if event.type == QUIT:
running = False
if(Direct_Data.get_Data() == "z"):
player_paddle.direction = -1
elif(Direct_Data.get_Data() == "s"):
player_paddle.direction = 1
#object updating phase
ai_paddle.update(pong)
player_paddle.update()
pong.update(player_paddle, ai_paddle)
if pong.hit_edge_left:
print ("You Won")
pong = Pong(screensize)
ai_paddle = AIPaddle(screensize)
player_paddle = PlayerPaddle(screensize)
elif pong.hit_edge_right:
print ("You Lose")
pong = Pong(screensize)
ai_paddle = AIPaddle(screensize)
player_paddle = PlayerPaddle(screensize)
#rendering phase
screen.fill((100,100,100))
ai_paddle.render(screen)
player_paddle.render(screen)
pong.render(screen)
pygame.display.flip()
pygame.quit()
def get_Directions(Direct_Data, client):
data = client.recv(1024).decode()
Direct_Data.set_Data(data)
client.send(data.encode())
def gameStart():
verrou = RLock()
Direct_Data = _Directions()
server = socket.socket()
host = '127.0.0.1'
port = 1234
server.bind((host, port))
server.listen(1)
print ("[*] Server started !")
client, addr = server.accept()
print("[*] Got connection from ip: ", addr[0])
while True:
with verrou:
t = threading.Thread(target=get_Directions, args=[Direct_Data, client])
t1 = threading.Thread(target=pongGame, args=[Direct_Data])
t.start()
t1.start()
client.close()
if __name__ == '__main__':
gameStart()