19 Commits

Author SHA1 Message Date
f35e404703 slowed down the sprite 2018-01-23 06:12:38 -07:00
748f605bff animated sprite 2018-01-23 06:06:35 -07:00
297e33d89e added frames only concerning sprite 2018-01-23 05:52:53 -07:00
452bd9cdc3 added frames only concerning sprite 2018-01-23 05:38:52 -07:00
965b7701bc Increase speed to 10 2018-01-23 03:20:29 -07:00
6d65d6b783 Change window title 2018-01-23 03:19:02 -07:00
4ad0995076 Merge jalen's changes 2018-01-23 03:15:39 -07:00
c6b9153782 added direction change to sprite 2018-01-22 10:09:33 -07:00
7ed2ca2ca0 Remove unused sprite images 2018-01-19 08:51:03 -07:00
0f232e3c92 Merge branch 'master' of https://gitbutter.pw/zolfite/project-rpg 2018-01-19 08:48:39 -07:00
a51d02daf6 change sprite image to character 2018-01-19 08:47:53 -07:00
69977a3096 Change tree sprite to character sprite; use wasd instead of arrow keys 2018-01-19 08:47:25 -07:00
373f5800ea Add background image for reference 2018-01-19 07:41:05 -07:00
ea62e2dca4 Bind camera and sprite 2018-01-19 07:40:48 -07:00
90daf028e1 Add license 2018-01-17 19:19:06 +00:00
311a15e3e0 add camera to move with w,a,s,d 2018-01-16 21:54:57 -07:00
a0de8fd135 add tree asset 2018-01-16 21:33:37 -07:00
3720389fef draw trees with left mouse click 2018-01-16 21:33:27 -07:00
b2e8ec4f3d Added a handler, update, and render 2018-01-16 07:35:04 -07:00
5 changed files with 156 additions and 38 deletions

21
LICENSE Normal file
View File

@ -0,0 +1,21 @@
MIT License
Copyright (c) 2018 Zolfite
Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"), to deal
in the Software without restriction, including without limitation the rights
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
copies of the Software, and to permit persons to whom the Software is
furnished to do so, subject to the following conditions:
The above copyright notice and this permission notice shall be included in all
copies or substantial portions of the Software.
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
SOFTWARE.

BIN
background.png Normal file

Binary file not shown.

After

Width:  |  Height:  |  Size: 17 KiB

Binary file not shown.

Before

Width:  |  Height:  |  Size: 68 KiB

173
main.go
View File

