Skip to content

Commit

Permalink
Fantasmas perseguem o pacman
Browse files Browse the repository at this point in the history
  • Loading branch information
gogo40 committed Jun 30, 2013
1 parent bbc681d commit b5debb8
Show file tree
Hide file tree
Showing 8 changed files with 186 additions and 38 deletions.
16 changes: 12 additions & 4 deletions build/lib.linux-x86_64-2.7/pypacman/ai/ai_control.py
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@
import threading
import Queue


"""
MACROS DO SISTEMA DE AI
"""
Expand Down Expand Up @@ -74,12 +75,14 @@ def ai_control(ai):
break

if is_running:
"""
#Mensagens de depuração
evt = pygame.event.Event(pygame.USEREVENT, {"action" : PRINT_G, "value" : ai.G})
pygame.event.post(evt)
"""

#Localiza fantasmas da grade
pht = find_phantoms(ai.G)
evt = pygame.event.Event(pygame.USEREVENT, {"action" : PRINT_P, "value" : pht})
pygame.event.post(evt)

for p in pht:
#copia e marca a grade
Expand All @@ -98,7 +101,7 @@ def ai_control(ai):
"origin" : p, "dest": t})
pygame.event.post(evt)

time.sleep(0.2)
time.sleep(ai.speed)
"""
Eventos da AI, utilizado para comunicação da AI com o resto do sistema
"""
Expand All @@ -111,10 +114,15 @@ def __init__(self, tp, value = {}):
Classe de controle da AI
"""
class AIControl:
def __init__(self, G):

"""
Speed controla a velocidade dos fantasmas
"""
def __init__(self, G, speed = 0.8):
self.G = G
self.t = threading.Thread(target=ai_control, args=(self,))
self.evts = Queue.Queue()
self.speed = speed

def put(self, evt):
self.evts.put(evt)
Expand Down
56 changes: 48 additions & 8 deletions build/lib.linux-x86_64-2.7/pypacman/gui/window.py
Original file line number Diff line number Diff line change
Expand Up @@ -14,9 +14,9 @@
Implementação da janela de visualização do jogo
"""
class Window:
def __init__(self, G, ai):
def __init__(self, G, ai, delay = 100):
self.ai = ai
self.delay = 100
self.delay = delay
#dimensao da celula
self.dim_cell = 15

Expand Down Expand Up @@ -97,19 +97,53 @@ def paintGrid(self):
self.position = self.position.move(x * self.dim_cell, y * self.dim_cell)
self.screen.blit(self.fruta, self.position)

"""
Atualiza posicao do fantasma
"""
def move_phantom(self, orig, dest):
xo, yo = orig
xf, yf = dest

l = list(self.G[xo]);
l[yo] = '.';
self.G[xo] = "".join(l);

l = list(self.G[xf]);
l[yf] = '*';
self.G[xf] = "".join(l);

position = self.fantasma[(xf+yf)%4].get_rect()
positiono = position.move(yo * self.dim_cell, xo * self.dim_cell)
self.screen.blit(self.empty, positiono)

position = self.fantasma[(xf+yf)%4].get_rect()
positionf = position.move(yf * self.dim_cell, xf * self.dim_cell)
self.screen.blit(self.fantasma[(xf+yf)%4], positionf)

pygame.display.flip()


"""
Atualiza posicao do pacman
"""
def move_pac_man(self):
self.position = self.position_pac_man
position = self.position_pac_man
real_pos = self.real_pos_pac_man

xo, yo = real_pos

x = (real_pos[0] + self.dr[0] + self.n_cell) % self.n_cell;
y = (real_pos[1] + self.dr[1] + self.m_cell) % self.m_cell;

"""
Verifica se pac-man ainda vive
"""
if self.G[xo][yo] != '+':
return False

if self.G[x][y] == '.' or self.G[x][y] == 'o':
self.real_pos_pac_man = [x, y]
self.screen.blit(self.empty, self.position)
self.screen.blit(self.empty, position)

