From 502ed1c0a2eca2661c21168cf356b491b3130829 Mon Sep 17 00:00:00 2001 From: Peter Hellberg Date: Thu, 11 Jan 2018 22:39:45 +0100 Subject: [PATCH] Add starfield experiment to community examples --- community/starfield/README.md | 20 ++++ community/starfield/screenshot.png | Bin 0 -> 12550 bytes community/starfield/starfield.go | 165 +++++++++++++++++++++++++++++ 3 files changed, 185 insertions(+) create mode 100644 community/starfield/README.md create mode 100644 community/starfield/screenshot.png create mode 100644 community/starfield/starfield.go diff --git a/community/starfield/README.md b/community/starfield/README.md new file mode 100644 index 0000000..9afba9f --- /dev/null +++ b/community/starfield/README.md @@ -0,0 +1,20 @@ +# starfield + +Classic starfield… with [supposedly accurate stellar colors](http://www.vendian.org/mncharity/dir3/starcolor/) + +Made by [Peter Hellberg](https://github.com/peterhellberg/) as part of his [pixel-experiments](https://github.com/peterhellberg/pixel-experiments) + +## Controls + +Arrow up and down to change speed. Space bar to almost stop. + +## Screenshots + +![starfield animation](https://user-images.githubusercontent.com/565124/32411599-a5fcba72-c1df-11e7-8730-a570470a4eee.gif) + +![starfield screenshot](screenshot.png) + +## Links + + - https://github.com/peterhellberg/pixel-experiments/tree/master/starfield + - https://gist.github.com/peterhellberg/4018e228cced61a0bb26991e49299c96 diff --git a/community/starfield/screenshot.png b/community/starfield/screenshot.png new file mode 100644 index 0000000000000000000000000000000000000000..513ca6fad330e8cfac80621d90b8e9e02a18c833 GIT binary patch literal 12550 zcma)i2|QH)`|oqk9AgG$>`G;_i!`EA!XY6kT1myEeNkGe)SM&~p%o$1?%S$RO69a_ z6GfI(ri3;bO)(gA?q~Y`{`cPh>)zLW-Sg^|IiKbEJfG)zKJRDwoV`KQeFta^)&PJ3 zQ~bPV0&vhz0yr%6$E#t=7W8LqP{8cT=r8l<8*?Z6)J^7}y9`sF!gPMPe4oj=#(e(5 zFm)H&e=^MXDyFBWtctn$oOxQu^vRi%<$v@61j$ppJ!i+f{qg?QbQ4D&Im2~Rf6!BH z1ygx+b7R3gTo)U@y>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sTy{>)EmdS->W)ih;X;-+HhAt@Xw{>FJPz26S3?E8 z$>^pIRYOOf@mq%x11Z@<`C?@-4=zlhuD5h*|1l(&j$&&-s2LD+4y>48qerDPJtCydOI1}}mDN;u%=Uw{ zQ7Vn{hNDj>we3VgDlc=m>Ji3Q96K=$S+XeUZ7a#pT|9wL%;#GsDk#OMnR1dolD`c?w-mlIA^ss zb=rSMMl8A2S~BLj;z1!-@IcXoKq^QT%kg0_LexG?roDV3J99Wp z{3{p|AgisccxOJw1W@Cz+-rWrLR+MOU*b6UgnhPrT?_`}e4KC8+z0?~a}YaVylHRe zj~%PaPi-0AC2#uefJtIaFZl4MtjmHVA0Rj5{A2Mq)?6e==jYQkQT1LMOAouY-3rWf zAt)D;Y+kvUm~eTO*LrgzP^n*bE6il}eiUxLnV83*S6$E_KwU+LjSE!1RoQBsKNbVt zn}?{T*qlbulR(3VLgt1bCj^W|=<*=nNOhnf$Q~XGn4C%lYtA2K@v2TP{3L+P0)g^2 zx>pIi%Xw0Wd_EUiEx~UnI>o_GjOwmi$paX_&|#;%+!ZazBfuBU8l5q;xw6lAr=;}# zgs~WpgY)H2&5e`#gD))LpJZRz8#S)_p~Dqp*dRbR=f@n57O*?jsnc303UPY>qtl~E zf7^=m54TqXn}(od4BWNp!J|i#9c*fZ76?HL)CPgDevI-aTdb~08n8hCY${F*1V4r_ oYgl~c?8WFqU?JE%LAP#WfBhKN`|z6RJfLUF 6 { + imd.Push(p, o) + imd.Line(r) + } + + imd.Push(p) + imd.Circle(r, 0) +} + +func run() { + win, err := pixelgl.NewWindow(pixelgl.WindowConfig{ + Bounds: pixel.R(0, 0, w, h), + VSync: true, + Undecorated: true, + }) + if err != nil { + panic(err) + } + + imd := imdraw.New(nil) + + imd.Precision = 7 + + imd.SetMatrix(pixel.IM.Moved(win.Bounds().Center())) + + last := time.Now() + + for !win.Closed() { + win.SetClosed(win.JustPressed(pixelgl.KeyEscape) || win.JustPressed(pixelgl.KeyQ)) + + if win.Pressed(pixelgl.KeyUp) { + speed += 10 + } + + if win.Pressed(pixelgl.KeyDown) { + if speed > 10 { + speed -= 10 + } + } + + if win.Pressed(pixelgl.KeySpace) { + speed = 100 + } + + d := time.Since(last).Seconds() + + last = time.Now() + + imd.Clear() + + for _, s := range stars { + s.update(d) + s.draw(imd) + } + + win.Clear(color.Black) + imd.Draw(win) + win.Update() + } +} + +func main() { + pixelgl.Run(run) +} + +func random(min, max float64) float64 { + return rand.Float64()*(max-min) + min +} + +func scale(unscaledNum, min, max, minAllowed, maxAllowed float64) float64 { + return (maxAllowed-minAllowed)*(unscaledNum-min)/(max-min) + minAllowed +} + +// Colors based on stellar types listed at +// http://www.vendian.org/mncharity/dir3/starcolor/ +var Colors = []color.RGBA{ + color.RGBA{157, 180, 255, 255}, + color.RGBA{162, 185, 255, 255}, + color.RGBA{167, 188, 255, 255}, + color.RGBA{170, 191, 255, 255}, + color.RGBA{175, 195, 255, 255}, + color.RGBA{186, 204, 255, 255}, + color.RGBA{192, 209, 255, 255}, + color.RGBA{202, 216, 255, 255}, + color.RGBA{228, 232, 255, 255}, + color.RGBA{237, 238, 255, 255}, + color.RGBA{251, 248, 255, 255}, + color.RGBA{255, 249, 249, 255}, + color.RGBA{255, 245, 236, 255}, + color.RGBA{255, 244, 232, 255}, + color.RGBA{255, 241, 223, 255}, + color.RGBA{255, 235, 209, 255}, + color.RGBA{255, 215, 174, 255}, + color.RGBA{255, 198, 144, 255}, + color.RGBA{255, 190, 127, 255}, + color.RGBA{255, 187, 123, 255}, + color.RGBA{255, 187, 123, 255}, +}