Remove useless client

This commit is contained in:
Loic Guegan 2019-05-11 17:52:01 +02:00
parent a39eedbcab
commit e087c17c05
2 changed files with 66 additions and 131 deletions

View file

@ -1,108 +0,0 @@
# -*- coding: utf-8 -*-
import socket, json, time
import pygame
# ---------- SOCKETS ----------
RESP_BUFFER_LENGTH = 1024
ip_adress="192.168.1.14"
port=8090
LARGEUR_BLOCK = 10
NB_BLOCKS = 30
LARGEUR_ECRAN = LARGEUR_BLOCK * NB_BLOCKS
def connect():
s = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
s.connect((ip_adress,port))
return s
def sendData(data):
s = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
s.connect((ip_adress,port))
s.sendall(data.encode('utf-8'))
s.send('#EOF\n'.encode('utf-8'))
received = s.recv(RESP_BUFFER_LENGTH)
s.close()
return received
def newGame():
received = sendData('{"type": "new-game"}')
return json.loads(received)
def update(gameId = 1, direction = None):
data = {
"type" : "update",
"game-id": gameId,
"direction": direction
}
received = sendData(json.dumps(data))
return json.loads(received)
# ---------- END SOCKETS ----------
LARGEUR_BLOCK = 20
NB_BLOCKS = 30
LARGEUR_ECRAN = LARGEUR_BLOCK * NB_BLOCKS
pygame.init()
ecran = pygame.display.set_mode((LARGEUR_ECRAN,LARGEUR_ECRAN))
snakeSprite = pygame.image.load("snake-sprite.png").convert_alpha()
snakeSprite = pygame.transform.scale(snakeSprite, (LARGEUR_BLOCK, LARGEUR_BLOCK))
appleSprite = pygame.image.load("apple-sprite.png").convert_alpha()
appleSprite = pygame.transform.scale(appleSprite, (LARGEUR_BLOCK, LARGEUR_BLOCK))
def afficher(ecran, snakeCoords, fruitsCoords):
snake = []
for coords in snakeCoords:
if coords not in snake:
snake.append(coords)
pygame.draw.rect(ecran, (255,255,255), (0,0,LARGEUR_ECRAN,LARGEUR_ECRAN))
for coords in fruitsCoords:
ecran.blit(appleSprite, (coords[0] * LARGEUR_BLOCK, coords[1] * LARGEUR_BLOCK))
for coords in snake:
ecran.blit(snakeSprite, (coords[0] * LARGEUR_BLOCK, coords[1] * LARGEUR_BLOCK))
def handleControls(event, gameId):
if event.key == pygame.K_LEFT:
return update(gameId, 'left')
elif event.key == pygame.K_RIGHT:
return update(gameId, 'right')
elif event.key == pygame.K_UP:
return update(gameId, 'down')
elif event.key == pygame.K_DOWN:
return update(gameId, 'up')
else:
return None
def main():
gameInit = newGame()
gameId = gameInit['ID']
up = gameInit
continuer = True
while continuer:
afficher(ecran, up['SNAKE'], up['FOOD'])
for event in pygame.event.get():
if event.type == pygame.QUIT:
continuer = False
elif event.type == pygame.KEYDOWN:
updateTmp = handleControls(event, gameId)
print(updateTmp)
if updateTmp != None and 'TYPE' in updateTmp:
if updateTmp['TYPE'] != 'error':
up = updateTmp
time.sleep(0.2)
up = update(gameId)
pygame.display.flip()
pygame.quit()
if __name__ == '__main__':
main()

View file

@ -1,10 +1,12 @@
# -*- coding: utf-8 -*- # -*- coding: utf-8 -*-
import socket, json import socket, json, time
import pygame import pygame
# ---------- SOCKETS ----------
RESP_BUFFER_LENGTH = 1024 RESP_BUFFER_LENGTH = 1024
ip_adress="127.0.0.1" ip_adress="192.168.1.14"
port=8080 port=8090
LARGEUR_BLOCK = 10 LARGEUR_BLOCK = 10
NB_BLOCKS = 30 NB_BLOCKS = 30
@ -18,8 +20,8 @@ def connect():
def sendData(data): def sendData(data):
s = socket.socket(socket.AF_INET, socket.SOCK_STREAM) s = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
s.connect((ip_adress,port)) s.connect((ip_adress,port))
s.sendall(data) s.sendall(data.encode('utf-8'))
s.send('#EOF\n') s.send('#EOF\n'.encode('utf-8'))
received = s.recv(RESP_BUFFER_LENGTH) received = s.recv(RESP_BUFFER_LENGTH)
s.close() s.close()
return received return received
@ -37,29 +39,70 @@ def update(gameId = 1, direction = None):
received = sendData(json.dumps(data)) received = sendData(json.dumps(data))
return json.loads(received) return json.loads(received)
""" # ---------- END SOCKETS ----------
def draw(up):
LARGEUR_BLOCK = 20
NB_BLOCKS = 30
LARGEUR_ECRAN = LARGEUR_BLOCK * NB_BLOCKS
pygame.init()
ecran = pygame.display.set_mode((LARGEUR_ECRAN,LARGEUR_ECRAN))
snakeSprite = pygame.image.load("snake-sprite.png").convert_alpha()
snakeSprite = pygame.transform.scale(snakeSprite, (LARGEUR_BLOCK, LARGEUR_BLOCK))
appleSprite = pygame.image.load("apple-sprite.png").convert_alpha()
appleSprite = pygame.transform.scale(appleSprite, (LARGEUR_BLOCK, LARGEUR_BLOCK))
def afficher(ecran, snakeCoords, fruitsCoords):
snake = [] snake = []
for coords in up['SNAKE']: for coords in snakeCoords:
if coords not in snake: if coords not in snake:
snake.append(coords) snake.append(coords)
pygame.draw.rect(ecran, (255,255,255), (0,0,LARGEUR_ECRAN,LARGEUR_ECRAN))
for coords in fruitsCoords:
ecran.blit(appleSprite, (coords[0] * LARGEUR_BLOCK, coords[1] * LARGEUR_BLOCK))
for i in range(10): for coords in snake:
s = '' ecran.blit(snakeSprite, (coords[0] * LARGEUR_BLOCK, coords[1] * LARGEUR_BLOCK))
for j in range(10):
for coords in snake: def handleControls(event, gameId):
if coords[0] == i and coords[1] == j: if event.key == pygame.K_LEFT:
s = s + '*' return update(gameId, 'left')
else: elif event.key == pygame.K_RIGHT:
s = s + ' ' return update(gameId, 'right')
elif event.key == pygame.K_UP:
return update(gameId, 'down')
elif event.key == pygame.K_DOWN:
return update(gameId, 'up')
else:
return None
def main(): def main():
#s = connect('192.168.1.14', 8090)
gameInit = newGame() gameInit = newGame()
up = update(gameInit['ID']) gameId = gameInit['ID']
up = gameInit
continuer = True
while continuer:
afficher(ecran, up['SNAKE'], up['FOOD'])
for event in pygame.event.get():
if event.type == pygame.QUIT:
continuer = False
elif event.type == pygame.KEYDOWN:
updateTmp = handleControls(event, gameId)
print(updateTmp)
if updateTmp != None and 'TYPE' in updateTmp:
if updateTmp['TYPE'] != 'error':
up = updateTmp
time.sleep(0.2)
up = update(gameId)
pygame.display.flip()
pygame.quit()
while True:
direction = raw_input() if __name__ == '__main__':
up = update(gameInit['ID'], direction) main()
draw(up)
"""