Games


When I’m building a game I like to jump right in and build the parts I don’t know much how to do.  In this time I felt like GD is a great place to make some fun images.  So today we start with a graphics port of hangman.

If men can run the world, why can’t they stop wearing neckties. How intelligent is it to start the day by tying a little noose around your neck.
-Linda Ellerbee

This code below is good for draw up to 13 misses, has code for drawing the word as well as the entire hangman graphic seen below

hangman

This is easily adaptable to several types of data sources including MYSQL, SQL Server, and any SQL or flat-file like csv. In the second part I will hook it up to a worldlist stored in Mysql.

0){
		//draw head
		imageellipse($im,$left_point,$bottom_point,$head_size,$head_size,$text_color);
	}
	if($misses>1){
		//draw body
		imageline($im,$left_point,$neck_point,$left_point,$leg_point,$text_color);
	}
	if($misses>2){
		//left leg
		imageline($im,$left_point,$leg_point,$left_legX,$left_legY,$text_color);
	}
	if($misses>3){
		//right leg
		imageline($im,$left_point,$leg_point,$right_legX,$right_legY,$text_color);
	}
	if($misses>4){
		//left arm
		imageline($im,$left_point,$neck_point,$left_arm,$neck_point,$text_color);
	}
	if($misses>5){
		//right arm
		imageline($im,$left_point,$neck_point,$right_arm,$neck_point,$text_color);
	}
	if($misses>6){
		//left leg
		imageellipse($im,$left_legX,$left_legY,$foot_size,$foot_size,$text_color);
	}
	if($misses>7){
		//right leg
		imageellipse($im,$right_legX,$right_legY,$foot_size,$foot_size,$text_color);
	}
	if($misses>8){
		//left hand
		imageellipse($im,$left_arm,$neck_point,$hand_size,$hand_size,$text_color);
	}
	if($misses>9){
		//right hand
		imageellipse($im,$right_arm,$neck_point,$hand_size,$hand_size,$text_color);
	}
	if($misses>10){
		//right eye
		imageellipse($im,$right_eyeX,$eyeY,$eye_width,$eye_height,$text_color);
	}
	if($misses>11){
		//left eye
		imageellipse($im,$left_eyeX,$eyeY,$eye_width,$eye_height,$text_color);
	}
	if($misses>12){
		//mouth
		imagearc($im,$left_point,$mouth_center,$mouth_width, $mouth_height, 160, 20,$text_color);
	}
	if($misses>13){
		//double x
		imageline($im,$right_eyeX-3,$eyeY-3,$right_eyeX+3,$eyeY+3,$text_color);
		imageline($im,$left_eyeX-3,$eyeY-3,$left_eyeX+3,$eyeY+3,$text_color);
		imageline($im,$right_eyeX+3,$eyeY-3,$right_eyeX-3,$eyeY+3,$text_color);
		imageline($im,$left_eyeX+3,$eyeY-3,$left_eyeX-3,$eyeY+3,$text_color);
	}

	return $im;
}

$im = drawTxt($im,$word,array('a'));
$im = drawHangMan($im,13);
header ("Content-type: image/png");
ImagePng ($im);
?>
Our Sponsors

Rock Paper Scissors is a simple script (no mysql needed) written in PHP for your downloading and playing pleasure.  This is mainly just an illustration of a simple login and execution.    It uses a text file named users.txt that contains  only user logins and scores for a simple session based tracking. No passwords needed.

“R~P~S is a slick improvement over the old R~R~R games we
used to play back in the Pleistocene.”
~ Grognar the Barbarian

$v){
		$user_parts = explode(':',$v);
		$users[$user_parts[0]] = $user_parts[1]; // users[username] = score
	}
	if(array_key_exists($login_user,$users)){
		session_start();
		$_SESSION['score'] = $users[$login_user];
		$_SESSION['user'] = $login_user;
	}
}
if($_POST['action'] == 'judge' && isset($_POST['choice'])){
	$winner=false;
	$choices = array('rock','paper','scissors');
	$player_choice = $_POST['choice'];
	if(in_array($player_choice,$choices)){
		session_start();
		$comp_choice = $choices[array_rand($choices)];
		echo 'Computer Picked :'.$comp_choice.'';
		echo 'You Picked :'.$player_choice.'';
		if($comp_choice == 'rock' && $player_choice == 'paper'){
			$winner = true;
		}elseif($comp_choice == 'scissors' && $player_choice == 'rock'){
			$winner = true;
		}elseif($comp_choice == 'paper' && $player_choice == 'scissors'){
			$winner = true;
		}

		if($winner == true){
			$score_t = $_SESSION['score'] + 1;
			$_SESSION['score'] = $score_t;
			echo 'You won. Your score is now:'.$_SESSION['score'];
		}else{
			$score_t = $_SESSION['score'] -1;
			$_SESSION['score'] = $score_t;
			echo 'You lost. Your score is now:'.$_SESSION['score'];
		}
	}
	$auth_arr = file(AUTHFILE);
	foreach($auth_arr as $k=>$v){
		$user_parts = explode(':',$v);
		$users[$user_parts[0]] = $user_parts[1]; // users[username] = score
	}
	$users[$_SESSION['user']] = $_SESSION['score'];
	foreach($users as $k=>$v){
		$temp_array = array($k,$v);
		$user_str = implode(':',$temp_array);
		$authstr .= $user_str . "\n";
	}
	file_put_contents(AUTHFILE,$authstr);
}
if($_POST['action'] == 'logout'){
	session_start();
	@session_destroy();
}

?>
Rock Paper Scissors
user

There will also be a demo and download version coming shortly so be sure to check back soon.

“Now, what beat rock ag’in?”
~ BillyBob

« Previous Page