The Server Pages

»

TheServerPages Scripts

class Procastinator

Author: Wojjie     Posted: 2005-05-20     Viewed: 6,828

Code:
// Class found at: http://www.theserverpages.com/scripts/
// Please leave this comment intact when using this code!
class Procastinator {
        var $courtheight, $courtwidth;
        var $score1, $score2;
        var $ballx, $bally;
        var $ball_in_court;
        var $speedy, $speedx, $startspeed;
        var $lastscorer;
        var $paddley1, $paddley2, $paddleheight;

        function setupGame() {
                $this->score1=0;
                $this->score2=0;
                $this->lastscorer=1; //who scored last will get ball
                $this->courtwidth=100;
                $this->courtheight=100;
                $this->startspeed=1;
                $this->paddleheight=10;
        }


        function setupRound() {
                if ($this->lastscorer==1) {
                        $this->speedx=$this->startspeed;
                } else {
                        $this->speedx=-$this->startspeed;
                }
                $this->speedy=$this->startspeed;

                $this->ballx=$this->courtheight/2;
                $this->bally=$this->courtwidth/4;
                $this->ball_in_court=true;
        }

        function moveBall() {
                $this->ballx+=$this->speedx;
                $this->bally+=$this->speedy;
        }

        function checkWallCollisions() {
                if ($this->bally<=0) {
                        //top wall collision
                        $this->speedy=-$this->speedy;
			//if you think about it, its obvious why
                        $this->bally=-$this->bally; 
                } else if ($this->bally>$this->courtheight) {
                        //bottom wall collision
                        $this->speedy=-$this->speedy;
			//similar as above reasoning
                        $this->bally=2*$this->courtheight-$this->bally; 
                }
        }

        function checkPaddleCollisions() {
                $paddleCollision=false;
                if ($this->ballx<=0) {
                        //check where the ball was when at x=0
			$colideoff=($this->ballx/$this->speedx);
                        $colidey=$this->bally+$colideoff*$this->speedy;
                        $pady=$colidey-$this->paddley1;
                        if ($pady>=0 && $pady<=$this->paddleheight) {
                                $paddleCollision=true;
                                $this->ballx=-$this->ballx;
                        }
                } else if ($this->ballx>=$this->courtwidth) {
                        //check where the ball was when at x=courtwidth
			$colideoff=(($this->courtwidth-$this->ballx)/$this->speedx);
                        $colidey=$this->bally+$colideoff*$this->speedy;
                        $pady=$colidey-$this->paddley2;
                        if ($pady>=0 && $pady<=$this->paddleheight) {
                                $paddleCollision=true;
                                $this->ballx=2*$this->courtwidth-$this->ballx;
                        }
                }

                if ($paddleCollision) {
                        $this->speedx=-$this->speedx;
                }
        }

        function checkScored() {
                if ($this->ballx<0) {
                        $this->score2++;
                        $this->lastscorer=2;
                        $this->ball_in_court=false;
                } else if ($this->ballx>$this->courtwidth) {
                        $this->score1++;
                        $this->lastscorer=1;
                        $this->ball_in_court=false;
                }
        }

        function movePaddle($player) {
                if ($player==1) {
                        $curPaddle=&$this->paddley1;
                } else {
                        $curPaddle=&$this->paddley2;
                }

                $curPaddle=$this->bally-($this->paddleheight/2);
                if ($curPaddle<0) $curPaddle=0;
                if ($curPaddle+$this->paddleheight>$this->courtheight) 
			$curPaddle=$this->courtheight-$this->paddleheight;
        }

        function start($patience=-1) {
                $sleep_int=50000;

                if ($patience<0) $patience=rand(1,100);

                $this->setupGame();
                $j=0;
                for ($i=0; $i<$patience; $i++) {
                        $this->setupRound();

                        while ($this->ball_in_court && $i<$patience) {
                                $this->moveBall();
                                if ($this->speedx<0) {
                                        $this->movePaddle(1);
                                } else {
                                        $this->movePaddle(2);
                                }

                                $this->checkWallCollisions();
                                $this->checkPaddleCollisions();
                                $this->checkScored();

                                usleep($sleep_int);

                                $j++;
                                if ($j>10) {
                                        $i++;
                                        $j=0;
                			//uncomment the following line
					// if you want to see 'soomething'
                                        //print_r($this);
                                }
                        }
                }
                return array($this->score1, $this->score2);
        }
}


Example Usage:
$p=new Procastinator();
$p->start();


Example Output:
[NONE, JUST RANDOM DELAY]

CAUTION:

This is a GAG script, and should not be used in a real working environment. For those that are unable to figure out what this script does scroll down (but you really should try to figure it out yourself for a better comedic effect)



.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

It plays the classic game Pong

Comments

Copyright © 2004-2015: TheServerPages.com