This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import pygame | |
import sys | |
import math | |
import time | |
import random | |
class Baliada(): ##clase de nodos principales | |
def __init__(self, posx,posy,life,ran): | |
self.posx = posx ##posicion en x | |
self.posy = posy ##posicion en y | |
self.life = life ##bateria u vida | |
self.ran = ran ##area de comunicacion y vista | |
self.cond = 0 ##condicion de movimiento o stop | |
self.ttl = 0 ##ttl de llegada | |
def recpos(self,mx,my): | |
self.posx = self.posx + mx | |
self.posy = self.posy + my | |
def lifes(self,gaste): | |
self.life = self.life - gaste | |
class Benemig(): | |
def __init__(self, posx,posy,life): | |
self.posx = posx ##pos x | |
self.posy = posy ##pos y | |
self.life = life # tiempo de vida | |
#self.ran = ran | |
def recpos(self,mx,my): | |
self.posx = self.posx + mx | |
self.posy = self.posy + my | |
def lifes(self,gaste): | |
self.life = self.life -gaste | |
###proceso para identificar si estan dentro del limite de rango | |
def proceso(x,y,limit,ex,ey): | |
algo = 0 | |
ancho = abs(ex-x) | |
altura = abs(ey-y) | |
distancia = (ancho**2+altura**2)**0.5 | |
if distancia < limit: | |
algo = 1 | |
return algo,distancia | |
def main(): | |
pygame.init() | |
screen = pygame.display.set_mode((600, 600)) | |
pygame.display.set_caption("Movilidad") | |
controlador = 0 | |
bolarray = [] ##lista de nuestros proximos nodos de comunicación | |
#tiempoi = time.time() | |
tota = 0 | |
tiempoi = time.time() | |
mal = Benemig(300,300,100) ##inicialisamos nodo de referencia | |
ttli = 5 ##ttl inicial | |
comp = 0 | |
actual = 5 | |
fuente = pygame.font.Font(None, 30) ###para letras | |
#texto1 = fuente.render("Texto de pruebas", 0, (255, 255, 255)) | |
while True: | |
if tota == 0: | |
total = int(math.e**controlador) | |
for i in range(total): | |
posx = random.randint(10,500) | |
posy = random.randint(10,500) | |
bolarray.append(Baliada(posx,posy,500,100)) | |
tota = 1 | |
controlador = controlador + 1 | |
screen.fill((0, 0, 0)) | |
###lo nuevo va despues de la limpiada | |
for i in range(len(bolarray)): | |
a = bolarray[i] | |
ax = a.posx | |
ay = a.posy | |
limit = a.ran | |
algo,distancia = proceso(ax,ay,limit,mal.posx,mal.posy) | |
if algo == 1 or bolarray[i].cond == 1: ##si algo == 1 es que encontro el nodo referencia o tiene condicion | |
pygame.draw.circle(screen, (255, 0, 255), (ax, ay), 10) ##para enviar mensajes | |
bolarray[i].cond = 1 | |
if bolarray[i].ttl <= 0: ##checa si es un nodo nuevo que acava de detectar nodo de referencia | |
ttl = ttli | |
actual = ttl | |
else: | |
ttl = bolarray[i].ttl - 1 ##modifica el ttl a pasar en caso de que sea nodo parado | |
actual = ttl | |
for j in range(len(bolarray)): ##verificar los vecinos si estan dentro del rango | |
if j != i : | |
algo,distancia = proceso(bolarray[i].posx, bolarray[i].posy, bolarray[i].ran, bolarray[j].posx, bolarray[j].posy) | |
if algo == 1 and bolarray[i].life - distancia > 0: ## si estan dentro del rango y si aun le alcanza | |
bolarray[j].cond = 1 ##la pila | |
bolarray[i].life = bolarray[i].life - distancia ##modificamos pila | |
bolarray[j].ttl = ttl ##guardamos el ttl para este nodo | |
#texto = fuente.render(""+str(ttl)+"", 0, (255, 255, 255)) | |
#screen.blit(texto, (bolarray[i].posx,bolarray[i].posy)) | |
else: ##en caso que no encuentre nada solo lo movemos | |
if bolarray[i].cond == 0: | |
nx = random.randint(0,1) | |
ny = random.randint(0,1) | |
if nx == 1: | |
bolarray[i].posx = bolarray[i].posx + 5 | |
else: | |
bolarray[i].posx = bolarray[i].posx - 5 | |
if ny == 1: | |
bolarray[i].posy = bolarray[i].posy + 5 | |
else: | |
bolarray[i].posy = bolarray[i].posy - 5 | |
pygame.draw.circle(screen, (255, 255, 0), (bolarray[i].posx, bolarray[i].posy), 10) | |
bolarray[i].life = bolarray[i].life -1 ##restamos vida a los nodos | |
#if bolarray[i].life == 0: | |
# bolarray.pop(i) | |
i = 0 | |
while i < len(bolarray): ##verificamos si los nodos si ya no tienen vida | |
if bolarray[i].life <= 0: | |
bolarray.pop(i) | |
i = 0 | |
else: | |
i = i + 1 | |
nx = random.randint(0,1) | |
ny = random.randint(0,1) ##para mover al boton de referencia | |
if nx == 1: | |
mal.posx = mal.posx + 2 | |
else: | |
mal.posx = mal.posx - 2 | |
if ny == 1: | |
mal.posy = mal.posy + 2 | |
else: | |
mal.posy = mal.posy - 2 | |
for i in range(len(bolarray)): ##para agregar los textos | |
if bolarray[i].ttl > 0: ##si tiene un ttl arriba de cero es que ya fueron revisados o avisados | |
texto = fuente.render(""+str(bolarray[i].ttl)+"", 0, (255, 255, 255)) | |
screen.blit(texto, (bolarray[i].posx,bolarray[i].posy)) | |
pygame.draw.circle(screen, (0, 255, 0), (mal.posx, mal.posy), 10) | |
pygame.display.flip() | |
tiempof = time.time() | |
tiempo = tiempof - tiempoi | |
total = int(math.e**controlador) | |
#print "simulando" | |
if tiempo > total: ##para agregar mas nodos y verificar el ttl | |
print "agrego nodos" | |
if actual > 0: ##si es mayor a cero el que quedo dando vueltas | |
ttli = ttli - 1 | |
if actual <= 0: ##si es menor o igual | |
ttli = ttli + 1 | |
# tiempoi = time.time() | |
tota = 0 | |
print "ttl actual: ",ttli | |
print "ttl sobrante:",actual | |
pygame.time.delay(30) | |
main() |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment