Skip to content
Projects
Groups
Snippets
Help
Loading...
Help
Submit feedback
Contribute to GitLab
Sign in
Toggle navigation
G
game-stydy
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
谌继荃
game-stydy
Commits
5a6b1cd8
Commit
5a6b1cd8
authored
Dec 01, 2021
by
谌继荃
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
管道增加随机高度,分数层级放置第一层
parent
6c0ccd04
Changes
7
Hide whitespace changes
Inline
Side-by-side
Showing
7 changed files
with
76 additions
and
75 deletions
+76
-75
Background.ts
src/bird/Background.ts
+1
-1
Bird2.ts
src/bird/Bird2.ts
+0
-30
Pipe.ts
src/bird/Pipe.ts
+18
-18
PipeFactory.ts
src/bird/PipeFactory.ts
+40
-0
addGame.ts
src/bird/addGame.ts
+3
-4
MovableManager.ts
src/lib/MovableManager.ts
+12
-20
Score.ts
src/tools/Score.ts
+2
-2
No files found.
src/bird/Background.ts
View file @
5a6b1cd8
...
...
@@ -47,7 +47,7 @@ export default class Background {
score
++
;
// console.log("scoreClass", scoreClass.change );
scoreClass
.
change
(
score
);
console
.
log
(
"score"
,
score
);
//
console.log("score", score);
}
if
(
bg
.
x
>
width
)
{
bg
.
x
=
bg2
.
x
-
width
;
...
...
src/bird/Bird2.ts
deleted
100644 → 0
View file @
6c0ccd04
import
Movable
from
"../lib/Movable"
;
import
MovableManager
from
"../lib/MovableManager"
;
import
Vector2
from
"../lib/Vector2"
;
class
BirdItem
extends
Movable
{
constructor
(
stage
)
{
super
();
this
.
texture
=
FYGE
.
Texture
.
fromUrl
(
"//yun.duiba.com.cn/aurora/assets/0bcb2c26a85addb1714b4c63f2a873aafe210749.png"
);
this
.
position
.
set
(
300
,
0
);
this
.
velocity
=
new
Vector2
(
0
,
0
);
this
.
acceleration
=
new
Vector2
(
0
,
1
);
}
getCanRemove
()
{
return
false
;
}
}
export
default
class
Bird
{
constructor
(
stage
:
FYGE
.
Stage
,
movableManager
:
MovableManager
)
{
var
bird
=
new
BirdItem
(
stage
);
movableManager
.
add
(
bird
);
stage
.
addChild
(
bird
);
stage
.
addEventListener
(
FYGE
.
Event
.
ENTER_FRAME
,
()
=>
{});
}
}
src/bird/
Enemy
.ts
→
src/bird/
Pipe
.ts
View file @
5a6b1cd8
import
Movable
from
"../lib/Movable"
;
import
MovableManager
from
"../lib/MovableManager"
;
export
default
class
Enemy
extends
Movable
{
movableManager
:
MovableManager
;
constructor
(
movableManager
:
MovableManager
)
{
super
();
this
.
movableManager
=
movableManager
;
movableManager
.
add
(
this
);
this
.
texture
=
FYGE
.
Texture
.
fromUrl
(
"//yun.duiba.com.cn/aurora/assets/9fe8d613157bddffdb2bf5ca9a748e8fbb7e9471.png"
);
this
.
addEventListener
(
FYGE
.
Event
.
REMOVED_FROM_STAGE
,
()
=>
{});
}
}
import
Movable
from
"../lib/Movable"
;
import
MovableManager
from
"../lib/MovableManager"
;
export
default
class
Pipe
extends
Movable
{
movableManager
:
MovableManager
;
constructor
(
movableManager
:
MovableManager
)
{
super
();
this
.
movableManager
=
movableManager
;
movableManager
.
add
(
this
);
this
.
texture
=
FYGE
.
Texture
.
fromUrl
(
"//yun.duiba.com.cn/aurora/assets/9fe8d613157bddffdb2bf5ca9a748e8fbb7e9471.png"
);
this
.
addEventListener
(
FYGE
.
Event
.
REMOVED_FROM_STAGE
,
()
=>
{});
}
}
src/bird/
Enemy
Factory.ts
→
src/bird/
Pipe
Factory.ts
View file @
5a6b1cd8
import
MovableManager
from
"../lib/MovableManager"
;
import
Vector2
from
"../lib/Vector2"
;
import
Enemy
from
"./Enemy"
;
export
default
class
EnemyFactory
{
private
_timer
;
private
_stage
:
FYGE
.
Stage
;
movableManager
:
MovableManager
;
constructor
(
stage
:
FYGE
.
Stage
,
movableManager
:
MovableManager
)
{
this
.
movableManager
=
movableManager
;
this
.
_stage
=
stage
;
this
.
_timer
=
setInterval
(
this
.
onTimer
,
1000
);
}
onTimer
=
()
=>
{
const
enemy1
=
this
.
_stage
.
addChild
(
new
Enemy
(
this
.
movableManager
));
const
enemy2
=
this
.
_stage
.
addChild
(
new
Enemy
(
this
.
movableManager
));
enemy1
.
position
.
set
(
750
,
0
);
enemy2
.
position
.
set
(
750
,
1060
);
enemy1
.
velocity
=
new
Vector2
(
-
10
,
0
);
enemy2
.
velocity
=
new
Vector2
(
-
10
,
0
);
};
destroy
()
{
clearInterval
(
this
.
_timer
);
}
}
import
MovableManager
from
"../lib/MovableManager"
;
import
Vector2
from
"../lib/Vector2"
;
import
pipe
from
"./Pipe"
;
export
default
class
PipeFactory
{
private
_timer
;
private
_stage
:
FYGE
.
Stage
;
movableManager
:
MovableManager
;
constructor
(
stage
:
FYGE
.
Stage
,
movableManager
:
MovableManager
)
{
this
.
movableManager
=
movableManager
;
this
.
_stage
=
stage
;
this
.
_timer
=
setInterval
(
this
.
onTimer
,
1000
);
}
onTimer
=
()
=>
{
const
pipe1
=
this
.
_stage
.
addChildAt
(
new
pipe
(
this
.
movableManager
),
this
.
_stage
.
children
.
length
-
2
);
const
pipe2
=
this
.
_stage
.
addChildAt
(
new
pipe
(
this
.
movableManager
),
this
.
_stage
.
children
.
length
-
2
);
// 管道高度420
pipe1
.
height
=
Math
.
random
()
*
100
+
400
;
pipe2
.
height
=
Math
.
random
()
*
100
+
400
;
pipe1
.
position
.
set
(
750
,
0
);
pipe2
.
position
.
set
(
750
,
this
.
_stage
.
stageHeight
-
pipe2
.
height
);
pipe1
.
velocity
=
new
Vector2
(
-
10
,
0
);
pipe2
.
velocity
=
new
Vector2
(
-
10
,
0
);
console
.
log
(
pipe1
.
height
);
};
destroy
()
{
clearInterval
(
this
.
_timer
);
}
}
src/bird/addGame.ts
View file @
5a6b1cd8
...
...
@@ -3,25 +3,24 @@ import Vector2 from "../lib/Vector2";
import
Background
from
"./Background"
;
import
Bird
from
"./Bird"
;
import
Score
from
"../tools/Score"
;
import
EnemyFactory
from
"./
Enemy
Factory"
;
import
EnemyFactory
from
"./
Pipe
Factory"
;
export
function
addGame
(
stage
:
FYGE
.
Stage
)
{
const
movableManager
=
new
MovableManager
(
stage
);
//创建管理器
const
background
=
new
Background
(
stage
,
movableManager
);
const
bird
=
stage
.
addChild
(
new
Bird
());
//有加速度
// const bird = new Bird(stage, movableManager); //有加速度
bird
.
position
.
set
(
300
,
500
);
bird
.
velocity
=
new
Vector2
(
0
,
0
);
bird
.
acceleration
=
new
Vector2
(
0
,
1
);
const
enemyFactory
=
new
EnemyFactory
(
stage
,
movableManager
);
movableManager
.
add
(
bird
);
// 当前分数
const
score
=
new
Score
(
stage
);
function
onGamOver
()
{
console
.
log
(
"onGamOver"
);
// alert(`gameOver 您获得的分数为${movableManager.Score}`);
...
...
src/lib/MovableManager.ts
View file @
5a6b1cd8
import
Enemy
from
"../bird/Enemy
"
;
import
Pipe
from
"../bird/Pipe
"
;
import
Bird
from
"../bird/Bird"
;
import
Movable
from
"./Movable"
;
import
ScoreClass
from
"../tools/Score"
;
let
scoreClass
;
let
birdClass
;
export
default
class
MovableManager
extends
FYGE
.
EventDispatcher
{
private
_stage
:
FYGE
.
Stage
;
public
Score
:
0
;
bird
:
Bird
;
private
_movableList
:
Movable
[]
=
[];
constructor
(
stage
:
FYGE
.
Stage
)
{
super
();
this
.
_stage
=
stage
;
this
.
Score
=
0
;
scoreClass
=
new
ScoreClass
(
stage
);
this
.
_stage
.
addEventListener
(
FYGE
.
Event
.
ENTER_FRAME
,
this
.
onEnterFrame
);
}
onEnterFrame
=
()
=>
{
this
.
step
();
this
.
checkBirdHeight
();
this
.
check
Enemy
();
this
.
check
Pipe
();
this
.
checkHitByBird
();
};
...
...
@@ -33,38 +27,37 @@ export default class MovableManager extends FYGE.EventDispatcher {
window
.
dispatchEvent
(
new
Event
(
"gameover"
));
}
// 小鸟碰到顶部和底部 死亡
checkBirdHeight
()
{
const
BirdList
=
this
.
_movableList
.
filter
((
i
)
=>
i
instanceof
Bird
);
const
bird
=
BirdList
[
0
];
console
.
log
(
"移动对象bird:"
,
bird
.
y
);
//
console.log("移动对象bird:", bird.y);
if
(
bird
.
y
>
this
.
_stage
.
stageHeight
-
bird
.
height
||
bird
.
y
<=
0
)
{
console
.
log
(
"死亡"
);
this
.
gameOver
();
}
}
// 检查管道 回收对象
check
Enemy
()
{
const
EnemyList
=
this
.
_movableList
.
filter
((
i
)
=>
i
instanceof
Enemy
);
console
.
log
(
"移动对象EnemyList:"
,
Enemy
List
);
Enemy
List
.
forEach
((
item
)
=>
{
// 检查管道
移除屏幕就
回收对象
check
Pipe
()
{
const
PipeList
=
this
.
_movableList
.
filter
((
i
)
=>
i
instanceof
Pipe
);
// console.log("移动对象PipeList:", Pipe
List);
Pipe
List
.
forEach
((
item
)
=>
{
if
(
item
.
x
<=
0
)
{
this
.
remove
(
item
);
}
});
console
.
log
(
"this._movableList"
,
this
.
_movableList
);
//
console.log("this._movableList", this._movableList);
}
// 小鸟和管道的碰撞
checkHitByBird
()
{
const
enemyList
=
this
.
_movableList
.
filter
((
i
)
=>
i
instanceof
Enemy
);
const
PipeList
=
this
.
_movableList
.
filter
((
i
)
=>
i
instanceof
Pipe
);
const
BirdList
=
this
.
_movableList
.
filter
((
i
)
=>
i
instanceof
Bird
);
const
bird
=
BirdList
[
0
];
enemyList
.
forEach
((
enemy
)
=>
{
if
(
this
.
checkHit
(
bird
,
enemy
))
{
PipeList
.
forEach
((
Pipe
)
=>
{
if
(
this
.
checkHit
(
bird
,
Pipe
))
{
console
.
log
(
"被碰撞了,游戏结束了"
);
this
.
gameOver
();
return
;
...
...
@@ -83,7 +76,6 @@ export default class MovableManager extends FYGE.EventDispatcher {
return
x0
<
x1
&&
y0
<
y1
;
}
private
step
()
{
this
.
_movableList
.
forEach
((
item
)
=>
item
.
step
());
}
...
...
src/tools/Score.ts
View file @
5a6b1cd8
...
...
@@ -8,7 +8,7 @@ export default class Score {
scoreText
.
x
=
250
;
scoreText
.
y
=
30
;
scoreText
.
size
=
45
;
stage
.
addChild
(
scoreText
);
stage
.
addChild
At
(
scoreText
,
stage
.
children
.
length
-
1
);
stage
.
addEventListener
(
"score"
,
this
.
change
);
window
.
addEventListener
(
"gameover"
,
this
.
onGamOver
);
...
...
@@ -17,7 +17,7 @@ export default class Score {
change
(
changeScore
)
{
score
=
changeScore
;
scoreText
.
text
=
`当前分数:
${
score
}
`
;
console
.
log
(
"change---scoreText.text"
,
scoreText
.
text
);
//
console.log("change---scoreText.text", scoreText.text);
}
onGamOver
()
{
...
...
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