Skip to content
Projects
Groups
Snippets
Help
Loading...
Help
Submit feedback
Contribute to GitLab
Sign in
Toggle navigation
G
games
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
CI / CD
CI / CD
Pipelines
Jobs
Schedules
Charts
Wiki
Wiki
Snippets
Snippets
Members
Members
Collapse sidebar
Close sidebar
Activity
Graph
Charts
Create a new issue
Jobs
Commits
Issue Boards
Open sidebar
熊东起
games
Commits
97019046
Commit
97019046
authored
May 31, 2021
by
熊东起
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
flex:--1
parent
bf370dff
Changes
2
Hide whitespace changes
Inline
Side-by-side
Showing
2 changed files
with
140 additions
and
127 deletions
+140
-127
GameCfg.ts
src/GameCfg.ts
+1
-1
GameScene.ts
src/GameScene.ts
+139
-126
No files found.
src/GameCfg.ts
View file @
97019046
...
@@ -24,5 +24,5 @@ export namespace Config {
...
@@ -24,5 +24,5 @@ export namespace Config {
export
const
GRID_COUNT
=
10
;
export
const
GRID_COUNT
=
10
;
/** 最大可玩次数 */
/** 最大可玩次数 */
export
const
MAX_MOVES
=
2
;
export
const
MAX_MOVES
=
2
00
;
}
}
src/GameScene.ts
View file @
97019046
...
@@ -10,148 +10,161 @@ import { Grid } from "./components/Grid";
...
@@ -10,148 +10,161 @@ import { Grid } from "./components/Grid";
import
{
Config
}
from
"./GameCfg"
;
import
{
Config
}
from
"./GameCfg"
;
/** 颜色类型 */
/** 颜色类型 */
export
type
Color
=
string
;
export
type
Color
=
string
;
enum
STATUS
{
win
=
1
,
lose
=
2
,
continue
=
3
}
export
class
GameScene
extends
FYGE
.
Container
{
export
class
GameScene
extends
FYGE
.
Container
{
private
moves
:
number
=
0
;
private
moves
:
number
=
0
;
private
btns
:
FYGE
.
Graphics
[]
=
[];
private
btns
:
FYGE
.
Graphics
[]
=
[];
private
grids
:
Grid
[][]
=
[];
private
gameContainer
:
FYGE
.
Container
;
private
gameContainer
:
FYGE
.
Container
;
private
stepNum
:
FYGE
.
TextField
;
private
matched
:
Grid
[]
=
[];
constructor
()
{
super
();
this
.
initUi
();
}
initUi
()
{
this
.
initBtns
();
this
.
createMap
();
}
/**
* @description: 初始化6个按钮
*/
initBtns
()
{
this
.
btns
=
[];
for
(
let
i
=
0
;
i
<
Config
.
GRID_COLORS
.
length
;
i
++
)
{
let
sx
=
0
;
let
sy
=
110
*
i
+
50
;
let
color
=
Config
.
GRID_COLORS
[
i
];
let
_rect
=
this
.
addChild
(
new
FYGE
.
Graphics
()).
lineStyle
(
0.5
,
0xffffff
,
1
).
beginFill
(
color
,
1
).
drawRect
(
sx
,
sy
,
100
,
100
).
endFill
();
_rect
.
name
=
String
(
color
);
_rect
.
addEventListener
(
FYGE
.
MouseEvent
.
CLICK
,
()
=>
this
.
handleBtnClick
(
String
(
color
)),
this
);
this
.
btns
[
i
]
=
_rect
;
}
}
/**
constructor
()
{
* @description: 点击按钮
super
();
* @param {*} color
this
.
initUi
();
*/
handleBtnClick
(
btnColor
:
Color
)
{
// TODO: 第二次点击不变了
console
.
log
(
"dd"
,
btnColor
);
let
oldColor
=
this
.
grids
[
0
][
0
].
oldColor
;
console
.
log
(
oldColor
);
this
.
moves
++
;
this
.
matched
=
[];
this
.
doMatch
(
oldColor
,
btnColor
,
0
,
0
);
if
(
this
.
matched
.
length
>
0
)
this
.
doChangeColor
();
}
/**
* @description: 匹配色块
* @param {Color} oldColor
* @param {Color} newColor
* @param {number} x
* @param {number} y
* @return {*}
*/
doMatch
(
oldColor
:
Color
,
newColor
:
Color
,
x
:
number
,
y
:
number
)
{
if
(
oldColor
===
newColor
||
this
.
grids
[
x
][
y
].
newColor
!==
oldColor
)
{
return
;
}
this
.
grids
[
x
][
y
].
newColor
=
newColor
;
if
(
this
.
matched
.
indexOf
(
this
.
grids
[
x
][
y
])
===
-
1
)
{
this
.
matched
.
push
(
this
.
grids
[
x
][
y
]);
}
}
// 上下左右搂一圈
initUi
()
{
if
(
x
>
0
)
{
this
.
initBtns
();
this
.
doMatch
(
oldColor
,
newColor
,
x
-
1
,
y
);
this
.
createMapByColor
(
);
}
}
if
(
x
<
Config
.
GRID_COUNT
-
1
)
{
/**
this
.
doMatch
(
oldColor
,
newColor
,
x
+
1
,
y
);
* @description: 初始化6个按钮
*/
initBtns
()
{
this
.
btns
=
[];
for
(
let
i
=
0
;
i
<
Config
.
GRID_COLORS
.
length
;
i
++
)
{
let
sx
=
0
;
let
sy
=
110
*
i
+
50
;
let
color
=
Config
.
GRID_COLORS
[
i
];
let
_rect
=
this
.
addChild
(
new
FYGE
.
Graphics
()).
lineStyle
(
0.5
,
0xffffff
,
1
).
beginFill
(
color
,
1
).
drawRect
(
sx
,
sy
,
100
,
100
).
endFill
();
_rect
.
name
=
String
(
color
);
_rect
.
addEventListener
(
FYGE
.
MouseEvent
.
CLICK
,
()
=>
this
.
onClickBtn
(
color
),
this
);
this
.
btns
[
i
]
=
_rect
;
}
let
text
=
this
.
addChild
(
new
FYGE
.
TextField
());
text
.
text
=
"步数0"
;
text
.
x
=
375
;
text
.
y
=
20
;
this
.
stepNum
=
text
;
}
}
if
(
y
>
0
)
{
this
.
doMatch
(
oldColor
,
newColor
,
x
,
y
-
1
);
get
step
():
number
{
return
this
.
moves
;
}
}
if
(
y
<
Config
.
GRID_COUNT
-
1
)
{
set
step
(
v
:
number
)
{
this
.
doMatch
(
oldColor
,
newColor
,
x
,
y
+
1
);
this
.
moves
=
v
;
this
.
stepNum
.
text
=
"步数"
+
String
(
v
);
}
}
}
/**
/** 创建地图 */
* @description: 开始更新格子
map
:
number
[][];
* @param {*}
createMapByColor
()
{
* @return {*}
this
.
gameContainer
=
new
FYGE
.
Container
();
*/
this
.
addChild
(
this
.
gameContainer
);
doChangeColor
()
{
this
.
map
=
[];
console
.
log
(
"doChangeColor"
);
for
(
let
i
=
0
;
i
<
Config
.
GRID_COUNT
;
i
++
)
{
this
.
map
[
i
]
=
[];
for
(
let
j
=
0
;
j
<
Config
.
GRID_COUNT
;
j
++
)
{
let
_color
=
Config
.
GRID_COLORS
[
Math
.
floor
(
Math
.
random
()
*
6
)];
this
.
map
[
i
].
push
(
_color
);
}
}
this
.
rendermap
();
this
.
matched
.
map
((
item
)
=>
{
}
item
.
clear
()
.
beginFill
(
+
item
.
newColor
,
1
)
.
drawRect
(
0
,
0
,
Config
.
GRID_SIZE
,
Config
.
GRID_SIZE
)
.
endFill
();
item
.
oldColor
=
item
.
newColor
;
});
/** 渲染地图 */
rendermap
()
{
if
(
this
.
map
&&
this
.
map
.
length
>
0
)
{
let
startX
=
(
Config
.
PSD_SIZE
[
0
]
-
Config
.
GRID_SIZE
*
Config
.
GRID_COUNT
)
/
2
;
this
.
gameContainer
.
removeAllChildren
();
this
.
map
.
forEach
((
colum
,
i
)
=>
{
colum
.
forEach
((
row
,
j
)
=>
{
//渲染
let
sx
=
startX
+
j
*
Config
.
GRID_SIZE
;
let
sy
=
66
+
i
*
Config
.
GRID_SIZE
;
let
_rect
=
this
.
gameContainer
.
addChild
(
new
FYGE
.
Graphics
());
_rect
.
lineStyle
(
1
,
0xffffff
,
1
).
beginFill
(
row
,
1
).
drawRect
(
0
,
0
,
Config
.
GRID_SIZE
,
Config
.
GRID_SIZE
).
endFill
();
_rect
.
position
.
x
=
sx
;
_rect
.
position
.
y
=
sy
;
});
});
}
if
(
this
.
checkWin
())
{
console
.
log
(
"你赢了"
);
}
else
if
(
this
.
moves
>=
Config
.
MAX_MOVES
)
{
console
.
log
(
"你输了"
);
}
}
}
checkWin
()
{
/** 匹配 */
let
startColor
=
this
.
grids
[
0
][
0
].
newColor
;
matchColor
(
newColor
:
number
)
{
for
(
var
x
=
0
;
x
<
Config
.
GRID_COUNT
;
x
++
)
{
let
lastMap
=
[];
for
(
var
y
=
0
;
y
<
Config
.
GRID_COUNT
;
y
++
)
{
let
oldColor
=
this
.
map
[
0
][
0
];
if
(
this
.
grids
[
x
][
y
].
newColor
!==
startColor
)
{
return
false
;
for
(
let
i
=
0
;
i
<
this
.
map
.
length
;
i
++
)
{
for
(
let
j
=
0
;
j
<
this
.
map
[
i
].
length
;
j
++
)
{
let
leftBlock
=
j
>
0
?
this
.
map
[
i
][
j
-
1
]
:
0
,
topBlock
=
i
>
0
?
this
.
map
[
i
-
1
][
j
]
:
0
;
let
str
=
`
${
i
}
-
${
j
}
`
;
if
(
this
.
map
[
i
][
j
]
==
oldColor
)
{
if
(
topBlock
==
oldColor
)
{
if
(
lastMap
.
indexOf
(
`
${
i
-
1
}
-
${
j
}
`
)
!==
-
1
)
{
if
(
lastMap
.
indexOf
(
str
)
===
-
1
)
{
lastMap
.
push
(
str
);
}
}
}
if
(
leftBlock
==
oldColor
)
{
if
(
lastMap
.
indexOf
(
`
${
i
}
-
${
j
-
1
}
`
)
!==
-
1
)
{
if
(
lastMap
.
indexOf
(
str
)
===
-
1
)
{
lastMap
.
push
(
str
);
}
}
}
if
(
i
==
0
&&
j
==
0
)
{
if
(
lastMap
.
indexOf
(
str
)
===
-
1
)
{
lastMap
.
push
(
str
);
}
}
}
}
}
}
}
lastMap
.
forEach
((
item
,
index
)
=>
{
let
i
=
item
.
split
(
"-"
)[
0
],
j
=
item
.
split
(
"-"
)[
1
];
this
.
map
[
i
][
j
]
=
newColor
;
});
}
}
return
true
;
/** 选择颜色 */
}
onClickBtn
(
color
:
number
)
{
this
.
matchColor
(
color
);
/**
this
.
rendermap
();
* @description: 创建格子地图
if
(
this
.
onCheck
()
==
STATUS
.
win
)
{
*/
alert
(
"成功"
);
createMap
()
{
}
else
if
(
this
.
onCheck
()
==
STATUS
.
lose
)
{
this
.
grids
=
[];
alert
(
"失败"
);
this
.
gameContainer
=
new
FYGE
.
Container
();
}
this
.
addChild
(
this
.
gameContainer
);
this
.
step
++
;
let
gSize
=
Config
.
GRID_SIZE
;
}
// 起始X
let
startX
=
(
Config
.
PSD_SIZE
[
0
]
-
gSize
*
Config
.
GRID_COUNT
)
/
2
;
for
(
var
x
=
0
;
x
<
Config
.
GRID_COUNT
;
x
++
)
{
this
.
grids
[
x
]
=
[];
for
(
var
y
=
0
;
y
<
Config
.
GRID_COUNT
;
y
++
)
{
let
sx
=
startX
+
x
*
gSize
;
let
sy
=
66
+
y
*
gSize
;
// 随机颜色
let
_index
=
Math
.
floor
(
Math
.
random
()
*
6
);
let
_rect
=
this
.
gameContainer
.
addChild
(
new
Grid
());
_rect
.
lineStyle
(
1
,
0xffffff
,
1
).
beginFill
(
Config
.
GRID_COLORS
[
_index
],
1
).
drawRect
(
0
,
0
,
gSize
,
gSize
).
endFill
();
/** 检查输赢 */
_rect
.
position
.
x
=
sx
;
onCheck
()
{
_rect
.
position
.
y
=
sy
;
if
(
this
.
step
>
Config
.
MAX_MOVES
)
{
let
color
=
String
(
Config
.
GRID_COLORS
[
_index
]);
return
STATUS
.
lose
;
_rect
.
oldColor
=
color
;
}
_rect
.
newColor
=
color
;
if
(
this
.
step
<=
Config
.
MAX_MOVES
)
{
this
.
grids
[
x
][
y
]
=
_rect
;
let
start
=
this
.
map
[
0
][
0
],
flag
=
0
;
}
for
(
let
i
=
0
;
i
<
this
.
map
.
length
;
i
++
)
{
for
(
let
j
=
0
;
j
<
this
.
map
[
i
].
length
;
j
++
)
{
if
(
this
.
map
[
i
][
j
]
!=
start
)
{
flag
=
1
;
}
}
}
if
(
flag
==
0
)
{
return
STATUS
.
win
;
}
}
return
STATUS
.
continue
;
}
}
}
}
}
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