snake.odin 4.0 KB

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