提交 2b6a8ac1 编写于 作者: chai2010's avatar chai2010

格式化 snake 例子

上级 6818c410
build: build:
-@rm ./web/*.wasm -@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 . go run ../../main.go build -o ./web/snake.wasm .
publish: publish:
make build make build
-rm -rf ../../docs/snake/ -rm -rf ../../docs/snake/
mkdir -p ../../docs/snake mkdir -p ../../docs/snake
cp -r ./web/ ../../docs/snake/ cp ./web/* ../../docs/snake/
#mv ../../docs/snake/snake.html ../../docs/snake/index.html mv ../../docs/snake/snake.html ../../docs/snake/index.html
clean: clean:
# 版权 @2022 凹语言 作者。保留所有权利。 # 版权 @2022 凹语言 作者。保留所有权利。
/* # 创建html.canvas的函数,由JS环境导入
创建html.canvas的函数,由JS环境导入 # w、h为画布以像素为单位的宽高
w、h为画布以像素为单位的宽高 # 返回值为画布对象对应的网页DOM对象id
返回值为画布对象对应的网页DOM对象id #
*/
#wa:import wa_js_env newCanvas #wa:import wa_js_env newCanvas
fn newCanvas_JS(w, h: u32) => u32 fn newCanvas_JS(w, h: u32) => u32
/* # 将帧缓存更新至html.canvas的函数,由JS环境导入
将帧缓存更新至html.canvas的函数,由JS环境导入 # id为画布对象对应的网页DOM对象id
id为画布对象对应的网页DOM对象id # buf为帧缓存指针
buf为帧缓存指针 #
*/
#wa:import wa_js_env updateCanvas #wa:import wa_js_env updateCanvas
fn updateCanvas_JS(id: u32, buf: *u32) 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 OnKey fn(key: u32)
#画布对象 # 画布对象
type Canvas struct { type Canvas struct {
device_id: u32 //画布对象对应的网页DOM对象id device_id :u32 # 画布对象对应的网页DOM对象id
width: u32 //画布宽度,以像素为单位 width :u32 # 画布宽度,以像素为单位
height: u32 //画布高度,以像素为单位 height :u32 # 画布高度,以像素为单位
frame_buf: []u32 //画布帧缓存,容量为Width * Height frame_buf :[]u32 # 画布帧缓存,容量为Width * Height
} }
#画布事件 # 画布事件
type CanvasEvents struct { type CanvasEvents struct {
Device_id: u32 //画布设备ID Device_id :u32 # 画布设备ID
OnMouseDown: OnTouch //鼠标按下或触屏按下时的回调处理函数,输入参数为画布内的像素坐标 OnMouseDown :OnTouch # 鼠标按下或触屏按下时的回调处理函数,输入参数为画布内的像素坐标
OnMouseUp: OnTouch //鼠标松开或触屏松开时的回调处理函数,输入参数为画布内的像素坐标 OnMouseUp :OnTouch # 鼠标松开或触屏松开时的回调处理函数,输入参数为画布内的像素坐标
OnKeyDown: OnKey //键盘按下,参数为键位。37=← 38=↑ 39=→ 40=↓ OnKeyDown :OnKey # 键盘按下,参数为键位。37=← 38=↑ 39=→ 40=↓
OnKeyUp: OnKey //键盘弹起,参数为键位 OnKeyUp :OnKey # 键盘弹起,参数为键位
} }
#创建一个宽度为w像素、高度为h像素的画布对象 # 创建一个宽度为w像素、高度为h像素的画布对象
fn NewCanvas(w, h: u32) => *Canvas { fn NewCanvas(w, h: u32) => *Canvas {
var canvas Canvas var canvas: Canvas
canvas.device_id = newCanvas_JS(w, h) canvas.device_id = newCanvas_JS(w, h)
canvas.width = w canvas.width = w
canvas.height = h canvas.height = h
canvas.frame_buf = make([]u32, w * h) canvas.frame_buf = make([]u32, w*h)
return &canvas return &canvas
} }
#获取画布设备ID # 获取画布设备ID
fn Canvas.GetDeviceID() => u32 { fn Canvas.GetDeviceID() => u32 {
return this.device_id return this.device_id
} }
#获取画布宽度 # 获取画布宽度
fn Canvas.GetWidth() => u32 { fn Canvas.GetWidth() => u32 {
return this.width return this.width
} }
#获取画布高度 # 获取画布高度
fn Canvas.GetHeight() => u32 { fn Canvas.GetHeight() => u32 {
return this.height return this.height
} }
#获取画布对象坐标为(x, y)处的像素颜色值 # 获取画布对象坐标为(x, y)处的像素颜色值
fn Canvas.GetPixel(x, y: u32) => u32 { 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) { 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) { fn Canvas.Clear(color: u32) {
for i := range this.frame_buf { for i := range this.frame_buf {
this.frame_buf[i] = color this.frame_buf[i] = color
} }
} }
#将画布对象的帧缓存更新至网页DOM对象 # 将画布对象的帧缓存更新至网页DOM对象
fn Canvas.Flush() { 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 var canvas_events: []CanvasEvents
...@@ -98,13 +96,11 @@ fn AttachCanvasEvents(e: CanvasEvents) { ...@@ -98,13 +96,11 @@ fn AttachCanvasEvents(e: CanvasEvents) {
canvas_events = append(canvas_events, e) canvas_events = append(canvas_events, e)
} }
/* # 供外部JS调用的按下事件响应函数
供外部JS调用的按下事件响应函数 # id为画布对象对应的网页DOM对象id
id为画布对象对应的网页DOM对象id # (x, y)为画布像素坐标系坐标
(x, y)为画布像素坐标系坐标 fn OnMouseDown(id: u32, x, y: u32) {
*/ for _, i := range canvas_events {
fn OnMouseDown(id: u32, x, y:u32) {
for _, i := range canvas_events {
if i.Device_id == id { if i.Device_id == id {
i.OnMouseDown(x, y) i.OnMouseDown(x, y)
return return
...@@ -112,13 +108,11 @@ fn OnMouseDown(id: u32, x, y:u32) { ...@@ -112,13 +108,11 @@ fn OnMouseDown(id: u32, x, y:u32) {
} }
} }
/* # 供外部JS调用的弹起事件响应函数
供外部JS调用的弹起事件响应函数 # id为画布对象对应的网页DOM对象id
id为画布对象对应的网页DOM对象id # (x, y)为画布像素坐标系坐标
(x, y)为画布像素坐标系坐标 fn OnMouseUp(id: u32, x, y: u32) {
*/ for _, i := range canvas_events {
fn OnMouseUp(id: u32, x, y:u32) {
for _, i := range canvas_events {
if i.Device_id == id { if i.Device_id == id {
i.OnMouseUp(x, y) i.OnMouseUp(x, y)
return return
...@@ -126,13 +120,11 @@ fn OnMouseUp(id: u32, x, y:u32) { ...@@ -126,13 +120,11 @@ fn OnMouseUp(id: u32, x, y:u32) {
} }
} }
/* # 供外部JS调用的键盘按下事件响应函数
供外部JS调用的键盘按下事件响应函数 # id为画布对象对应的网页DOM对象id
id为画布对象对应的网页DOM对象id # key为键位
key为键位 fn OnKeyDown(id, key: u32) {
*/ for _, i := range canvas_events {
fn OnKeyDown(id, key:u32) {
for _, i := range canvas_events {
if i.Device_id == id { if i.Device_id == id {
i.OnKeyDown(key) i.OnKeyDown(key)
return return
...@@ -140,16 +132,14 @@ fn OnKeyDown(id, key:u32) { ...@@ -140,16 +132,14 @@ fn OnKeyDown(id, key:u32) {
} }
} }
/* # 供外部JS调用的键盘弹起事件响应函数
供外部JS调用的键盘弹起事件响应函数 # id为画布对象对应的网页DOM对象id
id为画布对象对应的网页DOM对象id # key为键位
key为键位 fn OnKeyUp(id, key: u32) {
*/ for _, i := range canvas_events {
fn OnKeyUp(id, key:u32) {
for _, i := range canvas_events {
if i.Device_id == id { if i.Device_id == id {
i.OnKeyUp(key) i.OnKeyUp(key)
return return
} }
} }
} }
\ No newline at end of file
...@@ -2,20 +2,21 @@ ...@@ -2,20 +2,21 @@
import "snake/canvas" import "snake/canvas"
var ca *canvas.Canvas var ca: *canvas.Canvas
fn Test(){
fn Test() {
println(42) println(42)
ca = canvas.NewCanvas(255, 255) ca = canvas.NewCanvas(255, 255)
var i, j u32 var i, j: u32
for i = 0; i < 255; i++ { for i = 0; i < 255; i++ {
for j = 0; j < 255; j++ { for j = 0; j < 255; j++ {
ca.SetPixel(i, j, 0xFF000000 + i + j * 256) ca.SetPixel(i, j, 0xFF000000+i+j*256)
} }
} }
ca.Flush() ca.Flush()
var caev canvas.CanvasEvents var caev: canvas.CanvasEvents
caev.Device_id = ca.GetDeviceID() caev.Device_id = ca.GetDeviceID()
caev.OnMouseDown = fn(x, y: u32) { caev.OnMouseDown = fn(x, y: u32) {
println("OnMouseDown: ", x, " ", y) println("OnMouseDown: ", x, " ", y)
...@@ -34,7 +35,7 @@ fn Test(){ ...@@ -34,7 +35,7 @@ fn Test(){
} }
type Position struct { type Position struct {
x, y: i32 x, y :i32
} }
const BodyColor = 0xFF0000FF const BodyColor = 0xFF0000FF
...@@ -54,28 +55,28 @@ const ( ...@@ -54,28 +55,28 @@ const (
DirDown DirDown
) )
var Dirs [5]Position var Dirs: [5]Position
type GameState struct { type GameState struct {
w, h: i32 w, h :i32
scale: i32 scale :i32
grid: []i8 grid :[]i8
body: []Position body :[]Position
dir: i32 dir :i32
ca: *canvas.Canvas ca :*canvas.Canvas
} }
var gameState GameState var gameState: GameState
fn GameState.Init(w, h: i32, scale: i32) { fn GameState.Init(w, h: i32, scale: i32) {
this.w = w this.w = w
this.h = h this.h = h
this.scale = scale this.scale = scale
this.grid = make([]i8, u32(w * h)) this.grid = make([]i8, u32(w*h))
this.ca = canvas.NewCanvas(u32(w * scale), u32(h * scale)) 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.Device_id = this.ca.GetDeviceID()
caev.OnMouseDown = fn(x, y: u32) {} caev.OnMouseDown = fn(x, y: u32) {}
caev.OnMouseUp = fn(x, y: u32) {} caev.OnMouseUp = fn(x, y: u32) {}
...@@ -107,8 +108,8 @@ fn GameState.Start() { ...@@ -107,8 +108,8 @@ fn GameState.Start() {
} }
fn GameState.SetGridType(p: Position, t: i8) { fn GameState.SetGridType(p: Position, t: i8) {
this.grid[p.y * this.w + p.x] = t this.grid[p.y*this.w+p.x] = t
var color u32 var color: u32
switch t { switch t {
case GridBody: case GridBody:
color = BodyColor color = BodyColor
...@@ -123,7 +124,7 @@ fn GameState.SetGridType(p: Position, t: i8) { ...@@ -123,7 +124,7 @@ fn GameState.SetGridType(p: Position, t: i8) {
var x, y: i32 var x, y: i32
for y = 0; y < this.scale; y++ { for y = 0; y < this.scale; y++ {
for x = 0; x < this.scale; x++ { 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) { ...@@ -131,10 +132,10 @@ fn GameState.SetGridType(p: Position, t: i8) {
#wa:import wa_js_env rand #wa:import wa_js_env rand
fn rand_JS(i32) => i32 fn rand_JS(i32) => i32
fn GameState.GenFood() => Position { fn GameState.GenFood() => Position {
var p Position var p: Position
for { for {
p = Position{x: rand_JS(this.w), y: rand_JS(this.h)} 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) this.SetGridType(p, GridFood)
return p return p
} }
...@@ -162,7 +163,7 @@ fn GameState.Step() { ...@@ -162,7 +163,7 @@ fn GameState.Step() {
return return
} }
newHead := this.body[len(this.body) - 1] newHead := this.body[len(this.body)-1]
newHead.x += Dirs[this.dir].x newHead.x += Dirs[this.dir].x
newHead.y += Dirs[this.dir].y newHead.y += Dirs[this.dir].y
...@@ -184,7 +185,7 @@ fn GameState.Step() { ...@@ -184,7 +185,7 @@ fn GameState.Step() {
// return // return
//} //}
switch this.grid[newHead.y * this.w + newHead.x] { switch this.grid[newHead.y*this.w+newHead.x] {
case GridBody: case GridBody:
this.Start() this.Start()
return return
......
...@@ -5,7 +5,7 @@ ...@@ -5,7 +5,7 @@
<meta charset="UTF-8"> <meta charset="UTF-8">
<meta http-equiv="X-UA-Compatible" content="IE=edge"> <meta http-equiv="X-UA-Compatible" content="IE=edge">
<meta name="viewport" content="width=device-width, initial-scale=1.0"> <meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>WA Canvas</title> <title>Wa: Snack Game</title>
<style> <style>
* { * {
margin: 0; margin: 0;
......
...@@ -263,7 +263,7 @@ func (p *parser) consumeComment() (comment *ast.Comment, endline int) { ...@@ -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. // /*-style comments may end on a different line than where they start.
// Scan the comment for '\n' chars and adjust endline accordingly. // Scan the comment for '\n' chars and adjust endline accordingly.
endline = p.file.Line(p.pos) 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 // don't use range here - no need to decode Unicode code points
for i := 0; i < len(p.lit); i++ { for i := 0; i < len(p.lit); i++ {
if p.lit[i] == '\n' { if p.lit[i] == '\n' {
......
...@@ -648,8 +648,13 @@ func (p *printer) writeComment(comment *ast.Comment) { ...@@ -648,8 +648,13 @@ func (p *printer) writeComment(comment *ast.Comment) {
p.indent = 0 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 // shortcut common case of //-style comments
if text[1] == '/' { if text[0] == '/' && text[1] == '/' {
p.writeString(pos, trimRight(text), true) p.writeString(pos, trimRight(text), true)
return return
} }
......
Markdown is supported
0% .
You are about to add 0 people to the discussion. Proceed with caution.
先完成此消息的编辑!
想要评论请 注册