当前位置:网站首页>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 :
边栏推荐
- After Australia, New Zealand announced the ban on Huawei 5g! Huawei official response
- 得不偿失!博士骗领210万元、硕士骗领3万元人才补贴,全被判刑了!
- 环境搭建-MongoDB
- Redis persistence - detailed analysis of RDB source code | nanny level analysis! The most complete network
- Method and voltage setting of exciting vibrating wire sensor with hand-held vibrating wire collector
- [ctfshow web] deserialization
- In depth exploration of ribbon load balancing
- Stop supporting Huawei! Signing 640billion 5g orders in Germany and 270billion 5g orders in the Middle East is pure nonsense!
- Redis hotspot key and big value
- 【无标题】
猜你喜欢

Take you a minute to learn about symmetric encryption and asymmetric encryption

Heavy announcement! Icml2022 Awards: 15 outstanding papers, selected by Fudan University, Xiamen University and Shanghai Jiaotong University

How can win11 system be reinstalled with one click?
![[express receives get, post, and route request parameters]](/img/6c/ac936a8dff50b803993bef5192723b.png)
[express receives get, post, and route request parameters]

How to ensure cache and database consistency

(25)Blender源码分析之顶层菜单Blender菜单
![[basic course of flight control development 1] crazy shell · open source formation UAV GPIO (LED flight information light and signal light control)](/img/48/6dcaf4c9695d90e62036396cd81366.png)
[basic course of flight control development 1] crazy shell · open source formation UAV GPIO (LED flight information light and signal light control)

OA项目之我的会议(会议排座&送审)

【OpenCV 例程 300篇】240. OpenCV 中的 Shi-Tomas 角点检测

After Oracle creates a table partition, the partition is not given during the query, but the value specified for the partition field will be automatically queried according to the partition?
随机推荐
Take you a minute to learn about symmetric encryption and asymmetric encryption
带你一分钟了解对称加密和非对称加密
Using MySQL master-slave replication delay to save erroneously deleted data
Heavy! Zeng Xuezhong was promoted to vice chairman and CEO of zhanrui, and Chu Qingren was appointed as co CEO!
Concepts and differences of DQL, DML, DDL and DCL
Is it safe for Guosen Securities to open an account? How can I find the account manager
Execution process of select statement in MySQL
浅谈数据技术人员的成长之路
Pytorch中的tensor操作
After Oracle creates a table partition, the partition is not given during the query, but the value specified for the partition field will be automatically queried according to the partition?
Who is safe to open the VIP account of CICC securities?
[Luogu p8063] shortest paths (graph theory)
Thoroughly uncover how epoll realizes IO multiplexing
6-19漏洞利用-nsf获取目标密码文件
SCCM tips - improve the download speed of drivers and shorten the deployment time of the system when deploying the system
Pytest(思维导图)
Anaconda download and Spyder error reporting solution
Relationship between standardization, normalization and regularization
Pass-19,20
Machine learning - what are machine learning, supervised learning, and unsupervised learning