Skip to content

Instantly share code, notes, and snippets.

@turnipsoup
Created August 16, 2021 01:18
Show Gist options
  • Save turnipsoup/d6845d66cb13e707c9906408c6552bae to your computer and use it in GitHub Desktop.
Save turnipsoup/d6845d66cb13e707c9906408c6552bae to your computer and use it in GitHub Desktop.
Just learning Ebiten - will slowly fill every pixel in the window starting from the top left.
package main
import (
"image/color"
"log"
"github.com/hajimehoshi/ebiten/v2"
)
// Game implements ebiten.Game interface.
type Game struct{}
// Variables Used By Me
const WIDTH int = 320
const HEIGHT int = 240
var pos_x int = -1
var pos_y int = 0
var myColor color.Color = color.RGBA{0xff, 0, 0, 0xff}
var pixelSlice [][]int
func drawAllPixels(screen *ebiten.Image, ps [][]int) {
for i:= 0; i<len(ps); i += 1 {
screen.Set(ps[i][0],ps[i][1],myColor)
}
}
// Update proceeds the game state.
// Update is called every tick (1/60 [s] by default).
func (g *Game) Update() error {
pos_x += 1
if pos_x == WIDTH {
pos_x = 0
pos_y += 1
}
pixel := []int{}
pixel = append(pixel, pos_x)
pixel = append(pixel, pos_y)
pixelSlice = append(pixelSlice, pixel)
log.Println(pos_x, pos_y)
return nil
}
// Draw draws the game screen.
// Draw is called every frame (typically 1/60[s] for 60Hz display).
func (g *Game) Draw(screen *ebiten.Image) {
drawAllPixels(screen, pixelSlice)
}
// Layout takes the outside size (e.g., the window size) and returns the (logical) screen size.
// If you don't have to adjust the screen size with the outside size, just return a fixed size.
func (g *Game) Layout(outsideWidth, outsideHeight int) (screenWidth, screenHeight int) {
return 320, 240
}
func main() {
game := &Game{}
// Specify the window size as you like. Here, a doubled size is specified.
ebiten.SetWindowSize(640, 480)
ebiten.SetWindowTitle("Your game's title")
// Call ebiten.RunGame to start your game loop.
if err := ebiten.RunGame(game); err != nil {
log.Fatal(err)
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment