aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorulrich <ulrich.lejoyeux@gmail.com>2019-05-11 17:48:18 +0200
committerulrich <ulrich.lejoyeux@gmail.com>2019-05-11 17:48:18 +0200
commita39eedbcab2bbb8a10788792eeac4c075da867c6 (patch)
tree918100cfa139feaac1e62f90b1fbee55802b9c9d
parentc709dc64e02c9ffecfe55c9dff68e5d4c0eb45dd (diff)
le zafer lé dos
-rw-r--r--client/__pycache__/client.cpython-37.pycbin0 -> 1173 bytes
-rw-r--r--client/apple-sprite.pngbin0 -> 48335 bytes
-rw-r--r--client/c.py108
-rw-r--r--client/client.py35
4 files changed, 112 insertions, 31 deletions
diff --git a/client/__pycache__/client.cpython-37.pyc b/client/__pycache__/client.cpython-37.pyc
new file mode 100644
index 0000000..2d71ec5
--- /dev/null
+++ b/client/__pycache__/client.cpython-37.pyc
Binary files differ
diff --git a/client/apple-sprite.png b/client/apple-sprite.png
new file mode 100644
index 0000000..5d19922
--- /dev/null
+++ b/client/apple-sprite.png
Binary files differ
diff --git a/client/c.py b/client/c.py
new file mode 100644
index 0000000..7b7a3d6
--- /dev/null
+++ b/client/c.py
@@ -0,0 +1,108 @@
+# -*- 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() \ No newline at end of file
diff --git a/client/client.py b/client/client.py
index 814dac5..67cece7 100644
--- a/client/client.py
+++ b/client/client.py
@@ -10,7 +10,7 @@ LARGEUR_BLOCK = 10
NB_BLOCKS = 30
LARGEUR_ECRAN = LARGEUR_BLOCK * NB_BLOCKS
-def connect(ip_adress, port):
+def connect():
s = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
s.connect((ip_adress,port))
return s
@@ -37,6 +37,7 @@ def update(gameId = 1, direction = None):
received = sendData(json.dumps(data))
return json.loads(received)
+"""
def draw(up):
snake = []
for coords in up['SNAKE']:
@@ -52,41 +53,13 @@ def draw(up):
else:
s = s + ' '
-
-import pygame
-
-pygame.init()
-screen = pygame.display.set_mode((400, 300))
-done = False
-
def main():
#s = connect('192.168.1.14', 8090)
gameInit = newGame()
up = update(gameInit['ID'])
- """
while True:
direction = raw_input()
up = update(gameInit['ID'], direction)
- draw(up)"""
-
- continuer = True
- pygame.init()
- ecran = pygame.display.set_mode((LARGEUR_ECRAN,LARGEUR_ECRAN))
-
-
- while continuer:
- snakeSprite = pygame.image.load("snake-sprite.png")
- snakeSprite = snakeSprite.convert_alpha()
- pygame.draw.rect(ecran, (0,0,90), (0,0,LARGEUR_ECRAN,LARGEUR_ECRAN))
- ecran.blit(snakeSprite, (-50,0))
- for event in pygame.event.get():
- if event.type == pygame.KEYDOWN:
- continuer = False
- pygame.display.flip()
-
- pygame.quit()
-
-
-if __name__ == '__main__':
- main() \ No newline at end of file
+ draw(up)
+"""