Eğitim 1 — Hello world: bir menü, bir metin, bir ses
En küçük JumPHP oyunu: bir menüsü olan (oyna, ses, çık) başlık ekranı, ardından ok tuşlarıyla hareket eden bir metin ve Boşluk tuşunda çalan bir ses. İki dosya. Hemen kum havuzunda değiştirebilir ya da evinizde bin/jumphp new hello-world ile oluşturabilirsiniz.
Yapılandırma: config/game.yaml
game:
id: hello-world
title: "HELLO WORLD"
width: 320 # oyunun çözünürlüğü, ekran boyutuna büyütülür
height: 180
start_scene: App\Scene\TitleScene
touch: { a: Space, b: Escape, start: Return } # telefondaki pad düğmeleri
smoke: # `bin/jumphp test` komutunun ekransız, kendi kendine oynadığı şey
scene: App\Scene\HelloScene
params: { demo: true }
Tuşlar: motor up down left right confirm back pause eylemlerini zaten tanımlar (oklar, Enter, Esc, oyun kolu, dokunmatik ekran). config/input.yaml dosyasına bir eylem ekleriz:
actions:
jump: [Space, pad0:a, touch:tap]
Başlık ekranı: src/Scene/TitleScene.php
Bir menü, MenuScene sınıfından türetilerek yazılır: başlığı ve satırları veririz, gerisini motor halleder (çizim, klavye / oyun kolu / parmakla gezinme, menü sesleri).
final class TitleScene extends MenuScene
{
protected string $title = 'HELLO WORLD';
public function enter(array $params = []): void
{
parent::enter($params);
// PHP ile üretilen sesler: hiçbir ses dosyası gerekmez
if (!$this->sound()->hasSfx('menu_move')) {
$this->sound()->synth('menu_move', Synth::tone(660, 0.04));
$this->sound()->synth('menu_confirm', Synth::sequence([[660, 0.05], [990, 0.08]]));
}
}
/** Satırlar: [etiket, sağda gösterilen değer, Enter'daki eylem, sol / sağdaki eylem] */
protected function items(): array
{
$items = [
['PLAY', null, fn () => $this->scenes()->switchTo(HelloScene::class)],
['SOUND', fn () => $this->sound()->isMuted() ? 'OFF' : 'ON', fn () => $this->toggleSound(), fn () => $this->toggleSound()],
];
if (Platform::isDesktop()) {
$items[] = ['QUIT', null, fn () => $this->engine->quit()]; // tarayıcıda “çık” yok
}
return $items;
}
private function toggleSound(): void
{
$this->sound()->setMuted(!$this->sound()->isMuted());
$this->refresh(); // menü yeni değeri yeniden gösterir
}
}
menu_move ve menu_confirm sesleri menü tarafından otomatik çalınır. Etiketler config/lang.yaml ile çevrilir (fr: { PLAY: JOUER }).
Oyun: src/Scene/HelloScene.php
Bir sahnenin üç temel metodu vardır:
enter($params): sahne başlar. Her şeyi burada sıfırlayın: aynı sahne birkaç kez kullanılır;update($dt): saniyede 60 kez oyunun durumunu değiştiririz.$dt, saniye cinsinden geçen süredir;render($r): çizeriz, başka bir şey yapmayız.
final class HelloScene extends AbstractScene
{
private float $x = 0.0;
private float $y = 0.0;
public function enter(array $params = []): void
{
$this->x = $this->width() / 2;
$this->y = $this->height() / 2;
if (!$this->sound()->hasSfx('beep')) {
$this->sound()->synth('beep', Synth::sweep(440, 880, 0.12)); // yükselen bir “bip”
}
}
public function update(float $dt): void
{
if ($this->actions()->isPressed('back')) { // Esc: başlığa dön
$this->scenes()->switchTo(TitleScene::class);
return;
}
// axis() -1, 0 ya da 1 verir: saniyede 90 piksel
$this->x += $this->actions()->axis('left', 'right') * 90 * $dt;
$this->y += $this->actions()->axis('up', 'down') * 90 * $dt;
if ($this->actions()->isPressed('jump') || $this->actions()->isPressed('confirm')) {
$this->sound()->playSfx('beep');
}
}
public function render(Renderer $r): void
{
$r->clear([16, 16, 40]);
// yerleşik font (5 × 7 piksel), 2 kat büyütülmüş, ortalanmış, siyah gölgeli
$this->font()->draw($r, 'HELLO WORLD!', $this->x, $this->y, 2, [255, 220, 80], 'center', [0, 0, 0]);
}
}
Gerçek dosya her seste fışkıran yıldızlar, renk değiştiren bir metin ve testler için bir demo modu ekler: oyunun kaynaklarında okuyun.
Deneyin
- Kum havuzunda: metni,
[255, 220, 80]rengini, hızı, sesi (Synth::tone(220, 0.3, 'triangle')) değiştirin, sonra ▶ Test et. - Evinizde:
bin/jumphp run hello-world,bin/jumphp test hello-world.
Sonraki: hareket eden ve canlanan bir karakter.