Jump to content

Coding feedable pets.


ohstereo

Recommended Posts

Alright, all day I've been looking at this tutorial: http://kofk.de/?p=howto

But this guy doesn't give me any help at all on what I should be doing..

I know he's telling me to read those articles, but that won't help. =/

 

So far, I have my image: http://prettyknives.net/pf/bg.jpg

My script: http://prettyknives.net/pf/sample.php

 

CODE:

<?php
// Begin Constant definitions
define('BACKGROUND', 'bg.jpg');
// End definitions

$image = imagecreatefromjpeg(BACKGROUND);

$hp = calculateHitpoints();

drawHitpointBar($image, $hp);

writeName($image, "Pet Name");	

// Output
header("Content-Type: image/jpeg");	
imagejpeg($image, "", 95);	
?>

<?php

function calculateHitpoints(){

$feed = isset($_GET['feed']);
}

$hp = loadFromDatafile(DATAFILE);

$hp -= (int)((time() - filemtime(DATAFILE)) / DRAIN);

// Insert these somewhere in the constants definition block
define('DATAFILE', "petdata.dat");
define('DRAIN', 300);
define('FOODVALUE', 10); 
define('MAXHP', 300); 

function calculateHitpoints(){

$feed = isset($_GET['feed']);

$hp = loadFromDatafile(DATAFILE); 

$hp -= (int)((time() - filemtime(DATAFILE)) / DRAIN);

if ($feed) 
	$hp += FOODVALUE;

if ($hp > MAXHP) 
	$hp = MAXHP;

if ($hp < 1) 
	$hp = 1;

if ($feed)
	saveToDatafile($hp, DATAFILE); 

return $hp;
}

// Insert these somewhere in the constants definition block
define('BAR_X', 10);
define('BAR_Y', 200);
define('BAR_MAXWIDTH', 400);
define('BAR_HEIGHT', 6);
define('BAR_R', 0xdd);
define('BAR_G', 0x5a);
define('BAR_B', 0x7c);

function paintHPBar($image, $hp) {

$width = ceil(BAR_MAXWIDTH * $hp / MAXHP);

$bar_color = imagecolorallocate($image, BAR_R, BAR_G, BAR_B);

imagefilledrectangle($image, BAR_X, BAR_Y, BAR_X + $width, 
			BAR_Y + BAR_HEIGHT, $bar_color);
}

// Insert these somewhere in the constants definition block
define('NAME_X', 20);
define('NAME_Y', 160);
define('NAME_R', 0x4f);
define('NAME_G', 0x66);
define('NAME_B', 0x81);
// GD has 5 built-in fonts, called 1, 2, 3, 4, and 5
define('FONT', 5);
//If you want to use TTF, you need these
define('ANGLE', 0);
define('FONTSIZE', 14);
define('FONTFILE', 'arial.ttf');

function writeName($image, $name) {

$text_color = imagecolorallocate($image, NAME_R, NAME_G, NAME_B);

// If you want to use a font file
imagettftext($image, FONTSIZE, ANGLE, NAME_X, NAME_Y, 
			$text_color, FONTFILE, $name);

// Else, use one of the built-in fonts
imagestring($image, FONT, NAME_X, NAME_Y, $name, $text_color);
}
?>

 

I just need someone to guide me through this.

 

Thank you! <3

Link to comment
Share on other sites

Yeah, but I don't understand what happens there.

 

See this?

// Insert these somewhere in the constants definition block
define('BAR_X', 10);
define('BAR_Y', 200);
define('BAR_MAXWIDTH', 400);
define('BAR_HEIGHT', 6);
define('BAR_R', 0xdd);
define('BAR_G', 0x5a);
define('BAR_B', 0x7c);

function paintHPBar($image, $hp) {

$width = ceil(BAR_MAXWIDTH * $hp / MAXHP);

$bar_color = imagecolorallocate($image, BAR_R, BAR_G, BAR_B);

imagefilledrectangle($image, BAR_X, BAR_Y, BAR_X + $width, 
			BAR_Y + BAR_HEIGHT, $bar_color);
}

 

The guy didn't explain what each define means. =P

Link to comment
Share on other sites

His tutorial is kind of crap, the drawHitpointBar($image, $hp) function magically transforms into paintHPBar($image, $hp), but the way you have your script posted it calls the function drawHitpointBar($image, $hp) before you define it. Even though the function changes to paintHPBar(). If PHP is what your interested in learning his tutorial is a little advanced and poorly written. Try w3schools.com

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.