123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125 |
- import bluetooth
- from robot import Robot
- from tag import TagClient
- from random import randint, random
- import signal, sys
- import math
- import time
- serverMACAddress = '00:16:53:18:2E:17'
- port = 1
- s = bluetooth.BluetoothSocket(bluetooth.RFCOMM)
- s.connect((serverMACAddress, port))
- #while 1:
- # text = bytes([1])
- # if text == "quit":
- # break
- # s.send(bytes([1]))
- #sock.close()
- rid = 2
- run = False
- board = None
- tagplayer = -1
- client = TagClient('localhost', 10318)
- client.info(rid, 8, 8, 4, 4)
- px, py, course = None, None, None
- # Sinal de ctrl-c
- def signal_handler(signal, frame):
- client.stop()
- sock.close()
- sys.exit(0)
- signal.signal(signal.SIGINT, signal_handler)
- players = {}
- while not client.quit:
- time.sleep(0.1)
- # nao recebeu informacoes do campo
- if client.getBoard() is None:
- continue
- elif px is None:
- board = client.getBoard()
- px, py = board[0]*random(), board[1]*random()
- course = math.pi*2.0*random()
- # jogo esta pausado
- if client.getStatus() is False:
- players = {}
- continue
- # nao recebeu informacoes do pegador
- if client.getTagInfo() is None:
- continue
- # verifica se ha posicoes novas
- newposs = client.getPosition()
- for info in newposs:
- posx, posy, courser, idd, xsize, ysize, xtag, ytag = info
- if idd != rid:
- players[idd] = [posx, posy]
- else:
- px, py, course = posx, posy, courser
- tag, interval = client.getTagInfo()
- pos = [players[p] for p in players]
- if time.time() <= interval:
- # comportamento de fuga
- if tag in players and tag != rid:
- # foge se nao for pegador
- x1 = px-players[tag][0]
- y1 = py-players[tag][1]
- x2 = math.cos(course)
- y2 = math.sin(course)
- dot = x1*x2 + y1*y2 # dot product between [x1, y1] and [x2, y2]
- det = x1*y2 - y1*x2 # determinant
- angle = math.atan2(det, dot)
- if angle < 0.3 or angle > math.pi-0.3:
- s.send(bytes([1]))
- elif angle > math.pi:
- s.send(bytes([3]))
- else:
- s.send(bytes([4]))
- else:
- if len(pos) >= 1 and tag == rid:
- x1 = px-pos[0][0]
- y1 = py-pos[0][1]
- x2 = math.cos(course)
- y2 = math.sin(course)
- dot = x1*x2 + y1*y2 # dot product between [x1, y1] and [x2, y2]
- det = x1*y2 - y1*x2 # determinant
- angle = math.atan2(det, dot)
- if (angle < 0.3) or (angle > math.pi-0.3):
- s.send(bytes([1]))
- elif angle > math.pi:
- s.send(bytes([3]))
- else:
- s.send(bytes([4]))
- if tag != rid:
- x1 = px-players[tag][0]
- y1 = py-players[tag][1]
- x2 = math.cos(course)
- y2 = math.sin(course)
- dot = x1*x2 + y1*y2 # dot product between [x1, y1] and [x2, y2]
- det = x1*y2 - y1*x2 # determinant
- angle = math.atan2(det, dot)
- if angle < 0.3 or angle > math.pi-0.3:
- s.send(bytes([1]))
- elif angle > math.pi:
- s.send(bytes([3]))
- else:
- s.send(bytes([4]))
|