PHP dasturlash tilida Snake Xenzia o'yini
o'yinni ishga tushirish uchun buyruqlar satrida
$: php snakeGame.php
<?php | |
class Snake { | |
public $width = 0; | |
public $height = 0; | |
public $positionX = 0; | |
public $positionY = 0; | |
public $appleX = 15; | |
public $appleY = 15; | |
public $movementX = 0; | |
public $movementY = 0; | |
public $trail = []; | |
public $tail = 5; | |
public $speed = 100000; | |
public function __construct( $width, $height ){ | |
$this->width = $width; | |
$this->height = $height; | |
$this->positionX = rand( 0, $width - 1 ); | |
$this->positionY = rand( 0, $height - 1 ); | |
$appleX = rand( 0, $width - 1 ); | |
$appleY = rand( 0, $height - 1 ); | |
while ( array_search( [ $appleX, $appleY ], $this->trail ) !== FALSE ) { | |
$appleX = rand( 0, $width - 1 ); | |
$appleY = rand( 0, $height - 1 ); | |
} | |
$this->appleX = $appleX; | |
$this->appleY = $appleY; | |
} | |
} | |
function renderGame( $snake ) { | |
$output = ''; | |
for ( $i = 0; $i < $snake->width; $i++ ) { | |
for ( $j = 0; $j < $snake->height; $j++ ) { | |
$cell = ( $snake->appleX == $i && $snake->appleY == $j ) ? '0' : '.'; | |
foreach ( $snake->trail as $trail ) { | |
if ( $trail[0] == $i && $trail[1] == $j ) $cell = 'X'; | |
} | |
$output .= $cell; | |
} | |
$output .= PHP_EOL; | |
} | |
$output .= PHP_EOL; | |
return $output; | |
} | |
function translateKeypress( $string ) { | |
switch ($string) { | |
// Yuqoriga harakatlantirish tugmasi | |
case "\033[A": | |
return "UP"; | |
// Pastga harakatlantirish tugmasi | |
case "\033[B": | |
return "DOWN"; | |
// O'ngga harakatlantirish tugmasi | |
case "\033[C": | |
return "RIGHT"; | |
// Chapga harakatlantirish tugmasi | |
case "\033[D": | |
return "LEFT"; | |
// ENTER (kiritish) tugmasi | |
case "\n": | |
return "ENTER"; | |
// Bo'sh joy tugmasi | |
case " ": | |
return "SPACE"; | |
// Satrdan belgini o'chirish tugmasi | |
case "\010": | |
case "\177": | |
return "BACKSPACE"; | |
// Tabulator tugmasi | |
case "\t": | |
return "TAB"; | |
// Bekor qilish tugmasi | |
case "\e": | |
return "ESC"; | |
} | |
return $string; | |
} | |
function direction( $stdin, $snake ) { | |
$key = fgets( $stdin ); | |
if ( $key ) { | |
$key = translateKeypress( $key ); | |
switch ( $key ) { | |
case "UP": | |
$snake->movementX = -1; | |
$snake->movementY = 0; | |
break; | |
case "DOWN": | |
$snake->movementX = 1; | |
$snake->movementY = 0; | |
break; | |
case "RIGHT": | |
$snake->movementX = 0; | |
$snake->movementY = 1; | |
break; | |
case "LEFT": | |
$snake->movementX = 0; | |
$snake->movementY = -1; | |
break; | |
} | |
} | |
} | |
function move( $snake ) { | |
// Ilonni harakatlantirish. | |
$snake->positionX += $snake->movementX; | |
$snake->positionY += $snake->movementY; | |
// Ilonni o'yin taxtasi chegarasi bo'ylab saqlash. | |
if ( $snake->positionX < 0 ) $snake->positionX = $snake->width - 1; | |
if ( $snake->positionX > $snake->width - 1 ) $snake->positionX = 0; | |
if ( $snake->positionY < 0 ) $snake->positionY = $snake->height - 1; | |
if ( $snake->positionY > $snake->height - 1 ) $snake->positionY = 0; | |
// Ilon izi old qismiga bitta element qo'shish | |
array_unshift( $snake->trail, [ $snake->positionX, $snake->positionY ] ); | |
// Aniq uzunlikni saqlagan holda ilondan bitta belgini olib tashlash. | |
if ( count( $snake->trail ) > $snake->tail ) array_pop($snake->trail); | |
if ( $snake->appleX == $snake->positionX && $snake->appleY == $snake->positionY ) { | |
// Ilon olma pozitsiyasiga teng kelishi | |
$snake->tail++; | |
if ($snake->speed > 2000) { | |
// O'yin tezligini ma'lum chegaragacha oshirish | |
$snake->speed = $snake->speed - ( $snake->tail * ( $snake->width / $snake->height + 10 ) ); | |
} | |
// Olma pozitsiyasini almashtirish. | |
$appleX = rand( 0, $snake->width - 1 ); | |
$appleY = rand( 0, $snake->height - 1 ); | |
while ( array_search( [ $appleX, $appleY ], $snake->trail ) !== FALSE ) { | |
$appleX = rand( 0, $snake->width - 1 ); | |
$appleY = rand( 0, $snake->height - 1 ); | |
} | |
$snake->appleX = $appleX; | |
$snake->appleY = $appleY; | |
} | |
} | |
function gameOver( $snake ) { | |
if ( $snake->tail > 5 ) { | |
// Agar iz 5 dan katta bo'lsa, oxirgi holatni tekshirish. | |
for ( $i = 1; $i < count( $snake->trail ); $i++ ) { | |
if ( $snake->trail[ $i ][ 0 ] == $snake->positionX && $snake->trail[ $i ][ 1 ] == $snake->positionY ) { | |
exit('O\'yin tugadi, ilon o\'ldi'); | |
} | |
} | |
} | |
} | |
system('stty cbreak -echo'); | |
$stdin = fopen('php://stdin', 'r'); | |
stream_set_blocking($stdin, 0); | |
$snake = new Snake( 20, 30 ); // kenglik. balandlik | |
while ( 1 ) { | |
system('clear'); // Satrni tozalab borish | |
echo 'Olmalar: ' . $snake->tail . PHP_EOL; | |
direction( $stdin, $snake ); // Ilon yo'nalishini belgilash | |
move( $snake ); // Ilon harakatini boshqarish | |
echo renderGame( $snake ); // O'yin taxtasini chop etish | |
gameOver( $snake ); // Ilon o'limi holatini nazorat qilish | |
usleep( $snake->speed ); // O'yin tezligini belgilash | |
} |