Teacher’s Challenge: Chasing A.I.

Image representing the Game Window

Teacher challenged us to create a basic A.I. that would guard a specific place and chase the player if he entered a specific area. If the player left the area of chase, the A.I. would go back to guarding position.

Program created using Python and Pygame.

Link to download the demo: https://mega.nz/#F!aQ9XjQ6I
I had troubles building the executable due to Python/Pygame versions, so in order to run this one you would need both Python 3.2 and Pygame installed.
I will upload a video demo in a couple of days.

 

[code lang=”py”]

import pygame, sys
from pygame.locals import *
pygame.init()
pygame.mixer.init()
pygame.mixer.set_num_channels(4)

FPS = 24
fpsClock = pygame.time.Clock()

pywindow = pygame.display.set_mode((600, 600))
pygame.display.set_caption("Trabalho IA")
WHITE = (255, 255, 255)
YELLOW = (255, 215, 5)
RED = (230, 0, 0)

soundChaseOff = pygame.mixer.Sound("no_chase.wav")
soundChaseOn = pygame.mixer.Sound("chase.wav")

redPlayerImg = pygame.image.load("icone.png")
greenIaImg = pygame.image.load("icone-ia.png")
alertImg = pygame.image.load("alert.png")

redPlayerX = 10
redPlayerY = 10
redPlayerSpeed = 10

greenIaX = 300
greenIaY = 500
greenIaSpeed = 5
movingRight = True

isPatrol = True
isChase = False

pygame.key.set_repeat(10, 10)

while True: #main game loop, should be self-explanatory
pywindow.fill(WHITE)

for event in pygame.event.get():
if event.type == pygame.KEYDOWN:
if event.key == pygame.K_UP and redPlayerY >= 20:
redPlayerY -= redPlayerSpeed
if event.key == pygame.K_DOWN and redPlayerY <= 510:
redPlayerY += redPlayerSpeed
if event.key == pygame.K_RIGHT and redPlayerX <= 510:
redPlayerX += redPlayerSpeed
if event.key == pygame.K_LEFT and redPlayerX >= 20:
redPlayerX -= redPlayerSpeed
if event.type == QUIT:
pygame.quit()
sys.exit()

# patrol
if isPatrol == True:
if greenIaX <= 530 and movingRight==True:
greenIaX += greenIaSpeed
if greenIaX == 530:
movingRight = False
elif greenIaX >= 10 and movingRight==False:
greenIaX -= greenIaSpeed
if greenIaX == 10:
movingRight = True

# extra stuff to happen while in diferent states
if isChase:
pywindow.fill(YELLOW)
soundChaseOff.stop()
soundChaseOn.play(loops = -1)

else:
pywindow.fill(WHITE)
soundChaseOn.fadeout(500)
soundChaseOff.play(loops = -1)

# stops patrol and alerts AI to start chase if player enters 200 area between 2 axis. Alert stops and chase is interrupted if player leaves area
if (redPlayerY <= (greenIaY + 200) and redPlayerY > greenIaY) and (redPlayerX <= (greenIaX + 200) and redPlayerX > greenIaX): #top left
isPatrol = False
isChase = True

elif (redPlayerY <= (greenIaY + 200) and redPlayerY >= greenIaY) and (redPlayerX >= (greenIaX – 200) and redPlayerX <= greenIaX): #top right
isPatrol = False
isChase = True

elif (redPlayerY >= (greenIaY – 200) and redPlayerY <= greenIaY) and (redPlayerX <= (greenIaX + 200) and redPlayerX >= greenIaX): #bot left
isPatrol = False
isChase = True

elif (redPlayerY >= (greenIaY – 200) and redPlayerY < greenIaY) and (redPlayerX >= (greenIaX – 200) and redPlayerX < greenIaX): #bot right
isPatrol = False
isChase = True

else:
isChase = False

# chase movement
if (redPlayerY <= (greenIaY + 200) and redPlayerY > greenIaY) and isChase == True:
greenIaY += greenIaSpeed

if (redPlayerY >= (greenIaY – 200) and redPlayerY < greenIaY) and isChase == True:
greenIaY -= greenIaSpeed

if (redPlayerX <= (greenIaX + 200) and redPlayerX > greenIaX) and isChase == True:
greenIaX += greenIaSpeed

if (redPlayerX >= (greenIaX – 200) and redPlayerX < greenIaX) and isChase == True:
greenIaX -= greenIaSpeed

# moves ia back and starts patrol
if isChase == False and isPatrol == False:
if greenIaY < 500:
greenIaY += greenIaSpeed
if greenIaY > 500:
greenIaY -= greenIaSpeed
if greenIaX > 300:
greenIaX -= greenIaSpeed
if greenIaX < 300:
greenIaX += greenIaSpeed
if greenIaX == 300 and greenIaY == 500:
isPatrol = True

pywindow.blit(redPlayerImg, (redPlayerX, redPlayerY))
pywindow.blit(greenIaImg, (greenIaX, greenIaY))
pygame.display.update()
fpsClock.tick(FPS)

[/code]

Leave a Reply

Your email address will not be published. Required fields are marked *