dcsimg
A Flash Developer Resource Site

Results 1 to 12 of 12

Thread: Paddle AI: For Pong game

  1. #1
    Junior Member
    Join Date
    Oct 2004
    Location
    -
    Posts
    27

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

    Cheers
    Peter.D

  2. #2
    Indefatigable JohnnySix's Avatar
    Join Date
    Nov 2000
    Location
    England
    Posts
    1,175
    I have something similar for a menu I had.

    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;
    }
    Last edited by JohnnySix; 10-26-2004 at 09:20 AM.
    . . . .

  3. #3
    Indefatigable JohnnySix's Avatar
    Join Date
    Nov 2000
    Location
    England
    Posts
    1,175
    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.
    Attached Files Attached Files
    Last edited by JohnnySix; 10-26-2004 at 09:26 AM.
    . . . .

  4. #4
    Junior Member
    Join Date
    Oct 2004
    Location
    -
    Posts
    27
    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:

    //paddle2 hit ball
    if (_root.ball.hitTest(_root.paddle2)) {
    _root.xspeed *=-1.05
    _root.yspeed = random (70)- 35;
    _root.hitCount += 1;
    _root.ball.gotoAndPlay("ball_hit");
    _root.paddle2.p2_paddleLoaded.gotoAndPlay("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. #5
    Indefatigable JohnnySix's Avatar
    Join Date
    Nov 2000
    Location
    England
    Posts
    1,175
    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" .

    You can add additional code to onClipEvent(load) and OnClipEvent(enterFrame).

    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. #6
    Indefatigable JohnnySix's Avatar
    Join Date
    Nov 2000
    Location
    England
    Posts
    1,175
    Okay, here's a better version with paddles that constrain to the box.
    Attached Files Attached Files
    . . . .

  7. #7
    Junior Member
    Join Date
    Oct 2004
    Location
    -
    Posts
    27
    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. #8
    Junior Member
    Join Date
    Oct 2004
    Location
    -
    Posts
    27
    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:

    onClipEvent(load) {

    //Default variables & settings
    if (gameReset == true)
    _root.paddle._y = 307.5;
    _root.paddle2._y = 307.5;
    _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<

    //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);
    }

    //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
    //Add +1 to score1
    if (_root.ball.hitTest(_root.rightwall)) {
    _root.score1 += 1;
    _root.ballreset1 = true;
    _root.audio.gotoAndPlay("blip1");
    }

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

    //paddle1 hit ball
    if (_root.ball.hitTest(_root.paddle1)) {
    _root.xspeed *=-1.05
    _root.yspeed = random (70)- 35;
    _root.hitCount += 1;
    _root.ball.gotoAndPlay("ball_hit");
    //Which ever paddle is loaded will gotoAndPlay "hit"
    _root.paddle1.p1_paddleLoaded.gotoAndPlay("hit");
    }

    //paddle2 hit ball
    if (_root.ball.hitTest(_root.paddle2)) {
    _root.xspeed *=-1.05
    _root.yspeed = random (70)- 35;
    _root.hitCount += 1;
    _root.ball.gotoAndPlay("ball_hit");
    _root.paddle2.p2_paddleLoaded.gotoAndPlay("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. #9
    Indefatigable JohnnySix's Avatar
    Join Date
    Nov 2000
    Location
    England
    Posts
    1,175
    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
    Attached Files Attached Files
    . . . .

  10. #10
    Indefatigable JohnnySix's Avatar
    Join Date
    Nov 2000
    Location
    England
    Posts
    1,175
    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. #11
    Junior Member
    Join Date
    Oct 2004
    Location
    -
    Posts
    27
    Cheers, i have looked at the code, and came up with a more logical code thats easier to edit using "mathsabs"

    thanks anyway

  12. #12
    Indefatigable JohnnySix's Avatar
    Join Date
    Nov 2000
    Location
    England
    Posts
    1,175
    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
  •  




Click Here to Expand Forum to Full Width

HTML5 Development Center