Merge jalen's changes
This commit is contained in:
commit
4ad0995076
94
main.go
94
main.go
@ -42,24 +42,34 @@ func run() {
|
|||||||
if err != nil {
|
if err != nil {
|
||||||
panic(err)
|
panic(err)
|
||||||
}
|
}
|
||||||
|
//batch := pixel.NewBatch(&pixel.TrianglesData{}, spritesheet)
|
||||||
|
|
||||||
background, err := loadPicture("background.png")
|
background, err := loadPicture("background.png")
|
||||||
|
|
||||||
if err != nil {
|
if err != nil {
|
||||||
panic(err)
|
panic(err)
|
||||||
}
|
}
|
||||||
|
bg := pixel.NewSprite(background, background.Bounds())
|
||||||
//batch := pixel.NewBatch(&pixel.TrianglesData{}, spritesheet)
|
//batch := pixel.NewBatch(&pixel.TrianglesData{}, spritesheet)
|
||||||
|
|
||||||
|
/*
|
||||||
|
When creating the sprite, you need to know big each frame is inside the spritesheet.
|
||||||
|
If you use aseprite or some other pixel art program, you can find the number of pixels
|
||||||
|
within each frame. For our sprite sheet, each frame is 96 px wide and 96 px high. So
|
||||||
|
we increment it by 96 for both x and y so that it can get the position of the frame
|
||||||
|
at the bottom right corner.
|
||||||
|
*/
|
||||||
var spritesFrames []pixel.Rect
|
var spritesFrames []pixel.Rect
|
||||||
for x := spritesheet.Bounds().Min.X; x < spritesheet.Bounds().Max.X; x += 32 {
|
for x := spritesheet.Bounds().Min.X; x < spritesheet.Bounds().Max.X; x += 96 {
|
||||||
for y := spritesheet.Bounds().Min.Y; y < spritesheet.Bounds().Max.Y; y += 32 {
|
for y := spritesheet.Bounds().Min.Y; y < spritesheet.Bounds().Max.Y; y += 96 {
|
||||||
spritesFrames = append(spritesFrames, pixel.R(x, y, x+79, y+92))
|
spritesFrames = append(spritesFrames, pixel.R(x, y, x+96, y+96)) // (x, y, width, height) of frame
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
Sprite := pixel.NewSprite(spritesheet, spritesFrames[3])
|
||||||
|
|
||||||
var (
|
var (
|
||||||
camPos = pixel.ZV
|
camPos = pixel.ZV
|
||||||
camSpeed = 500.0
|
//camSpeed = 500.0
|
||||||
camZoom = 0.3
|
camZoom = 0.3
|
||||||
camZoomSpeed = 1.2
|
camZoomSpeed = 1.2
|
||||||
)
|
)
|
||||||
@ -78,44 +88,75 @@ func run() {
|
|||||||
}
|
}
|
||||||
)
|
)
|
||||||
|
|
||||||
last := time.Now()
|
var fps int64 = 60
|
||||||
|
timePerFrame := 1000000000 / fps
|
||||||
|
var lastTime int64 = time.Now().UnixNano()
|
||||||
|
var now int64
|
||||||
|
var dt int64
|
||||||
|
|
||||||
|
// Game Loop
|
||||||
for !win.Closed() {
|
for !win.Closed() {
|
||||||
dt := time.Since(last).Seconds()
|
now = time.Now().UnixNano()
|
||||||
last = time.Now()
|
dt += (now - lastTime)
|
||||||
|
lastTime = time.Now().UnixNano()
|
||||||
|
|
||||||
|
//Execute a frame.
|
||||||
|
if dt >= timePerFrame {
|
||||||
|
|
||||||
|
// *** Update begins *** //
|
||||||
|
|
||||||
playerXY = pixel.Vec{
|
playerXY = pixel.Vec{
|
||||||
playerX,
|
playerX,
|
||||||
playerY,
|
playerY,
|
||||||
}
|
}
|
||||||
cam := pixel.IM.Scaled(camPos, camZoom).Moved(win.Bounds().Center().Sub(camPos))
|
cam := pixel.IM.Scaled(camPos, camZoom).Moved(win.Bounds().Center().Sub(camPos))
|
||||||
|
|
||||||
win.SetMatrix(cam)
|
win.SetMatrix(cam)
|
||||||
bg := pixel.NewSprite(background, background.Bounds())
|
/*
|
||||||
Sprite := pixel.NewSprite(spritesheet, spritesFrames[3])
|
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())
|
//mouse := cam.Unproject(win.MousePosition())
|
||||||
|
|
||||||
|
speed := 2.0
|
||||||
|
if win.Pressed(pixelgl.KeyA) { //left
|
||||||
|
playerX -= speed
|
||||||
|
Sprite.Set(spritesheet, spritesFrames[2])
|
||||||
|
}
|
||||||
|
if win.Pressed(pixelgl.KeyD) { //Right
|
||||||
|
playerX += speed
|
||||||
|
Sprite.Set(spritesheet, spritesFrames[1])
|
||||||
|
}
|
||||||
|
if win.Pressed(pixelgl.KeyS) { //Down
|
||||||
|
playerY -= speed
|
||||||
|
Sprite.Set(spritesheet, spritesFrames[3])
|
||||||
|
}
|
||||||
|
if win.Pressed(pixelgl.KeyW) { //up
|
||||||
|
playerY += speed
|
||||||
|
Sprite.Set(spritesheet, spritesFrames[0])
|
||||||
|
}
|
||||||
|
camPos.X = playerX
|
||||||
|
camPos.Y = playerY
|
||||||
|
camZoom *= math.Pow(camZoomSpeed, win.MouseScroll().Y)
|
||||||
|
|
||||||
|
// *** Update Ends *** //
|
||||||
|
|
||||||
|
// *** Render begin *** //
|
||||||
win.Clear(colornames.Forestgreen)
|
win.Clear(colornames.Forestgreen)
|
||||||
bg.Draw(win, pixel.IM.Moved(win.Bounds().Center()))
|
bg.Draw(win, pixel.IM.Moved(win.Bounds().Center()))
|
||||||
Sprite.Draw(win, pixel.IM.Scaled(pixel.ZV, 4).Moved(playerXY))
|
Sprite.Draw(win, pixel.IM.Scaled(pixel.ZV, 4).Moved(playerXY))
|
||||||
if win.Pressed(pixelgl.KeyA) {
|
|
||||||
playerX -= camSpeed * dt
|
|
||||||
|
|
||||||
}
|
|
||||||
|
|
||||||
camPos.X = playerX
|
|
||||||
camPos.Y = playerY
|
|
||||||
if win.Pressed(pixelgl.KeyD) {
|
|
||||||
playerX += camSpeed * dt
|
|
||||||
}
|
|
||||||
if win.Pressed(pixelgl.KeyS) {
|
|
||||||
playerY -= camSpeed * dt
|
|
||||||
}
|
|
||||||
if win.Pressed(pixelgl.KeyW) {
|
|
||||||
playerY += camSpeed * dt
|
|
||||||
}
|
|
||||||
camZoom *= math.Pow(camZoomSpeed, win.MouseScroll().Y)
|
|
||||||
|
|
||||||
//batch.Draw(win)
|
//batch.Draw(win)
|
||||||
win.Update()
|
win.Update()
|
||||||
|
|
||||||
|
// *** Render End *** //
|
||||||
|
|
||||||
frames++
|
frames++
|
||||||
select {
|
select {
|
||||||
case <-second:
|
case <-second:
|
||||||
@ -125,6 +166,7 @@ func run() {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
}
|
||||||
|
|
||||||
func main() {
|
func main() {
|
||||||
pixelgl.Run(run)
|
pixelgl.Run(run)
|
||||||
|
Loading…
x
Reference in New Issue
Block a user