@ -1,28 +1,25 @@
// main
package main
import (
"fmt"
"image"
_ "image/png"
"math"
"os"
"time"
_ "image/png"
"github.com/faiface/pixel"
"github.com/faiface/pixel/pixelgl"
"golang.org/x/image/colornames"
)
//open file from the system
func loadPicture(path string) (pixel.Picture, error) {
file, err := os.Open(path)
if err != nil {
return nil, err
}
//user 'defer' to close the file later on
defer file.Close()
//decode the file to find it's type
img, _, err := image.Decode(file)
if err != nil {
return nil, err
@ -30,59 +27,159 @@ func loadPicture(path string) (pixel.Picture, error) {
return pixel.PictureDataFromImage(img), nil
}
//Pixel's main function (because GO forced them to do it this way)
func run() {
//set the Window parameters
cfg := pixelgl.WindowConfig{
Title: "Pixel Rocks!",
Title: "Project RPG",
Bounds: pixel.R(0, 0, 1024, 768),
VSync: true,
}
//create a window and pass the parameters
win, err := pixelgl.NewWindow(cfg)
if err != nil {
panic(err)
}
//smooth the edges of the image (antialiasing)
win.SetSmooth(true)
//load a specific image and catch any errors
pic, err := loadPicture("hiking.png")
spritesheet, err := loadPicture("sprites.png")
if err != nil {
panic(err)
}
//batch := pixel.NewBatch(&pixel.TrianglesData{}, spritesheet)
//create a sprite using the loaded image
sprite := pixel.NewSprite(pic, pic.Bounds())
background, err := loadPicture("background.png")
//set the angle of the image
angle := 0.0
if err != nil {
panic(err)
}
bg := pixel.NewSprite(background, background.Bounds())
//batch := pixel.NewBatch(&pixel.TrianglesData{}, spritesheet)
//set the time to compare later
last := time.Now()
/* setting-character-sprite
//loop until window is closed by "X" button
basically changed it around so the 12 frames of the character with yellow hair
are added to spritesFrames.
*/
var spritesFrames []pixel.Rect
var frameWidth, frameHeight float64 = 96, 96
for y := spritesheet.Bounds().Min.Y; y < frameHeight*4; y += 96 {
for x := spritesheet.Bounds().Min.X; x < frameWidth*3; x += 96 {
spritesFrames = append(spritesFrames, pixel.R(x, y, x+96, y+96)) // (x, y, width, height) of frame
}
}
Sprite := pixel.NewSprite(spritesheet, spritesFrames[9])
var (
camPos = pixel.ZV
//camSpeed = 500.0
camZoom = 0.3
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,
}
)
var fps int64 = 60
timePerFrame := 1000000000 / fps
var lastTime int64 = time.Now().UnixNano()
var now int64
var dt int64
frameCounter := 0
// Game Loop
for !win.Closed() {
//compare the last time to delta time
dt := time.Since(last).Seconds()
last = time.Now()
now = time.Now().UnixNano()
dt += (now - lastTime)
lastTime = time.Now().UnixNano()
//set the angle according to delta time
angle += 3 * dt
//Execute a frame.
if dt >= timePerFrame {
//refresh screen and set background color
win.Clear(colornames.Firebrick)
// *** Update begins *** //
//set the matrix and location of spirte in window
mat := pixel.IM
mat = mat.Rotated(pixel.ZV, angle)
mat = mat.Moved(win.Bounds().Center())
sprite.Draw(win, mat)
playerXY = pixel.Vec{
playerX,
playerY,
}
cam := pixel.IM.Scaled(camPos, camZoom).Moved(win.Bounds().Center().Sub(camPos))
//update the window
win.Update()
win.SetMatrix(cam)
/*
This is where Sprite and bg was originally. Creating them over and over inside
the main loop is not very good resource management. When i added my own dt, the game was
only going about 22 fps, when it should've been going 60. I moved them
out of the loop and created them up top. It fixed it and now runs faster.
the tutorial only has it in the loop with the exception that it creates it only
if you click the button to make a tree. It only created it once and added it.
*/
//mouse := cam.Unproject(win.MousePosition())
if frames%10==0 {
frameCounter += 1
}
if frameCounter >= 3 {
frameCounter = 0
}
speed := 10.0
sprFrame := 0
if win.Pressed(pixelgl.KeyA) { //left
sprFrame = 6
playerX -= speed
//Sprite.Set(spritesheet, spritesFrames[sprFrame+frameCounter])
}
if win.Pressed(pixelgl.KeyD) { //Right
sprFrame = 3
playerX += speed
//Sprite.Set(spritesheet, spritesFrames[3])
}
if win.Pressed(pixelgl.KeyS) { //Down
sprFrame = 9
playerY -= speed
//Sprite.Set(spritesheet, spritesFrames[9])
}
if win.Pressed(pixelgl.KeyW) { //up
sprFrame = 0
playerY += speed
//Sprite.Set(spritesheet, spritesFrames[0])
}
Sprite.Set(spritesheet, spritesFrames[sprFrame+frameCounter])
camPos.X = playerX
camPos.Y = playerY
camZoom *= math.Pow(camZoomSpeed, win.MouseScroll().Y)
// *** Update Ends *** //
// *** Render begin *** //
win.Clear(colornames.Forestgreen)
bg.Draw(win, pixel.IM.Moved(win.Bounds().Center()))
Sprite.Draw(win, pixel.IM.Scaled(pixel.ZV, 4).Moved(playerXY))
//batch.Draw(win)
win.Update()
// *** Render End *** //
frames++
select {
case <-second:
win.SetTitle(fmt.Sprintf("%s | FPS: %d", cfg.Title, frames))
frames = 0
default:
}
}
}
}

BIN
sprites.png Normal file

Binary file not shown.

After

Width:  |  Height:  |  Size: 62 KiB