snake.odin 4.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211
  1. package snake
  2. import k2 "../.."
  3. import "core:math"
  4. import "core:fmt"
  5. import "core:time"
  6. import "core:math/rand"
  7. import "base:intrinsics"
  8. import "core:log"
  9. import "core:mem"
  10. WINDOW_SIZE :: 1000
  11. GRID_WIDTH :: 20
  12. CELL_SIZE :: 16
  13. CANVAS_SIZE :: GRID_WIDTH*CELL_SIZE
  14. TICK_RATE :: 0.13
  15. Vec2i :: [2]int
  16. MAX_SNAKE_LENGTH :: GRID_WIDTH*GRID_WIDTH
  17. snake: [MAX_SNAKE_LENGTH]Vec2i
  18. snake_length: int
  19. tick_timer: f32 = TICK_RATE
  20. move_direction: Vec2i
  21. game_over: bool
  22. food_pos: Vec2i
  23. place_food :: proc() {
  24. occupied: [GRID_WIDTH][GRID_WIDTH]bool
  25. for i in 0..<snake_length {
  26. occupied[snake[i].x][snake[i].y] = true
  27. }
  28. free_cells := make([dynamic]Vec2i, context.temp_allocator)
  29. for x in 0..<GRID_WIDTH {
  30. for y in 0..<GRID_WIDTH {
  31. if !occupied[x][y] {
  32. append(&free_cells, Vec2i {x, y})
  33. }
  34. }
  35. }
  36. if len(free_cells) > 0 {
  37. random_cell_index := rand.int31_max(i32(len(free_cells)))
  38. food_pos = free_cells[random_cell_index]
  39. }
  40. }
  41. restart :: proc() {
  42. start_head_pos := Vec2i { GRID_WIDTH / 2, GRID_WIDTH / 2 }
  43. snake[0] = start_head_pos
  44. snake[1] = start_head_pos - {0, 1}
  45. snake[2] = start_head_pos - {0, 2}
  46. snake_length = 3
  47. move_direction = {0, 1}
  48. game_over = false
  49. place_food()
  50. }
  51. main :: proc() {
  52. context.logger = log.create_console_logger()
  53. when ODIN_DEBUG {
  54. track: mem.Tracking_Allocator
  55. mem.tracking_allocator_init(&track, context.allocator)
  56. context.allocator = mem.tracking_allocator(&track)
  57. defer {
  58. if len(track.allocation_map) > 0 {
  59. for _, entry in track.allocation_map {
  60. fmt.eprintf("%v leaked: %v bytes\n", entry.location, entry.size)
  61. }
  62. }
  63. mem.tracking_allocator_destroy(&track)
  64. }
  65. }
  66. k2.init(WINDOW_SIZE, WINDOW_SIZE, "Snake")
  67. prev_time := time.now()
  68. restart()
  69. food_sprite := k2.load_texture_from_file("food.png")
  70. head_sprite := k2.load_texture_from_file("head.png")
  71. body_sprite := k2.load_texture_from_file("body.png")
  72. tail_sprite := k2.load_texture_from_file("tail.png")
  73. for !k2.window_should_close() {
  74. time_now := time.now()
  75. dt := f32(time.duration_seconds(time.diff(prev_time, time_now)))
  76. prev_time = time_now
  77. k2.process_events()
  78. if k2.key_is_held(.Up) {
  79. move_direction = {0, -1}
  80. }
  81. if k2.key_is_held(.Down) {
  82. move_direction = {0, 1}
  83. }
  84. if k2.key_is_held(.Left) {
  85. move_direction = {-1, 0}
  86. }
  87. if k2.key_is_held(.Right) {
  88. move_direction = {1, 0}
  89. }
  90. if game_over {
  91. if k2.key_went_down(.Enter) {
  92. restart()
  93. }
  94. } else {
  95. tick_timer -= dt
  96. }
  97. if tick_timer <= 0 {
  98. next_part_pos := snake[0]
  99. snake[0] += move_direction
  100. head_pos := snake[0]
  101. if head_pos.x < 0 || head_pos.y < 0 || head_pos.x >= GRID_WIDTH || head_pos.y >= GRID_WIDTH {
  102. game_over = true
  103. }
  104. for i in 1..<snake_length {
  105. cur_pos := snake[i]
  106. if cur_pos == head_pos {
  107. game_over = true
  108. }
  109. snake[i] = next_part_pos
  110. next_part_pos = cur_pos
  111. }
  112. if head_pos == food_pos {
  113. snake_length += 1
  114. snake[snake_length - 1] = next_part_pos
  115. place_food()
  116. }
  117. tick_timer = TICK_RATE + tick_timer
  118. }
  119. k2.clear({76, 53, 83, 255})
  120. camera := k2.Camera {
  121. zoom = f32(WINDOW_SIZE) / CANVAS_SIZE,
  122. }
  123. k2.set_camera(camera)
  124. food_sprite.width = CELL_SIZE
  125. food_sprite.height = CELL_SIZE
  126. k2.draw_texture(food_sprite, {f32(food_pos.x), f32(food_pos.y)}*CELL_SIZE)
  127. for i in 0..<snake_length {
  128. part_sprite := body_sprite
  129. dir: Vec2i
  130. if i == 0 {
  131. part_sprite = head_sprite
  132. dir = snake[i] - snake[i + 1]
  133. } else if i == snake_length - 1 {
  134. part_sprite = tail_sprite
  135. dir = snake[i - 1] - snake[i]
  136. } else {
  137. dir = snake[i - 1] - snake[i]
  138. }
  139. rot := math.atan2(f32(dir.y), f32(dir.x)) * math.DEG_PER_RAD
  140. source := k2.Rect {
  141. 0, 0,
  142. f32(part_sprite.width), f32(part_sprite.height),
  143. }
  144. dest := k2.Rect {
  145. f32(snake[i].x)*CELL_SIZE + 0.5*CELL_SIZE,
  146. f32(snake[i].y)*CELL_SIZE + 0.5*CELL_SIZE,
  147. CELL_SIZE,
  148. CELL_SIZE,
  149. }
  150. k2.draw_texture_ex(part_sprite, source, dest, {CELL_SIZE, CELL_SIZE}*0.5, rot)
  151. }
  152. if game_over {
  153. k2.draw_text("Game Over!", {4, 4}, 25, k2.RED)
  154. k2.draw_text("Press Enter to play again", {4, 30}, 15, k2.BLACK)
  155. }
  156. score := snake_length - 3
  157. score_str := fmt.tprintf("Score: %v", score)
  158. k2.draw_text(score_str, {4, CANVAS_SIZE - 14}, 10, k2.GRAY)
  159. k2.present()
  160. free_all(context.temp_allocator)
  161. }
  162. k2.destroy_texture(head_sprite)
  163. k2.destroy_texture(food_sprite)
  164. k2.destroy_texture(body_sprite)
  165. k2.destroy_texture(tail_sprite)
  166. k2.shutdown()
  167. }