From 872074cda8343467465e37015392102f2a3435a0 Mon Sep 17 00:00:00 2001 From: Zywl <86253343+opZywl@users.noreply.github.com> Date: Sun, 12 Jan 2025 20:41:17 -0300 Subject: [PATCH] chore: cleanup snakegame --- .../module/modules/client/SnakeGame.kt | 60 ++++++++++++------- 1 file changed, 38 insertions(+), 22 deletions(-) diff --git a/src/main/java/net/ccbluex/liquidbounce/features/module/modules/client/SnakeGame.kt b/src/main/java/net/ccbluex/liquidbounce/features/module/modules/client/SnakeGame.kt index 83627a1dcb..8bf9b4480f 100644 --- a/src/main/java/net/ccbluex/liquidbounce/features/module/modules/client/SnakeGame.kt +++ b/src/main/java/net/ccbluex/liquidbounce/features/module/modules/client/SnakeGame.kt @@ -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 @@ -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 } @@ -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) { @@ -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() @@ -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, @@ -137,12 +145,15 @@ 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 @@ -150,21 +161,25 @@ object SnakeGame : Module("SnakeGame", Category.CLIENT, gameDetecting = false, h 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 @@ -175,11 +190,17 @@ 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 @@ -187,10 +208,7 @@ object SnakeGame : Module("SnakeGame", Category.CLIENT, gameDetecting = false, h "Hard" -> { generateObstacles(7) } - "Normal" -> { - obstacles.clear() - } - else -> { + "Normal", "Easy" -> { obstacles.clear() } } @@ -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) { @@ -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() { @@ -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) -} +} \ No newline at end of file