l = list(self.G[real_pos[0]]);
l[real_pos[1]]='.';
Expand All @@ -122,12 +156,13 @@ def move_pac_man(self):
N = self.dim_cell * self.n_cell
M = self.dim_cell * self.m_cell

self.position_pac_man[0] = (self.position[0] + self.d[0] + M) % M
self.position_pac_man[1] = (self.position[1] + self.d[1] + N) % N
self.position_pac_man[0] = (position[0] + self.d[0] + M) % M
self.position_pac_man[1] = (position[1] + self.d[1] + N) % N

self.screen.blit(self.pacman, self.position_pac_man)

pygame.display.flip()
return True

"""
Roda renderização do jogo e captura de movimentos do jogador e da AI
Expand Down Expand Up @@ -171,6 +206,8 @@ def run(self):
print orig
print "dest:"
print dest

self.move_phantom(orig, mov)

#Trata Entrada de usuario
elif event.type == pygame.KEYDOWN:
Expand Down Expand Up @@ -211,8 +248,11 @@ def run(self):
self.pacman = pygame.transform.rotate(self.pacmano, 90)


#Move o pac man
self.move_pac_man()
#Move o pac man e verifica se ele tá vivo ainda
if not self.move_pac_man():
print "You Lose!"
is_running = False
break

#Dorme um pouco
pygame.time.delay(self.delay)
Expand Down
16 changes: 12 additions & 4 deletions build/scripts-2.7/ai_control.py
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@
import threading
import Queue


"""
MACROS DO SISTEMA DE AI
"""
Expand Down Expand Up @@ -74,12 +75,14 @@ def ai_control(ai):
break

if is_running:
"""
#Mensagens de depuração
evt = pygame.event.Event(pygame.USEREVENT, {"action" : PRINT_G, "value" : ai.G})
pygame.event.post(evt)
"""

#Localiza fantasmas da grade
pht = find_phantoms(ai.G)
evt = pygame.event.Event(pygame.USEREVENT, {"action" : PRINT_P, "value" : pht})
pygame.event.post(evt)

for p in pht:
#copia e marca a grade
Expand All @@ -98,7 +101,7 @@ def ai_control(ai):
"origin" : p, "dest": t})
pygame.event.post(evt)

time.sleep(0.2)
time.sleep(ai.speed)
"""
Eventos da AI, utilizado para comunicação da AI com o resto do sistema
"""
Expand All @@ -111,10 +114,15 @@ def __init__(self, tp, value = {}):
Classe de controle da AI
"""
class AIControl:
def __init__(self, G):

"""
Speed controla a velocidade dos fantasmas
"""
def __init__(self, G, speed = 0.8):
self.G = G
self.t = threading.Thread(target=ai_control, args=(self,))
self.evts = Queue.Queue()
self.speed = speed

