Skip to content

Commit

Permalink
first commit
Browse files Browse the repository at this point in the history
  • Loading branch information
y-hatano-accelia committed Dec 6, 2019
0 parents commit b1331a2
Show file tree
Hide file tree
Showing 7 changed files with 186 additions and 0 deletions.
21 changes: 21 additions & 0 deletions LICENSE
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
MIT License

Copyright (c) 2019 y-hatano-github

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.
28 changes: 28 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
![gocandle](gocandle.gif)

# What is this?
Fire effect written in Golang.
Seeing fire make you feel calm.

## How to get and run the code.
### 1st step
Get the code.
```
go get -d github.com/y-hatano-github/go-candle
```
### 2nd step
Change directory to the source code directory.
```
cd $GOPATH/src/github.com/y-hatano-github/go-candle
```
### 3rd step
Run the code.
```
go run main.go
```

## Key bindings
[esc / CTRL+C] - exit<br>

## Win/Mac/Linux binary
https://github.com/y-hatano-github/go-candle/releases/
Binary file added go-candle
Binary file not shown.
8 changes: 8 additions & 0 deletions go.mod
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
module github.com/y-hatano-github/go-candle

go 1.12

require (
github.com/mattn/go-runewidth v0.0.7 // indirect
github.com/nsf/termbox-go v0.0.0-20190817171036-93860e161317
)
4 changes: 4 additions & 0 deletions go.sum
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
github.com/mattn/go-runewidth v0.0.7 h1:Ei8KR0497xHyKJPAv59M1dkC+rOZCMBJ+t3fZ+twI54=
github.com/mattn/go-runewidth v0.0.7/go.mod h1:H031xJmbD/WCDINGzjvQ9THkh0rPKHF+m2gUSrubnMI=
github.com/nsf/termbox-go v0.0.0-20190817171036-93860e161317 h1:hhGN4SFXgXo61Q4Sjj/X9sBjyeSa2kdpaOzCO+8EVQw=
github.com/nsf/termbox-go v0.0.0-20190817171036-93860e161317/go.mod h1:IuKpRQcYE1Tfu+oAQqaLisqDeXgjyyltCfsaoYN18NQ=
Binary file added gocandle.gif
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
125 changes: 125 additions & 0 deletions main.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,125 @@
package main

import (
"github.com/nsf/termbox-go"
"math/rand"
// "strconv"
"time"
)

const WIDTH = 16
const HEIGHT = 16
const FLUCTUATION = 5

func main() {
var m [WIDTH][HEIGHT]int
for y := 0; y < HEIGHT; y++ {
for x := 0; x < WIDTH; x++ {
m[x][y] = 0
}
}

colors := ([]termbox.Attribute{
0,
197,
203,
209,
215,
221,
227,
228,
229,
230,
231})

rand.Seed(time.Now().UnixNano())

err := termbox.Init()
if err != nil {
panic(err)
}
defer termbox.Close()
termbox.SetOutputMode(termbox.Output256)
termbox.Clear(termbox.ColorDefault, termbox.ColorDefault)

ch := make(chan termbox.Event)
go keyEvent(ch)

loop:
for {

select {
case ev := <-ch:
switch ev.Type {
case termbox.EventKey:
if ev.Key == termbox.KeyCtrlC || ev.Key == termbox.KeyEsc {
break loop
}
}
break
default:
termbox.Clear(termbox.ColorDefault, termbox.ColorDefault)
fire(&m, colors)
drawFire(m, colors)
termbox.Flush()
}
}
}

func fire(m *[WIDTH][HEIGHT]int, colors []termbox.Attribute) {
for x := 0; x < WIDTH; x++ {
for y := 0; y < HEIGHT; y++ {

if y < HEIGHT-2 {
if x == WIDTH-1 {
m[x][y] = (m[x][y] + m[x-1][y+1] + m[x][y+1] + m[x][y+2]) / 5
} else if x == 0 {
m[x][y] = (m[x][y] + m[x][y+1] + m[x+1][y+1] + m[x][y+2]) / 5
} else {
m[x][y] = (m[x][y] + m[x-1][y+1] + m[x][y+1] + m[x+1][y+1] + m[x][y+2]) / 5
}
} else if y < HEIGHT-1 {
if x == WIDTH-1 {
m[x][y] = (m[x][y] + m[x-1][y+1] + m[x][y+1]) / 5
} else if x == 0 {
m[x][y] = (m[x][y] + m[x][y+1] + m[x+1][y+1]) / 5
} else if x < 3 || x > WIDTH-4 {
m[x][y] = (m[x][y] + m[x-1][y+1] + m[x][y+1] + m[x+1][y+1]) / 5
}
} else {
if x < 3 || x > WIDTH-4 {
m[x][y] = m[x][y] / 5
}
}
if rand.Intn(FLUCTUATION) == 1 {
m[x][y] = m[x][y] - 1
}
if m[x][y] < 0 {
m[x][y] = 0
}
}
}

for x := 5; x < WIDTH-5; x++ {
m[x][HEIGHT-1] = rand.Intn(3) + len(colors) - 3

}
for x := 4; x < WIDTH-4; x++ {
m[x][HEIGHT-2] = rand.Intn(3) + len(colors) - 3
}

}

func keyEvent(ch chan termbox.Event) {
for {
ch <- termbox.PollEvent()
}
}

func drawFire(m [WIDTH][HEIGHT]int, colors []termbox.Attribute) {
for x := 0; x < WIDTH; x++ {
for y := 0; y < HEIGHT; y++ {
termbox.SetCell(x+2, y+2, ' ', termbox.ColorDefault, colors[m[x][y]])
}
}
}

0 comments on commit b1331a2

Please sign in to comment.