game.go 3.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184
  1. package main
  2. import (
  3. "fmt"
  4. "log"
  5. "math/rand"
  6. "github.com/gdamore/tcell/v2"
  7. )
  8. func gameinit() {
  9. game, err := initialize()
  10. if err != nil {
  11. log.Fatalln("Initilization error: ", err)
  12. }
  13. go game.gameDirector()
  14. game.keyboardProcessor()
  15. // Quit the screen
  16. quit(&game)
  17. fmt.Println("\nDebug time: Trash")
  18. for i, t := range game.trash {
  19. fmt.Println("Trash: ", i, t)
  20. }
  21. // Give closure
  22. if game.gameover == 1 {
  23. fmt.Println("\nYou got rekt by a robot")
  24. } else if game.gameover == 2 {
  25. fmt.Println("\nYou took the easy way out")
  26. }
  27. fmt.Println("Score:", game.player.score)
  28. fmt.Println("Moves:", game.player.moves)
  29. fmt.Println("Teleports:", game.player.teleports)
  30. }
  31. func initialize() (Game, error) {
  32. var err error
  33. screen, err := tcell.NewScreen()
  34. if err != nil {
  35. log.Println("Error creating screen: ", err)
  36. }
  37. err = screen.Init()
  38. if err != nil {
  39. log.Fatalln("Error initializing screen: ", err)
  40. }
  41. style := tcell.StyleDefault.Background(tcell.ColorReset).Foreground(tcell.ColorReset)
  42. game := Game{
  43. screen: screen,
  44. style: style,
  45. keypresses: make(chan int),
  46. player: Player{
  47. position: Position{
  48. x: 40,
  49. y: 12,
  50. },
  51. score: 0,
  52. moves: 0,
  53. teleports: 0,
  54. },
  55. level: 1,
  56. // robots: []Robot, // I need this maybe
  57. // trash: []Position, // I will need this
  58. gameover: 0,
  59. }
  60. game.initRobots()
  61. return game, err
  62. }
  63. func (game *Game) gameDirector() {
  64. defer quit(game)
  65. // TODO reorder this
  66. for game.gameover == 0 {
  67. // Clean the screen
  68. game.screen.Clear()
  69. // Draw starter conditions
  70. game.drawBox()
  71. game.drawScore()
  72. game.drawPlayer()
  73. //game.drawCoords()
  74. game.drawDebug()
  75. // Draw robots??
  76. if len(game.robots) == 0 {
  77. game.level++
  78. game.cleanTrash()
  79. game.initRobots()
  80. }
  81. game.drawRobots()
  82. // Draw my ex-wife
  83. game.drawTrash()
  84. // Draw the screen
  85. game.screen.Show()
  86. // Process input
  87. game.movePlayer(<-game.keypresses)
  88. // Move robots
  89. game.moveRobots()
  90. }
  91. }
  92. func (game *Game) keyboardProcessor() {
  93. defer close(game.keypresses)
  94. for game.gameover == 0 {
  95. // Poll for an event
  96. ev := game.screen.PollEvent()
  97. // Fetch the type, and check if any other actions are required.
  98. switch ev := ev.(type) {
  99. case *tcell.EventKey:
  100. // Keys to bug out
  101. if ev.Key() == tcell.KeyEscape || ev.Key() == tcell.KeyCtrlC {
  102. game.gameover = 2
  103. return
  104. // Screen management
  105. } else if ev.Key() == tcell.KeyCtrlL {
  106. game.screen.Sync()
  107. // Player Movement
  108. } else if ev.Rune() == 'q' {
  109. game.keypresses <- upleft
  110. } else if ev.Rune() == 'e' {
  111. game.keypresses <- upright
  112. } else if ev.Rune() == 'z' {
  113. game.keypresses <- downleft
  114. } else if ev.Rune() == 'c' {
  115. game.keypresses <- downright
  116. } else if ev.Key() == tcell.KeyUp || ev.Rune() == 'w' {
  117. game.keypresses <- Up
  118. } else if ev.Key() == tcell.KeyRight || ev.Rune() == 'd' {
  119. game.keypresses <- Right
  120. } else if ev.Key() == tcell.KeyDown || ev.Rune() == 's' || ev.Rune() == 'x' {
  121. game.keypresses <- Down
  122. } else if ev.Key() == tcell.KeyLeft || ev.Rune() == 'a' {
  123. game.keypresses <- Left
  124. // Teleport the player
  125. } else if ev.Rune() == 't' {
  126. game.keypresses <- teleport
  127. }
  128. }
  129. }
  130. }
  131. func quit(game *Game) {
  132. game.screen.Clear()
  133. maybePanic := recover()
  134. game.screen.Fini()
  135. if maybePanic != nil {
  136. panic(maybePanic)
  137. }
  138. }
  139. func (game *Game) randPos() Position {
  140. var pos Position
  141. x := 1 + rand.Intn(77)
  142. y := 1 + rand.Intn(22)
  143. pos.x = x
  144. pos.y = y
  145. return pos
  146. }