0% found this document useful (0 votes)
43 views6 pages

PHP Snake Game Logic Implementation

This document contains the code for a snake game implemented with PHP and JavaScript. It defines classes and functions for the game logic, including: - Capturing form parameters like board size and snake pace - Drawing the snake, food, and updating the score - Handling snake movement and checking for collisions/death - Changing game levels and speeds as the score thresholds are passed

Uploaded by

gsgurpreetsinghh
Copyright
© All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as TXT, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
43 views6 pages

PHP Snake Game Logic Implementation

This document contains the code for a snake game implemented with PHP and JavaScript. It defines classes and functions for the game logic, including: - Capturing form parameters like board size and snake pace - Drawing the snake, food, and updating the score - Handling snake movement and checking for collisions/death - Changing game levels and speeds as the score thresholds are passed

Uploaded by

gsgurpreetsinghh
Copyright
© All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as TXT, PDF, TXT or read online on Scribd

<!-- FileName: Snake.

php
* Author: Pratibha Natani
* Description: This file contains the Main game logic. Contains the php and
javascript code.
* References:
* [Link]
* [Link]
* [Link]
-->
<html>
<head>
<link rel="stylesheet" type="text/css" href="[Link]">
</head>

<?php
//form parameters captured
$BoardSize = $_POST['BoardSize'];
$BoardSizex =substr($BoardSize, 0, 3);
$BoardSizey =substr($BoardSize, 6, 3);
$Pace= $_POST['SnakePace'];
if($Pace=="Slow")
$SnakePace=300;
else if($Pace=="Medium")
$SnakePace=250;
else if($Pace=="Fast")
$SnakePace=200;
$Goals= $_POST['Goals'];
?>

<body>
<label type="text" id='scoreLabel'>Score: </label>
<input type='text' id='scoreText' value='0'></input>
<div id="Board">
<img src="[Link]" id="snake0" />
<img src="[Link]" id="snake1" />
<img src="[Link]" id="snake2" />

</div>

</body>
<script type="text/javascript">

//to set the different game levels


var level1Score =50;
var level2Score =90;
var level3Score =120;
var levelSpeed =200;

//Game Flags
var moveOne = true;
var snakeDead = false;

//for setting Game Score


var Score=0;
var snakePart = 2;

//retrieve form parameters


var BoardSizex = "<?php echo $BoardSizex ?>px";
var BoardSizey = "<?php echo $BoardSizey ?>px";
var SnakePace = "<?php echo $SnakePace ?>";
var Goals = "<?php echo $Goals ?>";
var BoardSize1x = "<?php echo $BoardSizex ?>";
var BoardSize1y = "<?php echo $BoardSizey ?>";

[Link]('Board').[Link] = BoardSizey;
[Link]('Board').[Link] = BoardSizex;

//flag set to not draw the food again and again on timer
firstDraw=false;
function drawFood() {

if(!firstDraw)
{
for(k=1;k<=Goals;k++)
{
callFood(k);
}
firstDraw=true;
}

function callFood(k) {
foodX= randomNumbergen(1,BoardSize1x-10);
foodX=foodX-(foodX%10);

foodY= randomNumbergen(1,BoardSize1y-10);
foodY=foodY-(foodY%10);
//generate img tags on fly
var incr = "<img src = \"[Link]\" id=\"food"+k+"\"
style = \"position:absolute;left:"+foodX+";top:"+foodY+";height:10px;\" />";
[Link]('Board').innerHTML += incr;

[Link]('food' + k).[Link] =
foodX;
[Link]('food' + k).[Link] =
foodY;
}

function redrawFood(k) {
callFood(k);

var changer = [Link]('scoreText');


[Link] = Score;
//change Game levels
if(Score == level3Score)
{
var answer = confirm("Voilaaaaa!You won the
game!!!")
alert("Bye bye!")
[Link] = "[Link]";
}
else if(Score == level2Score)
{
var answer = confirm("Yippiee!You cleared 2nd
level...do you want to continue?")
if (answer){
alert("Continue to next Level with faster
pace...")
clearInterval(timer);
timer=setInterval('[Link]()', 50);
}
else{
alert("Bye bye!")
[Link] = "[Link]";
}
}
else if (Score == level1Score)
{
var answer = confirm("Yippiee!You cleared 1st
level...do you want to continue?")
if (answer){
alert("Continue to next Level with faster
pace...")
clearInterval(timer);
timer=setInterval('[Link]()',
100);
}
else{
alert("Bye bye!")
[Link] = "[Link]";

}
}

return;
}

//generates a random number between range x,y


function randomNumbergen(x, y){
return [Link]([Link]() * y) + x;
}

function Snake() {
this.x = 30;
this.y = 30;
[Link] = 'R';
}

[Link] = function(dir) {
[Link] = dir;
}

[Link] = function() {
if ( snakeDead )
{
return false;
}
else if( this.x < 0 || this.x +10> BoardSize1x )
{
return false;
}
else if( this.y < 0 || this.y +10> BoardSize1y )
{
return false;
}
//let snake tail take position of head
for(i=snakePart; i>0; --i)
{
[Link]("snake"+i).[Link] =
[Link]("snake"+ (i-1)).[Link];
[Link]("snake"+i).[Link] =
[Link]("snake"+ (i-1)).[Link];
[Link]("snake"+i).[Link] =
'visible';
}
[Link]('snake0').[Link] = this.x;
[Link]('snake0').[Link] = this.y;

if( !moveOne )
{
//check if snake touches itself-dead
for(i=snakePart; !snakeDead && i>0; --i)
{
if(
((((this.x) + "px") ==
[Link]("snake"+i).[Link]) &&
((this.y + "px") ==
[Link]("snake"+i).[Link]))

)
snakeDead = true;
}

//for eating all the foods present on the screen


for(i=1;i<=Goals;i++)
{
if((this.x + "px") ==
[Link]("food"+i).[Link] &&
(this.y + "px") ==
[Link]("food"+i).[Link])
{
//dynamically generate snake size
++snakePart;
var imageSnake = "<img src
= \"[Link]\" id=\"snake"+(snakePart)+"\" style
= \"position:absolute;left:0;top:0;height:10px;Visibility:hidden\" />";

[Link]('Board').innerHTML += imageSnake;

//to disappear food that is eaten up


var image_x =
[Link]('Board');

image_x.removeChild([Link]('food'+i));
Score=Score+10;
//to generate another goal
redrawFood(i);
}
}
}
moveOne = false;
return true;
}

[Link] = function() {
switch ([Link]) {
case 'L': // Left
this.x -= 10;
break;
case 'U': // Up
this.y -= 10;
break;
case 'R': // Right
this.x += 10;
break;
case 'D': // Down
this.y += 10;
break;
}

retval= [Link]();
if(!retval){
var answer = confirm("Oops Your snake is dead! Click Ok to
Quit.")
if(answer)
{
clearInterval(timer);
[Link] = "[Link]";
}
}
drawFood();

var mrSnake = new Snake();

[Link]('keydown', function(event) {
switch ([Link]) {
case 37: // Left
if([Link]!='R')
{
[Link]('L');
}
break;
case 38: // Up
if([Link]!='D')
{
[Link]('U');
}
break;
case 39: // Right
if([Link]!='L')
{
[Link]('R');
}
break;
case 40: // Down
if([Link]!='U')
{
[Link]('D');
}
break;
}
}, false);

timer=setInterval('[Link]()', SnakePace);

</script>

</html>

You might also like