Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
7 changes: 7 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
@@ -1,4 +1,11 @@
project_gamecenter_browser
==========================

Demo at https://adrianmui.github.io/project_game_center_browser/

![](http://imgur.com/VXomDQw.png)

hiss hiss gulp grow hiss...

David
Adrian
51 changes: 51 additions & 0 deletions controller.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,51 @@

var controller = {
init: function(){
this.view = view;
this.model = model;
this.view.init();
this.model.init(parseInt(this.view.gridSize));
var coords = {
game: this.model.snake.dead,
snake: this.model.snake.body,
food: this.model.food
};
this.view.render(coords);

var thatView = this.view;
var thatController = this;

var play = setInterval( function(e) {
if (thatController.model.snake.dead) {
thatView.done();
alert("Game is Over");
clearInterval(play);
} else {

thatController.moveSnake();

coords = {
game: thatController.model.snake.dead,
snake: thatController.model.snake.body,
food: thatController.model.food
};



//debugger;
thatController.view.clear();
thatController.view.render(coords);
}


}, 200);



},

moveSnake: function(){
var direction = this.view.keyPress;
this.model.updateSnake(direction);
}
}
Binary file added images/body-bg.jpg
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added images/download-button.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added images/github-button.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added images/header-bg.jpg
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added images/highlight-bg.jpg
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added images/sidebar-bg.jpg
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
27 changes: 27 additions & 0 deletions index.html
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
<html>
<head>
<script src="https://ajax.googleapis.com/ajax/libs/jquery/1.12.4/jquery.min.js"></script>
<script src = "model.js"></script>
<script src = "view.js"></script>
<script src = "controller.js"></script>
<script src = "script.js"></script>

<link rel="stylesheet" href ="styles.css">
<!-- Latest compiled and minified CSS -->
<link rel="stylesheet" href="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.7/css/bootstrap.min.css" integrity="sha384-BVYiiSIFeK1dGmJRAkycuHAHRg32OmUcww7on3RYdg4Va+PmSTsz/K68vbdEjh4u" crossorigin="anonymous">
</head>


<div class="col-sm-6">
<body>


</body>
</div>

<div class="col-sm-6">
<div id="score"></div> <div id="status"></div>
</div>


</html>
1 change: 1 addition & 0 deletions javascripts/main.js
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
console.log('This would be the main JS file.');
181 changes: 181 additions & 0 deletions model.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,181 @@
function Food(x,y){
this.x = x;
this.y = y;
}

function Snake(x,y) {
this.length = 1;
// holds the coordinates of each part of the snake.
this.body = [];
this.direction = 40;
this.dead = false;
this.body.push([x,y]);
// // eats food, grows in length and body
// this.eatFood = function(){

// }
}

var model = {

init: function(gridSize) {
this.gridSize = gridSize;
this.score = 0;
this.createSnake();
this.createFood();
//array of Food


},

// update Snake's position by x, y coordinates
updateSnake: function(direction) {
// is snake dead?
console.log(this.checkSnake());
this.snake.dead = this.checkSnake();




var head = this.snake.body[0];
var scale = Math.floor(this.gridSize / 40);
var newPart = [];

if (direction === undefined) {
direction = this.snake.direction;
}
switch(direction) {
//left
case 37:
newPart = [(head[0] - scale), head[1]];
this.snake.direction = direction;

//updates body and length of snake
if(this.checkEatFood()) {
this.snake.length++;
this.snake.body.unshift([this.food.x - 1, this.food.y]);
this.destroyFood();
this.createFood();
}
break;
//up
case 40:
newPart = [head[0], (head[1] + scale)];
this.snake.direction = direction;

//updates body and length of snake
if(this.checkEatFood()) {
this.snake.length++;
this.snake.body.unshift([this.food.x, this.food.y + 1]);
this.destroyFood();
this.createFood();
}
break;
//right
case 39:
newPart = [(head[0] + scale), head[1]];
this.snake.direction = direction;
if(this.checkEatFood()) {
this.snake.length++;
this.snake.body.unshift([this.food.x + 1, this.food.y]);
this.destroyFood();
this.createFood();
}
break;
//down
case 38:
newPart = [head[0], (head[1] - scale)];
this.snake.direction = direction;
if(this.checkEatFood()) {
this.snake.length++;
this.snake.body.unshift([this.food.x, this.food.y - 1]);
this.destroyFood();
this.createFood();
}
break;

default:
break;
}

this.snake.body.unshift(newPart);
this.snake.body.pop();

},

// snake height and width will be 1/40 x gridSize
// create the snake as an instance of the constructor Snake
createSnake: function() {
var x = Math.floor(this.gridSize / 2);
var y = Math.floor(this.gridSize / 2);
this.snake = new Snake(x,y);
},

// checks if snake hit wall, hits itself, dead or alive?
checkSnake: function() {

// if the snake overlaps itself
var repeat_x = this.snake.body.map( function (el) {
return el[0];
});
var repeat_y = this.snake.body.map( function (el) {
return el[1];
});

for (var j = 1; j < repeat_x.length; j++) {
if (repeat_x[0] === repeat_x[j] && repeat_y[0] === repeat_y[j]) {
return true;
}

}

//left wall, right wall, upper wall, lower wall

if (this.snake.body[0][0] < (this.gridSize * 1/40) || this.snake.body[0][0] > ( this.gridSize *38/40)) {
return true;
}
if (this.snake.body[0][1] < (this.gridSize *1/40 ) || this.snake.body[0][1] > ( this.gridSize *38/40)) {
return true;
}
return false;
},

checkEatFood: function(){
//does snake head overlap food?
var head = this.snake.body[0];
console.log("head coord: " + head);
console.log("x " + this.withinX(head));
console.log("y " + this.withinY(head));
return (this.withinX(head) && this.withinY(head));
//this.food.x === head[0] && this.food.y === head[1]);
},

withinX: function(head){
return (this.food.x <= head[0] + 10 && this.food.x >= head[0] - 10)
},

withinY: function(head){
//might need to switch this?
return (this.food.y <= head[1] + 10 && this.food.y >= head[1] - 10)
},

keepScore: function() {

},

// handles food
// food height and width will be 1/40 x gridSize
// cannot be the same position as the snake
createFood: function() {
var x = Math.floor(Math.random()*(39/40)*this.gridSize);
var y = Math.floor(Math.random()*(39/40)*this.gridSize);
this.food = new Food(x,y);
},

destroyFood: function (){
this.food = null;
},



}
6 changes: 6 additions & 0 deletions params.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
{
"name": "Project game center browser",
"tagline": "hiss hiss gulp grow hiss...",
"body": "project_gamecenter_browser\r\n==========================\r\n\r\nhiss hiss gulp grow hiss...\r\n\r\nDavid\r\nAdrian",
"note": "Don't delete this file! It's used internally to help with page regeneration."
}
3 changes: 3 additions & 0 deletions script.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
$(document).ready(function(){
controller.init();
})
15 changes: 15 additions & 0 deletions styles.css
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
.box {
border: 1px solid yellow;
background-color: black;

}

.food {
background-color:red;
position: absolute;
}

.snake {
background-color: #ffa500;
position: absolute;
}
Loading