Skip to content
Projects
Groups
Snippets
Help
Loading...
Help
Submit feedback
Contribute to GitLab
Sign in
Toggle navigation
M
MingSnake_241120
Project
Project
Details
Activity
Releases
Cycle Analytics
Repository
Repository
Files
Commits
Branches
Tags
Contributors
Graph
Compare
Charts
Issues
0
Issues
0
List
Board
Labels
Milestones
Merge Requests
0
Merge Requests
0
Wiki
Wiki
Snippets
Snippets
Members
Members
Collapse sidebar
Close sidebar
Activity
Graph
Charts
Create a new issue
Commits
Issue Boards
Open sidebar
SparkProjects
MingSnake_241120
Commits
920008f0
Commit
920008f0
authored
Nov 19, 2024
by
haiyoucuv
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
init
parent
1c3ec0d6
Changes
5
Show whitespace changes
Inline
Side-by-side
Showing
5 changed files
with
205 additions
and
0 deletions
+205
-0
Test.meta
assets/Scripts/Scenes/Test.meta
+9
-0
MainGame1.ts
assets/Scripts/Scenes/Test/MainGame1.ts
+116
-0
MainGame1.ts.meta
assets/Scripts/Scenes/Test/MainGame1.ts.meta
+9
-0
Snake1.ts
assets/Scripts/Scenes/Test/Snake1.ts
+62
-0
Snake1.ts.meta
assets/Scripts/Scenes/Test/Snake1.ts.meta
+9
-0
No files found.
assets/Scripts/Scenes/Test.meta
0 → 100644
View file @
920008f0
{
"ver": "1.2.0",
"importer": "directory",
"imported": true,
"uuid": "bb1f1bdf-5a9e-42dd-8fe3-7b71358284df",
"files": [],
"subMetas": {},
"userData": {}
}
assets/Scripts/Scenes/Test/MainGame1.ts
0 → 100644
View file @
920008f0
import
{
_decorator
,
EventKeyboard
,
Input
,
input
,
KeyCode
,
Node
,
}
from
"cc"
;
import
Scene
from
"db://assets/Module/Scene"
;
import
{
Snake
}
from
"db://assets/Scripts/Scenes/MainGame/Snake"
;
const
{
ccclass
,
property
}
=
_decorator
;
export
enum
DIR
{
UP
,
DOWN
,
LEFT
,
RIGHT
,
}
@
ccclass
(
"MainGame1"
)
export
class
MainGame1
extends
Scene
{
static
bundle
:
string
=
"MainGame"
;
static
skin
:
string
=
"MainGame"
;
@
property
(
Node
)
snake
:
Node
=
null
;
@
property
speed
:
number
=
200
;
onLoad
()
{
input
.
on
(
Input
.
EventType
.
TOUCH_START
,
this
.
onTouchStart
,
this
);
input
.
on
(
Input
.
EventType
.
TOUCH_END
,
this
.
onTouchEnd
,
this
);
input
.
on
(
Input
.
EventType
.
TOUCH_CANCEL
,
this
.
onTouchEnd
,
this
);
input
.
on
(
Input
.
EventType
.
KEY_DOWN
,
this
.
onKeyDown
,
this
);
input
.
on
(
Input
.
EventType
.
KEY_UP
,
this
.
onKeyUp
,
this
);
}
curDir
:
DIR
=
DIR
.
UP
;
keyArr
=
[];
async
start
()
{
// this.schedule(() => {
// this.snake.getComponent(Snake).addNode();
// }, 1);
}
onDestroy
()
{
input
.
off
(
Input
.
EventType
.
TOUCH_START
,
this
.
onTouchStart
,
this
);
input
.
off
(
Input
.
EventType
.
TOUCH_END
,
this
.
onTouchEnd
,
this
);
input
.
off
(
Input
.
EventType
.
TOUCH_CANCEL
,
this
.
onTouchEnd
,
this
);
input
.
off
(
Input
.
EventType
.
KEY_DOWN
,
this
.
onKeyDown
,
this
);
input
.
off
(
Input
.
EventType
.
KEY_UP
,
this
.
onKeyUp
,
this
);
}
onKeyDown
(
event
:
EventKeyboard
)
{
const
keyArr
=
[
KeyCode
.
KEY_W
,
KeyCode
.
KEY_S
,
KeyCode
.
KEY_A
,
KeyCode
.
KEY_D
,
];
if
(
keyArr
.
indexOf
(
event
.
keyCode
)
>
-
1
)
{
this
.
keyArr
.
push
(
event
.
keyCode
);
}
this
.
setDir
();
}
onKeyUp
(
event
:
EventKeyboard
)
{
const
index
=
this
.
keyArr
.
indexOf
(
event
.
keyCode
);
if
(
index
>
-
1
)
{
this
.
keyArr
.
splice
(
index
,
1
);
}
this
.
setDir
();
}
setDir
()
{
if
(
!
this
.
keyArr
.
length
)
return
;
this
.
curDir
=
{
[
KeyCode
.
KEY_W
]:
DIR
.
UP
,
[
KeyCode
.
KEY_S
]:
DIR
.
DOWN
,
[
KeyCode
.
KEY_A
]:
DIR
.
LEFT
,
[
KeyCode
.
KEY_D
]:
DIR
.
RIGHT
,
}[
this
.
keyArr
[
this
.
keyArr
.
length
-
1
]];
}
onTouchStart
(
event
:
any
)
{
}
onTouchEnd
(
event
:
any
)
{
}
update
(
dt
:
number
)
{
if
(
this
.
curDir
==
DIR
.
UP
)
{
const
newY
=
this
.
snake
.
position
.
y
+
this
.
speed
*
dt
;
this
.
snake
.
setPosition
(
this
.
snake
.
position
.
x
,
newY
);
}
else
if
(
this
.
curDir
==
DIR
.
DOWN
)
{
const
newY
=
this
.
snake
.
position
.
y
-
this
.
speed
*
dt
;
this
.
snake
.
setPosition
(
this
.
snake
.
position
.
x
,
newY
);
}
else
if
(
this
.
curDir
==
DIR
.
LEFT
)
{
const
newX
=
this
.
snake
.
position
.
x
-
this
.
speed
*
dt
;
this
.
snake
.
setPosition
(
newX
,
this
.
snake
.
position
.
y
);
}
else
if
(
this
.
curDir
==
DIR
.
RIGHT
)
{
const
newX
=
this
.
snake
.
position
.
x
+
this
.
speed
*
dt
;
this
.
snake
.
setPosition
(
newX
,
this
.
snake
.
position
.
y
);
}
}
}
assets/Scripts/Scenes/Test/MainGame1.ts.meta
0 → 100644
View file @
920008f0
{
"ver": "4.0.24",
"importer": "typescript",
"imported": true,
"uuid": "701bec3a-f5a6-484b-92cf-5cb67aff8961",
"files": [],
"subMetas": {},
"userData": {}
}
assets/Scripts/Scenes/Test/Snake1.ts
0 → 100644
View file @
920008f0
import
{
_decorator
,
Component
,
instantiate
,
Node
,
Prefab
,
Vec3
}
from
"cc"
;
const
{
ccclass
,
property
}
=
_decorator
;
class
SnakeNode
{
node
:
Node
=
null
;
next
:
SnakeNode
=
null
;
}
const
_tempPos
=
new
Vec3
();
const
_tempPrePos
=
new
Vec3
();
@
ccclass
(
"Snake1"
)
export
class
Snake1
extends
Component
{
@
property
(
Prefab
)
nodePrefab
:
Prefab
=
null
;
@
property
gap
:
number
=
50
;
head
:
SnakeNode
=
null
;
last
:
SnakeNode
=
null
;
onLoad
()
{
this
.
last
=
this
.
head
=
new
SnakeNode
();
this
.
head
.
node
=
this
.
node
;
}
addNode
()
{
const
node
=
instantiate
(
this
.
nodePrefab
);
this
.
node
.
parent
.
addChild
(
node
);
node
.
position
=
this
.
last
.
node
.
position
;
const
snakeNode
=
new
SnakeNode
();
snakeNode
.
node
=
node
;
this
.
last
.
next
=
snakeNode
;
this
.
last
=
snakeNode
;
}
update
(
dt
:
number
)
{
let
pre
:
SnakeNode
=
this
.
head
;
let
current
:
SnakeNode
=
this
.
head
.
next
;
while
(
current
)
{
const
posDir
=
current
.
node
.
getPosition
(
_tempPos
)
.
subtract
(
pre
.
node
.
position
)
.
normalize
();
const
pos
=
pre
.
node
.
getPosition
(
_tempPrePos
)
.
add
(
posDir
.
multiplyScalar
(
this
.
gap
));
current
.
node
.
setPosition
(
pos
);
pre
=
current
;
current
=
current
.
next
;
}
}
}
\ No newline at end of file
assets/Scripts/Scenes/Test/Snake1.ts.meta
0 → 100644
View file @
920008f0
{
"ver": "4.0.24",
"importer": "typescript",
"imported": true,
"uuid": "917a4745-8677-4e4a-83f1-07545cfd07ef",
"files": [],
"subMetas": {},
"userData": {}
}
Write
Preview
Markdown
is supported
0%
Try again
or
attach a new file
Attach a file
Cancel
You are about to add
0
people
to the discussion. Proceed with caution.
Finish editing this message first!
Cancel
Please
register
or
sign in
to comment