跳转到内容
View in the app

A better way to browse. Learn more.

彼岸论坛

A full-screen app on your home screen with push notifications, badges and more.

To install this app on iOS and iPadOS
  1. Tap the Share icon in Safari
  2. Scroll the menu and tap Add to Home Screen.
  3. Tap Add in the top-right corner.
To install this app on Android
  1. Tap the 3-dot menu (⋮) in the top-right corner of the browser.
  2. Tap Add to Home screen or Install app.
  3. Confirm by tapping Install.
欢迎抵达彼岸 彼岸花开 此处谁在 -彼岸论坛

[程序员] 凹语言支持 Wasm4 游戏平台

发表于

凹语言是国内首个面向 WebAssembly 设计的通用编程语言,也是国内首个被 CNCF 基金会 wasm 全景图收录的开源编程语言。在 2022 年刚刚开源时,凹语言开发组以贪吃蛇为例做了第一个案例。具体请参考: https://wa-lang.org/smalltalk/st0018.html

st0018-01.png

在线试玩地址: https://wa-lang.org/wa/snake/

Wasm4 版本的贪吃蛇

Wasm4 是一个使用 WebAssembly 构建复古风格游戏的框架。它提供了一个 160 x 160 像素、64K 内存的游戏主机。通过使用 WebAssembly 技术使得开发的游戏能够在所有网页浏览器和一些低端设备上运行。随着凹语言支持 Wasm4 平台,现在开发者也能使用凹语言轻松开发 Wasm4 游戏。

以下是 Wasm4 贪吃蛇的效果图:

st0046-01.png

实现代码在 waroot/examples/w4-snake 目录下。

Wasm4 快速入门

先安装 wa-v0.15.0 命令行程序,然后执行以下命令:

$ wa init -wasm4
$ tree hello
hello
├── README.md
├── src
│   └── main.wa
└── wa.mod

在 hello 目录生成一个 Wasm4 版本的你好世界例子。其中 main.wa 代码如下:

import "syscall/wasm4"

首先是导入syscall/wasm4包,然后定义 Update 函数:

global smiley = [8]byte{...}

#wa:export update
func Update {
	wasm4.SetDrawColors(2)
	wasm4.Text("Hello from Wa-lang!", 10, 10)

	gamepad := wasm4.GetGamePad1()
	if gamepad&wasm4.BUTTON_1 != 0 {
		wasm4.SetDrawColors(4)
	}

	wasm4.Blit(smiley[:], 76, 76, 8, 8, wasm4.BLIT_1BPP)
	wasm4.Text("Press X to blink", 16, 90)
}

首先是调用wasm4.SetDrawColors设置绘制颜色,然后调用wasm4.Text在屏幕的指定坐标绘制文字。然后根据wasm4.GetGamePad1()获得游戏按键状态,并有条件调整绘制颜色。最后wasm4.Blit()调用绘制一个笑脸精灵。

进入 hello 目录编译和执行:

$ wa build -target=wasm4
$ w4 run output/hello.wasm

st0046-02.png

Featured Replies

No posts to show

创建帐户或登录来提出意见

Configure browser push notifications

Chrome (Android)
  1. Tap the lock icon next to the address bar.
  2. Tap Permissions → Notifications.
  3. Adjust your preference.
Chrome (Desktop)
  1. Click the padlock icon in the address bar.
  2. Select Site settings.
  3. Find Notifications and adjust your preference.