Skip to content

Commit

Permalink
feat: score
Browse files Browse the repository at this point in the history
  • Loading branch information
tianqing617 committed Nov 13, 2021
1 parent 37bb06c commit 66e9995
Show file tree
Hide file tree
Showing 6 changed files with 37 additions and 16 deletions.
12 changes: 10 additions & 2 deletions src/GameControl.ts
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
import { Pointer } from './tools'
import Snake from './components/playGround/Snake'
import Food from './components/playGround/Food'
import InfoPanel from './components/infoPanel/InfoPanel'
Expand Down Expand Up @@ -46,8 +47,13 @@ export default class GameControl {
return 300 - (this.infoPanel.level - 1) * 30;
}

checkEat() {
//
checkEat(pointer: Pointer): void {
console.log('checkEat', pointer.isSame(this.food.pointer));
if (pointer.isSame(this.food.pointer)) {
this.food.changeLocation();
this.infoPanel.increaseScore();
this.snake.increaseBody();
}
}

run(): void {
Expand All @@ -69,6 +75,8 @@ export default class GameControl {
break;
}

this.checkEat(pointer);

try {
this.snake.headPointer = pointer;
} catch (error: any) {
Expand Down
13 changes: 9 additions & 4 deletions src/components/infoPanel/InfoPanel.ts
Original file line number Diff line number Diff line change
Expand Up @@ -14,21 +14,26 @@ export default class InfoPanel {
return this.score;
}

// set score(num: number) {
// this.score = this.score + num;
// }

get gameLevel(): number {
return this.level;
}

get info(): {score: number, level: number} {
return {
score: this.score,
level: this.level,
}
}

increaseScore(): {score: number, level: number} {
this.score = this.score + 1;
// 控制升级
if (this.score % this.upgrade === 0) {
this.upLevel();
}

console.log('increase', this);

return {
score: this.score,
level: this.level,
Expand Down
15 changes: 5 additions & 10 deletions src/components/infoPanel/InfoPanel.vue
Original file line number Diff line number Diff line change
Expand Up @@ -19,16 +19,11 @@ export default {
name: 'InfoPanel',
setup(): Record<string, unknown> {
const infoPanel = new InfoPanel();
const info = reactive({
score: infoPanel.gameScore,
level: infoPanel.gameLevel,
});
// function test() {
// const { score, level } = infoPanel.increaseScore();
// info.score = score;
// info.level = level;
// }
// const info = reactive({
// score: infoPanel.gameScore,
// level: infoPanel.gameLevel,
// });
const info = reactive(infoPanel.info);
return {
info,
Expand Down
6 changes: 6 additions & 0 deletions src/components/playGround/Food.ts
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
import { Pointer } from '@/tools';

export default class Food {
element: HTMLElement

Expand All @@ -13,6 +15,10 @@ export default class Food {
return this.element.offsetTop
}

get pointer(): Pointer {
return new Pointer(this.element.offsetLeft, this.element.offsetTop);
}

changeLocation (): void {
// 食物的位置最小是0 最大是290
// 蛇移动一次就是一格,一格的大小就是10,所以就要求食物的坐标必须是整10
Expand Down
1 change: 1 addition & 0 deletions src/components/playGround/Snake.ts
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@ export default class Snake {
}

set headPointer(pointer: Pointer) {
// console.log('pointer', pointer);
// 设置蛇头
// console.log('headPointer', pointer);
this.headEl.style.top = pointer.y + 'px';
Expand Down
6 changes: 6 additions & 0 deletions src/tools/Pointer.ts
Original file line number Diff line number Diff line change
Expand Up @@ -22,4 +22,10 @@ export default class Pointer {
get y(): number {
return this.locationY;
}

isSame(pointer: Pointer): boolean {
return (
this.locationX === pointer.x && this.locationY === pointer.y
)
}
}

0 comments on commit 66e9995

Please sign in to comment.