当前位置: 首页 > 编程日记 > 正文

react-dnd-dom_我如何使用react-dnd和react-flip-move构建React游戏

react-dnd-dom

by Nicholas Vincent-Hill

尼古拉斯·文森特·希尔(Nicholas Vincent-Hill)

我如何使用react-dnd和react-flip-move构建React游戏 (How I built a React game with react-dnd and react-flip-move)

This is a high level overview of my implementation of a puzzle/word game built entirely in React. It’s not the prettiest code ever written, but I’m happy with my first browser-based game.

这是我完全使用React构建的益智/文字游戏的实现的高级概述。 这不是有史以来最漂亮的代码,但是我对我的第一个基于浏览器的游戏感到满意。

Here’s what I built:

这是我建造的:

Scrabblr —在获得大量乐趣的同时提高您的拼字游戏技能 (Scrabblr — Improve your Scrabble skills while having tons of fun)

背景 (Background)

I was looking at animation solutions in React.js and stumbled upon Joshua Comeau’s react-flip-move demo. I saw the potential to build all sorts of scrabble-like games with his cool drag-and-drop and animated motion interface.

我当时在看React.js中的动画解决方案,偶然发现了Joshua Comeau的react-flip-move 演示 。 我看到了通过其炫酷的拖放和动画运动界面来构建各种拼字游戏的潜力。

技术挑战 (Technical challenges)

装饰与通天塔 (Decorators with Babel)

Joshua Comeau’s react-flip-move requires function decorators, a language feature not supported by create-react-app’s compiler Babel.

Joshua Comeau的react-flip-move需要函数装饰器,create-react-app的编译器Babel不支持该语言功能。

The package react-app-rewired solves this problem by overriding create-react-app Webpack configs without ejecting (something that’s super scary for a noob like me who didn’t want to deal with Webpack’s complexity under the hood).

软件包react-app- rewired通过覆盖create-react-app Webpack配置而不弹出来解决了这个问题(对于像我这样不想接触Webpack复杂性的菜鸟来说,这是非常可怕的)。

The above code allowed me to inject the transform-decorators-legacy plugin (without ejecting) to handle decorators. One problem solved with many more to go.

上面的代码允许我注入transform-decorators-legacy插件(不弹出)来处理装饰器。 一个问题解决了很多。

创建游戏循环 (Creating a game loop)

To me, what separates a cool thing and a “game” is the concept of a game loop.

对我而言,将酷炫的事物和“游戏”分开的是游戏循环的概念。

The main game loop begins with user input (a button clicked to start a new game). The user shuffles tiles and finds words.

游戏的主要循环从用户输入(单击按钮以开始新游戏)开始。 用户洗牌并找到单词。

Every time the user changes the location of a tile, the game checks for any valid words. If the word is valid, it scores that word and increments the total score by the word’s score. It also adds that word to a “Found Words” array so it doesn’t double count if it’s found again in the future.

每次用户更改图块的位置时,游戏都会检查任何有效的单词。 如果该单词有效,它将对该单词进行评分,并将总得分增加该单词的得分。 它还将该单词添加到“ Found Words”数组中,因此将来再次找到它时不会重复计算。

When the user finds all possible words (see below for the implementation of the all possible matches problem) or gives up (clicks the surrender button) the game loop ends and a results modal appears.

当用户找到所有可能的单词(所有可能的匹配问题的实现,请参见下文)或放弃(单击投降按钮)时,游戏循环结束,并出现结果模式。

The data or source of truth is all contained in state and passed to my components with the new React.context API (to avoid prop-drilling). When a new game loop starts, these values are all reset in state.

事实的数据或来源都包含在state并通过新的React.context API传递给我的组件(以避免prop-drilling )。 当新的游戏循环开始时,这些值都将全部重置为state

访问图块位置以查找字符 (Accessing tile location to find characters)

This problem took me an embarrassingly long time to solve. The tile’s (x, y) position in the grid matrix is contained in state.tiles.

这个问题使我尴尬地解决了很长时间。 网格矩阵中图块的(x,y)位置包含在state.tiles

I needed to be able to scrape letters from tiles by their position and assemble a string to validate. My solution is a complete hack job and I won’t show it here — it’s the checkForWords() method in App.js on my Github, which is in desperate need of refactoring.

我需要能够按其位置从磁贴上刮下一些字母,并组装一个字符串进行验证。 我的解决方案是一项完整的黑客工作,在此不做介绍-这是我在Github上App.js中的checkForWords()方法,它非常需要重构。

