aboutsummaryrefslogtreecommitdiff
path: root/client/client.py
diff options
context:
space:
mode:
Diffstat (limited to 'client/client.py')
-rw-r--r--client/client.py89
1 files changed, 66 insertions, 23 deletions
diff --git a/client/client.py b/client/client.py
index 67cece7..7b7a3d6 100644
--- a/client/client.py
+++ b/client/client.py
@@ -1,10 +1,12 @@
# -*- coding: utf-8 -*-
-import socket, json
+import socket, json, time
import pygame
+# ---------- SOCKETS ----------
+
RESP_BUFFER_LENGTH = 1024
-ip_adress="127.0.0.1"
-port=8080
+ip_adress="192.168.1.14"
+port=8090
LARGEUR_BLOCK = 10
NB_BLOCKS = 30
@@ -18,8 +20,8 @@ def connect():
def sendData(data):
s = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
s.connect((ip_adress,port))
- s.sendall(data)
- s.send('#EOF\n')
+ s.sendall(data.encode('utf-8'))
+ s.send('#EOF\n'.encode('utf-8'))
received = s.recv(RESP_BUFFER_LENGTH)
s.close()
return received
@@ -37,29 +39,70 @@ def update(gameId = 1, direction = None):
received = sendData(json.dumps(data))
return json.loads(received)
-"""
-def draw(up):
+# ---------- 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 up['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 i in range(10):
- s = ''
- for j in range(10):
- for coords in snake:
- if coords[0] == i and coords[1] == j:
- s = s + '*'
- else:
- s = s + ' '
+ 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():
- #s = connect('192.168.1.14', 8090)
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()
- up = update(gameInit['ID'], direction)
- draw(up)
-"""
+if __name__ == '__main__':
+ main() \ No newline at end of file