Skip to content
GitLab
Explore
Sign in
Primary navigation
Search or go to…
Project
G
GBS
Manage
Activity
Members
Labels
Plan
Issues
Issue boards
Milestones
Wiki
Code
Merge requests
Repository
Branches
Commits
Tags
Repository graph
Compare revisions
Snippets
Build
Pipelines
Jobs
Pipeline schedules
Artifacts
Deploy
Releases
Package registry
Container registry
Model registry
Operate
Environments
Terraform modules
Monitor
Incidents
Analyze
Value stream analytics
Contributor analytics
CI/CD analytics
Repository analytics
Model experiments
Help
Help
Support
GitLab documentation
Compare GitLab plans
GitLab community forum
Contribute to GitLab
Provide feedback
Keyboard shortcuts
?
Snippets
Groups
Projects
Show more breadcrumbs
CS375
GBS
Commits
b57b2cda
Commit
b57b2cda
authored
11 months ago
by
zm343
Browse files
Options
Downloads
Patches
Plain Diff
Fixed multiplayer movement + Added banana collision
parent
09f0b9b7
No related branches found
No related tags found
1 merge request
!10
Update game.js for collision & growing features
Changes
1
Show whitespace changes
Inline
Side-by-side
Showing
1 changed file
src/game/game.js
+30
-10
30 additions, 10 deletions
src/game/game.js
with
30 additions
and
10 deletions
src/game/game.js
+
30
−
10
View file @
b57b2cda
...
...
@@ -3,6 +3,7 @@ const cellTypes = {
PLAYERHEAD
:
1
,
PLAYERBODY
:
2
,
FOOD
:
3
,
BANANA
:
4
};
function
createCell
(
x
,
y
,
type
)
{
...
...
@@ -31,12 +32,19 @@ function addFood(gameBoard) {
}
}
let
bananaExists
=
gameBoard
.
some
(
row
=>
row
.
some
(
cell
=>
cell
.
type
===
cellTypes
.
BANANA
));
//Create food cells randomly
if
(
emptySpaces
.
length
>
0
)
{
let
randomCell
=
emptySpaces
[
getRandomInt
(
emptySpaces
.
length
)];
randomCell
.
type
=
cellTypes
.
FOOD
;
}
if
(
!
bananaExists
&&
emptySpaces
.
length
>
0
)
{
let
randomCell
=
emptySpaces
[
getRandomInt
(
emptySpaces
.
length
)];
randomCell
.
type
=
cellTypes
.
BANANA
;
}
return
gameBoard
;
}
...
...
@@ -90,6 +98,9 @@ module.exports = {
},
moveOneStep
:
(
gameBoard
)
=>
{
// Save board state to allow multiple snake movements
let
updatedBoard
=
gameBoard
.
map
(
row
=>
row
.
map
(
cell
=>
({
...
cell
})));
// Loop through board until we find a PLAYERHEAD
for
(
var
i
=
0
;
i
<
gameBoard
.
length
;
i
++
)
{
for
(
var
j
=
0
;
j
<
gameBoard
[
i
].
length
;
j
++
)
{
...
...
@@ -126,9 +137,10 @@ module.exports = {
)
{
console
.
log
(
`Player
${
cell
.
pid
}
has collided with the wall!`
);
// Remove the player from the game (indicating death)
return
game
Board
.
map
(
row
=>
updatedBoard
=
updated
Board
.
map
(
row
=>
row
.
map
(
c
=>
(
c
.
pid
===
cell
.
pid
?
createCell
(
c
.
x
,
c
.
y
,
cellTypes
.
EMPTY
)
:
c
))
);
continue
;
}
// Handle collision with food
...
...
@@ -140,7 +152,18 @@ module.exports = {
newBodySegment
.
next
=
cell
.
next
;
cell
.
next
=
newBodySegment
;
// Place new food somewhere else
gameBoard
=
addFood
(
gameBoard
);
updatedBoard
=
addFood
(
updatedBoard
);
}
// Handle collision with banana
if
(
gameBoard
[
newY
][
newX
].
type
==
cellTypes
.
BANANA
)
{
console
.
log
(
'
Slip
'
);
updatedBoard
[
newY
][
newX
]
=
createCell
(
newX
,
newY
,
cellTypes
.
EMPTY
);
updatedBoard
=
updatedBoard
.
map
(
row
=>
row
.
map
(
c
=>
(
c
.
pid
===
cell
.
pid
?
createCell
(
c
.
x
,
c
.
y
,
cellTypes
.
EMPTY
)
:
c
))
);
updatedBoard
=
addFood
(
updatedBoard
);
continue
;
}
// Snake head to new position by updating cell object
...
...
@@ -148,10 +171,10 @@ module.exports = {
newHeadCell
.
pid
=
cell
.
pid
;
newHeadCell
.
direction
=
tempVar
;
newHeadCell
.
next
=
cell
.
next
;
game
Board
[
newY
][
newX
]
=
newHeadCell
;
updated
Board
[
newY
][
newX
]
=
newHeadCell
;
// Remove previous head position
game
Board
[
oldY
][
oldX
]
=
createCell
(
oldX
,
oldY
,
cellTypes
.
EMPTY
);
updated
Board
[
oldY
][
oldX
]
=
createCell
(
oldX
,
oldY
,
cellTypes
.
EMPTY
);
// Move the body segments
let
currentBody
=
newHeadCell
.
next
;
...
...
@@ -162,7 +185,7 @@ module.exports = {
currentBody
.
y
=
oldY
;
// Update cell body position
game
Board
[
oldY
][
oldX
]
=
{
updated
Board
[
oldY
][
oldX
]
=
{
...
currentBody
,
type
:
cellTypes
.
PLAYERBODY
,
};
...
...
@@ -173,15 +196,12 @@ module.exports = {
// Remove previous body position if it's no longer part of the snake
if
(
oldX
!==
newHeadCell
.
x
||
oldY
!==
newHeadCell
.
y
)
{
game
Board
[
oldY
][
oldX
]
=
createCell
(
oldX
,
oldY
,
cellTypes
.
EMPTY
);
updated
Board
[
oldY
][
oldX
]
=
createCell
(
oldX
,
oldY
,
cellTypes
.
EMPTY
);
}
console
.
log
(
gameBoard
);
return
gameBoard
;
}
}
}
return
game
Board
;
return
updated
Board
;
},
checkCollisions
:
(
gameBoard
)
=>
{
...
...
This diff is collapsed.
Click to expand it.
Preview
0%
Loading
Try again
or
attach a new file
.
Cancel
You are about to add
0
people
to the discussion. Proceed with caution.
Finish editing this message first!
Save comment
Cancel
Please
register
or
sign in
to comment