# -*- coding: utf-8 -*- import socket, json import pygame RESP_BUFFER_LENGTH = 1024 ip_adress="127.0.0.1" port=8080 LARGEUR_BLOCK = 10 NB_BLOCKS = 30 LARGEUR_ECRAN = LARGEUR_BLOCK * NB_BLOCKS def connect(ip_adress, port): 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) s.send('#EOF\n') 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) def draw(up): snake = [] for coords in up['SNAKE']: if coords not in snake: snake.append(coords) 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 + ' ' 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()