mirror of
https://github.com/hajimehoshi/ebiten.git
synced 2025-01-12 20:18:59 +01:00
example: Add mascot
This change also revert ScreenSizeInFullscree from the deprecated state. Fixes #1108
This commit is contained in:
parent
63caca720b
commit
4eedeb6ab7
181
examples/mascot/main.go
Normal file
181
examples/mascot/main.go
Normal file
@ -0,0 +1,181 @@
|
||||
// Copyright 2020 The Ebiten Authors
|
||||
//
|
||||
// Licensed under the Apache License, Version 2.0 (the "License");
|
||||
// you may not use this file except in compliance with the License.
|
||||
// You may obtain a copy of the License at
|
||||
//
|
||||
// http://www.apache.org/licenses/LICENSE-2.0
|
||||
//
|
||||
// Unless required by applicable law or agreed to in writing, software
|
||||
// distributed under the License is distributed on an "AS IS" BASIS,
|
||||
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
||||
// See the License for the specific language governing permissions and
|
||||
// limitations under the License.
|
||||
|
||||
// +build example jsgo
|
||||
|
||||
// Mascot is a desktop mascot on cross platforms.
|
||||
// This is inspired by mattn's gopher (https://github.com/mattn/gopher).
|
||||
package main
|
||||
|
||||
import (
|
||||
"bytes"
|
||||
"image"
|
||||
_ "image/png"
|
||||
"log"
|
||||
"math/rand"
|
||||
"time"
|
||||
|
||||
"github.com/hajimehoshi/ebiten"
|
||||
rmascot "github.com/hajimehoshi/ebiten/examples/resources/images/mascot"
|
||||
)
|
||||
|
||||
const (
|
||||
width = 200
|
||||
height = 200
|
||||
)
|
||||
|
||||
var (
|
||||
gopher1 *ebiten.Image
|
||||
gopher2 *ebiten.Image
|
||||
gopher3 *ebiten.Image
|
||||
)
|
||||
|
||||
func init() {
|
||||
// Decode image from a byte slice instead of a file so that
|
||||
// this example works in any working directory.
|
||||
// If you want to use a file, there are some options:
|
||||
// 1) Use os.Open and pass the file to the image decoder.
|
||||
// This is a very regular way, but doesn't work on browsers.
|
||||
// 2) Use ebitenutil.OpenFile and pass the file to the image decoder.
|
||||
// This works even on browsers.
|
||||
// 3) Use ebitenutil.NewImageFromFile to create an ebiten.Image directly from a file.
|
||||
// This also works on browsers.
|
||||
img1, _, err := image.Decode(bytes.NewReader(rmascot.Out01_png))
|
||||
if err != nil {
|
||||
log.Fatal(err)
|
||||
}
|
||||
gopher1, _ = ebiten.NewImageFromImage(img1, ebiten.FilterDefault)
|
||||
|
||||
img2, _, err := image.Decode(bytes.NewReader(rmascot.Out02_png))
|
||||
if err != nil {
|
||||
log.Fatal(err)
|
||||
}
|
||||
gopher2, _ = ebiten.NewImageFromImage(img2, ebiten.FilterDefault)
|
||||
|
||||
img3, _, err := image.Decode(bytes.NewReader(rmascot.Out03_png))
|
||||
if err != nil {
|
||||
log.Fatal(err)
|
||||
}
|
||||
gopher3, _ = ebiten.NewImageFromImage(img3, ebiten.FilterDefault)
|
||||
}
|
||||
|
||||
func init() {
|
||||
rand.Seed(time.Now().UnixNano())
|
||||
}
|
||||
|
||||
type dir int
|
||||
|
||||
const (
|
||||
right dir = iota
|
||||
left
|
||||
)
|
||||
|
||||
type state int
|
||||
|
||||
const (
|
||||
walking state = iota
|
||||
jumping
|
||||
)
|
||||
|
||||
type mascot struct {
|
||||
x16 int
|
||||
y16 int
|
||||
vy16 int
|
||||
|
||||
dir dir
|
||||
state state
|
||||
count int
|
||||
}
|
||||
|
||||
func (m *mascot) update(screen *ebiten.Image) error {
|
||||
m.count++
|
||||
|
||||
sw, sh := ebiten.ScreenSizeInFullscreen()
|
||||
ebiten.SetWindowPosition(m.x16/16, m.y16/16+sh-height)
|
||||
|
||||
switch m.dir {
|
||||
case right:
|
||||
m.x16 += 64
|
||||
case left:
|
||||
m.x16 -= 64
|
||||
default:
|
||||
panic("not reached")
|
||||
}
|
||||
if m.x16/16 > sw-width && m.dir == right {
|
||||
m.dir = left
|
||||
}
|
||||
if m.x16 <= 0 && m.dir == left {
|
||||
m.dir = right
|
||||
}
|
||||
|
||||
// Accelarate the mascot in the Y direction.
|
||||
m.vy16 += 8
|
||||
m.y16 += m.vy16
|
||||
|
||||
// If the mascot is on the ground, stop it in the Y direction.
|
||||
if m.y16 >= 0 {
|
||||
m.y16 = 0
|
||||
m.vy16 = 0
|
||||
}
|
||||
|
||||
// If the mascto is on the ground, cause an action in random.
|
||||
if rand.Intn(60) == 0 && m.y16 == 0 {
|
||||
switch rand.Intn(2) {
|
||||
case 0:
|
||||
// Jump.
|
||||
m.vy16 = -240
|
||||
case 1:
|
||||
// Turn.
|
||||
if m.dir == right {
|
||||
m.dir = left
|
||||
} else {
|
||||
m.dir = right
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
if ebiten.IsDrawingSkipped() {
|
||||
return nil
|
||||
}
|
||||
|
||||
img := gopher1
|
||||
if m.state == walking {
|
||||
switch (m.count / 3) % 4 {
|
||||
case 0:
|
||||
img = gopher1
|
||||
case 1, 3:
|
||||
img = gopher2
|
||||
case 2:
|
||||
img = gopher3
|
||||
}
|
||||
}
|
||||
op := &ebiten.DrawImageOptions{}
|
||||
if m.dir == left {
|
||||
op.GeoM.Scale(-1, 1)
|
||||
op.GeoM.Translate(width, 0)
|
||||
}
|
||||
screen.DrawImage(img, op)
|
||||
return nil
|
||||
}
|
||||
|
||||
func main() {
|
||||
ebiten.SetScreenTransparent(true)
|
||||
ebiten.SetWindowDecorated(false)
|
||||
ebiten.SetRunnableOnUnfocused(true)
|
||||
ebiten.SetWindowFloating(true)
|
||||
m := &mascot{}
|
||||
if err := ebiten.Run(m.update, width, height, 1, "Mascot (Ebiten Demo)"); err != nil {
|
||||
log.Fatal(err)
|
||||
}
|
||||
}
|
File diff suppressed because one or more lines are too long
File diff suppressed because one or more lines are too long
File diff suppressed because one or more lines are too long
File diff suppressed because one or more lines are too long
@ -30,6 +30,9 @@
|
||||
//go:generate file2byteslice -package=blocks -input=./images/blocks/blocks.png -output=./images/blocks/blocks.go -var=Blocks_png
|
||||
//go:generate file2byteslice -package=flappy -input=./images/flappy/gopher.png -output=./images/flappy/gopher.go -var=Gopher_png
|
||||
//go:generate file2byteslice -package=flappy -input=./images/flappy/tiles.png -output=./images/flappy/tiles.go -var=Tiles_png
|
||||
//go:generate file2byteslice -package=mascot -input=./images/mascot/out01.png -output=./images/mascot/out01.go -var=Out01_png
|
||||
//go:generate file2byteslice -package=mascot -input=./images/mascot/out02.png -output=./images/mascot/out02.go -var=Out02_png
|
||||
//go:generate file2byteslice -package=mascot -input=./images/mascot/out03.png -output=./images/mascot/out03.go -var=Out03_png
|
||||
//go:generate file2byteslice -package=platformer -input=./images/platformer/background.png -output=./images/platformer/background.go -var=Background_png
|
||||
//go:generate file2byteslice -package=platformer -input=./images/platformer/left.png -output=./images/platformer/left.go -var=Left_png
|
||||
//go:generate file2byteslice -package=platformer -input=./images/platformer/mainchar.png -output=./images/platformer/mainchar.go -var=MainChar_png
|
||||
|
File diff suppressed because one or more lines are too long
File diff suppressed because one or more lines are too long
File diff suppressed because one or more lines are too long
@ -71,6 +71,20 @@ https://opengameart.org/content/super-mario-world-style-tiles
|
||||
CC0 1.0
|
||||
```
|
||||
|
||||
## mascot/*.png
|
||||
|
||||
```
|
||||
https://github.com/mattn/gopher
|
||||
|
||||
MIT License
|
||||
```
|
||||
|
||||
```
|
||||
The Go gopher was designed by Renee French. (http://reneefrench.blogspot.com/)
|
||||
The design is licensed under the Creative Commons 3.0 Attributions license.
|
||||
Read this article for more details: https://blog.golang.org/gopher
|
||||
```
|
||||
|
||||
## platformer/right.png platformer/left.png platformer/mainchar.png
|
||||
|
||||
```
|
||||
|
6
examples/resources/images/mascot/out01.go
Normal file
6
examples/resources/images/mascot/out01.go
Normal file
File diff suppressed because one or more lines are too long
BIN
examples/resources/images/mascot/out01.png
Normal file
BIN
examples/resources/images/mascot/out01.png
Normal file
Binary file not shown.
After Width: | Height: | Size: 24 KiB |
6
examples/resources/images/mascot/out02.go
Normal file
6
examples/resources/images/mascot/out02.go
Normal file
File diff suppressed because one or more lines are too long
BIN
examples/resources/images/mascot/out02.png
Normal file
BIN
examples/resources/images/mascot/out02.png
Normal file
Binary file not shown.
After Width: | Height: | Size: 22 KiB |
6
examples/resources/images/mascot/out03.go
Normal file
6
examples/resources/images/mascot/out03.go
Normal file
File diff suppressed because one or more lines are too long
BIN
examples/resources/images/mascot/out03.png
Normal file
BIN
examples/resources/images/mascot/out03.png
Normal file
Binary file not shown.
After Width: | Height: | Size: 23 KiB |
File diff suppressed because one or more lines are too long
File diff suppressed because one or more lines are too long
File diff suppressed because one or more lines are too long
File diff suppressed because one or more lines are too long
File diff suppressed because one or more lines are too long
File diff suppressed because one or more lines are too long
File diff suppressed because one or more lines are too long
File diff suppressed because one or more lines are too long
20
run.go
20
run.go
@ -250,14 +250,26 @@ func RunGameWithoutMainLoop(game Game) {
|
||||
uiDriver().RunWithoutMainLoop(theUIContext)
|
||||
}
|
||||
|
||||
// ScreenSizeInFullscreen is deprecated as of 1.11.0-alpha.
|
||||
// Use SetFulllscreen, RunGame and the interface Game's Layout instead.
|
||||
// ScreenSizeInFullscreen returns the size in device-independent pixels when the game is fullscreen.
|
||||
// The adopted monitor is the 'current' monitor which the window belongs to.
|
||||
// The returned value can be given to Run or SetSize function if the perfectly fit fullscreen is needed.
|
||||
//
|
||||
// On browsers, ScreenSizeInFullscreen returns the 'window' (global object) size, not 'screen' size since an Ebiten
|
||||
// game should not know the outside of the window object. For more details, see SetFullscreen API comment.
|
||||
//
|
||||
// On mobiles, ScreenSizeInFullscreen returns (0, 0) so far.
|
||||
//
|
||||
// ScreenSizeInFullscreen's use cases are limited. If you are making a fullscreen application, you can use RunGame and
|
||||
// the Game interface's Layout function instead. If you are making a not-fullscreen application but the application's
|
||||
// behavior depends on the monitor size, ScreenSizeInFullscreen is useful.
|
||||
//
|
||||
// ScreenSizeInFullscreen must be called on the main thread before ebiten.Run, and is concurrent-safe after
|
||||
// ebiten.Run.
|
||||
func ScreenSizeInFullscreen() (int, int) {
|
||||
return uiDriver().ScreenSizeInFullscreen()
|
||||
}
|
||||
|
||||
// MonitorSize is deprecated as of 1.8.0-alpha.
|
||||
// Use SetFulllscreen, RunGame and the interface Game's Layout instead.
|
||||
// MonitorSize is deprecated as of 1.8.0-alpha. Use ScreenSizeInFullscreen instead.
|
||||
func MonitorSize() (int, int) {
|
||||
return ScreenSizeInFullscreen()
|
||||
}
|
||||
|
Loading…
Reference in New Issue
Block a user