A Flash Developer Resource Site

1. ## Paddle AI: For Pong game

I have created a multiplayer version of pong now i want to make a single player version with AI for the 2nd paddle to function.

The ball movieclip is called ball
and the stage size is set to 1024*768px

How could i make the comuters paddle follow the y position of the ball so it can return the ball?

I also want to do some more advanced code so the ball will be more random when it hits the paddle. Also the computer needs some kind of random function on the y axis so player 1 has the ability to beat the computer.
(Maybe when the xspeed of the ball reaches a certain speed the computer starts being more random. DUNNO???)

If anyone can atleast help me with the code above that would be great. Because I was also interested to know how you can tweak the code above so that the compuer is more difficult to beat, so i can have a difficulty setting.

If i can atleast get some code to get me started that would be great!!!
But it would definetely be more helpful if i can get some idea of how to code the other tasks.

Cheers
Peter.D

Depending on how you set the acceleration rate (ainc) it takes a while to react, and will even overshoot the taget a bit (originally, ballX was _root._xmouse )

Hope this helps you.

This code would go on the computer controlled paddle MC

ballX = Position of ball on _root
accel = acceleration on x axis
amax = max acceleration along x axis
ainc = increment accel can inrease by to reach value of amax

Code:
```onClipEvent (load) {
ballX = 0;
accel = 5;
amax = 15;
ainc = .25;
}
onClipEvent (enterFrame) {
ballX = _root._ball._x;
myMiddle = this._x+20;
if (myMiddle<ballX) {
accel += ainc;
this._x += accel;
} else if (myMiddle>ballX) {
accel -= ainc;
this._x += accel;
}
this._x += accel;
ainc = int(math.random()*128)/1000;
}```

3. Okay, I got carried away and made two, REALLY bad computer controlled players.

They aren't constrained at all, so tend to fly off the screen.

They behave more apropriately now.

4. Just another thing, in the code u submitted is there anyneed to use code such as _root.paddle2.hitTest = more code here

in my multiplayer game i have got code such as this on the paddle2:

_root.xspeed *=-1.05
_root.yspeed = random (70)- 35;
_root.hitCount += 1;
_root.ball.gotoAndPlay("ball_hit");
}

Is there any need to continue using this code or add to this code with the single player game???

I'm not sure where exactly i am meant to put that code that u submitted in the thread.

is that just placed in the same enterFrame script as another if statement???

Could u get back to me ASAP.

Cheers
Peter.D
__________________

5. Hi.

I couldn't find the right syntax for hit-test so I used positioning instead.

I've just updated the .fla.

The code just goes straight on the movieclips "AIP" and "WOT" .

I reckon there must me a load of better .fla's in the movies section here on FK to learn from though. I was just botching.

6. Okay, here's a better version with paddles that constrain to the box.

7. That seems to work nicely, my game works on the y axis (opposite direction). Is this gonna actually make the ball hit back without a proper hitTest code used???

can i send u a skeleton of the file so u can have a look at the code i have made and the elements???

Could u get back to me ASAP.

Cheers
Peter.D

8. actually i wouldnt b able to send the file, too big for the attachments. i can send u the code that i have apllied to the game so far on that particular frame and see if there any way of re-editing it to get the paddle2 AI goin.

Heres the code:

//Default variables & settings
if (gameReset == true)
_root.xspeed = 20;//speed
_root.yspeed = 20;//speed
_root.score1 = 0;
_root.score2 = 0;
_root.hitCount = 0;
_root.xspeedHigh = 0;
_root.ballreset1 = true;
_root.ballreset2 = false;
_root.gameReset = false;

//hide cursor
Mouse.hide();
}

onClipEvent(enterFrame) {

//If global.freeze = 0 then do all of the following:
_root.freezegame();
if (_global.freeze == 0) {

//movement is controlled by this._y - or + = 30;
//constrained to y> & y<

}
}
//Power hit
if (Key.isDown(Key.SPACE)){
//_root.audio.gotoAndPlay("metal_hit");
}

}
}
//Power hit
if (Key.isDown(Key.LEFT)){
}

