예제 #1
0
func main() {

	// 0 - CONSTANTES Y VARIABLES GLOBALES

	const (
		winWidth  = 1080
		winHeight = 720
	)

	// para sincronizar el framerate a 60fps
	ticker := time.NewTicker(time.Second / 60)

	// 1 - INICIAR EL JUEGO

	// creamos la ventana del juego
	window := sf.NewRenderWindow(
		sf.VideoMode{Width: winWidth, Height: winHeight, BitsPerPixel: 32},
		"Manami - Simple Game Skeleton for Go/SFML2",
		sf.StyleTitlebar|sf.StyleClose,
		sf.DefaultContextSettings())

	// cerrado de ventana al final de la ejecucion
	defer window.Close()

	// activamos la sincronizacion vertical
	window.SetVSyncEnabled(true)

	// centramos la ventana
	window.SetPosition(sf.Vector2i{
		X: int((sf.GetDesktopVideoMode().Width - window.GetSize().X) / 2),
		Y: int((sf.GetDesktopVideoMode().Height - window.GetSize().Y) / 2),
	})

	// cargamos la textura de la imagen de fondo
	texture, err := sf.NewTextureFromFile(ResourcePath("gfx", "manami_logo.png"), nil)
	if err != nil {
		panic(err)
	}
	// creamos un sprite a partir de la textura
	sprite, err := sf.NewSprite(texture)
	if err != nil {
		panic(err)
	}

	// cargamos la musica de fondo
	music, err := sf.NewMusicFromFile(ResourcePath("sfx", "bg_music.ogg"))
	if err != nil {
		panic(err)
	}
	// reproducimos la musica de fondo
	music.SetLoop(true)
	music.Play()

	// 2 - BUCLE PRINCIPAL DEL JUEGO

	gameloop := true
	for gameloop && window.IsOpen() {
		select {
		case <-ticker.C: // cada 60 fps
			// 2.1 - PROCESA LA ENTRADA
			for event := window.PollEvent(); event != nil; event = window.PollEvent() {
				switch ev := event.(type) {
				case sf.EventClosed:
					gameloop = false

				case sf.EventKeyReleased:
					switch ev.Code {
					case sf.KeyEscape:
						gameloop = false
					}
				}
			}

			// 2.2 - ACTUALIZAR EL ESTADO DEL JUEGO

			// 2.3 - RENDERIZAR EL JUEGO
			window.Clear(sf.ColorBlack())
			window.Draw(sprite, sf.DefaultRenderStates())
			window.Display()
		}
	}

	// 3 - FINALIZAR EL JUEGO
	// defer's de go ya se encargan de liberar y finalizar todo! ;)

}
예제 #2
0
func main() {
	ticker := time.NewTicker(time.Second / 60)

	var (
		gameWidth  uint = 544
		gameHeight uint = 650
	)

	// main window
	renderWindow := sf.NewRenderWindow(sf.VideoMode{Width: gameWidth, Height: gameHeight, BitsPerPixel: 32}, "Slide Numbers Puzzle", sf.StyleClose, sf.DefaultContextSettings())

	// bg texture
	bgTex, _ := sf.NewTextureFromFile("assets/images/bg.png", nil)
	bg, _ := sf.NewSprite(bgTex)
	bg.SetColor(sf.ColorCyan())

	// Load font
	font, _ := sf.NewFontFromFile("assets/fonts/LuckiestGuy.ttf")

	// Title
	testTxt, _ := sf.NewText(font)
	testTxt.SetString("Slide Numbers Puzzle")
	testTxt.SetCharacterSize(36)

	btn1 := mix.NewButton()
	btn1.SetText("1")
	//btn1.SetSize(128, 128)
	btn1.Move(0, 0)
	btn1.SetTextSize(48)
	btn1.SetTexture("assets/images/block.png")
	//btn1.SetTextSize(48)

	// btn1.Clicked(func() {
	// 	fmt.Println("Clicked the button on main")
	// 	btn1.Move(128, 0)
	// })

	btn1.Clicked(movePiece)

	btn2 := mix.NewButton()
	btn2.SetText("2")
	// btn2.SetSize(128, 128)
	btn2.Move(0, 128)
	btn2.SetTextSize(48)

	for renderWindow.IsOpen() {
		select {
		case <-ticker.C:
			// poll events
			for event := renderWindow.PollEvent(); event != nil; event = renderWindow.PollEvent() {
				switch ev := event.(type) {
				case sf.EventKeyPressed:
					if ev.Code == sf.KeyEscape {
						renderWindow.Close()
					}
				case sf.EventClosed:
					renderWindow.Close()

				default:
					// send click to button
					btn1.Events(ev)
					btn2.Events(ev)

				}
			}

			// clear window
			renderWindow.Clear(sf.ColorCyan())

			// mix.Mover(btn1, 0, 0, 5, "left")

			// Draw here
			renderWindow.Draw(bg, sf.DefaultRenderStates())
			//renderWindow.Draw(btn, sf.DefaultRenderStates())
			renderWindow.Draw(btn1, sf.DefaultRenderStates())
			renderWindow.Draw(btn2, sf.DefaultRenderStates())
			renderWindow.Draw(testTxt, sf.DefaultRenderStates())

			// and display it
			renderWindow.Display()
		}
	}
}