project-rpg/main.go

132 lines
2.5 KiB
Go
Raw Normal View History

2018-01-15 21:41:59 -07:00
package main
import (
2018-01-19 07:40:48 -07:00
"fmt"
2018-01-15 23:20:34 -07:00
"image"
2018-01-19 07:40:48 -07:00
"math"
2018-01-15 23:20:34 -07:00
"os"
2018-01-16 21:54:57 -07:00
"time"
2018-01-15 23:20:34 -07:00
2018-01-19 07:40:48 -07:00
_ "image/png"
2018-01-15 22:42:11 -07:00
"github.com/faiface/pixel"
"github.com/faiface/pixel/pixelgl"
2018-01-15 22:50:18 -07:00
"golang.org/x/image/colornames"
2018-01-15 21:41:59 -07:00
)
2018-01-15 23:20:34 -07:00
func loadPicture(path string) (pixel.Picture, error) {
file, err := os.Open(path)
if err != nil {
return nil, err
}
defer file.Close()
img, _, err := image.Decode(file)
if err != nil {
return nil, err
}
return pixel.PictureDataFromImage(img), nil
}
2018-01-15 22:42:11 -07:00
func run() {
cfg := pixelgl.WindowConfig{
Title: "Pixel Rocks!",
Bounds: pixel.R(0, 0, 1024, 768),
2018-01-15 22:50:18 -07:00
VSync: true,
2018-01-15 22:42:11 -07:00
}
win, err := pixelgl.NewWindow(cfg)
if err != nil {
panic(err)
}
spritesheet, err := loadPicture("sprites.png")
2018-01-15 23:20:34 -07:00
if err != nil {
panic(err)
}
2018-01-19 07:40:48 -07:00
background, err := loadPicture("background.png")
if err != nil {
panic(err)
}
//batch := pixel.NewBatch(&pixel.TrianglesData{}, spritesheet)
var spritesFrames []pixel.Rect
2018-01-16 21:33:27 -07:00
for x := spritesheet.Bounds().Min.X; x < spritesheet.Bounds().Max.X; x += 32 {
for y := spritesheet.Bounds().Min.Y; y < spritesheet.Bounds().Max.Y; y += 32 {
spritesFrames = append(spritesFrames, pixel.R(x, y, x+79, y+92))
2018-01-16 21:33:27 -07:00
}
}
2018-01-16 21:10:29 -07:00
2018-01-16 21:33:27 -07:00
var (
2018-01-19 07:40:48 -07:00
camPos = pixel.ZV
camSpeed = 500.0
camZoom = 0.3
2018-01-19 07:40:48 -07:00
camZoomSpeed = 1.2
)
var (
frames = 0
second = time.Tick(time.Second)
)
playerX := win.Bounds().Max.X / 2
playerY := win.Bounds().Max.Y / 2
var (
playerXY = pixel.Vec{
playerX,
playerY,
}
2018-01-16 21:33:27 -07:00
)
2018-01-15 22:50:18 -07:00
2018-01-16 21:54:57 -07:00
last := time.Now()
2018-01-15 22:42:11 -07:00
for !win.Closed() {
2018-01-16 21:54:57 -07:00
dt := time.Since(last).Seconds()
last = time.Now()
2018-01-19 07:40:48 -07:00
playerXY = pixel.Vec{
playerX,
playerY,
2018-01-16 21:54:57 -07:00
}
2018-01-19 07:40:48 -07:00
cam := pixel.IM.Scaled(camPos, camZoom).Moved(win.Bounds().Center().Sub(camPos))
win.SetMatrix(cam)
bg := pixel.NewSprite(background, background.Bounds())
Sprite := pixel.NewSprite(spritesheet, spritesFrames[3])
2018-01-19 07:40:48 -07:00
//mouse := cam.Unproject(win.MousePosition())
win.Clear(colornames.Forestgreen)
bg.Draw(win, pixel.IM.Moved(win.Bounds().Center()))
Sprite.Draw(win, pixel.IM.Scaled(pixel.ZV, 4).Moved(playerXY))
2018-01-16 21:54:57 -07:00
if win.Pressed(pixelgl.KeyA) {
2018-01-19 07:40:48 -07:00
playerX -= camSpeed * dt
2018-01-16 21:54:57 -07:00
}
2018-01-19 07:40:48 -07:00
camPos.X = playerX
camPos.Y = playerY
2018-01-16 21:54:57 -07:00
if win.Pressed(pixelgl.KeyD) {
2018-01-19 07:40:48 -07:00
playerX += camSpeed * dt
2018-01-16 21:54:57 -07:00
}
if win.Pressed(pixelgl.KeyS) {
2018-01-19 07:40:48 -07:00
playerY -= camSpeed * dt
2018-01-16 21:54:57 -07:00
}
if win.Pressed(pixelgl.KeyW) {
2018-01-19 07:40:48 -07:00
playerY += camSpeed * dt
2018-01-16 21:33:27 -07:00
}
2018-01-19 07:40:48 -07:00
camZoom *= math.Pow(camZoomSpeed, win.MouseScroll().Y)
//batch.Draw(win)
2018-01-15 22:42:11 -07:00
win.Update()
2018-01-19 07:40:48 -07:00
frames++
select {
case <-second:
win.SetTitle(fmt.Sprintf("%s | FPS: %d", cfg.Title, frames))
frames = 0
default:
}
2018-01-15 22:42:11 -07:00
}
}
2018-01-15 21:41:59 -07:00
func main() {
2018-01-15 22:42:11 -07:00
pixelgl.Run(run)
2018-01-15 21:41:59 -07:00
}