From f7a2b25a63ed2197030ab74a37c4f6c73ba99873 Mon Sep 17 00:00:00 2001 From: Luke Meyers Date: Fri, 7 Feb 2020 23:51:00 -0800 Subject: [PATCH] Sprites! --- gfx/gfx.go | 69 ++++++++++++++++++++++++++++++++++---------- main.go | 24 ++++++++++----- simplebot-white.png | Bin 0 -> 181 bytes 3 files changed, 70 insertions(+), 23 deletions(-) create mode 100644 simplebot-white.png diff --git a/gfx/gfx.go b/gfx/gfx.go index d670801..f309805 100644 --- a/gfx/gfx.go +++ b/gfx/gfx.go @@ -1,7 +1,11 @@ package gfx import ( + "fmt" + "image" "image/color" + _ "image/png" + "os" "relay/game" "github.com/faiface/pixel" @@ -23,8 +27,35 @@ type context struct { w *pixelgl.Window } -func Render(rs RenderState, sOld, sNew game.State, w *pixelgl.Window) RenderState { - w.Clear(colornames.Olivedrab) +type spriteBank struct { + bot pixel.Picture +} + +func NewSpriteBank() (*spriteBank, error) { + pic, err := loadPicture("simplebot-white.png") + if err != nil { + return nil, fmt.Errorf("load picture: %w", err) + } + return &spriteBank{ + bot: pic, + }, nil +} + +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 +} + +func Render(rs RenderState, sOld, sNew game.State, w *pixelgl.Window, sb spriteBank) RenderState { + w.Clear(colornames.Black) colors := teamColors(sNew.Teams) ctx := context{ @@ -33,7 +64,7 @@ func Render(rs RenderState, sOld, sNew game.State, w *pixelgl.Window) RenderStat tween: float64(rs.Frame) / float64(rs.Frames), w: w, } - renderBots(ctx, colors) + renderBots(ctx, colors, sb.bot) renderObstacles(sNew, w) rs.Frame++ @@ -43,12 +74,12 @@ func Render(rs RenderState, sOld, sNew game.State, w *pixelgl.Window) RenderStat return rs } -func renderBots(ctx context, colors map[*game.Team]pixel.RGBA) { +func renderBots(ctx context, colors map[*game.Team]pixel.RGBA, pic pixel.Picture) { for i, t := range ctx.sNew.Teams { c := colors[&ctx.sNew.Teams[i]] for j, bot := range t.Bots { oldBot := ctx.sOld.Teams[i].Bots[j] - renderBot(oldBot, bot, ctx.sOld, ctx.sNew, ctx.w, c, ctx.tween) + renderBot(ctx, oldBot, bot, c, pic) } oldHolder, newHolder := game.ActiveBot(ctx.sOld.Teams[i]), game.ActiveBot(ctx.sNew.Teams[i]) @@ -63,27 +94,33 @@ func renderBots(ctx context, colors map[*game.Team]pixel.RGBA) { } } -func renderBot(oldBot, bot game.Bot, sOld, sNew game.State, w *pixelgl.Window, c pixel.RGBA, tween float64) { +func renderBot(ctx context, oldBot, bot game.Bot, c pixel.RGBA, pic pixel.Picture) { im := imdraw.New(nil) im.Color = c - oldPos := lanePos(oldBot.Position.Pos, oldBot.Position.Lane, botWidth, w.Bounds()) - newPos := lanePos(bot.Position.Pos, bot.Position.Lane, botWidth, w.Bounds()) + oldPos := lanePos(oldBot.Position.Pos, oldBot.Position.Lane, botWidth, ctx.w.Bounds()) + newPos := lanePos(bot.Position.Pos, bot.Position.Lane, botWidth, ctx.w.Bounds()) pos := pixel.Vec{ - X: oldPos.X + tween*(newPos.X-oldPos.X), - Y: oldPos.Y + tween*(newPos.Y-oldPos.Y), + X: oldPos.X + ctx.tween*(newPos.X-oldPos.X), + Y: oldPos.Y + ctx.tween*(newPos.Y-oldPos.Y), } im.Push(pos) im.Clear() - im.Circle(botWidth, 0) - im.Draw(w) + //im.Circle(botWidth, 0) + im.Draw(ctx.w) + bounds := pic.Bounds() + //log.Println("bounds:", bounds) + //bounds = bounds.Resized(bounds.Center(), pixel.Vec{bounds.W() * 2, bounds.H() * 4}) + //log.Println("resize:", bounds) + sprite := pixel.NewSprite(pic, bounds) + sprite.DrawColorMask(ctx.w, pixel.IM.Moved(pos).ScaledXY(pos, pixel.Vec{3, 3}), c) } func renderBaton(pos pixel.Vec, w *pixelgl.Window) { im := imdraw.New(nil) - im.Color = pixel.RGB(0, 0, 0) + im.Color = colornames.Bisque im.Push(pos) im.Clear() im.Circle(batonWidth, 3) @@ -103,7 +140,7 @@ func renderObstacles(s game.State, w *pixelgl.Window) { im := imdraw.New(nil) for _, o := range s.Obstacles { - im.Color = pixel.RGB(0.1, 0.1, 0.2) + im.Color = colornames.Slategray pos := lanePos(o.Position.Pos, o.Position.Lane, botWidth, b) @@ -126,13 +163,15 @@ func teamColors(ts []game.Team) map[*game.Team]pixel.RGBA { case 1: c = colornames.Green case 2: - c = colornames.Blue + c = colornames.Cornflowerblue case 3: c = colornames.Magenta case 4: c = colornames.Cyan case 5: c = colornames.Yellow + case 6: + c = colornames.Blueviolet } m[&ts[i]] = pixel.ToRGBA(c) } diff --git a/main.go b/main.go index 66a61b7..5105b5d 100644 --- a/main.go +++ b/main.go @@ -9,10 +9,9 @@ import ( "github.com/faiface/pixel" "github.com/faiface/pixel/pixelgl" - "golang.org/x/image/colornames" ) -func run() { +func run() error { cfg := pixelgl.WindowConfig{ Title: "Relay", Bounds: pixel.R(0, 0, 2048, 512), @@ -21,28 +20,30 @@ func run() { w, err := pixelgl.NewWindow(cfg) if err != nil { - panic(err) + return err } rand.Seed(time.Now().UnixNano()) s := game.NewState() - w.Clear(colornames.Peachpuff) - rs := gfx.RenderState{ Animating: true, Frames: 20, } + sb, err := gfx.NewSpriteBank() + if err != nil { + return err + } sOld := s turn := 1 for !w.Closed() && !s.GameOver { switch { case w.Pressed(pixelgl.KeyQ): - return + return nil case rs.Animating: - rs = gfx.Render(rs, sOld, s, w) + rs = gfx.Render(rs, sOld, s, w, *sb) if !rs.Animating { sOld = s } @@ -61,8 +62,15 @@ func run() { w.Update() } + return nil +} + +func pixelRun() { + if err := run(); err != nil { + log.Fatal(err) + } } func main() { - pixelgl.Run(run) + pixelgl.Run(pixelRun) } diff --git a/simplebot-white.png b/simplebot-white.png new file mode 100644 index 0000000000000000000000000000000000000000..e7fb519b8bc2ecc824489bfec4cea46a9a3e8595 GIT binary patch literal 181 zcmeAS@N?(olHy`uVBq!ia0vp^0wB!61|;P_|4#%`jKx9jP7LeL$-D$|@;zM~Lo9mF z208K_FyLrP`~Tl)_n9TeNw?=7TPfP)!ph}w$&Z)8O(4?XzpT3F;v!atNmrOBZP`^Y z%T+J$+Jq-pIS;S!{$Nxvw|K8f$z=PR9+|s@ZtK2z