验证字 (Validating words)

I also needed a quick way to check if a string of characters was a valid English word. The free dictionary API’s I considered had prohibitively low requests allowed per day and high latency. I needed a solution that allowed me to check an exhaustive local list quickly. The Object.hasOwnProperty() method invoked on a dictionary of 270,000+ key-value pairs works nicely and quickly.

我还需要一种快速的方法来检查字符串是否是有效的英语单词。 我认为免费字典API每天允许的请求数量过低,而延迟却很高。 我需要一个解决方案,使我能够快速检查详尽的本地列表。 在270,000多个键值对的字典上调用的Object.hasOwnProperty()方法可以很好且快速地工作。

The code below is my solution to find all possible valid words given an array of letters. This is how I calculate the number of words remaining to display to the user and know when to end the game loop.

下面的代码是我的解决方案,可以找到给定字母数组的所有可能有效单词。 这是我计算要显示给用户的剩余单词数以及知道何时结束游戏循环的方式。

My original attempt, without utilizing the Object.hasOwnProperty() method and speed optimizations took nearly ten seconds to calculate all possible valid words. This implementation appears nearly instantaneous to the user and is called at the start of a new game loop.

我最初的尝试,没有利用Object.hasOwnProperty()方法和速度优化,花费了将近十秒钟来计算所有可能的有效单词。 这种实现几乎对用户而言是瞬时的,并在新游戏循环开始时被调用。

结论 (Conclusion)

I learned a lot building this project and I’m proud of the result: my game runs fast and looks good. Eventually I’d like to add OAuth and data permanence, high score record keeping, and user-designed levels.

我在构建该项目方面学到了很多东西,我为结果感到骄傲:我的游戏运行速度很快,看起来不错。 最终,我想添加OAuth和数据永久性,高分记录保存以及用户设计的级别。

I’m still new to programming and web design — I’d love to hear your comments/suggestions/critiques in the comments.

我还是编程和网页设计的新手,很想听听您在评论中的评论/建议/评论。

Play Scrabblr here!

在这里玩Scrabblr!

Read Next:

继续阅读:

Introduction and a little about accessing IEX’s API with JavaScript

简介以及有关使用JavaScript访问IEX API的一些知识

翻译自: https://www.freecodecamp.org/news/how-i-built-a-react-game-with-react-dnd-and-react-flip-move-26300156a825/

react-dnd-dom

相关文章:

web应用程序和web网站_Web应用程序和移动应用程序的基本启动清单

web应用程序和web网站by Ben Cheng通过本诚 Web应用程序和移动应用程序的基本启动清单 (The Essential Launch Checklist for Web Apps and Mobile Apps) This is a simple launch checklist for web and mobile apps that I’ve prepared for product and project managers t…

javascript禁止修改对象

禁止扩展 Object.preventExtensions(obj);var me {name: "xiaowtz" }; console.log(Object.isExtensible(me)); //true,对象默认都是可扩展的Object.preventExtensions(me); //禁止对象扩展后,不可以给对象添加新的属性console.log(Object.isExtensible(…

webpack入门之简单例子跑起来

webpack入门之简单例子跑起来 webpack介绍 Webpack是当下最热门的前端资源模块化管理和打包工具,它可以将很多松散的模块按照依赖和规则打包成符合生产环境部署的前端资源,还可以将按需加载的模块进行代码分割,等到实际需要的时候再异步加载。…

微信小程序 scroll-view 根据内容的高度自适应

微信小程序开发交流qq群 173683895 承接微信小程序开发。扫码加微信。 正文&#xff1a; 1 <video autoplay src"{{videoPlayUrl}}" controls></video> <scroll-view scroll-y style"height: {{height?heightpx:auto}}"><view c…

org.hibernate.hibernate.connection.release_mode

org.hibernate.connection包的主要封装了通过JDBC来连接数据库的操作&#xff0c;用户可以以数据源的方式&#xff0c;或者通过特定数据库驱动的方式&#xff0c;甚至是自己定义连接类的方式来完成数据库的连接操作&#xff0c;包下面的代码文件并不多&#xff0c;只有5个&…

添加百度地图最简单的办法

http://map.baidu.com/?newmap1&ieutf-8&ss%26wd%3D%E4%B8%8A%E6%B5%B7%E9%9D%92%E6%B5%A6%E5%8C%BA%E5%BE%90%E9%BE%99%E8%B7%AF77%E5%8F%B7后面加上地址就好了 比方说&#xff1a;http://map.baidu.com/?newmap1&ieutf-8&ss%26wd%3D%E4%B8%8A%E6%B5%B7%E9%…

