Skip to content

Commit

Permalink
chore: cleanup snakegame
Browse files Browse the repository at this point in the history
  • Loading branch information
opZywl committed Jan 12, 2025
1 parent 17c3e8f commit 872074c
Showing 1 changed file with 38 additions and 22 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -20,16 +20,17 @@ import net.minecraft.client.gui.ScaledResolution
import net.minecraft.client.renderer.GlStateManager.*
import org.lwjgl.opengl.GL11.*
import java.awt.Color
import javax.vecmath.Point2i

object SnakeGame : Module("SnakeGame", Category.CLIENT, gameDetecting = false, hideModule = false) {

private var snake = mutableListOf(Pos(0, 0))
private var snake = mutableListOf(Point2i(0, 0))
private var lastKey = 208
private var food = Pos(0, 0)
private var food = Point2i(0, 0)
private var score = 0
private var highScore = 0
private val Mode by choices("Mode", arrayOf("Easy", "Normal", "Hard"), "Easy")
private var obstacles = mutableListOf(Pos(0, 0))
private var obstacles = mutableListOf(Point2i(0, 0))

private const val BLOCK_SIZE = 10
private const val FIELD_WIDTH = 200
Expand Down Expand Up @@ -57,8 +58,10 @@ object SnakeGame : Module("SnakeGame", Category.CLIENT, gameDetecting = false, h
if (k == 1) {
toggle()
}
if ((k == 205 && lastKey != 203) || (k == 203 && lastKey != 205)
|| (k == 200 && lastKey != 208) || (k == 208 && lastKey != 200)
if ((k == 205 && lastKey != 203) ||
(k == 203 && lastKey != 205) ||
(k == 200 && lastKey != 208) ||
(k == 208 && lastKey != 200)
) {
lastKey = k
}
Expand All @@ -82,18 +85,21 @@ object SnakeGame : Module("SnakeGame", Category.CLIENT, gameDetecting = false, h
}
}
moveFood()
snake.add(Pos(snake[0].x, snake[0].y))
snake.add(Point2i(snake[0].x, snake[0].y))
}

for (i in snake.size - 1 downTo 1) {
snake[i].x = snake[i - 1].x
snake[i].y = snake[i - 1].y
}

when (lastKey) {
205 -> snake[0].x++
203 -> snake[0].x--
200 -> snake[0].y--
208 -> snake[0].y++
}

if (Mode == "Hard") {
for (obs in obstacles) {
if (snake[0].x == obs.x && snake[0].y == obs.y) {
Expand All @@ -103,6 +109,7 @@ object SnakeGame : Module("SnakeGame", Category.CLIENT, gameDetecting = false, h
}
}
}

for (i in 1 until snake.size) {
if (snake[i].x == snake[0].x && snake[i].y == snake[0].y) {
checkHighScore()
Expand All @@ -119,6 +126,7 @@ object SnakeGame : Module("SnakeGame", Category.CLIENT, gameDetecting = false, h
val h = sr.scaledHeight
val sx = (w / 2 - FIELD_WIDTH / 2).toDouble()
val sy = (h / 2 - FIELD_HEIGHT / 2).toDouble()

for (i in 0 until 18) {
drawGradientRect(
sx.toInt() - i + 4,
Expand All @@ -137,34 +145,41 @@ object SnakeGame : Module("SnakeGame", Category.CLIENT, gameDetecting = false, h
Color(6, 70, 255, 120).rgb
)
}

drawRect(sx, sy, sx + FIELD_WIDTH, sy + FIELD_HEIGHT, Color(30, 0, 0, 0).rgb)

val fx = food.x * BLOCK_SIZE + sx
val fy = food.y * BLOCK_SIZE + sy
val cFood = ColorUtils.fade(Color(255, 15, 15), 1, 3)
drawRect(fx, fy, fx + BLOCK_SIZE, fy + BLOCK_SIZE, cFood.rgb)
if (Mode == "Hard" || Mode == "Normal" || Mode == "Easy") {

if (Mode in listOf("Hard", "Normal", "Easy")) {
for (obs in obstacles) {
val ox = obs.x * BLOCK_SIZE + sx
val oy = obs.y * BLOCK_SIZE + sy
val cObs = ColorUtils.fade(Color(255, 255, 0), 1, 3)
drawRect(ox, oy, ox + BLOCK_SIZE, oy + BLOCK_SIZE, cObs.rgb)
}
}

for (i in snake.indices) {
val xx = snake[i].x * BLOCK_SIZE + sx
val yy = snake[i].y * BLOCK_SIZE + sy
val cc = ColorUtils.fade(Color(255, 253, 255), i, snake.size)
drawRect(xx, yy, xx + BLOCK_SIZE, yy + BLOCK_SIZE, cc.rgb)
}
if (snake[0].x * BLOCK_SIZE + sx >= sx + FIELD_WIDTH
|| snake[0].x * BLOCK_SIZE + sx < sx
|| snake[0].y * BLOCK_SIZE + sy < sy
|| snake[0].y * BLOCK_SIZE + sy >= sy + FIELD_HEIGHT

if (snake[0].x * BLOCK_SIZE + sx >= sx + FIELD_WIDTH ||
snake[0].x * BLOCK_SIZE + sx < sx ||
snake[0].y * BLOCK_SIZE + sy < sy ||
snake[0].y * BLOCK_SIZE + sy >= sy + FIELD_HEIGHT
) {
checkHighScore()
setupGame()
}

font35.drawStringWithShadow("Score: §a$score", sx.toFloat(), (sy - 14.0).toFloat(), Color(220, 220, 220).rgb)

val hsTxt = "High Score: §a$highScore"
val hsW = font35.getStringWidth(hsTxt)
val hsH = font35.FONT_HEIGHT
Expand All @@ -175,22 +190,25 @@ object SnakeGame : Module("SnakeGame", Category.CLIENT, gameDetecting = false, h
drawGradientRect(hsX1, hsY1, hsX2, hsY2, Color(0, 0, 0, 120).rgb, Color(0, 0, 0, 120).rgb, 0f)
drawBorder(hsX1.toDouble(), hsY1.toDouble(), hsX2.toDouble(), hsY2.toDouble(), Color(6, 70, 255, 120).rgb)
font35.drawStringWithShadow(hsTxt, (hsX1 + 3).toFloat(), (hsY1 + 2).toFloat(), Color(220, 220, 220).rgb)
font35.drawStringWithShadow("Mode: $Mode", (sx + FIELD_WIDTH - 50).toFloat(), (sy - 14.0).toFloat(), Color(220, 220, 220).rgb)

font35.drawStringWithShadow(
"Mode: $Mode",
(sx + FIELD_WIDTH - 50).toFloat(),
(sy - 14.0).toFloat(),
Color(220, 220, 220).rgb
)
}

private fun setupGame() {
snake = mutableListOf(Pos(0, 0))
snake = mutableListOf(Point2i(0, 0))
moveFood()
lastKey = 208
score = 0
when (Mode) {
"Hard" -> {
generateObstacles(7)
}
"Normal" -> {
obstacles.clear()
}
else -> {
"Normal", "Easy" -> {
obstacles.clear()
}
}
Expand All @@ -206,7 +224,7 @@ object SnakeGame : Module("SnakeGame", Category.CLIENT, gameDetecting = false, h
snake.any { it.x == px && it.y == py } ||
obstacles.any { it.x == px && it.y == py }
)
food = Pos(px, py)
food = Point2i(px, py)
}

private fun generateObstacles(count: Int) {
Expand All @@ -227,7 +245,7 @@ object SnakeGame : Module("SnakeGame", Category.CLIENT, gameDetecting = false, h
obstacles.any { it.x == ox && it.y == oy } ||
snake.any { it.x == ox && it.y == oy }
)
obstacles.add(Pos(ox, oy))
obstacles.add(Point2i(ox, oy))
}

private fun checkHighScore() {
Expand Down Expand Up @@ -284,6 +302,4 @@ object SnakeGame : Module("SnakeGame", Category.CLIENT, gameDetecting = false, h
glDisable(GL_LINE_SMOOTH)
color(1f, 1f, 1f, 1f)
}

data class Pos(var x: Int, var y: Int)
}
}

0 comments on commit 872074c

Please sign in to comment.