le zafer lé dos

This commit is contained in:
ulrich 2019-05-11 17:48:18 +02:00
parent c709dc64e0
commit a39eedbcab
4 changed files with 112 additions and 31 deletions

Binary file not shown.

BIN
client/apple-sprite.png Normal file

Binary file not shown.

After

Width:  |  Height:  |  Size: 47 KiB

108
client/c.py Normal file
View file

@ -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()

View file

@ -10,7 +10,7 @@ LARGEUR_BLOCK = 10
NB_BLOCKS = 30 NB_BLOCKS = 30
LARGEUR_ECRAN = LARGEUR_BLOCK * NB_BLOCKS LARGEUR_ECRAN = LARGEUR_BLOCK * NB_BLOCKS
def connect(ip_adress, port): def connect():
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))
return s return s
@ -37,6 +37,7 @@ def update(gameId = 1, direction = None):
received = sendData(json.dumps(data)) received = sendData(json.dumps(data))
return json.loads(received) return json.loads(received)
"""
def draw(up): def draw(up):
snake = [] snake = []
for coords in up['SNAKE']: for coords in up['SNAKE']:
@ -52,41 +53,13 @@ def draw(up):
else: else:
s = s + ' ' s = s + ' '
import pygame
pygame.init()
screen = pygame.display.set_mode((400, 300))
done = False
def main(): def main():
#s = connect('192.168.1.14', 8090) #s = connect('192.168.1.14', 8090)
gameInit = newGame() gameInit = newGame()
up = update(gameInit['ID']) up = update(gameInit['ID'])
"""
while True: while True:
direction = raw_input() direction = raw_input()
up = update(gameInit['ID'], direction) up = update(gameInit['ID'], direction)
draw(up)""" 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()