//Ball reset "if/else statement"
if (_root.ballreset1 == true) {
_root.ball._x = 600;
_root.ball._y = 200;
_root.xspeed = -20;//speed
_root.yspeed = -20;//speed
_root.ballreset1 = false;
}
else
if (_root.ballreset2 == true) {
_root.ball._x = 350;
_root.ball._y = 200;
_root.xspeed = 20;//speed
_root.yspeed = 20;//speed
_root.ballreset2 = false;
}

//Ball reset: FIX BALL GLITCH ("If ball goes off stage")
if (_root.ball._y<-80 or _root.ball._y>785) {
_root.ballreset1 = true;
}

//hitTest top + bottom wall
if (_root.ball.hitTest(_root.topwall) or _root.ball.hitTest(_root.bottomwall)) {
_root.yspeed = -_root.yspeed;
//_root.bg_graphics.gotoAndPlay("flash");
_root.audio.gotoAndPlay("flash_bang");
}

//hitTest scoreBoard sides
if (_root.ball.hitTest(_root.scoreBoard_side)) {
_root.xspeed = -_root.xspeed;
//_root.sou.gotoAndPlay("throw");
}

//hitTest scoreBoard Top
if (_root.ball.hitTest(_root.scoreTop)) {
_root.yspeed = -_root.yspeed;
//_root.sou.gotoAndPlay("throw");
}

//hitTest right wall
if (_root.ball.hitTest(_root.rightwall)) {
_root.score1 += 1;
_root.ballreset1 = true;
_root.audio.gotoAndPlay("blip1");
}

//hitTest left wall
if (_root.ball.hitTest(_root.leftwall)) {
_root.score2 += 1;
_root.ballreset2 = true;
_root.audio.gotoAndPlay("blip2");
}

_root.xspeed *=-1.05
_root.yspeed = random (70)- 35;
_root.hitCount += 1;
_root.ball.gotoAndPlay("ball_hit");
}

_root.xspeed *=-1.05
_root.yspeed = random (70)- 35;
_root.hitCount += 1;
_root.ball.gotoAndPlay("ball_hit");
}

//ball motion + speed
_root.ball._x += _root.xspeed;
_root.ball._y += _root.yspeed;

//Which players score = 10, Player wins!!!
//Go to that frame label
if (_root.score1 == 10) {
_root.P1_winCount += 1;
_root.gotoAndPlay("player1");
}
if (_root.score2 == 10) {
_root.P2_winCount += 1;
_root.gotoAndPlay("Player2");
}
}
}

Any suggestions would b appreciated.

cheers
Peter.D

9. To get my paddle moving I just had a small script on the onClipEvent(enterFrame){} event of my paddle clips.

The ball does all the hit testing by looking at the position of the ball within a bounding box and checking which way the y component of it's velocity is facing.

Code:
```	for (i=1; i<totalbats+1; i++) {
batname = "bat"+i;
ex = _root[batname]._x;
ey = _root[batname]._y;
eyb = ey+8;
eyt = ey-8;
if (this._y>eyb && this._y<eyb+8 && ys<0) {
if (this._x<ex+32 && this._x>ex-32) {
ys = sp;
}
}
if (this._y<eyt && this._y>eyt-8 && ys>0) {
if (this._x<ex+32 && this._x>ex-32) {
ys = -sp;
}
}
}```
I've attached the most recent version. I think it would be easy enough to simply do a find replace and turn all x's to y's and all y's to x's

10. By the way, if you've not already posted, it's probably a good idea to go post any other pong related questions over at theGames Forum.

11. Cheers, i have looked at the code, and came up with a more logical code thats easier to edit using "mathsabs"

thanks anyway

12. Cool stuff. Glad you got it sorted.

#### Posting Permissions

• You may not post new threads
• You may not post replies
• You may not post attachments
• You may not edit your posts
•

 » Home » Movies » Tutorials » Submissions » Board » Links » Reviews » Feedback » Gallery » Fonts » The Lounge » Sound Loops » Sound FX » About FK » Sitemap