Advertisement
Guest User

Untitled

a guest
Jan 28th, 2020
73
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.37 KB | None | 0 0
  1. package main
  2.  
  3. import (
  4. "os"
  5. "time"
  6. "github.com/nsf/termbox-go"
  7. )
  8.  
  9. type (
  10. Runner struct {
  11. x, y int
  12. vx, vy int
  13. color termbox.Attribute
  14. }
  15.  
  16. Game struct {
  17. rs []Runner
  18. delay time.Duration
  19. }
  20. )
  21.  
  22. func NewRunner (x, y int, vx, vy int, color termbox.Attribute) Runner {
  23. var r Runner
  24. r.x, r.y = x, y
  25. r.vx, r.vy = vx, vy
  26. r.color = color
  27. return r
  28. }
  29.  
  30. func (r *Runner) Step() {
  31. width, height := termbox.Size()
  32. termbox.SetCell(r.x, r.y, ' ', termbox.ColorDefault, termbox.ColorDefault)
  33. r.x += r.vx
  34. r.y += r.vy
  35. termbox.SetCell(r.x, r.y, ' ', termbox.ColorDefault, r.color)
  36. if r.x<=0 || r.x>=width-1 {
  37. r.vx = -r.vx
  38. }
  39. if r.y<=0 || r.y>=height-1 {
  40. r.vy = -r.vy
  41. }
  42. }
  43.  
  44. func (g *Game) Step () {
  45. for i:=0; i < len(g.rs); i++ {
  46. g.rs[i].Step()
  47. }
  48. }
  49.  
  50. func (g *Game) Run () {
  51. for {
  52. g.Step()
  53. termbox.Flush()
  54. time.Sleep(g.delay * time.Millisecond)
  55. }
  56. }
  57.  
  58. func (g *Game) Add (r Runner) {
  59. g.rs = append(g.rs, r)
  60. }
  61.  
  62.  
  63. func main() {
  64. if err := termbox.Init(); err != nil {
  65. os.Exit(1) // Ошибка инициализации termbox
  66. }
  67. defer termbox.Close()
  68.  
  69. width, height := termbox.Size()
  70. termbox.HideCursor()
  71. var p Game
  72. p.Add (NewRunner(width/2, height/2, 1, -1, termbox.ColorGreen))
  73. p.Add (NewRunner(width-4, height-2, -1, -1, termbox.ColorRed))
  74. p.delay = 30
  75. go p.Run()
  76. time.Sleep(5*time.Second)
  77. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement