snake.odin 5.3 KB

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