微信小程序的数字有部分会自动加粗的解决方法

微信小程序开发交流qq群 173683895 承接微信小程序开发。扫码加微信。 正文&#xff1a; bug图&#xff1a; 能看到&#xff0c;0和1是一个标签里面的&#xff0c;但是不统一显示 此时的代码&#xff1a; <view>2018-08-01</view> 修改后的代码&#xff1a;…

ux设计_从UX设计人员的角度来看Microsoft Build 2018

ux设计by Samuele Dassatti通过萨穆尔达萨蒂 从UX设计人员的角度来看Microsoft Build 2018 (Microsoft Build 2018 from the perspective of a UX designer) I recently attended Microsoft Build 2018 in Seattle, because one of my apps was nominated for Design Innovato…

DFS template and summary

最近一直在学习Deep Frist Search&#xff0c;也在leetcode上练习了不少题目。从最开始的懵懂&#xff0c;到现在遇到问题基本有了思路。依然清晰的记得今年2月份刚开始刷题的时做subsets的那个吃力劲&#xff0c;脑子就是转不过来到底该如何的递归&#xff0c;甚至试过使用deb…

linux gcc安装

2004年4月20日最新版本的GCC编译器3.4.0发布了。目前&#xff0c;GCC可以用来编译C/C、FORTRAN、java、OBJC、ADA等语言的程序&#xff0c;可根据需要选择安装支持的语言。GCC 3.4.0比以前版本更好地支持了C标准。本文以在Redhatlinux上安装GCC3.4.0为例&#xff0c;介绍了GCC的…

js中 let var const 的差异和使用场景

微信小程序开发交流qq群 173683895 承接微信小程序开发。扫码加微信。 正文&#xff1a; 建议使用的优先级&#xff1a;const > let > var ES6 提出了两个新的声明变量的命令&#xff1a;let和const。其中&#xff0c;let完全可以取代var&#xff0c;因为两…

bulma.css_如何建立一个? 具有Bulma CSS的特斯拉响应页面

bulma.cssby ZAYDEK由ZAYDEK 0-60 in 1.9s&#xff1f; (0-60 in 1.9s ?) 如何建立一个&#xff1f; 具有Bulma CSS的特斯拉响应页面 (How To Build A ? Responsive Tesla Launch Page With Bulma CSS) 加速可持续网页设计的到来 (To accelerate the advent of sustainable …

hdu 5099 Comparison of Android versions 枚举题意

题目链接&#xff1a;http://acm.hdu.edu.cn/showproblem.php?pid5099 卡读题&#xff0c;实际上题目中表述的题意并不完整&#xff0c;所以要认真读并且加上一些现实的“常识” 关于枚举题意&#xff0c;感觉应该三个人分别看&#xff0c;然后讨论出最有可能的题意是什么 为了…

去除按钮的样式

微信小程序开发交流qq群 173683895 承接微信小程序开发。扫码加微信。 正文&#xff1a; 去除按钮默认点击效果&#xff1a; 在button标签里面添加属性&#xff1a; hover-class"none"&#xff1b; 为了方便大家&#xff0c;下面列出微信请求服务器常用的几种方…

移动应用程序和网页应用程序_您的移动应用程序运行缓慢的主要原因以及如何修复它...

移动应用程序和网页应用程序by Rajput Mehul通过拉杰普特梅胡尔(Rajput Mehul) 您的移动应用程序运行缓慢的主要原因以及如何修复它 (Top Reasons Why Your Mobile App is Slow and How to Fix it) At a time when technology is moving ahead at an express pace and people …

邮箱验证功能原理 语法 属性

邮箱验证功能原理 1 [已解决问题] 浏览: 3508次 很多地方都在注册账号的时候使用邮箱验证功能。注册后发送一封邮件到注册邮箱里面。然后点击 邮箱里面的链接 激活邮箱。 还有手机验证 这些的原理是 怎么样的。忘指点 .NET技术 ASP.NETyzy | 菜鸟二级 | 园豆&#xff1a;295 提…

直接通过OptionalAttribute, DefaultParameterValueAttribute定义缺省参数

转载于:https://www.cnblogs.com/liuxiaoji/p/4519266.html

微信小程序学习做动画效果

微信小程序开发交流qq群 173683895 承接微信小程序开发。扫码加微信。 微信扫码学习&#xff0c;在线指导微信小程序动画效果的实现

