2017-10-28 16:54:42 +02:00
|
|
|
// Copyright 2017 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.
|
|
|
|
|
2020-10-06 17:45:54 +02:00
|
|
|
// +build example
|
2017-10-28 16:54:42 +02:00
|
|
|
|
|
|
|
package main
|
|
|
|
|
|
|
|
import (
|
2018-03-14 16:21:16 +01:00
|
|
|
"bytes"
|
2017-10-28 16:54:42 +02:00
|
|
|
"fmt"
|
2018-03-14 16:21:16 +01:00
|
|
|
"image"
|
2018-02-10 19:31:30 +01:00
|
|
|
_ "image/png"
|
2017-10-28 17:06:50 +02:00
|
|
|
|
2020-10-03 19:35:13 +02:00
|
|
|
"github.com/hajimehoshi/ebiten/v2"
|
|
|
|
"github.com/hajimehoshi/ebiten/v2/ebitenutil"
|
|
|
|
rplatformer "github.com/hajimehoshi/ebiten/v2/examples/resources/images/platformer"
|
|
|
|
"github.com/hajimehoshi/ebiten/v2/inpututil"
|
2017-10-28 16:54:42 +02:00
|
|
|
)
|
|
|
|
|
|
|
|
const (
|
|
|
|
// Settings
|
2020-03-18 17:26:34 +01:00
|
|
|
screenWidth = 960
|
|
|
|
screenHeight = 540
|
2017-10-28 16:54:42 +02:00
|
|
|
)
|
|
|
|
|
|
|
|
var (
|
|
|
|
leftSprite *ebiten.Image
|
|
|
|
rightSprite *ebiten.Image
|
|
|
|
idleSprite *ebiten.Image
|
|
|
|
backgroundImage *ebiten.Image
|
2018-01-29 19:57:07 +01:00
|
|
|
)
|
|
|
|
|
|
|
|
func init() {
|
|
|
|
// Preload images
|
2018-03-14 16:21:16 +01:00
|
|
|
img, _, err := image.Decode(bytes.NewReader(rplatformer.Right_png))
|
2018-01-29 19:57:07 +01:00
|
|
|
if err != nil {
|
|
|
|
panic(err)
|
|
|
|
}
|
2020-10-05 18:03:30 +02:00
|
|
|
rightSprite = ebiten.NewImageFromImage(img)
|
2018-03-14 16:21:16 +01:00
|
|
|
|
|
|
|
img, _, err = image.Decode(bytes.NewReader(rplatformer.Left_png))
|
2018-01-29 19:57:07 +01:00
|
|
|
if err != nil {
|
|
|
|
panic(err)
|
|
|
|
}
|
2020-10-05 18:03:30 +02:00
|
|
|
leftSprite = ebiten.NewImageFromImage(img)
|
2018-03-14 16:21:16 +01:00
|
|
|
|
|
|
|
img, _, err = image.Decode(bytes.NewReader(rplatformer.MainChar_png))
|
2018-01-29 19:57:07 +01:00
|
|
|
if err != nil {
|
|
|
|
panic(err)
|
|
|
|
}
|
2020-10-05 18:03:30 +02:00
|
|
|
idleSprite = ebiten.NewImageFromImage(img)
|
2018-03-14 16:21:16 +01:00
|
|
|
|
|
|
|
img, _, err = image.Decode(bytes.NewReader(rplatformer.Background_png))
|
2018-01-29 19:57:07 +01:00
|
|
|
if err != nil {
|
|
|
|
panic(err)
|
|
|
|
}
|
2020-10-05 18:03:30 +02:00
|
|
|
backgroundImage = ebiten.NewImageFromImage(img)
|
2018-01-29 19:57:07 +01:00
|
|
|
}
|
2017-10-28 16:54:42 +02:00
|
|
|
|
2020-03-18 18:09:45 +01:00
|
|
|
const (
|
|
|
|
unit = 16
|
|
|
|
groundY = 380
|
2017-10-28 16:54:42 +02:00
|
|
|
)
|
|
|
|
|
2020-03-18 18:09:45 +01:00
|
|
|
type char struct {
|
|
|
|
x int
|
|
|
|
y int
|
|
|
|
vx int
|
|
|
|
vy int
|
|
|
|
}
|
|
|
|
|
|
|
|
func (c *char) tryJump() {
|
|
|
|
// Now the character can jump anytime, even when the character is not on the ground.
|
|
|
|
// If you want to restrict the character to jump only when it is on the ground, you can add an 'if' clause:
|
|
|
|
//
|
|
|
|
// if gopher.y == groundY * unit {
|
|
|
|
// ...
|
2020-05-23 16:18:21 +02:00
|
|
|
c.vy = -10 * unit
|
2020-03-18 18:09:45 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
func (c *char) update() {
|
|
|
|
c.x += c.vx
|
|
|
|
c.y += c.vy
|
|
|
|
if c.y > groundY*unit {
|
|
|
|
c.y = groundY * unit
|
|
|
|
}
|
|
|
|
if c.vx > 0 {
|
|
|
|
c.vx -= 4
|
|
|
|
} else if c.vx < 0 {
|
|
|
|
c.vx += 4
|
|
|
|
}
|
|
|
|
if c.vy < 20*unit {
|
|
|
|
c.vy += 8
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func (c *char) draw(screen *ebiten.Image) {
|
|
|
|
s := idleSprite
|
|
|
|
switch {
|
|
|
|
case c.vx > 0:
|
|
|
|
s = rightSprite
|
|
|
|
case c.vx < 0:
|
|
|
|
s = leftSprite
|
|
|
|
}
|
|
|
|
|
|
|
|
op := &ebiten.DrawImageOptions{}
|
|
|
|
op.GeoM.Scale(0.5, 0.5)
|
|
|
|
op.GeoM.Translate(float64(c.x)/unit, float64(c.y)/unit)
|
|
|
|
screen.DrawImage(s, op)
|
|
|
|
}
|
|
|
|
|
2020-05-23 16:18:21 +02:00
|
|
|
type Game struct {
|
|
|
|
gopher *char
|
|
|
|
}
|
|
|
|
|
2020-10-04 10:42:54 +02:00
|
|
|
func (g *Game) Update() error {
|
2020-05-23 16:18:21 +02:00
|
|
|
if g.gopher == nil {
|
|
|
|
g.gopher = &char{x: 50 * unit, y: groundY * unit}
|
|
|
|
}
|
2020-03-18 18:09:45 +01:00
|
|
|
|
2017-10-28 16:54:42 +02:00
|
|
|
// Controls
|
2021-04-17 18:13:17 +02:00
|
|
|
if ebiten.IsKeyPressed(ebiten.KeyA) || ebiten.IsKeyPressed(ebiten.KeyArrowLeft) {
|
2020-05-23 16:18:21 +02:00
|
|
|
g.gopher.vx = -4 * unit
|
2021-04-17 18:13:17 +02:00
|
|
|
} else if ebiten.IsKeyPressed(ebiten.KeyD) || ebiten.IsKeyPressed(ebiten.KeyArrowRight) {
|
2020-05-23 16:18:21 +02:00
|
|
|
g.gopher.vx = 4 * unit
|
2017-10-28 16:54:42 +02:00
|
|
|
}
|
2020-03-18 18:09:45 +01:00
|
|
|
if inpututil.IsKeyJustPressed(ebiten.KeySpace) {
|
2020-05-23 16:18:21 +02:00
|
|
|
g.gopher.tryJump()
|
2017-10-28 17:06:50 +02:00
|
|
|
}
|
2020-05-23 16:18:21 +02:00
|
|
|
g.gopher.update()
|
|
|
|
return nil
|
|
|
|
}
|
2017-10-28 17:06:50 +02:00
|
|
|
|
2020-05-23 16:18:21 +02:00
|
|
|
func (g *Game) Draw(screen *ebiten.Image) {
|
2017-10-28 17:06:50 +02:00
|
|
|
// Draws Background Image
|
|
|
|
op := &ebiten.DrawImageOptions{}
|
|
|
|
op.GeoM.Scale(0.5, 0.5)
|
|
|
|
screen.DrawImage(backgroundImage, op)
|
|
|
|
|
2020-03-18 18:09:45 +01:00
|
|
|
// Draws the Gopher
|
2020-05-23 16:18:21 +02:00
|
|
|
g.gopher.draw(screen)
|
2017-10-28 16:54:42 +02:00
|
|
|
|
2020-03-18 18:09:45 +01:00
|
|
|
// Show the message
|
|
|
|
msg := fmt.Sprintf("TPS: %0.2f\nPress the space key to jump.", ebiten.CurrentTPS())
|
|
|
|
ebitenutil.DebugPrint(screen, msg)
|
2020-05-23 16:18:21 +02:00
|
|
|
}
|
2017-10-28 16:54:42 +02:00
|
|
|
|
2020-05-23 16:18:21 +02:00
|
|
|
func (g *Game) Layout(outsideWidth, outsideHeight int) (int, int) {
|
|
|
|
return screenWidth, screenHeight
|
2017-10-28 16:54:42 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
func main() {
|
2020-05-23 16:18:21 +02:00
|
|
|
ebiten.SetWindowSize(screenWidth, screenHeight)
|
|
|
|
ebiten.SetWindowTitle("Platformer (Ebiten Demo)")
|
|
|
|
if err := ebiten.RunGame(&Game{}); err != nil {
|
2017-10-28 16:54:42 +02:00
|
|
|
panic(err)
|
|
|
|
}
|
|
|
|
}
|