def put(self, evt):
self.evts.put(evt)
Expand Down
56 changes: 48 additions & 8 deletions build/scripts-2.7/window.py
Original file line number Diff line number Diff line change
Expand Up @@ -14,9 +14,9 @@
Implementação da janela de visualização do jogo
"""
class Window:
def __init__(self, G, ai):
def __init__(self, G, ai, delay = 100):
self.ai = ai
self.delay = 100
self.delay = delay
#dimensao da celula
self.dim_cell = 15

Expand Down Expand Up @@ -97,19 +97,53 @@ def paintGrid(self):
self.position = self.position.move(x * self.dim_cell, y * self.dim_cell)
self.screen.blit(self.fruta, self.position)

"""
Atualiza posicao do fantasma
"""
def move_phantom(self, orig, dest):
xo, yo = orig
xf, yf = dest

l = list(self.G[xo]);
l[yo] = '.';
self.G[xo] = "".join(l);

l = list(self.G[xf]);
l[yf] = '*';
self.G[xf] = "".join(l);

position = self.fantasma[(xf+yf)%4].get_rect()
positiono = position.move(yo * self.dim_cell, xo * self.dim_cell)
self.screen.blit(self.empty, positiono)

position = self.fantasma[(xf+yf)%4].get_rect()
positionf = position.move(yf * self.dim_cell, xf * self.dim_cell)
self.screen.blit(self.fantasma[(xf+yf)%4], positionf)

pygame.display.flip()


"""
Atualiza posicao do pacman
"""
def move_pac_man(self):
self.position = self.position_pac_man
position = self.position_pac_man
real_pos = self.real_pos_pac_man

xo, yo = real_pos

x = (real_pos[0] + self.dr[0] + self.n_cell) % self.n_cell;
y = (real_pos[1] + self.dr[1] + self.m_cell) % self.m_cell;

"""
Verifica se pac-man ainda vive
"""
if self.G[xo][yo] != '+':
return False

if self.G[x][y] == '.' or self.G[x][y] == 'o':
self.real_pos_pac_man = [x, y]
self.screen.blit(self.empty, self.position)
self.screen.blit(self.empty, position)

l = list(self.G[real_pos[0]]);
l[real_pos[1]]='.';
Expand All @@ -122,12 +156,13 @@ def move_pac_man(self):
N = self.dim_cell * self.n_cell
M = self.dim_cell * self.m_cell

self.position_pac_man[0] = (self.position[0] + self.d[0] + M) % M
self.position_pac_man[1] = (self.position[1] + self.d[1] + N) % N
self.position_pac_man[0] = (position[0] + self.d[0] + M) % M
self.position_pac_man[1] = (position[1] + self.d[1] + N) % N

self.screen.blit(self.pacman, self.position_pac_man)

pygame.display.flip()
return True

"""
Roda renderização do jogo e captura de movimentos do jogador e da AI
Expand Down Expand Up @@ -171,6 +206,8 @@ def run(self):
print orig
print "dest:"
print dest

self.move_phantom(orig, mov)

#Trata Entrada de usuario
elif event.type == pygame.KEYDOWN:
Expand Down Expand Up @@ -211,8 +248,11 @@ def run(self):
self.pacman = pygame.transform.rotate(self.pacmano, 90)


#Move o pac man
self.move_pac_man()
#Move o pac man e verifica se ele tá vivo ainda
if not self.move_pac_man():
print "You Lose!"
is_running = False
break

#Dorme um pouco
pygame.time.delay(self.delay)
Expand Down
Binary file modified dist/PyPacMan-0.1.5.tar.gz
Binary file not shown.
16 changes: 12 additions & 4 deletions pypacman/ai/ai_control.py
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@
import threading
import Queue


"""
MACROS DO SISTEMA DE AI
"""
Expand Down Expand Up @@ -74,12 +75,14 @@ def ai_control(ai):
break

if is_running:
"""
#Mensagens de depuração
evt = pygame.event.Event(pygame.USEREVENT, {"action" : PRINT_G, "value" : ai.G})
pygame.event.post(evt)
"""

#Localiza fantasmas da grade
pht = find_phantoms(ai.G)
evt = pygame.event.Event(pygame.USEREVENT, {"action" : PRINT_P, "value" : pht})
pygame.event.post(evt)

for p in pht:
#copia e marca a grade
Expand All @@ -98,7 +101,7 @@ def ai_control(ai):
"origin" : p, "dest": t})
pygame.event.post(evt)

time.sleep(0.2)
time.sleep(ai.speed)
"""
Eventos da AI, utilizado para comunicação da AI com o resto do sistema
"""
Expand All @@ -111,10 +114,15 @@ def __init__(self, tp, value = {}):
Classe de controle da AI
"""
class AIControl:
def __init__(self, G):

"""
Speed controla a velocidade dos fantasmas
"""
def __init__(self, G, speed = 0.8):
self.G = G
self.t = threading.Thread(target=ai_control, args=(self,))
self.evts = Queue.Queue()
self.speed = speed

def put(self, evt):
self.evts.put(evt)
Expand Down
Loading

0 comments on commit b5debb8

Please sign in to comment.