当前位置:网站首页>Summer Challenge openharmony greedy snake based on JS
Summer Challenge openharmony greedy snake based on JS
2022-07-26 17:27:00 【51CTO】
This article is participating in the starlight project 3.0– Summer Challenge
Preface
I don't know what to do , Then type the code , Write about a snake , Obviously , Cried by my own food , I can't play with the greedy snake I wrote (ps: I used to finish for a long time without dying >_<)
The effect is as follows :

Specific implementation ideas
Container initialization
- stay onShow Get the width and height of the game container from the hook function , In fact, I don't want to get it here , But there is no way. , As if getBoundingClientRect() You need to mount it to get the value , In the previous hook function, we can't get the specific attribute value (ps: This is my guess , Because the document is written very … I can't find it, so I have to try one by one )
- Get the width and height of the container , According to the length of the snake ( Every dot ) To determine how many grids to divide , Form a coordinate axis , Rear food , The movement of the snake is determined according to this coordinate axis
The snake
Use an array to realize , Array index 0 For the snake's tail , Indexes length-1 For the head
food
Food is randomly generated , Position anywhere in the grid , But it cannot be generated in the body position of the snake
The movement of the snake
The snake moves through the array push and shift Realization , Snakes have a direction of movement , Modify the new snake head according to the direction x and y Value
The moving diagram is as follows

The death judgment of the snake
Be a snake head x >= On the map x Maximum || x < 0 || Snakehead Y >= On the map Y Maximum || Y < 0 || Snakehead (x,y) == Any one of the snake's body (x,y)
Operate the movement of the snake
-20,20,10,-10, It was originally used to determine whether the current moving direction conflicts with the original direction , Later, it was found that the coordinate axis was still used , I'm too lazy to change
Specific properties and methods
attribute
| name | type | remarks |
|---|---|---|
| result | number | fraction |
| conW | number | Container width |
| conH | number | Container height |
| snakeBody | number | Snake body unit |
| h | number | Gridded y length |
| w | number | Gridded x length |
| grid | Array<Array> | Grid map |
| food | object | food |
| timeId | number | Timer id |
| level | number | Game difficulty level |
| des | Object<Object> | Four directions of the snake |
| isStart | Boolean | Decide whether to start |
| snake | Object<Object> | The snake |
| currDes | object | The current direction of the snake |
| isEndP | Boolean | Determine if the game is over |
Method
| name | Parameters | remarks |
|---|---|---|
| init | nothing | Initialization function |
| onShow | nothing | Framework life cycle hook function |
| isEnd | newHead : object | Determine if the game is over |
| setIsEnd | nothing | Set relevant data about the end of the game |
| randomFood | nothing | Randomly generate food |
| addHead | des : object | Add new head |
| move | des : object | The movement of the snake |
| intervalMove | d :object | The snake moves automatically |
| isCuurDes | value:object ,x1:string,x2:string | Timer id |
| clickBut | m:object | Click events that operate the movement of snakes |
| reInit | nothing | Restart the game |
Code
HTML
css
JS
Last
Um. … Ask is to optimize
Attachment link : https://ost.51cto.com/resource/2199
Want to know more about open source , Please visit :
边栏推荐
- How does win11 automatically clean the recycle bin?
- 2019 popularization group summary
- Common super easy to use regular expressions!
- 03|实现 useReducer 和 useState
- Linear regression from zero sum using mxnet
- Pass-19,20
- Detailed explanation of tcpdump command
- 机器学习-什么是机器学习、监督学习和无监督学习
- SCCM tips - improve the download speed of drivers and shorten the deployment time of the system when deploying the system
- 使用 Dired 快速移动文件
猜你喜欢

(24)Blender源码分析之顶层菜单显示代码分析
![Leetcode:1206. design jump table [jump table board]](/img/4f/2b6df8e2151b8bce70c8227c69d8bc.png)
Leetcode:1206. design jump table [jump table board]
![[ctfshow web] deserialization](/img/cd/b76e148adfc4d61049ab2cf429d4d7.png)
[ctfshow web] deserialization

ASEMI整流桥KBPC2510,KBPC2510参数,KBPC2510规格书

Speaker recruitment | AI time recruit icml/ijcai 2022 as a Chinese speaker!!!

How to ensure cache and database consistency

UPC 2022 summer personal training game 07 (part)

Establishment of Eureka registration center Eureka server

Comparison between agile development and Devops

2019 popularization group summary
随机推荐
OA项目之我的会议(会议排座&送审)
[basic course of flight control development 2] crazy shell · open source formation UAV - timer (LED flight information light and indicator light flash)
How emqx 5.0 under the new architecture of mria+rlog realizes 100million mqtt connections
Stop supporting Huawei! Signing 640billion 5g orders in Germany and 270billion 5g orders in the Middle East is pure nonsense!
Review the past and know the new MySQL isolation level
[ctfshow web] deserialization
机器视觉在服务机器人中的应用
"Green is better than blue". Why is TPC the last white lotus to earn interest with money
Is it safe for Guosen Securities to open an account? How can I find the account manager
SQL注入(思维导图)
Is the rolling update of pod similar to Canary deployment or blue-green deployment?
Recurrence of historical loopholes in ThinkPHP
Thoroughly uncover how epoll realizes IO multiplexing
[C language classic topic exercise 2]
Is it safe to open an account online now? Who do you want to open a stock account?
Implement softmax classification from zero sum using mxnet
浅谈数据技术人员的成长之路
Quickly learn to configure local and network sources of yum, and learn to use yum
Linear regression from zero sum using mxnet
SCCM tips - improve the download speed of drivers and shorten the deployment time of the system when deploying the system