Skip to content

Commit

Permalink
Merge pull request #5 from nickrhalvorsen/snake-adjustments
Browse files Browse the repository at this point in the history
snake style and game mechanic adjustments
  • Loading branch information
johncbowers authored Sep 11, 2018
2 parents 933bf49 + 16b4481 commit b597cff
Show file tree
Hide file tree
Showing 3 changed files with 48 additions and 26 deletions.
68 changes: 46 additions & 22 deletions framework/games/snake.js
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,8 @@ function gameMessageHandler(msg) {

var score = 0;
var highScore = 0;
var snakeSize = 40;
var gameSpeed = 9;

var foodX;
var foodY;
Expand All @@ -28,7 +30,7 @@ function setup() {
canv.parent("bgCanvas");

//Setup New Game
frameRate(10); //TODO Change speed based on score?
frameRate(gameSpeed); //TODO Change speed based on score?
generateFood();
newGame();
}
Expand Down Expand Up @@ -64,10 +66,11 @@ function draw() {
background(51);

drawScoreboard();
changeSpeed();

moveSnake();
displaySnake();
displayFood();
displaySnake();
checkEatFood();
}

Expand All @@ -76,19 +79,31 @@ function drawScoreboard() {
rectMode(CENTER);
textAlign(CENTER, CENTER);

stroke(118, 22, 167);
stroke(69, 0, 132);
strokeWeight(2);
fill(255, 204 , 0, 150);
fill(203, 182, 119);
rect(150, 100, 230, 90);

fill(118, 22, 167);
fill(69, 0, 132);
strokeWeight(0);
textSize(25);
text( "Score: " + score, 120, 75);
text( "Score: " + score, 110, 85);

fill(118, 22, 167);
fill(69, 0, 132);
textSize(25);
text( "High Score: " + highScore, 150, 120);
text( "High Score: " + highScore, 143, 120);
}

/*Change speed based on score*/
function changeSpeed() {
if(score >= 5 && score < 10) {
gameSpeed = 10;
} else if(score > 10 && score < 15) {
gameSpeed = 12;
} else if(score >= 15) {
gameSpeed = 13;
}
frameRate(gameSpeed);
}

/* FOOD */
Expand All @@ -98,9 +113,13 @@ function generateFood() {
}

function displayFood() {
strokeWeight(0);
fill(118,22,167);
rect(foodX, foodY, 25, 25);
strokeWeight(1);
stroke(0, 0, 0);
fill(198,0,0);
rect(foodX, foodY, 35, 35, 20);

strokeWeight(2);
line(foodX, foodY-10, foodX+5, foodY-20);
}


Expand All @@ -120,16 +139,20 @@ function displaySnake() {
rectMode(CENTER);
var i;
for (i = 0; i < len; i++) {
stroke(179, 140, 198);
stroke(69, 0, 132);
strokeWeight(2);
fill(255, 204 , 0);
rect(xpos[i], ypos[i], 25, 25);
fill(203, 182, 119);
rect(xpos[i], ypos[i], snakeSize, snakeSize, 5);
if(i == 0) {
fill(51);
ellipse(xpos[i]+8, ypos[i]-8, 6, 6);
}
}
}

function increaseSnakeSize() {
xpos[len] = xpos[len-1]+25;
ypos[len] = ypos[len-1]+25;
xpos[len] = xpos[len-1]+snakeSize;
ypos[len] = ypos[len-1]+snakeSize;
len++;
}

Expand All @@ -140,16 +163,16 @@ function moveSnake() {
ypos[i] = ypos[i-1];
}
if(dir == "up") {
ypos[0] = ypos[0] - 25;
ypos[0] = ypos[0] - snakeSize;
}
if(dir == "down") {
ypos[0] = ypos[0] + 25;
ypos[0] = ypos[0] + snakeSize;
}
if(dir == "right") {
xpos[0] = xpos[0] + 25;
xpos[0] = xpos[0] + snakeSize;
}
if(dir == "left") {
xpos[0] = xpos[0] - 25;
xpos[0] = xpos[0] - snakeSize;
}
//Wrap on screen
xpos[0] = (xpos[0] + windowWidth) % windowWidth;
Expand All @@ -161,11 +184,12 @@ function moveSnake() {
xpos = [400];
ypos = [300];
score = 0;
gameSpeed = 9;
}
}

function checkEatFood() {
if(dist(foodX, foodY, xpos[0], ypos[0]) < 25 ) {
if(dist(foodX, foodY, xpos[0], ypos[0]) < snakeSize ) {
generateFood();
increaseSnakeSize();
score++;
Expand All @@ -178,7 +202,7 @@ function checkEatFood() {
function checkCollide() {
var i;
for(i = 1; i < len; i++) {
if(dist(xpos[0], ypos[0], xpos[i], ypos[i]) < 25) {
if(dist(xpos[0], ypos[0], xpos[i], ypos[i]) < snakeSize) {
return true;
}
}
Expand Down
4 changes: 2 additions & 2 deletions framework/player.html
Original file line number Diff line number Diff line change
Expand Up @@ -35,10 +35,10 @@
</div>
</div>

<div id="shoutout" style="z-index: 200; margin-top: 0; right: 100px; position:fixed; background-color: rgba(0,0,0,200.0); padding: 0.25em; font-size: 14pt; display:none;">
<div id="shoutout" style="z-index: 200; margin-top: 0; right: 100px; position:fixed; background-color: rgba(0,0,0,.5); padding: 0.25em; font-size: 14pt; display:none;">
<p style="text-align: center; padding-left: 0.5em; padding-right: 0.5em;">Designed by:</p>
<p id="usericon" style="text-align: center;"></p>
<p style="text-align: center; padding-bottom:0;margin-bottom: 0; font-size: 10pt;">GitHub</p>
<p style="text-align: center; padding-bottom:0;margin-bottom: 0; font-size: 10pt;">GitHub</p>
<p id="github" style="vertical-align: middle; text-align: center;margin-top:0;margin-bottom: 0; padding-bottom:0;"></p>
<p id="graddate" style="vertical-align: middle; text-align: center; margin-top:0; margin-bottom: 0; font-size: 10pt; padding-bottom:0;"></p>
</div>
Expand Down
2 changes: 0 additions & 2 deletions index.js
Original file line number Diff line number Diff line change
Expand Up @@ -41,14 +41,12 @@ io.of("/controller").on('connection', function(socket) {
});

socket.on('touch start', function(msg) {
console.log("touch start: " + msg);
if (playerSocket != null) {
playerSocket.emit('touch start', msg);
}
})

socket.on('touch end', function(msg) {
console.log("touch end: " + msg);
if (playerSocket != null) {
playerSocket.emit('touch end', msg);
}
Expand Down

0 comments on commit b597cff

Please sign in to comment.