Skip to content
GitLab
Explore
Sign in
Register
Primary navigation
Search or go to…
Project
J
jrag_nodeWorkshop
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
Service Desk
Analyze
Value stream analytics
Contributor analytics
CI/CD analytics
Repository analytics
Model experiments
Help
Help
Support
GitLab documentation
Compare GitLab plans
Community forum
Contribute to GitLab
Provide feedback
Keyboard shortcuts
?
Snippets
Groups
Projects
Show more breadcrumbs
Jonas Leder
jrag_nodeWorkshop
Commits
56e0d0ab
Commit
56e0d0ab
authored
4 years ago
by
Jonas Leder
Browse files
Options
Downloads
Patches
Plain Diff
wait for button press, before starting game
parent
a64db354
Branches
Branches containing commit
No related tags found
No related merge requests found
Changes
2
Show whitespace changes
Inline
Side-by-side
Showing
2 changed files
_src/assets/ts/snake.ts
+23
-11
23 additions, 11 deletions
_src/assets/ts/snake.ts
_src/snake.html
+5
-2
5 additions, 2 deletions
_src/snake.html
with
28 additions
and
13 deletions
_src/assets/ts/snake.ts
+
23
−
11
View file @
56e0d0ab
...
...
@@ -2,10 +2,13 @@ let fieldSize = 20;
let
snakeField
:
HTMLElement
=
document
.
getElementById
(
"
snakeField
"
);
let
scoreField
:
HTMLElement
=
document
.
getElementById
(
"
score
"
);
let
highscoreField
:
HTMLElement
=
document
.
getElementById
(
"
highscore
"
);
let
snake
:
number
[][]
=
[[
10
,
10
],
[
10
,
11
],
[
10
,
12
]];
let
snakedir
:
number
=
2
;
let
target
:
number
[]
=
[]
let
score
=
0
;
let
score
:
number
=
0
;
let
highscore
:
number
=
Number
(
localStorage
.
getItem
(
"
lastname
"
));
let
firstButtonPressed
:
boolean
=
false
;
function
getRandomInt
(
max
:
number
)
{
return
Math
.
floor
(
Math
.
random
()
*
Math
.
floor
(
max
))
+
1
;
...
...
@@ -73,13 +76,19 @@ function detectEnd(){
}
if
(
gameFinished
){
if
(
highscore
<
score
){
localStorage
.
setItem
(
"
lastname
"
,
String
(
score
));
alert
(
"
Game Over, your score:
"
+
score
+
"
\n
This is your new highscore
"
);
}
else
{
alert
(
"
Game Over, your score:
"
+
score
);
}
clearInterval
(
mainLoop
);
location
.
reload
();
}
}
let
mainLoop
=
window
.
setInterval
(
function
(){
if
(
firstButtonPressed
)
{
for
(
let
y
:
number
=
0
;
y
<
snake
.
length
;
y
++
)
{
let
currentSnake
=
snake
[
y
];
if
(
currentSnake
[
0
]
==
target
[
0
]
&&
currentSnake
[
1
]
==
target
[
1
])
{
...
...
@@ -91,10 +100,12 @@ let mainLoop = window.setInterval(function(){
moveSnake
();
detectEnd
()
drawField
();
}
},
600
);
//keypress handler
document
.
addEventListener
(
'
keydown
'
,
function
(
event
)
{
firstButtonPressed
=
true
;
const
key
:
string
=
event
.
key
;
switch
(
key
){
case
"
ArrowRight
"
:
...
...
@@ -111,5 +122,6 @@ document.addEventListener('keydown', function(event) {
}
});
highscoreField
.
innerText
=
String
(
highscore
);
target
=
[
getRandomInt
(
fieldSize
),
getRandomInt
(
fieldSize
)];
drawField
();
\ No newline at end of file
This diff is collapsed.
Click to expand it.
_src/snake.html
+
5
−
2
View file @
56e0d0ab
...
...
@@ -24,13 +24,16 @@
</header>
<main
id=
"content"
>
<div
class=
"snake-textbox"
id=
"text-Left"
>
<span>
<h1>
Score:
<span
id=
"score"
>
0
</span></h1>
<h2>
Highscore:
<span
id=
"highscore"
>
0
</span></h2>
</span>
</div>
<div
id=
"snakeField"
></div>
<div
class=
"snake-textbox"
id=
"text-Right"
>
<span>
<h2>
Usage:
</h2>
<p>
U
se your arrow buttons to navigate the snake
onto the red target
</p>
<p>
To start the game press any key, then u
se your arrow buttons to navigate the snake
.
</p>
</span>
</div>
</main>
...
...
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