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
aa7315ad
Commit
aa7315ad
authored
Nov 20, 2024
by
haiyoucuv
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
init
parent
29f47b46
Changes
6
Expand all
Show whitespace changes
Inline
Side-by-side
Showing
6 changed files
with
1112 additions
and
441 deletions
+1112
-441
AISnake.ts
assets/Scripts/Scenes/MainGame/AISnake.ts
+794
-127
Animal.ts
assets/Scripts/Scenes/MainGame/Animal.ts
+3
-3
FondManger.ts
assets/Scripts/Scenes/MainGame/FondManger.ts
+2
-2
Global.ts
assets/Scripts/Scenes/MainGame/Global.ts
+1
-1
MainGame.ts
assets/Scripts/Scenes/MainGame/MainGame.ts
+6
-6
Snake.ts
assets/Scripts/Scenes/MainGame/Snake.ts
+306
-302
No files found.
assets/Scripts/Scenes/MainGame/AISnake.ts
View file @
aa7315ad
This diff is collapsed.
Click to expand it.
assets/Scripts/Scenes/MainGame/Animal.ts
View file @
aa7315ad
...
@@ -365,7 +365,7 @@ export class Animal extends Component {
...
@@ -365,7 +365,7 @@ export class Animal extends Component {
);
);
const
halfWidth
=
Global
.
MAP_WIDTH
/
2
;
const
halfWidth
=
Global
.
MAP_WIDTH
/
2
;
const
halfHeight
=
Global
.
MAP_HIGHT
/
2
;
const
halfHeight
=
Global
.
MAP_H
E
IGHT
/
2
;
// 如果要超出地图边界,则转向
// 如果要超出地图边界,则转向
if
(
this
.
v
.
x
<=
-
halfWidth
||
this
.
v
.
x
>=
halfWidth
||
if
(
this
.
v
.
x
<=
-
halfWidth
||
this
.
v
.
x
>=
halfWidth
||
...
@@ -418,8 +418,8 @@ export class Animal extends Component {
...
@@ -418,8 +418,8 @@ export class Animal extends Component {
Global
.
MAP_WIDTH
/
2
-
50
Global
.
MAP_WIDTH
/
2
-
50
);
);
const
y
=
math
.
randomRangeInt
(
const
y
=
math
.
randomRangeInt
(
-
(
Global
.
MAP_HIGHT
/
2
-
50
),
-
(
Global
.
MAP_H
E
IGHT
/
2
-
50
),
Global
.
MAP_HIGHT
/
2
-
50
Global
.
MAP_H
E
IGHT
/
2
-
50
);
);
this
.
reInit
(
x
,
y
);
this
.
reInit
(
x
,
y
);
});
});
...
...
assets/Scripts/Scenes/MainGame/FondManger.ts
View file @
aa7315ad
...
@@ -70,7 +70,7 @@ export class FondManger extends Component {
...
@@ -70,7 +70,7 @@ export class FondManger extends Component {
}
}
if
(
!
y
)
{
if
(
!
y
)
{
y
=
math
.
randomRangeInt
(
-
(
Global
.
MAP_H
IGHT
/
2
-
50
),
Global
.
MAP_H
IGHT
/
2
-
50
);
y
=
math
.
randomRangeInt
(
-
(
Global
.
MAP_H
EIGHT
/
2
-
50
),
Global
.
MAP_HE
IGHT
/
2
-
50
);
}
}
// 从对象池获取食物节点
// 从对象池获取食物节点
...
@@ -101,7 +101,7 @@ export class FondManger extends Component {
...
@@ -101,7 +101,7 @@ export class FondManger extends Component {
initItem
=
(
_
:
number
)
=>
{
initItem
=
(
_
:
number
)
=>
{
// 随机生成位置
// 随机生成位置
const
x
=
math
.
randomRangeInt
(
-
(
Global
.
MAP_WIDTH
/
2
-
50
),
Global
.
MAP_WIDTH
/
2
-
50
);
const
x
=
math
.
randomRangeInt
(
-
(
Global
.
MAP_WIDTH
/
2
-
50
),
Global
.
MAP_WIDTH
/
2
-
50
);
const
y
=
math
.
randomRangeInt
(
-
(
Global
.
MAP_H
IGHT
/
2
-
50
),
Global
.
MAP_H
IGHT
/
2
-
50
);
const
y
=
math
.
randomRangeInt
(
-
(
Global
.
MAP_H
EIGHT
/
2
-
50
),
Global
.
MAP_HE
IGHT
/
2
-
50
);
this
.
addFood
(
x
,
y
);
this
.
addFood
(
x
,
y
);
};
};
...
...
assets/Scripts/Scenes/MainGame/Global.ts
View file @
aa7315ad
...
@@ -22,7 +22,7 @@ export class Global {
...
@@ -22,7 +22,7 @@ export class Global {
public
static
MAP_WIDTH
:
number
=
5000
;
public
static
MAP_WIDTH
:
number
=
5000
;
/** 地图高度 */
/** 地图高度 */
public
static
MAP_HIGHT
:
number
=
4000
;
public
static
MAP_H
E
IGHT
:
number
=
4000
;
/** 当前皮肤ID */
/** 当前皮肤ID */
public
static
skinId
:
number
=
0
;
public
static
skinId
:
number
=
0
;
...
...
assets/Scripts/Scenes/MainGame/MainGame.ts
View file @
aa7315ad
...
@@ -95,24 +95,24 @@ export class MainGame extends Scene {
...
@@ -95,24 +95,24 @@ export class MainGame extends Scene {
// EPhysics2DDrawFlags.Shape;
// EPhysics2DDrawFlags.Shape;
Global
.
MAP_WIDTH
=
this
.
uiBg
.
contentSize
.
x
;
Global
.
MAP_WIDTH
=
this
.
uiBg
.
contentSize
.
x
;
Global
.
MAP_HIGHT
=
this
.
uiBg
.
contentSize
.
y
;
Global
.
MAP_H
E
IGHT
=
this
.
uiBg
.
contentSize
.
y
;
// 初始化墙壁
// 初始化墙壁
const
wallTop
=
this
.
uiBg
.
node
.
getChildByName
(
"WALL_TOP"
);
const
wallTop
=
this
.
uiBg
.
node
.
getChildByName
(
"WALL_TOP"
);
wallTop
.
setPosition
(
0
,
Global
.
MAP_HIGHT
/
2
);
wallTop
.
setPosition
(
0
,
Global
.
MAP_H
E
IGHT
/
2
);
wallTop
.
getComponent
(
UITransform
).
width
=
Global
.
MAP_WIDTH
;
wallTop
.
getComponent
(
UITransform
).
width
=
Global
.
MAP_WIDTH
;
const
wallBottom
=
this
.
uiBg
.
node
.
getChildByName
(
"WALL_BOTTOM"
);
const
wallBottom
=
this
.
uiBg
.
node
.
getChildByName
(
"WALL_BOTTOM"
);
wallBottom
.
setPosition
(
0
,
-
Global
.
MAP_HIGHT
/
2
);
wallBottom
.
setPosition
(
0
,
-
Global
.
MAP_H
E
IGHT
/
2
);
wallBottom
.
getComponent
(
UITransform
).
width
=
Global
.
MAP_WIDTH
;
wallBottom
.
getComponent
(
UITransform
).
width
=
Global
.
MAP_WIDTH
;
const
wallLeft
=
this
.
uiBg
.
node
.
getChildByName
(
"WALL_LEFT"
);
const
wallLeft
=
this
.
uiBg
.
node
.
getChildByName
(
"WALL_LEFT"
);
wallLeft
.
setPosition
(
-
Global
.
MAP_WIDTH
/
2
,
0
);
wallLeft
.
setPosition
(
-
Global
.
MAP_WIDTH
/
2
,
0
);
wallLeft
.
getComponent
(
UITransform
).
height
=
Global
.
MAP_HIGHT
;
wallLeft
.
getComponent
(
UITransform
).
height
=
Global
.
MAP_H
E
IGHT
;
const
wallRight
=
this
.
uiBg
.
node
.
getChildByName
(
"WALL_RIGHT"
);
const
wallRight
=
this
.
uiBg
.
node
.
getChildByName
(
"WALL_RIGHT"
);
wallRight
.
setPosition
(
Global
.
MAP_WIDTH
/
2
,
0
);
wallRight
.
setPosition
(
Global
.
MAP_WIDTH
/
2
,
0
);
wallRight
.
getComponent
(
UITransform
).
height
=
Global
.
MAP_HIGHT
;
wallRight
.
getComponent
(
UITransform
).
height
=
Global
.
MAP_H
E
IGHT
;
this
.
player
.
init
();
this
.
player
.
init
();
...
@@ -190,7 +190,7 @@ export class MainGame extends Scene {
...
@@ -190,7 +190,7 @@ export class MainGame extends Scene {
initItem
=
(
index
:
number
)
=>
{
initItem
=
(
index
:
number
)
=>
{
const
node
=
PoolManager
.
instance
.
getNode
(
this
.
animalPrefab
);
const
node
=
PoolManager
.
instance
.
getNode
(
this
.
animalPrefab
);
const
x
=
math
.
randomRangeInt
(
-
(
Global
.
MAP_WIDTH
/
2
-
50
),
Global
.
MAP_WIDTH
/
2
-
50
);
const
x
=
math
.
randomRangeInt
(
-
(
Global
.
MAP_WIDTH
/
2
-
50
),
Global
.
MAP_WIDTH
/
2
-
50
);
const
y
=
math
.
randomRangeInt
(
-
(
Global
.
MAP_H
IGHT
/
2
-
50
),
Global
.
MAP_H
IGHT
/
2
-
50
);
const
y
=
math
.
randomRangeInt
(
-
(
Global
.
MAP_H
EIGHT
/
2
-
50
),
Global
.
MAP_HE
IGHT
/
2
-
50
);
node
.
getComponent
(
AISnake
)?.
init
({
node
.
getComponent
(
AISnake
)?.
init
({
x
,
y
,
x
,
y
,
...
...
assets/Scripts/Scenes/MainGame/Snake.ts
View file @
aa7315ad
...
@@ -46,15 +46,15 @@ export class Snake extends Component {
...
@@ -46,15 +46,15 @@ export class Snake extends Component {
@
property
(
Prefab
)
bodyPrefab
:
Prefab
=
null
;
@
property
(
Prefab
)
bodyPrefab
:
Prefab
=
null
;
// 私有成员变量
// 私有成员变量
private
bodyArr
:
Node
[]
=
[];
bodyArr
:
Node
[]
=
[];
private
imgHead
:
SpriteFrame
=
null
;
private
imgHead
:
SpriteFrame
=
null
;
private
imgBody1
:
SpriteFrame
=
null
;
private
imgBody1
:
SpriteFrame
=
null
;
private
imgBody2
:
SpriteFrame
=
null
;
private
imgBody2
:
SpriteFrame
=
null
;
// 蛇的状态
// 蛇的状态
protected
isLife
:
boolean
=
false
;
isLife
:
boolean
=
false
;
private
scale
:
number
=
0.2
;
private
scale
:
number
=
0.2
;
protected
speed
:
number
=
600
;
speed
:
number
=
600
;
private
energy
:
number
=
0
;
private
energy
:
number
=
0
;
private
tag
:
number
=
0
;
private
tag
:
number
=
0
;
...
@@ -63,6 +63,10 @@ export class Snake extends Component {
...
@@ -63,6 +63,10 @@ export class Snake extends Component {
private
vh
:
number
=
Global
.
visibleSize
.
height
/
2
+
100
;
private
vh
:
number
=
Global
.
visibleSize
.
height
/
2
+
100
;
private
ready
:
boolean
=
false
;
private
ready
:
boolean
=
false
;
get
radius
()
{
return
this
.
scale
*
50
;
}
// 初始化方法
// 初始化方法
public
async
init
(
config
:
IInitConfig
=
{})
{
public
async
init
(
config
:
IInitConfig
=
{})
{
...
@@ -280,7 +284,7 @@ export class Snake extends Component {
...
@@ -280,7 +284,7 @@ export class Snake extends Component {
// 边界检查
// 边界检查
const
mapHalfWidth
=
Global
.
MAP_WIDTH
/
2
;
const
mapHalfWidth
=
Global
.
MAP_WIDTH
/
2
;
const
mapHalfHeight
=
Global
.
MAP_H
IGHT
/
2
;
const
mapHalfHeight
=
Global
.
MAP_HE
IGHT
/
2
;
if
(
if
(
newHeadPos
.
x
<=
-
mapHalfWidth
||
newHeadPos
.
x
>=
mapHalfWidth
||
newHeadPos
.
x
<=
-
mapHalfWidth
||
newHeadPos
.
x
>=
mapHalfWidth
||
newHeadPos
.
y
<=
-
mapHalfHeight
||
newHeadPos
.
y
>=
mapHalfHeight
newHeadPos
.
y
<=
-
mapHalfHeight
||
newHeadPos
.
y
>=
mapHalfHeight
...
...
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