keras 香草编码器_如何使用香草javascript制作简单的游戏循环

2023-11-07 16:31

本文主要是介绍keras 香草编码器_如何使用香草javascript制作简单的游戏循环,希望对大家解决编程问题提供一定的参考价值,需要的开发者们随着小编来一起学习吧!

keras 香草编码器

Hey everybody! Today I want to talk about how you can create a simple game loop using only vanilla JavaScript. Recently, my friend and I decided to work on a little game project to up our JS skills, and we had to restart a few times until we could get a proper game loop going. So I decided to write a quick tutorial for all you beginner game programmers having a bit of trouble getting started.

大家好! 今天,我想谈谈如何仅使用原始JavaScript创建简单的游戏循环。 最近,我和我的朋友决定开发一个小游戏项目来提高我们的JS技能,我们不得不重新启动几次,直到可以进行正确的游戏循环。 因此,我决定为所有入门入门有点麻烦的初学者游戏程序员编写一个快速教程。

设置画布 (Setting Up Your Canvas)

In order to follow this tutorial, you are going to need to set up three files. The first is an index.html file, the second is a script.js file, and lastly you’ll need a style.css file.

为了遵循本教程,您将需要设置三个文件。 第一个是index.html文件,第二个是script.js文件,最后需要一个style.css文件。

First things first. If we’re making a game, we are going to inevitably want to draw some stuff on the page. The best way to do this is to use something in HTML called the canvas tag.

首先是第一件事。 如果要制作游戏,那么不可避免地要在页面上绘制一些东西。 最好的方法是在HTML中使用一个叫做canvas标签的东西。

<canvas> id="canvas"></canvas>

<canvas> id="canvas"></canvas>

The canvas tag is just a way to draw graphics on the screen using JavaScript and HTML. To use it in our cool new game, we are going to add it to our HTML file like so.

canvas标签只是使用JavaScript和HTML在屏幕上绘制图形的一种方式。 要在我们的炫酷新游戏中使用它,我们将像这样将其添加到HTML文件中。

Image for post

Now that we have our HTML taken care of, we’re going to want to use JavaScript to access our canvas tag, and alter it. To do so, we will do this:

现在我们已经处理了HTML,我们将要使用JavaScript来访问canvas标记并对其进行更改。 为此,我们将这样做:

Image for post

What we are doing here is storing our canvas tag into a variable called “canvas,” and then we set its context to 2D since we are going to be drawing 2D graphics on our screen. After this, we set the height and width of our canvas to equal our window’s height and width.

我们这里要做的是将canvas标记存储到一个名为“ canvas”的变量中,然后将其上下文设置为2D,因为我们将在屏幕上绘制2D图形。 此后,我们将画布的高度和宽度设置为等于窗口的高度和宽度。

Before we move on, let’s jump over to our CSS and quickly style our canvas:

在继续之前,让我们跳到CSS并快速设置画布样式:

Image for post

Okay almost done. Hopefully, I haven’t lost you yet. The last thing we are going to do is set up an event listener so that if our user ends up resizing our screen, we can adjust our canvas as well. We’ll do that like this:

好的,快完成了。 希望我还没有失去你。 我们要做的最后一件事是设置一个事件监听器,以便如果用户最终调整屏幕大小,我们也可以调整画布。 我们将这样做:

Image for post

使游戏循环 (Making the Game Loop)

To make a game loop, we need to clear out our canvas and redraw it every frame of our game. This is because in order to get things to move continuously, we have to draw them in new places every frame, and we don’t want the previously drawn sprites or shapes to stick around. So we are going to create an animate function that will clear out our canvas every frame, like so:

要进行游戏循环,我们需要清除画布,然后在游戏的每一帧重新绘制它。 这是因为为了使事物连续移动,我们必须在每帧中将它们绘制在新位置,并且我们不希望先前绘制的精灵或形状停留在周围。 因此,我们将创建一个animate函数,该函数将在每一帧清除画布,如下所示:

Image for post

So what are we doing here? Well, first we are making a function called animate to clear out our canvas . This is essentially our game loop and where you will end up drawing your sprites to the screen. We then use the clearRect function to completely wipe our canvas. Then when our window loads, we want to start running our game loop.

那我们在这里做什么? 好吧,首先我们要创建一个叫做animate的函数来清除画布。 从本质上讲,这是我们的游戏循环,您最终将在屏幕上绘制精灵。 然后,我们使用clearRect函数完全擦除画布。 然后,当窗口加载时,我们要开始运行游戏循环。

To do this, we call the setInterval function, which takes in two parameters. The first is a callback function, which will be the function you want it to execute. And the second is the time interval in which you want it to fire. The interval is in milliseconds, so I put 1000/30 which makes it so our function runs 30 times per second. If you’ve ever played video games before, you’ve probably heard the term 30 frames per second. That’s exactly what we are doing here!

为此,我们调用setInterval函数,该函数接受两个参数。 第一个是回调函数,它将是您要执行的函数。 第二个是您希望它触发的时间间隔。 时间间隔以毫秒为单位,因此我将其设置为1000/30,因此我们的函数每秒运行30次。 如果您以前玩过视频游戏,那么您可能听说过术语“每秒30帧” 。 这正是我们在这里所做的!

结论 (Conclusion)

And that’s it. You are now a bonafide game developer. Congratulations! Now you have a simple game loop in which you can create sprites, shapes, platforms and all sorts of cool stuff. Good luck and happy programming.

就是这样。 您现在是一个真正的游戏开发人员。 恭喜你! 现在,您有了一个简单的游戏循环,可以在其中创建精灵,形状,平台和各种酷炫的东西。 祝你好运,编程愉快。

资料来源 (Sources)

演示地址

