Pong bat a.i issues

For questions about using Classic.

Post » Wed Jun 25, 2014 11:47 am

Hopefully I got the right sub forum as this is more to do with the actual python logic code and/or controlling event sheet.

While working with construct i created a variation of the 8dir behavior to allow an event sheet to control it all is working but the a.i is not perfect, at this time it is not correctly going to the last location the ball goes off screen, then resuming its normal behavior once it has deflected the ball, rather maintaining its normal behavior, in addition a minor flaw i suspect is that the movement is not taking acceleration into account.

I have two bats, one being the player on the left of the layout using a an 8 direction behavior for movement, the other being a a.i bat using a modded 8 direction movement that allows me to send input via the event sheet(so both are using the same movement setup, just ones using a pretend keyboard).

They have a max speed of 500, a acceleration of 600, a deceleration of 600.
The ball uses the stand ball behavior with a speed of 300 and a max speed of 1000.

The event sheet responsible for the a.i has the following(python script included)

LIB - On function "AIStop" AIBat : Move stop

LIB - On function "AIUp" AIBat : Move Up

LIB - On function "AIDown" AIBat : Move Down
Code: Select all
#This script controls the core logic
#For the Bat AI
import random;




def AI_AdjustToAngle():
   global AI_AdjustRate;
   global AI_BallAngle;
   if ((AI_BallAngle > 90) and (AI_BallAngle < 180)):
      return AI_AdjustRate;
      return -AI_AdjustRate;
   return AI_AdjustRate;

#Track last area ball whent out of.
def SetBallParkArea(val):
   global AI_LastOutY;

#On/Off switch for player 2 a.i
def EnableAI(arg1):

#Give a bit of randomness to the a.i reaction distance.
def AI_GetRandomiseMidFeild():
   #return random.randint(0, 5);
   return 0;

#Skill related, tells our a.i how much
#to compensate for the ball.
#Take into account the balls angle here
def AI_GetCompensation():
   global AI_BallAngle;
   global AI_NoCompensate;
   global AI_MaxRandom;
   global AI_MinRandom;
   if (AI_NoCompensate == 1):
      return 0;
   if ((AI_BallAngle > 90) and (AI_BallAngle < 180)):
      return random.randint(AI_MinRandom, AI_MaxRandom);
      return -random.randint(AI_MinRandom, AI_MaxRandom);
   return random.randint(AI_MinRandom, AI_MaxRandom);

#A.I think loop
def AI_Think():
   if (AI_Enable == 0):
   global AI_BallAngle;
   global AI_TargetY;
   global AI_NoCompensate;
   global AI_ReactionDistance;
   global AI_LastOutY;
   #Its on the players side of the playing feild, wait for it to come to ours.
   if (Ball.X < (320-AI_GetRandomiseMidFeild())):
      LIB.Call("AIStop", 0);
   #For now disable A.I compensation
   #Always goto the last spot the ball whent off screen at for the a.i bat.
   if (AI_LastOutY != 0):

   AI_TargetY += AI_GetCompensation();

   #Adjust in relation to ball angle.
   #AI_TargetY += AI_AdjustToAngle()+AI_GetCompensation();
   #Adjust our position to line up with the ball.
   if (abs(AI_TargetY-AIBat.Y) > AI_ReactionDistance):
      if (AI_TargetY > AIBat.Y):
         LIB.Call("AIStop", 0);
         LIB.Call("AIDown", 0);
      if (AI_TargetY < AIBat.Y):
         LIB.Call("AIStop", 0);
         LIB.Call("AIUp", 0);
   if (abs(Ball.Y-AIBat.Y) < AI_ReactionDistance):
      LIB.Call("AIStop", 0);

Ball - Is outside layout System : run script "SetBallParkArea("+str(Ball.Y)+");"

abs(AIBat.X-Ball.X) greater than 8
On collision between Ball and AIBat
System : run script "SetBallParkArea(0);"

Always(every tick) System : run script "AI_Think();"

Any help or advise would go a long way.
Posts: 15
Reputation: 384

Return to Help & Support using Construct Classic

Who is online

Users browsing this forum: No registered users and 3 guests