A Flash Developer Resource Site

# Thread: Pong AI problems....

1. I cant beleive I am having trouble with one of the esiest games to program, but I am. I am having trouble getting the computer paddle to work properly. Its hard to explain. Just go play it and youll know. Its only 28kb. Its here:

http://www.panzerware.netfirms.com/flash/pong.html

Please give me some help with the AI.

2. The easiest way to develop a pong AI (not terribly bright though) is to have the AI set its "target" vertical position equal to the vertical position of the ball. You can then have it accelerate towards the target position, and vary variables like friction (deceleration) and rate of acceleration to give it a more realistic appearance, and will get rid of the jittering your current AI is exhibiting. It will not exhibit any predictive behaviour though - that is a completely different (and more complex) problem.

I built a really simple version of pong for my site:
http://www.zeroera.com

it's in: FlashOS: System: Desktop Settings:
Choose "Pong? Active desktop" then hit the "Set Desktop" button and it will play pong on the destop.

Let me know if you have any questions about the above, I would be happy to elaborate.

Good luck!
ZE.

3. I understand what you are saying. I had not planned on creating any forces to act on the paddles. I was mainly just wantnig to recreate the classic pong.

my paddle code is:

onClipEvent(enterFrame){
dist = _root.ball._y - _y;

if(dist > _height){
_y += 10;
}
if(dist < _height){
_y -= 10;
}
}

Ive tried different methods but this has worked best. I can not seem to get rid of the jerkyness.

4. Simply try this minor modification:

Code:
```onClipEvent(enterFrame){
dist = _root.ball._y - _y;

if(dist > _height/1.5){
_y += 10;
}
if(dist < -_height/1.5){
_y -= 10;
}
}```
this assumes that the paddle's registration point is in the center of the paddle.

Should fix it.

Cheers,
ZE.

5. I tried this, but it is impossible to beat the CPU. The ball moved faster then the CPU, but still it was quite easy for the CPU to get it each time. Isnt there a way to make it react like a real human person?

6. This is what I have created:

As you can see, the AI is far from perfect. Im stuck with it, I dont know how to modify it beter. Its harder then I though. Please help me!

7. Well, there are a number of ways you can implement this:

1) Each time the ball hits a paddle, have it accelerate a tiny amount (speed *= 1.01. This way it becomes more difficult for the AI to keep up with it, and gives the player an advantage (because he can presuppose the location of the ball).

2) Build a "stupid" factor into your AI. A basic version of this would look something like:

onClipEvent(enterFrame){
myOffset += (random(3)-1)/10;
Math.min(myOffset,Math.max(-_height-4,myOffset));
dist = _root.ball._y - _y + myOffset;

if(dist > _height){
_y += 10;
}
if(dist < _height){
_y -= 10;
}
}

Make sure to reset myOffset to 0 each time a point is scored though.

Hope it helps,
ZE.

8. zeroEra, your my hero! Finally my game becomes a bit cooler! Thanx!

9. Baukereg,

I've always wanted to be someone's hero!

I'd love to see it - interested in seeing how that code performs.

Cheers,
ZE.

PS. Sign my guestbook if you have a few minutes to kill.

10. Maybe someone can help me with my next problem. Id like to create a 2 player mode. I use the usual Key.isDown code for the control of player one. Player 2 can be controled by using the Z and X keys, but these keys wont work with Key.isDown. How can I fix this?

11. Umm... yes, they do.

if (Key.isDown(Key.getCode("z"))) {
trace("z-down");
}

Did you remember to use Key.getCode()?

Cheers,
ZE.

12. getCode? I have never used that before! This is ****ing cool man!!!! Yippie! Yeah!!!

zeroEra, your my man!

#### 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

Click Here to Expand Forum to Full Width