翻译自: https://medium.com/better-programming/how-to-make-a-simple-game-loop-using-vanilla-javascript-f7f6360f68a2

keras 香草编码器


http://www.taodudu.cc/news/show-8178403.html

相关文章:

  • keras 香草编码器_使用香草JavaScript在浏览器中进行事件委托
  • keras 香草编码器_为什么香草ECS不够
  • keras 香草编码器_2020年的香草指数HTML
  • vanilla(香草) JavaScript 是什么
  • 香草卷筒纸组件
  • 香草世界
  • 桌游香草之路
  • Java 买机票
  • 查看电脑重启次数、原因
  • 【Android Exception】android.content.pm.PackageManager$NameNotFoundException
  • php中获取文件夹目录大小写,批量替换目录下面的文件为大写或小写
  • 我不是驴子
  • MVP的一些小知识
  • 驴的一天
  • 【产品学习】从技术转型到产品的一些心得和建议
  • 【Andoid Studio升级】Android Studio new project 或者打开SDK Manager没反应
  • 证据对抗、证据链标准 z
  • python读取crl吊销列表
  • python发送https请求并验证服务端证书
  • 第五讲 其他设置(5)
  • 使用openssl生成根证书以及签发服务器证书
  • 解决An error occurred while calling None.org.apache.spark.api.java.JavaSparkContext. : java.lang.Illeg
  • Linux 之加密类型,CA,Openssl,Openssh
  • RH413企业安全加固 第12章 安装 CA 中心
  • 证书格式与读取
  • ut5311通信接口单元_总线接口与计算机通信(五)CAN总线
  • openssl之带你走CA认证
  • 单元测试之单元测试实战运用、xml运用、csv运用
  • Linux:openssl创建CA及颁发证书
  • 【前端项目资源网站】—— 你想要的都有
  • 这篇关于keras 香草编码器_如何使用香草javascript制作简单的游戏循环的文章就介绍到这儿,希望我们推荐的文章对编程师们有所帮助!



    http://www.chinasem.cn/article/364877

    相关文章

    Spring Boot中的路径变量示例详解

    《SpringBoot中的路径变量示例详解》SpringBoot中PathVariable通过@PathVariable注解实现URL参数与方法参数绑定,支持多参数接收、类型转换、可选参数、默认值及... 目录一. 基本用法与参数映射1.路径定义2.参数绑定&nhttp://www.chinasem.cnbs

    C++中assign函数的使用

    《C++中assign函数的使用》在C++标准模板库中,std::list等容器都提供了assign成员函数,它比操作符更灵活,支持多种初始化方式,下面就来介绍一下assign的用法,具有一定的参考价... 目录​1.assign的基本功能​​语法​2. 具体用法示例​​​(1) 填充n个相同值​​(2)

    JAVA中安装多个JDK的方法

    《JAVA中安装多个JDK的方法》文章介绍了在Windows系统上安装多个JDK版本的方法,包括下载、安装路径修改、环境变量配置(JAVA_HOME和Path),并说明如何通过调整JAVA_HOME在... 首先去oracle官网下载好两个版本不同的jdk(需要登录Oracle账号,没有可以免费注册)下载完

    Spring StateMachine实现状态机使用示例详解

    《SpringStateMachine实现状态机使用示例详解》本文介绍SpringStateMachine实现状态机的步骤,包括依赖导入、枚举定义、状态转移规则配置、上下文管理及服务调用示例,重点解... 目录什么是状态机使用示例什么是状态机状态机是计算机科学中的​​核心建模工具​​,用于描述对象在其生命

    Spring Boot 结合 WxJava 实现文章上传微信公众号草稿箱与群发

    《SpringBoot结合WxJava实现文章上传微信公众号草稿箱与群发》本文将详细介绍如何使用SpringBoot框架结合WxJava开发工具包,实现文章上传到微信公众号草稿箱以及群发功能,... 目录一、项目环境准备1.1 开发环境1.2 微信公众号准备二、Spring Boot 项目搭建2.1 创建

    Java中Integer128陷阱

    《Java中Integer128陷阱》本文主要介绍了Java中Integer与int的区别及装箱拆箱机制,重点指出-128至127范围内的Integer值会复用缓存对象,导致==比较结果为true,下... 目录一、Integer和int的联系1.1 Integer和int的区别1.2 Integer和in

    SpringSecurity整合redission序列化问题小结(最新整理)

    《SpringSecurity整合redission序列化问题小结(最新整理)》文章详解SpringSecurity整合Redisson时的序列化问题,指出需排除官方Jackson依赖,通过自定义反序... 目录1. 前言2. Redission配置2.1 RedissonProperties2.2 Red

    IntelliJ IDEA2025创建SpringBoot项目的实现步骤

    《IntelliJIDEA2025创建SpringBoot项目的实现步骤》本文主要介绍了IntelliJIDEA2025创建SpringBoot项目的实现步骤,文中通过示例代码介绍的非常详细,对大家... 目录一、创建 Spring Boot 项目1. 新建项目2. 基础配置3. 选择依赖4. 生成项目5.

    JSONArray在Java中的应用操作实例

    《JSONArray在Java中的应用操作实例》JSONArray是org.json库用于处理JSON数组的类,可将Java对象(Map/List)转换为JSON格式,提供增删改查等操作,适用于前后端... 目录1. jsONArray定义与功能1.1 JSONArray概念阐释1.1.1 什么是JSONA

    Java JDK1.8 安装和环境配置教程详解

    《JavaJDK1.8安装和环境配置教程详解》文章简要介绍了JDK1.8的安装流程,包括官网下载对应系统版本、安装时选择非系统盘路径、配置JAVA_HOME、CLASSPATH和Path环境变量,... 目录1.下载JDK2.安装JDK3.配置环境变量4.检验JDK官网下载地址:Java Downloads