game.go 3.5 KB

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