|
| 1 | +UsingMoudle("All") |
| 2 | + |
| 3 | +x_1 = 420 |
| 4 | +y_1 = 270 |
| 5 | +game = 1 --判断是否游戏结束 |
| 6 | +point = 0 |
| 7 | +I = 3 |
| 8 | +times = 1 |
| 9 | +x = 30 --x为蛇的左右方向 |
| 10 | +y = 0 --y为蛇的上下方向 |
| 11 | + |
| 12 | +snake = {} --初始身长 |
| 13 | +snake[1] = {} |
| 14 | +snake[2] = {} |
| 15 | +snake[3] = {} |
| 16 | +snake[1].x = x_1 |
| 17 | +snake[1].y = y_1 |
| 18 | +snake[2].x = 390 |
| 19 | +snake[2].y = 270 |
| 20 | +snake[3].x = 360 |
| 21 | +snake[3].y = 270 |
| 22 | + |
| 23 | +color = {} |
| 24 | +color.r = 0 |
| 25 | +color.g = 0 |
| 26 | +color.b = 0 |
| 27 | +color.a = 255 |
| 28 | + |
| 29 | + |
| 30 | +color_2 = {} |
| 31 | +color_2.r = 255 |
| 32 | +color_2.g = 255 |
| 33 | +color_2.b = 255 |
| 34 | +color_2.a = 255 |
| 35 | + |
| 36 | +CreateWindow("贪吃蛇",{x = WINDOW_POSITION_DEFAULT,y = WINDOW_POSITION_DEFAULT, w = 900, h = 600},{ }) --创建窗口 |
| 37 | + |
| 38 | +image_background = LoadImage("./background.png") |
| 39 | +textrue_background = CreateTexture(image_background) |
| 40 | + |
| 41 | +image_food = LoadImage("./food_2.png") |
| 42 | +textrue_food = CreateTexture(image_food) |
| 43 | + |
| 44 | +math.randomseed(os.time()) --获取食物位置 |
| 45 | +timenow = os.time() |
| 46 | +food_x = math.random(1,29) |
| 47 | +food_y = math.random(1,19) |
| 48 | + |
| 49 | +while true do |
| 50 | + |
| 51 | + UpdateWindow() --将缓存区纹理冲刷到屏幕 |
| 52 | + |
| 53 | + while times == 2000 and game == 1 do |
| 54 | + |
| 55 | + CopyTexture(textrue_background,{x = 0,y = 0,w = 900,h = 600}) |
| 56 | + CopyTexture(textrue_food,{x = 30 * food_x -20,y = 30 * food_y - 20,w = 45,h = 45}) |
| 57 | + |
| 58 | + if UpdateEvent() then --获取玩家操作 |
| 59 | + local _event = GetEventType() |
| 60 | + |
| 61 | + if _event == EVENT_KEYDOWN_UP then --判断蛇的行走 |
| 62 | + x = 0 |
| 63 | + y = - 30 |
| 64 | + elseif _event == EVENT_KEYDOWN_DOWN then |
| 65 | + x = 0 |
| 66 | + y = 30 |
| 67 | + elseif _event == EVENT_KEYDOWN_LEFT then |
| 68 | + y = 0 |
| 69 | + x = -30 |
| 70 | + elseif _event == EVENT_KEYDOWN_RIGHT then |
| 71 | + y = 0 |
| 72 | + x = 30 |
| 73 | + end |
| 74 | + |
| 75 | + end |
| 76 | + |
| 77 | + for i = I, 1, -1 do --贪吃蛇前进(将体节坐标变为前一体节坐标) |
| 78 | + if i == 1 then |
| 79 | + break |
| 80 | + end |
| 81 | + snake[i].x = snake[i-1].x |
| 82 | + snake[i].y = snake[i-1].y |
| 83 | + end |
| 84 | + |
| 85 | + x_1 = x_1 + x |
| 86 | + y_1 = y_1 + y |
| 87 | + |
| 88 | + snake[1] = {} --变更第一体节坐标 |
| 89 | + snake[1].x = x_1 |
| 90 | + snake[1].y = y_1 |
| 91 | + |
| 92 | + |
| 93 | + if (snake[1].x == 30 * food_x and snake[1].y == 30 * food_y) then --是否吃到食物 |
| 94 | + I = I + 1 |
| 95 | + snake[I] = {} |
| 96 | + snake[I].x = snake[I-1].x - x |
| 97 | + snake[I].y = snake[I-1].y - y |
| 98 | + table.insert(snake,snake[I]) |
| 99 | + math.randomseed(os.time()) --获取食物位置 |
| 100 | + timenow = os.time() |
| 101 | + food_x = math.random(1,29) |
| 102 | + food_y = math.random(1,19) |
| 103 | + point = point + 1 |
| 104 | + end |
| 105 | + |
| 106 | + for i = 2, I do --判断是否吃到自身 |
| 107 | + if snake[1].x == snake[i].x then |
| 108 | + if snake[1].y == snake[i].y then |
| 109 | + game = 0 |
| 110 | + break |
| 111 | + end |
| 112 | + end |
| 113 | + end |
| 114 | + |
| 115 | + SetDrawColor(color) |
| 116 | + |
| 117 | + for i = 1, I do --将蛇身坐标具象化 |
| 118 | + FillCircle(snake[i], 15) |
| 119 | + end |
| 120 | + |
| 121 | + if (IfPointInRectStrict(snake[1],{x = 0,y = 0,w = 900,h = 600}) ~= true ) then --判断是否撞墙 |
| 122 | + game = 0 |
| 123 | + end |
| 124 | + |
| 125 | + if game == 0 then --判断是否死亡 |
| 126 | + break |
| 127 | + end |
| 128 | + |
| 129 | + times = 1 |
| 130 | + |
| 131 | + font = LoadFont("./font.ttf",60) |
| 132 | + image_font = CreateUTF8TextImageBlended(font,"你的得分为:" .. point,color_2) |
| 133 | + texture_font = CreateTexture(image_font) |
| 134 | + |
| 135 | +end |
| 136 | + |
| 137 | +times = times + 1 |
| 138 | + |
| 139 | +if game == 0 then |
| 140 | + CopyTexture(texture_font, {x = 300, y = 250, w = 300, h = 100}) |
| 141 | + if UpdateEvent() then |
| 142 | + local _event = GetEventType() |
| 143 | + if _event == EVENT_QUIT then |
| 144 | + break |
| 145 | + end |
| 146 | + end |
| 147 | +end |
| 148 | + |
| 149 | +end |
| 150 | + |
0 commit comments