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 computers paddle movieclip is called paddle2
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.
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
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???
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.
//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<
//Paddle 1 movements
if (Key.isDown(81) and _root.paddle1._y>2) {
_root.paddle1._y -= 30;
}
if (Key.isDown(65) and _root.paddle1._y<620) {
_root.paddle1._y += 30;
}
//Power hit
if (Key.isDown(Key.SPACE)){
_root.paddle1.gotoAndPlay(2);
//_root.audio.gotoAndPlay("metal_hit");
}
//Paddle 2 movements
if (Key.isDown(Key.UP) and _root.paddle2._y>5) {
_root.paddle2._y -= 30;
}
if (Key.isDown(Key.DOWN) and _root.paddle2._y<620) {
_root.paddle2._y += 30;
}
//Power hit
if (Key.isDown(Key.LEFT)){
_root.paddle2.gotoAndPlay(2);
}
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