Exemplo n.º 1
0
func update(screen *ebiten.Image) error {
	if ebiten.IsKeyPressed(ebiten.KeySpace) {
		thePlayer.MoveForward()
	}
	rotated := false
	if ebiten.IsKeyPressed(ebiten.KeyRight) {
		thePlayer.RotateRight()
		rotated = true
	}
	if ebiten.IsKeyPressed(ebiten.KeyLeft) {
		thePlayer.RotateLeft()
		rotated = true
	}
	if !rotated {
		thePlayer.Stabilize()
	}

	if err := screen.Fill(skyColor); err != nil {
		return err
	}
	if err := updateGroundImage(groundImage); err != nil {
		return err
	}
	if err := drawGroundImage(screen, groundImage); err != nil {
		return err
	}

	tutrial := "Space: Move forward\nLeft/Right: Rotate"
	msg := fmt.Sprintf("FPS: %0.2f\n%s", ebiten.CurrentFPS(), tutrial)
	if err := ebitenutil.DebugPrint(screen, msg); err != nil {
		return err
	}
	return nil
}
Exemplo n.º 2
0
func update(screen *ebiten.Image) error {
	if ebiten.IsKeyPressed(ebiten.KeyLeft) {
		sprites.num -= 20
		if sprites.num < MinSprites {
			sprites.num = MinSprites
		}
	}
	if ebiten.IsKeyPressed(ebiten.KeyRight) {
		sprites.num += 20
		if MaxSprites < sprites.num {
			sprites.num = MaxSprites
		}
	}
	sprites.Update()

	if ebiten.IsRunningSlowly() {
		return nil
	}
	op := &ebiten.DrawImageOptions{
		ImageParts: sprites,
	}
	op.ColorM.Scale(1.0, 1.0, 1.0, 0.5)
	if err := screen.DrawImage(ebitenImage, op); err != nil {
		return err
	}
	msg := fmt.Sprintf(`FPS: %0.2f
Num of sprites: %d
Press <- or -> to change the number of sprites`, ebiten.CurrentFPS(), sprites.Len())
	if err := ebitenutil.DebugPrint(screen, msg); err != nil {
		return err
	}
	return nil
}
Exemplo n.º 3
0
func (g *Game) Run(width, height, scale int, title string) error {
	ticker := time.NewTicker(time.Millisecond * 70)
	go func() {
		for range ticker.C {
			if ebiten.IsKeyPressed(ebiten.KeyR) {
				g.Score = 0
				g.newMaze()
			}

			if ebiten.IsKeyPressed(ebiten.KeyLeft) && g.canGoLeft() {
				g.X--
			}

			if ebiten.IsKeyPressed(ebiten.KeyRight) && g.canGoRight() {
				g.X++
			}

			if ebiten.IsKeyPressed(ebiten.KeyUp) && g.canGoUp() {
				g.Y--
			}

			if ebiten.IsKeyPressed(ebiten.KeyDown) && g.canGoDown() {
				g.Y++
			}
		}
	}()
	defer ticker.Stop()

	return ebiten.Run(g.update, width, height, scale, title)
}
Exemplo n.º 4
0
func (p *Player) updateSE() error {
	if seStream == nil {
		return nil
	}
	if !ebiten.IsKeyPressed(ebiten.KeyP) {
		keyState[ebiten.KeyP] = 0
		return nil
	}
	keyState[ebiten.KeyP]++
	if keyState[ebiten.KeyP] != 1 {
		return nil
	}
	if sePlayer == nil {
		var err error
		sePlayer, err = audio.NewPlayer(audioContext, seStream)
		if err != nil {
			return err
		}
	}
	if sePlayer.IsPlaying() {
		return nil
	}
	if err := sePlayer.Rewind(); err != nil {
		return err
	}
	return sePlayer.Play()
}
Exemplo n.º 5
0
func (i *Input) Update() {
	if i.keyStates == nil {
		i.keyStates = map[ebiten.Key]int{}
	}
	for key := ebiten.Key(0); key <= ebiten.KeyMax; key++ {
		if !ebiten.IsKeyPressed(ebiten.Key(key)) {
			i.keyStates[key] = 0
			continue
		}
		i.keyStates[key]++
	}

	const gamepadID = 0
	if i.gamepadButtonStates == nil {
		i.gamepadButtonStates = map[ebiten.GamepadButton]int{}
	}
	for b := ebiten.GamepadButton(0); b <= ebiten.GamepadButtonMax; b++ {
		if !ebiten.IsGamepadButtonPressed(gamepadID, b) {
			i.gamepadButtonStates[b] = 0
			continue
		}
		i.gamepadButtonStates[b]++
	}

	if i.gamepadAbstractButtonStates == nil {
		i.gamepadAbstractButtonStates = map[abstractButton]int{}
	}
	for _, b := range gamepadAbstractButtons {
		if !i.gamepadConfig.IsButtonPressed(gamepadID, b) {
			i.gamepadAbstractButtonStates[b] = 0
			continue
		}
		i.gamepadAbstractButtonStates[b]++
	}
}
Exemplo n.º 6
0
func (i *Input) Update() {
	for key := range i.keyStates {
		if !ebiten.IsKeyPressed(ebiten.Key(key)) {
			i.keyStates[key] = 0
			continue
		}
		i.keyStates[key]++
	}

	const gamepadID = 0
	for b := range i.gamepadButtonStates {
		if !ebiten.IsGamepadButtonPressed(gamepadID, ebiten.GamepadButton(b)) {
			i.gamepadButtonStates[b] = 0
			continue
		}
		i.gamepadButtonStates[b]++
	}

	for _, b := range gamepadStdButtons {
		if !i.gamepadConfig.IsButtonPressed(gamepadID, b) {
			i.gamepadStdButtonStates[b] = 0
			continue
		}
		i.gamepadStdButtonStates[b]++
	}
}
Exemplo n.º 7
0
func updateInput() {
	for _, key := range keys {
		if !ebiten.IsKeyPressed(key) {
			keyStates[key] = 0
			continue
		}
		keyStates[key]++
	}
}
Exemplo n.º 8
0
func readKeys() {
	for key, _ := range keyStates {
		if !ebiten.IsKeyPressed(key) {
			keyStates[key] = -1
			continue
		}
		keyStates[key]++

	}
}
Exemplo n.º 9
0
func (i *Input) Update() error {
	for k := range dirKeys {
		if ebiten.IsKeyPressed(k) {
			i.keyState[k]++
		} else {
			i.keyState[k] = 0
		}
	}
	return nil
}
Exemplo n.º 10
0
func (p *Player) updateVolume() error {
	if p.audioPlayer == nil {
		return nil
	}
	if ebiten.IsKeyPressed(ebiten.KeyZ) {
		volume128--
	}
	if ebiten.IsKeyPressed(ebiten.KeyX) {
		volume128++
	}
	if volume128 < 0 {
		volume128 = 0
	}
	if 128 < volume128 {
		volume128 = 128
	}
	p.audioPlayer.SetVolume(float64(volume128) / 128)
	return nil
}
Exemplo n.º 11
0
func update(screen *ebiten.Image) error {
	for key, _ := range keyStates {
		if !ebiten.IsKeyPressed(key) {
			keyStates[key] = 0
			continue
		}
		keyStates[key]++
	}
	d := 0
	switch screenScale {
	case 1:
		d = 32
	case 2:
		d = 16
	}

	if keyStates[ebiten.KeyUp] == 1 {
		screenHeight += d
	}
	if keyStates[ebiten.KeyDown] == 1 {
		if 16 < screenHeight {
			screenHeight -= d
		}
	}
	if keyStates[ebiten.KeyLeft] == 1 {
		if 16 < screenWidth {
			screenWidth -= d
		}
	}
	if keyStates[ebiten.KeyRight] == 1 {
		screenWidth += d
	}
	if keyStates[ebiten.KeyS] == 1 {
		screenScale = 3 - screenScale // Swap 1 and 2
	}
	ebiten.SetScreenSize(screenWidth, screenHeight)
	ebiten.SetScreenScale(screenScale)

	screen.Fill(color.RGBA{0x80, 0x80, 0xc0, 0xff})
	w, h := gophersImage.Size()
	w2, h2 := screen.Size()
	op := &ebiten.DrawImageOptions{}
	op.GeoM.Translate(float64(-w+w2)/2, float64(-h+h2)/2)
	if err := screen.DrawImage(gophersImage, op); err != nil {
		return err
	}

	x, y := ebiten.CursorPosition()
	msg := fmt.Sprintf(`Press arrow keys to change the window size
Press S key to change the window scale
Cursor: (%d, %d)
FPS: %0.2f`, x, y, ebiten.CurrentFPS())
	ebitenutil.DebugPrint(screen, msg)
	return nil
}
Exemplo n.º 12
0
func update(screen *ebiten.Image) error {
	const offsetX, offsetY = 24, 40
	op := &ebiten.DrawImageOptions{}
	op.GeoM.Translate(offsetX, offsetY)
	op.ColorM.Scale(0.5, 0.5, 0.5, 1)
	if err := screen.DrawImage(keyboardImage, op); err != nil {
		return err
	}

	pressed := []string{}
	for i := 0; i <= 9; i++ {
		if ebiten.IsKeyPressed(ebiten.Key(i) + ebiten.Key0) {
			pressed = append(pressed, string(i+'0'))
		}
	}
	for c := 'A'; c <= 'Z'; c++ {
		if ebiten.IsKeyPressed(ebiten.Key(c) - 'A' + ebiten.KeyA) {
			pressed = append(pressed, string(c))
		}
	}
	for i := 1; i <= 12; i++ {
		if ebiten.IsKeyPressed(ebiten.Key(i) + ebiten.KeyF1 - 1) {
			pressed = append(pressed, "F"+strconv.Itoa(i))
		}
	}
	for key, name := range keyNames {
		if ebiten.IsKeyPressed(key) {
			pressed = append(pressed, name)
		}
	}

	op = &ebiten.DrawImageOptions{
		ImageParts: pressedKeysParts(pressed),
	}
	op.GeoM.Translate(offsetX, offsetY)
	if err := screen.DrawImage(keyboardImage, op); err != nil {
		return err
	}

	return nil
}
Exemplo n.º 13
0
func (p *Player) updatePlayPause() error {
	if p.audioPlayer == nil {
		return nil
	}
	if !ebiten.IsKeyPressed(ebiten.KeyS) {
		keyState[ebiten.KeyS] = 0
		return nil
	}
	keyState[ebiten.KeyS]++
	if keyState[ebiten.KeyS] != 1 {
		return nil
	}
	if p.audioPlayer.IsPlaying() {
		return p.audioPlayer.Pause()
	}
	return p.audioPlayer.Play()
}
Exemplo n.º 14
0
func (p *Player) updateSE() error {
	if seBytes == nil {
		return nil
	}
	if !ebiten.IsKeyPressed(ebiten.KeyP) {
		keyState[ebiten.KeyP] = 0
		return nil
	}
	keyState[ebiten.KeyP]++
	if keyState[ebiten.KeyP] != 1 {
		return nil
	}
	sePlayer, err := audio.NewPlayerFromBytes(audioContext, seBytes)
	if err != nil {
		return err
	}
	return sePlayer.Play()
}
Exemplo n.º 15
0
func update(screen *ebiten.Image) error {
	if ebiten.IsKeyPressed(ebiten.KeyQ) {
		hueInt--
	}
	if ebiten.IsKeyPressed(ebiten.KeyW) {
		hueInt++
	}
	if ebiten.IsKeyPressed(ebiten.KeyA) {
		saturationInt--
	}
	if ebiten.IsKeyPressed(ebiten.KeyS) {
		saturationInt++
	}
	if ebiten.IsKeyPressed(ebiten.KeyZ) {
		valueInt--
	}
	if ebiten.IsKeyPressed(ebiten.KeyX) {
		valueInt++
	}
	hueInt = clamp(hueInt, -256, 256)
	saturationInt = clamp(saturationInt, 0, 256)
	valueInt = clamp(valueInt, 0, 256)

	w, h := gophersImage.Size()
	op := &ebiten.DrawImageOptions{}
	op.GeoM.Translate(float64(screenWidth-w)/2, float64(screenHeight-h)/2)
	hue := float64(hueInt) * 2 * math.Pi / 128
	saturation := float64(saturationInt) / 128
	value := float64(valueInt) / 128
	op.ColorM.ChangeHSV(hue, saturation, value)
	if err := screen.DrawImage(gophersImage, op); err != nil {
		return err
	}

	msg := fmt.Sprintf(`Hue:        %0.2f [Q][W]
Saturation: %0.2f [A][S]
Value:      %0.2f [Z][X]`, hue, saturation, value)
	if err := ebitenutil.DebugPrint(screen, msg); err != nil {
		return err
	}
	return nil
}
Exemplo n.º 16
0
func (g *Game) update(screen *ebiten.Image) error {
	if g.Frame == nil ||
		ebiten.IsKeyPressed(ebiten.KeyUp) ||
		ebiten.IsKeyPressed(ebiten.KeyDown) ||
		ebiten.IsKeyPressed(ebiten.KeyLeft) ||
		ebiten.IsKeyPressed(ebiten.KeyRight) {
		g.updateFrame()
	}

	if err := screen.DrawImage(g.Frame, g.DrawImageOptions); err != nil {
		return err
	}

	if g.Maze[g.X][g.Y] == maze.Finish {
		g.Score += (g.W * g.H) / 100
		g.newMaze()
	}

	if ebiten.IsKeyPressed(ebiten.KeyEscape) || ebiten.IsKeyPressed(ebiten.KeyQ) {
		return errors.New("exited")
	}

	return nil
}