Skip to content
体验新版
项目
组织
正在加载...
登录
切换导航
打开侧边栏
wa-lang
wa
提交
2b6a8ac1
wa
项目概览
wa-lang
/
wa
12 个月 前同步成功
通知
68
Star
655
Fork
45
代码
文件
提交
分支
Tags
贡献者
分支图
Diff
Issue
0
列表
看板
标记
里程碑
合并请求
0
DevOps
流水线
流水线任务
计划
Wiki
0
Wiki
分析
仓库
DevOps
项目成员
Pages
wa
项目概览
项目概览
详情
发布
仓库
仓库
文件
提交
分支
标签
贡献者
分支图
比较
Issue
0
Issue
0
列表
看板
标记
里程碑
合并请求
0
合并请求
0
Pages
DevOps
DevOps
流水线
流水线任务
计划
分析
分析
仓库分析
DevOps
Wiki
0
Wiki
成员
成员
收起侧边栏
关闭侧边栏
动态
分支图
创建新Issue
流水线任务
提交
Issue看板
体验新版 GitCode,发现更多精彩内容 >>
提交
2b6a8ac1
编写于
12月 24, 2022
作者:
chai2010
浏览文件
操作
浏览文件
下载
电子邮件补丁
差异文件
格式化 snake 例子
上级
6818c410
变更
6
隐藏空白更改
内联
并排
Showing
6 changed file
with
93 addition
and
94 deletion
+93
-94
_examples/snake/Makefile
_examples/snake/Makefile
+5
-2
_examples/snake/src/canvas/canvas.wa
_examples/snake/src/canvas/canvas.wa
+55
-65
_examples/snake/src/main.wa
_examples/snake/src/main.wa
+25
-24
_examples/snake/web/snake.html
_examples/snake/web/snake.html
+1
-1
internal/parser/parser.go
internal/parser/parser.go
+1
-1
internal/printer/printer.go
internal/printer/printer.go
+6
-1
未找到文件。
_examples/snake/Makefile
浏览文件 @
2b6a8ac1
build
:
-
@rm ./web/
*
.wasm
go run ../../main.go
fmt
./src/main.wa
go run ../../main.go
fmt
./src/canvas/canvas.wa
go
run
../../main.go
build
-o
./web/snake.wasm
.
publish
:
make build
-
rm
-rf
../../docs/snake/
mkdir
-p
../../docs/snake
cp
-r
./web/
../../docs/snake/
#
mv ../../docs/snake/snake.html ../../docs/snake/index.html
cp
./web/
*
../../docs/snake/
mv
../../docs/snake/snake.html ../../docs/snake/index.html
clean
:
_examples/snake/src/canvas/canvas.wa
浏览文件 @
2b6a8ac1
# 版权 @2022 凹语言 作者。保留所有权利。
/*
创建html.canvas的函数,由JS环境导入
w、h为画布以像素为单位的宽高
返回值为画布对象对应的网页DOM对象id
*/
# 创建html.canvas的函数,由JS环境导入
# w、h为画布以像素为单位的宽高
# 返回值为画布对象对应的网页DOM对象id
#
#wa:import wa_js_env newCanvas
fn newCanvas_JS(w, h: u32) => u32
/*
将帧缓存更新至html.canvas的函数,由JS环境导入
id为画布对象对应的网页DOM对象id
buf为帧缓存指针
*/
# 将帧缓存更新至html.canvas的函数,由JS环境导入
# id为画布对象对应的网页DOM对象id
# buf为帧缓存指针
#
#wa:import wa_js_env updateCanvas
fn updateCanvas_JS(id: u32, buf: *u32)
#画布事件回调函数原型
type OnTouch fn
(x, y: u32)
#
画布事件回调函数原型
type OnTouch fn(x, y: u32)
type OnKey fn(key: u32)
#画布对象
#
画布对象
type Canvas struct {
device_id: u32 //
画布对象对应的网页DOM对象id
width: u32 //
画布宽度,以像素为单位
height: u32 //
画布高度,以像素为单位
frame_buf: []u32 //
画布帧缓存,容量为Width * Height
device_id :u32 #
画布对象对应的网页DOM对象id
width :u32 #
画布宽度,以像素为单位
height :u32 #
画布高度,以像素为单位
frame_buf :[]u32 #
画布帧缓存,容量为Width * Height
}
#画布事件
#
画布事件
type CanvasEvents struct {
Device_id
: u32 //
画布设备ID
OnMouseDown: OnTouch //
鼠标按下或触屏按下时的回调处理函数,输入参数为画布内的像素坐标
OnMouseUp: OnTouch //
鼠标松开或触屏松开时的回调处理函数,输入参数为画布内的像素坐标
OnKeyDown
: OnKey //
键盘按下,参数为键位。37=← 38=↑ 39=→ 40=↓
OnKeyUp
: OnKey //
键盘弹起,参数为键位
Device_id
:u32 #
画布设备ID
OnMouseDown :OnTouch #
鼠标按下或触屏按下时的回调处理函数,输入参数为画布内的像素坐标
OnMouseUp :OnTouch #
鼠标松开或触屏松开时的回调处理函数,输入参数为画布内的像素坐标
OnKeyDown
:OnKey #
键盘按下,参数为键位。37=← 38=↑ 39=→ 40=↓
OnKeyUp
:OnKey #
键盘弹起,参数为键位
}
#创建一个宽度为w像素、高度为h像素的画布对象
#
创建一个宽度为w像素、高度为h像素的画布对象
fn NewCanvas(w, h: u32) => *Canvas {
var canvas
Canvas
var canvas:
Canvas
canvas.device_id = newCanvas_JS(w, h)
canvas.width = w
canvas.height = h
canvas.frame_buf = make([]u32, w
*
h)
canvas.frame_buf = make([]u32, w
*
h)
return &canvas
}
#获取画布设备ID
#
获取画布设备ID
fn Canvas.GetDeviceID() => u32 {
return this.device_id
}
#获取画布宽度
#
获取画布宽度
fn Canvas.GetWidth() => u32 {
return this.width
}
#获取画布高度
#
获取画布高度
fn Canvas.GetHeight() => u32 {
return this.height
}
#获取画布对象坐标为(x, y)处的像素颜色值
#
获取画布对象坐标为(x, y)处的像素颜色值
fn Canvas.GetPixel(x, y: u32) => u32 {
return this.frame_buf[y * this.width +
x]
return this.frame_buf[y*this.width+
x]
}
#设置画布对象坐标(x, y)处的颜色值为color
#
设置画布对象坐标(x, y)处的颜色值为color
fn Canvas.SetPixel(x, y, color: u32) {
this.frame_buf[y * this.width +
x] = color
this.frame_buf[y*this.width+
x] = color
}
#用color指定的颜色清除整个画布
#
用color指定的颜色清除整个画布
fn Canvas.Clear(color: u32) {
for i := range this.frame_buf {
this.frame_buf[i] = color
}
}
#将画布对象的帧缓存更新至网页DOM对象
#
将画布对象的帧缓存更新至网页DOM对象
fn Canvas.Flush() {
updateCanvas_JS(this.device_id, &this.frame_buf[0])
updateCanvas_JS(this.device_id, &this.frame_buf[0])
}
var canvas_events: []CanvasEvents
...
...
@@ -98,13 +96,11 @@ fn AttachCanvasEvents(e: CanvasEvents) {
canvas_events = append(canvas_events, e)
}
/*
供外部JS调用的按下事件响应函数
id为画布对象对应的网页DOM对象id
(x, y)为画布像素坐标系坐标
*/
fn OnMouseDown(id: u32, x, y:u32) {
for _, i := range canvas_events {
# 供外部JS调用的按下事件响应函数
# id为画布对象对应的网页DOM对象id
# (x, y)为画布像素坐标系坐标
fn OnMouseDown(id: u32, x, y: u32) {
for _, i := range canvas_events {
if i.Device_id == id {
i.OnMouseDown(x, y)
return
...
...
@@ -112,13 +108,11 @@ fn OnMouseDown(id: u32, x, y:u32) {
}
}
/*
供外部JS调用的弹起事件响应函数
id为画布对象对应的网页DOM对象id
(x, y)为画布像素坐标系坐标
*/
fn OnMouseUp(id: u32, x, y:u32) {
for _, i := range canvas_events {
# 供外部JS调用的弹起事件响应函数
# id为画布对象对应的网页DOM对象id
# (x, y)为画布像素坐标系坐标
fn OnMouseUp(id: u32, x, y: u32) {
for _, i := range canvas_events {
if i.Device_id == id {
i.OnMouseUp(x, y)
return
...
...
@@ -126,13 +120,11 @@ fn OnMouseUp(id: u32, x, y:u32) {
}
}
/*
供外部JS调用的键盘按下事件响应函数
id为画布对象对应的网页DOM对象id
key为键位
*/
fn OnKeyDown(id, key:u32) {
for _, i := range canvas_events {
# 供外部JS调用的键盘按下事件响应函数
# id为画布对象对应的网页DOM对象id
# key为键位
fn OnKeyDown(id, key: u32) {
for _, i := range canvas_events {
if i.Device_id == id {
i.OnKeyDown(key)
return
...
...
@@ -140,16 +132,14 @@ fn OnKeyDown(id, key:u32) {
}
}
/*
供外部JS调用的键盘弹起事件响应函数
id为画布对象对应的网页DOM对象id
key为键位
*/
fn OnKeyUp(id, key:u32) {
for _, i := range canvas_events {
# 供外部JS调用的键盘弹起事件响应函数
# id为画布对象对应的网页DOM对象id
# key为键位
fn OnKeyUp(id, key: u32) {
for _, i := range canvas_events {
if i.Device_id == id {
i.OnKeyUp(key)
return
}
}
}
\ No newline at end of file
}
_examples/snake/src/main.wa
浏览文件 @
2b6a8ac1
...
...
@@ -2,20 +2,21 @@
import "snake/canvas"
var ca *canvas.Canvas
fn Test(){
var ca: *canvas.Canvas
fn Test() {
println(42)
ca = canvas.NewCanvas(255, 255)
var i, j u32
var i, j
:
u32
for i = 0; i < 255; i++ {
for j = 0; j < 255; j++ {
ca.SetPixel(i, j, 0xFF000000
+ i + j *
256)
ca.SetPixel(i, j, 0xFF000000
+i+j*
256)
}
}
ca.Flush()
var caev canvas.CanvasEvents
var caev
:
canvas.CanvasEvents
caev.Device_id = ca.GetDeviceID()
caev.OnMouseDown = fn(x, y: u32) {
println("OnMouseDown: ", x, " ", y)
...
...
@@ -34,7 +35,7 @@ fn Test(){
}
type Position struct {
x, y
:
i32
x, y
:
i32
}
const BodyColor = 0xFF0000FF
...
...
@@ -54,28 +55,28 @@ const (
DirDown
)
var Dirs [5]Position
var Dirs
:
[5]Position
type GameState struct {
w, h
:
i32
scale
:
i32
grid
:
[]i8
body
:
[]Position
dir
:
i32
w, h
:
i32
scale
:
i32
grid
:
[]i8
body
:
[]Position
dir
:
i32
ca
:
*canvas.Canvas
ca
:
*canvas.Canvas
}
var gameState GameState
var gameState
:
GameState
fn GameState.Init(w, h: i32, scale: i32) {
this.w = w
this.h = h
this.scale = scale
this.grid = make([]i8, u32(w
*
h))
this.ca = canvas.NewCanvas(u32(w
* scale), u32(h *
scale))
this.grid = make([]i8, u32(w
*
h))
this.ca = canvas.NewCanvas(u32(w
*scale), u32(h*
scale))
var caev canvas.CanvasEvents
var caev
:
canvas.CanvasEvents
caev.Device_id = this.ca.GetDeviceID()
caev.OnMouseDown = fn(x, y: u32) {}
caev.OnMouseUp = fn(x, y: u32) {}
...
...
@@ -107,8 +108,8 @@ fn GameState.Start() {
}
fn GameState.SetGridType(p: Position, t: i8) {
this.grid[p.y
* this.w +
p.x] = t
var color u32
this.grid[p.y
*this.w+
p.x] = t
var color
:
u32
switch t {
case GridBody:
color = BodyColor
...
...
@@ -123,7 +124,7 @@ fn GameState.SetGridType(p: Position, t: i8) {
var x, y: i32
for y = 0; y < this.scale; y++ {
for x = 0; x < this.scale; x++ {
this.ca.SetPixel(u32(p.x
* this.scale + x), u32(p.y * this.scale +
y), color)
this.ca.SetPixel(u32(p.x
*this.scale+x), u32(p.y*this.scale+
y), color)
}
}
}
...
...
@@ -131,10 +132,10 @@ fn GameState.SetGridType(p: Position, t: i8) {
#wa:import wa_js_env rand
fn rand_JS(i32) => i32
fn GameState.GenFood() => Position {
var p Position
var p
:
Position
for {
p = Position{x: rand_JS(this.w), y: rand_JS(this.h)}
if this.grid[p.y
* this.w +
p.x] == GridNull {
if this.grid[p.y
*this.w+
p.x] == GridNull {
this.SetGridType(p, GridFood)
return p
}
...
...
@@ -162,7 +163,7 @@ fn GameState.Step() {
return
}
newHead := this.body[len(this.body)
-
1]
newHead := this.body[len(this.body)
-
1]
newHead.x += Dirs[this.dir].x
newHead.y += Dirs[this.dir].y
...
...
@@ -184,7 +185,7 @@ fn GameState.Step() {
// return
//}
switch this.grid[newHead.y
* this.w +
newHead.x] {
switch this.grid[newHead.y
*this.w+
newHead.x] {
case GridBody:
this.Start()
return
...
...
_examples/snake/web/snake.html
浏览文件 @
2b6a8ac1
...
...
@@ -5,7 +5,7 @@
<meta
charset=
"UTF-8"
>
<meta
http-equiv=
"X-UA-Compatible"
content=
"IE=edge"
>
<meta
name=
"viewport"
content=
"width=device-width, initial-scale=1.0"
>
<title>
W
A Canvas
</title>
<title>
W
a: Snack Game
</title>
<style>
*
{
margin
:
0
;
...
...
internal/parser/parser.go
浏览文件 @
2b6a8ac1
...
...
@@ -263,7 +263,7 @@ func (p *parser) consumeComment() (comment *ast.Comment, endline int) {
// /*-style comments may end on a different line than where they start.
// Scan the comment for '\n' chars and adjust endline accordingly.
endline
=
p
.
file
.
Line
(
p
.
pos
)
if
p
.
lit
[
1
]
==
'*'
{
if
p
.
lit
[
0
]
==
'/'
&&
p
.
lit
[
1
]
==
'*'
{
// don't use range here - no need to decode Unicode code points
for
i
:=
0
;
i
<
len
(
p
.
lit
);
i
++
{
if
p
.
lit
[
i
]
==
'\n'
{
...
...
internal/printer/printer.go
浏览文件 @
2b6a8ac1
...
...
@@ -648,8 +648,13 @@ func (p *printer) writeComment(comment *ast.Comment) {
p
.
indent
=
0
}
// shortcut common case of #-style comments
if
text
[
0
]
==
'#'
{
p
.
writeString
(
pos
,
trimRight
(
text
),
true
)
return
}
// shortcut common case of //-style comments
if
text
[
1
]
==
'/'
{
if
text
[
0
]
==
'/'
&&
text
[
1
]
==
'/'
{
p
.
writeString
(
pos
,
trimRight
(
text
),
true
)
return
}
...
...
编辑
预览
Markdown
is supported
0%
请重试
或
添加新附件
.
添加附件
取消
You are about to add
0
people
to the discussion. Proceed with caution.
先完成此消息的编辑!
取消
想要评论请
注册
或
登录