Exemplo n.º 1
0
func appMain(driver gxui.Driver) {
	theme = light.CreateTheme(driver)
	window = theme.CreateWindow(800, 600, "Snake")

	playGround = field.Create(800, 600, 20, 20)
	sn = snake.Create(math.Point{X: 380, Y: 200}, math.Point{X: 380, Y: 280}, 20)

	drawScene(driver)
	fmt.Println(sn.Direction())

	window.OnKeyUp(func(event gxui.KeyboardEvent) {
		dir := sn.Direction()
		key := event.Key

		switch {
		case key == gxui.KeyRight && dir == snake.Top:
			sn.TurnRight()
		case key == gxui.KeyRight && dir == snake.Bottom:
			sn.TurnLeft()
		case key == gxui.KeyLeft && dir == snake.Top:
			sn.TurnLeft()
		case key == gxui.KeyLeft && dir == snake.Bottom:
			sn.TurnRight()
		case key == gxui.KeyUp && dir == snake.Left:
			sn.TurnRight()
		case key == gxui.KeyUp && dir == snake.Right:
			sn.TurnLeft()
		case key == gxui.KeyDown && dir == snake.Left:
			sn.TurnLeft()
		case key == gxui.KeyDown && dir == snake.Right:
			sn.TurnRight()

		}
	})

	ticker := time.NewTicker(time.Millisecond * 200)

	go func() {
		for _ = range ticker.C {
			driver.Call(func() {
				(&sn).Move()
				drawScene(driver)
			})
		}
	}()

	window.OnClose(func() {
		driver.Terminate()
		ticker.Stop()
	})

}