forked from milq/milq
-
Notifications
You must be signed in to change notification settings - Fork 0
/
06.php
245 lines (175 loc) · 4.95 KB
/
06.php
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
<?php
class Point
{
private $x;
private $y;
public function __construct($x, $y)
{
$this->x = $x;
$this->y = $y;
}
public function get_x()
{
return $this->x;
}
public function set_x($x)
{
$this->x = $x;
}
public function get_y()
{
return $this->y;
}
public function set_y($y)
{
$this->y = $y;
}
}
class Character
{
private $name;
private $x = 50;
private $y = 50;
private $max_health;
private $health;
private $speed = 10;
private $attack = 8;
private $defend = 4;
public function __construct($name, $max_health)
{
$this->name = $name;
$this->max_health = $max_health;
$this->health = $max_health;
}
public function get_name()
{
return $this->name;
}
public function get_x()
{
return $this->x;
}
public function get_health()
{
return $this->health;
}
public function get_attack()
{
return $this->attack;
}
public function move_right()
{
$this->x = $this->x + $this->speed;
}
public function move_left()
{
$this->x = $this->x - $this->speed;
}
public function take_damage($damage)
{
$this->health = $this->health - $damage;
}
}
class Animal
{
protected $name;
protected $energy;
public function __construct($name, $energy)
{
$this->name = $name;
$this->energy = $energy;
}
public function get_name()
{
return $this->name;
}
public function get_energy()
{
return $this->energy;
}
public function eat()
{
$this->energy = $this->energy + 5;
}
public function move()
{
$this->energy = $this->energy - 5;
}
}
class Dog extends Animal
{
private $sound;
private $breed;
public function __construct($name, $energy, $sound, $breed)
{
$this->name = $name;
$this->energy = $energy;
$this->sound = $sound;
$this->breed = $breed;
}
public function get_sound()
{
return $this->sound;
}
public function get_breed()
{
return $this->breed;
}
}
class Cat extends Animal
{
private $sound;
public function __construct($name, $energy)
{
$this->name = $name;
$this->energy = $energy;
$this->sound = 'Miaow!';
}
public function get_sound()
{
return $this->sound;
}
public function purr()
{
$this->sound = 'Purr...';
}
}
// EXAMPLE WITH CLASS POINT
echo '<p><b>POINTS</b></p>';
$point = new Point(2, 3); // We create an instance (or object) of class Point.
echo '<p>' . $point->get_x() . ' ' . $point->get_y() . '</p>';
$point->set_x(5);
$point->set_y(-4);
echo '<p>' . $point->get_x() . ' ' . $point->get_y() . '</p>';
// EXAMPLE WITH CLASS CHARACTER
echo '<p><b>WELCOME, PLAYER!</b></p>';
$hero = new Character('Reinwald', 50); // We create an instance (or object) of class Character.
$evil = new Character('Zarosh', 40); // We create an instance (or object) of class Character.
$hero->move_right();
$hero->move_right();
echo '<p>The current x position of ' . $hero->get_name() . ' is ' . $hero->get_x() . '.</p>';
$evil->take_damage($hero->get_attack());
echo '<p>The current hit points of ' . $evil->get_name() . ' is ', $evil->get_health() . '.</p>';
// EXAMPLE WITH CLASS ANIMAL AND CLASSES DOG AND CAT (inheritance)
echo '<p><b>ANIMALS</b></p>';
$ruperta = new Cat('Ruperta', 100); // We create an instance (or object) of class Cat.
$chloe = new Cat('Chloe', 75); // We create an instance (or object) of class Cat.
$ruperta_energy = $ruperta->get_energy();
echo '<p>The energy of ' . $ruperta->get_name() . ' is ' . $ruperta_energy . ' and her sound is ' . $ruperta->get_sound() . '</p>';
echo '<p>The energy of ' . $chloe->get_name() . ' is ' . $chloe->get_energy() . ' and her sound is ' . $chloe->get_sound() . '</p>';
$chloe->move();
$chloe->eat();
$chloe->move();
$ruperta->purr();
echo '<p>Now, the energy of ' . $chloe->get_name() . ' is ' . $chloe->get_energy();
echo '<p>Now, the sound of ' . $ruperta->get_name() . ' is ' . $ruperta->get_sound();
$odessa = new Animal('Odessa', 50); // We create an instance (or object) of class Animal.
$odessa->move();
$odessa->move();
$odessa->move();
echo '<p>Suddenly, ' . $odessa->get_name() . ' with ', $odessa->get_energy() . ' of energy appeared!</p>';
$toby = new Dog('Toby', 50, 'Woof!', 'Golden Retriever'); // We create an instance (or object) of class Dog.
$akira = new Dog('Akira', 125, 'Woof, woof!', 'Siberian Husky'); // We create an instance (or object) of class Dog.
echo '<p>' . $toby->get_name() . ' is a ' . $toby->get_breed() . ' and his sound is ' . $toby->get_sound() . '</p>';
echo '<p>' . $akira->get_name() . ' is a ' . $akira->get_breed() . ' and his sound is ' . $akira->get_sound() . '</p>';
?>