rails 添加外键_如何在Rails后端中添加功能强大的搜索引擎

rails 添加外键by Domenico Angilletta通过多梅尼科安吉列塔(Domenico Angilletta) In my experience as a Ruby on Rails Developer, I often had to deal with adding search functionality to web applications. In fact, almost all applications I worked on at some poi…

基础总结篇之一:Activity生命周期

子曰&#xff1a;溫故而知新&#xff0c;可以為師矣。《論語》 学习技术也一样&#xff0c;对于技术文档或者经典的技术书籍来说&#xff0c;指望看一遍就完全掌握&#xff0c;那基本不大可能&#xff0c;所以我们需要经常回过头再仔细研读几遍&#xff0c;以领悟到作者的思想精…

spring mvc 关键接口 HandlerMapping HandlerAdapter

HandlerMapping Spring mvc 使用HandlerMapping来找到并保存url请求和处理函数间的mapping关系。 以DefaultAnnotationHandlerMapping为例来具体看HandlerMapping的作用 DefaultAnnotationHandlerMapping将扫描当前所有已经注册的spring beans中的requestmapping标注以找出…

js 微信小程序日期 时间转时间戳

微信小程序开发交流qq群 173683895 承接微信小程序开发。扫码加微信。 微信小程序开发交流qq群 173683895 日期转换成时间戳&#xff1a;new Date(2018-09-03 15:46:13).getTime() 示例代码&#xff1a; console.log(new Date(2018-09-03 15:46:13)) 这个打印结果应该是…

小狗钱钱_✅每次构建待办事项列表应用程序时,都会有一只小狗? 死了?

小狗钱钱by Hrishi Mittal由Hrishi Mittal ✅每次构建待办事项列表应用程序时&#xff0c;都会有一只小狗 &#xff1f; 死了&#xff1f; (✅ Every time you build a to-do list app, a puppy ? dies ?) You know when you’re trying to learn something new, but get re…

[Android编程心得] Camera(OpenCV)自动对焦和触摸对焦的实现

http://blog.csdn.net/candycat1992/article/details/21617741 实现 以OpenCV的JavaCameraView为例&#xff0c;首先需要定制自己的Camera&#xff0c;主要代码如下&#xff1a;[java] view plaincopy print?import java.util.ArrayList; import java.util.List; import o…

create-react-app my-app 报错解决方法

微信小程序开发交流qq群 173683895 承接微信小程序开发。扫码加微信。 第一个原因&#xff1a;可能是没安装镜像&#xff0c; 解决方法&#xff1a; $ npm install -g cnpm --registryhttps://registry.npm.taobao.org 第二个原因&#xff1a; 报错日志&#xff1a; h…

越南一难倒博士的趣味数学题

越南有一道难倒博士的趣味数学题&#xff0c;见下图&#xff1a; 在空格中填入1...9&#xff0c;可以重复&#xff0c;求使等式成立的一个组合 我吐槽一下&#xff0c;这题在NOIP中肯定算水题了&#xff0c;爆搜都能过。O(9n),n9 我就不具体代码实现了。 据说有人跟我一样的想…

学习使用React和Electron一次构建自己的桌面聊天应用程序

by Alex Booker通过亚历克斯布克 学习使用React和Electron一次构建自己的桌面聊天应用程序 (Learn to build your own desktop chat app with React and Electron, one step at a time) This tutorial was written in collaboration with the awesome Christian Nwamba.本教程…

数据库删除,存储

布局主要分两个 其中主布局是 <?xml version"1.0" encoding"utf-8"?> <EditText android:id"id/yf_input" android:layout_width"wrap_content" android:layout_height"wrap_content" android:hint"" …

【Ant Design Pro 一】 环境搭建,创建一个demo

技术交流qq群 173683895 搭建 Ant Design Pro 的前期准备&#xff1a;你的本地环境需要安装 cnpm、node。 注&#xff1a;代码块中的 $ 代表&#xff1a; $后面是在命令行输入的命令&#xff0c;举例 $ npm start 解&#xff1a;实际上是应该打开命令行输入 npm start 下…

JOptionPane

JOptionPane类 1、属于javax.swing 包。 2、功能&#xff1a;定制四种不同种类的标准对话框。 ConfirmDialog 确认对话框。提出问题&#xff0c;然后由用户自己来确认&#xff08;按"Yes"或"No"按钮&#xff09; InputDialog 提示输入文本 MessageDialog …