Last active
December 19, 2017 22:03
-
-
Save talkahe/bdb1fd46419c67085b79c6d7d4ddcd6e to your computer and use it in GitHub Desktop.
Simón en Python con SDL v2
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import socket, select, threading, sdl2.ext, sys | |
from sdl2 import * | |
from time import sleep | |
class Client(): | |
def __init__(self): | |
''' Constructor ''' | |
self.running = True | |
self.state = "connecting" | |
self.socket = socket.socket(socket.AF_INET, socket.SOCK_STREAM) | |
self.socket.setsockopt(socket.SOL_SOCKET, socket.SO_REUSEADDR, 1) | |
self.socketList = [self.socket] | |
self.WHITE = sdl2.ext.Color() | |
self.GREEN = sdl2.ext.Color(18, 183, 65) | |
self.BLUE = sdl2.ext.Color(159, 235, 252) | |
self.RED = sdl2.ext.Color(244, 104, 66) | |
self.mapping = ['0123456789', | |
'ABCDEFGHIJ', | |
'KLMNOPQRST', | |
'UVWXYZ ', | |
'abcdefghij', | |
'klmnopqrst', | |
'uvwxyz ', | |
',;.:!?+-()'] | |
self.fontSurface = sdl2.ext.load_image("font.bmp") | |
self.bmpFont = sdl2.ext.BitmapFont(self.fontSurface, (32,32), self.mapping) | |
def stop(self): | |
self.running = False | |
def onclick(self, button, event): | |
if button == self.buttonNo: | |
self.socket.send(b"stop") | |
elif button == self.buttonYes: | |
self.socket.send(b"restart") | |
def startGUI(self): | |
self.window = sdl2.ext.Window("Simon", size=(640, 480), position=None, flags=sdl2.SDL_WINDOW_FULLSCREEN_DESKTOP) | |
print("Window opened") | |
self.screen = self.window.get_surface() | |
softwareRenderer = sdl2.ext.SoftwareSpriteRenderSystem(self.window) | |
factory = sdl2.ext.SpriteFactory(sdl2.ext.SOFTWARE) | |
uiFactory = sdl2.ext.UIFactory(factory) | |
uiProcessor = sdl2.ext.UIProcessor() | |
spriteRenderer = factory.create_sprite_render_system(self.window) | |
self.textSpriteKey = self.bmpFont.render("Your turn!") | |
self.textSpriteRestart = self.bmpFont.render("You have lost! Play again?") | |
self.buttonYes = uiFactory.from_image(sdl2.ext.BUTTON, "yes.bmp") | |
self.buttonYes.position = 150, 200 | |
self.buttonYes.click += self.onclick | |
self.buttonNo = uiFactory.from_image(sdl2.ext.BUTTON, "no.bmp") | |
self.buttonNo.position = 300, 200 | |
self.buttonNo.click += self.onclick | |
while self.running: | |
events = sdl2.ext.get_events() | |
for event in events: | |
if event.type == sdl2.SDL_QUIT: | |
self.state = "stopping" | |
break | |
if self.state == "waiting": | |
sdl2.ext.fill(self.screen, self.WHITE) | |
self.window.refresh() | |
elif self.state == "showing": | |
sdl2.ext.fill(self.screen, self.GREEN) | |
self.window.refresh() | |
elif self.state == "waitingUserKeyUp": | |
sdl2.ext.fill(self.screen, self.BLUE) | |
softwareRenderer.render(self.textSpriteKey, 150, 0) | |
for event in events: | |
if event.type == sdl2.SDL_KEYUP: | |
self.socket.send(b"keyPressed") | |
break | |
elif self.state == "askingForRestart": | |
sdl2.ext.fill(self.screen, self.RED) | |
softwareRenderer.render(self.textSpriteRestart, 0, 0) | |
spriteRenderer.render((self.buttonYes, self.buttonNo)) | |
for event in events: | |
uiProcessor.dispatch([self.buttonYes, self.buttonNo], event) | |
elif self.state == "stopping": | |
self.stop() | |
sys.exit() | |
# connecting, waiting, showing, waitingUserKeyUp, askingForRestart, stopping | |
def start(self, ip, port): | |
while self.running: | |
if self.state == "connecting": | |
print("State: connecting") | |
threadGUI = threading.Thread(target=self.startGUI) | |
threadGUI.start() | |
try: | |
self.socket.connect((ip, port)) | |
print("Connected to server") | |
self.state = "waiting" | |
except: | |
print("Unable to connect") | |
self.state = "stopping" | |
read_sockets, write_sockets, error_sockets = select.select(self.socketList , [], []) | |
for socket in read_sockets: | |
if socket == self.socket: | |
data = socket.recv(1024).decode() | |
#print(data) | |
if data == "wait": | |
print("State: waiting") | |
self.state = "waiting" | |
elif data == "show": | |
print("State: showing") | |
self.state = "showing" | |
sleep(1.5) | |
self.state = "waiting" | |
elif data == "waitUserKeyUp": | |
print("State: waitUserKeyUp") | |
self.state = "waitingUserKeyUp" | |
elif data == "askForRestart": | |
print("askForRestart") | |
self.state = "askingForRestart" | |
elif data == "exit": | |
print("State: stopping") | |
self.state = "stopping" |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
from client import clientClass | |
if __name__ == '__main__': | |
client = clientClass.Client() | |
client.start("127.0.0.1", 12346) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
from server import serverClass | |
if __name__ == '__main__': | |
server = serverClass.Server("127.0.0.1", 12346) | |
server.start() |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import socket, select, threading, sys | |
from random import randint | |
from time import sleep | |
class Server(): | |
def __init__(self, ip, port): | |
''' Constructor ''' | |
self.running = True | |
self.accepting = False | |
self.state = "waiting" | |
self.socketList = [] | |
self.sequence = [] | |
self.stopping = False | |
self.server_socket = socket.socket(socket.AF_INET, socket.SOCK_STREAM) | |
self.server_socket.setsockopt(socket.SOL_SOCKET, socket.SO_REUSEADDR, 1) | |
self.server_socket.bind((ip, port)) | |
self.server_socket.listen(30) | |
self.socketList.append(self.server_socket) | |
def stop(self): | |
self.running = False | |
def broadcastMsg(self, data): | |
for socket in self.socketList: | |
if socket != self.server_socket: | |
try: | |
socket.send(data) | |
except: | |
self.socketList().remove(socket) | |
self.stopping == True | |
self.state = "stopping" | |
def acceptingConnections(self): | |
while self.accepting and self.running: | |
read_sockets,write_sockets,error_sockets = select.select(self.socketList,[],[]) | |
for client in read_sockets: | |
if client == self.server_socket: | |
conn, addr = self.server_socket.accept() | |
self.socketList.append(conn) | |
print("New connection from " + str(addr[0]) + ":" + str(addr[1])) | |
else: | |
data = client.recv(1024) | |
if len(data) == 0: | |
print("cliente desconectado") | |
self.stopping = True | |
self.state = "stopping" | |
self.accepting = False | |
print("Stop accepting clients") | |
def start(self): | |
while self.running: | |
if self.state == "waiting": | |
if self.accepting == False: | |
print("State: waiting") | |
self.accepting = True | |
thread = threading.Thread(target=self.acceptingConnections) | |
thread.start() | |
if len(self.socketList) > 1: | |
if self.stopping == False: | |
self.state = "generatingSeq" | |
self.broadcastMsg(b"wait") | |
elif self.state == "generatingSeq": | |
print("State: generatingSeq") | |
ramdonNumberClient = randint(1,len(self.socketList) - 1) | |
self.sequence.append(ramdonNumberClient) | |
sleep(1) | |
if self.stopping == False: | |
self.state = "showingSeq" | |
elif self.state == "showingSeq": | |
print("State: showingSeq") | |
print(self.sequence) | |
for socketIndex in self.sequence: | |
self.socketList[socketIndex].send(b"show") | |
sleep(2.5) | |
if self.stopping == False: | |
self.state = "checkingSeq" | |
elif self.state == "checkingSeq": | |
print("State: checkingSeq") | |
self.broadcastMsg(b"waitUserKeyUp") | |
sequenceCount = 0 | |
while self.running and self.state == "checkingSeq": | |
read_sockets,write_sockets,error_sockets = select.select(self.socketList,[],[]) | |
for client in read_sockets: | |
if client != self.server_socket: | |
data = client.recv(1024).decode() | |
socketIndex = self.socketList.index(client) | |
if self.sequence[sequenceCount] == socketIndex and data == "keyPressed": | |
sequenceCount = sequenceCount + 1 | |
print(sequenceCount) | |
if sequenceCount == len(self.sequence): | |
# Si la secuencia se ha comprobado completamente | |
self.broadcastMsg(b"wait") | |
if self.stopping == False: | |
self.state = "generatingSeq" | |
else: | |
self.broadcastMsg(b"wait") | |
self.accepting = False | |
if self.stopping == False: | |
self.state = "askForRestart" | |
elif self.state == "askForRestart": | |
print("State: askForRestart") | |
self.broadcastMsg(b"askForRestart") | |
try: | |
read_sockets,write_sockets,error_sockets = select.select(self.socketList,[],[]) | |
for client in read_sockets: | |
if client != self.server_socket: | |
data = client.recv(1024).decode() | |
if data == "restart": | |
self.sequence = [] | |
sleep(0.1) | |
if self.stopping == False: | |
self.state = "waiting" | |
elif data == "stop": | |
if self.stopping == False: | |
self.state = "stopping" | |
except: | |
self.state = "stopping" | |
elif self.state == "stopping": | |
print("State: stopping") | |
self.broadcastMsg(b"exit") | |
self.stop() | |
sys.exit() |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment