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
09f0b9b7
Commit
09f0b9b7
authored
11 months ago
by
ys678
Browse files
Options
Downloads
Patches
Plain Diff
Update game.js for collision & growing features
parent
a8928b64
Branches
Branches containing commit
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
+60
-41
60 additions, 41 deletions
src/game/game.js
with
60 additions
and
41 deletions
src/game/game.js
+
60
−
41
View file @
09f0b9b7
...
...
@@ -117,26 +117,44 @@ module.exports = {
newX
-=
1
;
}
//
Continue if within bound
s
//
Check for collisions with wall
s
if
(
newX
>=
0
&&
newX
<
gameBoard
[
0
].
length
&&
newY
>=
0
&&
newY
<
gameBoard
.
length
newX
<
0
||
newX
>=
gameBoard
[
0
].
length
||
newY
<
0
||
newY
>=
gameBoard
.
length
)
{
console
.
log
(
`Player
${
cell
.
pid
}
has collided with the wall!`
);
// Remove the player from the game (indicating death)
return
gameBoard
.
map
(
row
=>
row
.
map
(
c
=>
(
c
.
pid
===
cell
.
pid
?
createCell
(
c
.
x
,
c
.
y
,
cellTypes
.
EMPTY
)
:
c
))
);
}
// Handle collision with food
if
(
gameBoard
[
newY
][
newX
].
type
===
cellTypes
.
FOOD
)
{
console
.
log
(
`Player
${
cell
.
pid
}
has eaten the food!`
);
// Add new body segment at the current head's position
let
newBodySegment
=
createCell
(
oldX
,
oldY
,
cellTypes
.
PLAYERBODY
);
newBodySegment
.
pid
=
cell
.
pid
;
newBodySegment
.
next
=
cell
.
next
;
cell
.
next
=
newBodySegment
;
// Place new food somewhere else
gameBoard
=
addFood
(
gameBoard
);
}
// Snake head to new position by updating cell object
let
newHeadCell
=
createCell
(
newX
,
newY
,
cellTypes
.
PLAYERHEAD
);
newHeadCell
.
pid
=
cell
.
pid
;
gameBoard
[
newY
][
newX
]
=
newHeadCell
;
newHeadCell
.
direction
=
tempVar
;
newHeadCell
.
next
=
cell
.
next
;
gameBoard
[
newY
][
newX
]
=
newHeadCell
;
// Remove previous head position
gameBoard
[
oldY
][
oldX
]
=
createCell
(
oldX
,
oldY
,
cellTypes
.
EMPTY
);
//Snake body to previous location
s
let
currentBody
=
gameBoard
[
newY
][
newX
]
.
next
;
// Move the body segment
s
let
currentBody
=
newHeadCell
.
next
;
while
(
currentBody
)
{
let
bodyOldX
=
currentBody
.
x
;
let
bodyOldY
=
currentBody
.
y
;
...
...
@@ -153,15 +171,16 @@ module.exports = {
currentBody
=
currentBody
.
next
;
}
//Remove previous body position
// Remove previous body position if it's no longer part of the snake
if
(
oldX
!==
newHeadCell
.
x
||
oldY
!==
newHeadCell
.
y
)
{
gameBoard
[
oldY
][
oldX
]
=
createCell
(
oldX
,
oldY
,
cellTypes
.
EMPTY
);
}
console
.
log
(
gameBoard
);
return
gameBoard
;
}
}
}
}
return
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