commit d1245b50d71a89e5ca67f7ffa996c37638dec1cb Author: lq <13849061902@qq.com> Date: Tue May 13 17:52:48 2025 +0800 add code diff --git a/boom.png b/boom.png new file mode 100644 index 0000000..c89a5a6 Binary files /dev/null and b/boom.png differ diff --git a/bullet.png b/bullet.png new file mode 100644 index 0000000..b80f568 Binary files /dev/null and b/bullet.png differ diff --git a/core.lua b/core.lua new file mode 100644 index 0000000..c6abca6 --- /dev/null +++ b/core.lua @@ -0,0 +1,167 @@ +core = {} +core.block = {} +core.score = 0 +core.best = 0 +love.filesystem.setIdentity('2048') + +local function get_best() + if not love.filesystem.exists('best') then + core.best = 0 + return + end + core.best = love.filesystem.read('best') + core.best = tonumber(core.best) +end + +function core.initial() + core.block = {} + local pos1 = love.math.random(1, 16) + local pos2 + while true do + pos2 = love.math.random(1, 16) + if pos2 ~= pos1 then break end + end + + local val + val = love.math.random() + if val < 0.8 then val = 2 else val = 4 end + core.block[pos2] = val + core.score = 0 +end + +function core.set_best() + if core.score > core.best then + core.best = core.score + local ret, err = love.filesystem.write('best', core.best) + end +end + +function core.tblclone(t1, num) + local t2 = {} + for i = 1, num do + t2[i] = t1[i] + end + return t2 +end + +function core.isfull(testtbl) + local block + if testtbl then block = testtbl else block = core.block end + + for i = 1, 16 do + if not block[i] then return false end + end + return true +end + +local function combine(lstart, lend, lstep, rstart, rend, rstep, flag, testtbl) + local index + local tflag, block + if testtbl then + tflag = true + block = testtbl + else + block = core.block + end + local cflag = false + for i = lstart, lend, lstep do + for j = rstart, rend, rstep do + if flag == 'up' then index = (i - 1) * 4 + j + elseif flag == 'down' then index = (i + 1) * 4 + j + elseif flag == 'left' then index = i * 4 + j - 1 + else index = i * 4 + j + 1 end + + if block[index] and block[i * 4 + j] and block[index] == block[i * 4 + j] and block[index] < 2048 then + cflag = true + if tflag then return cflag end + block[index] = 2 * block[i * 4 + j] + block[i* 4 + j] = nil + core.score = core.score + block[index] + end + end + end + return cflag +end + +local function move(lstart, lned, lstep, rstart, rend, rstep, flag) + local mflag = false + local index, kstart, kend, kstep + for i = lstart, lend, lstep do + for j = rstart, rend, rstep do + if flag == 'up' then + kstart = 0 + kend = i - 1 + kstep = 1 + elseif flag == 'down' then + kstart = 3 + kend = i + 1 + kstep = -1 + elseif flag == 'left' then + kstart = 1 + kend = j - 1 + kstep = 1 + else + kstart = 4 + kend = j + 1 + kstep = 1 + end + + for k = kstart, kend, kstep do + if flag == 'up' or flag == 'down' then index = k *4 +j + else index = i * 4 + kend + if not core.block[index] and core.block[i * 4 + j] then + core.block[index] = core.block[i * 4 + j] + core.block[i * 4 + j] = nil + mflag = true + break + end + end + end + end + return mflag +end +end + +local function do_tsk(lstart, lend, lstep, rstart, rend, rstep, flag, testtbl) + if testtbl then return combine(lstart, lend, lstep, rstart, rend, rstep, flag, testtbl) end + local mret = move(lstart, lend, lstep, rstart, rend, rstep, flag) + local cret = combine(lstart, lend, lstep, rstart, rend, rstep, flag) + + if not mret and not cret then return false end + move(lstart, lend, lstep, rstart, rend, rstep, flag) + return true +end + +function core.up_move(testtbl) + return do_tsk(1,3,1,1,4,1, 'up', testtbl) +end +function core.down_move(testtbl) + return do_tsk(2, 0, -1, 1, 4, 1,"down", testtbl) +end +function core.left_move(testtbl) + return do_tsk(0, 3, 1, 2, 4, 1, "left", testtbl) +end +function core.right_move(testtbl) + return do_tsk(0, 3, 1, 3, 1, -1, "right", testtbl) +end + +function core.new_block() + local val = love.math.random() + if val < 0.8 then val = 2 else val = 4 end + local empty_tbl = {} + for i = 1, 16 do + if not core.block[i] then + table.insert(empty_tbl, i) + end + end + if #empty_tbl == 1 then + return {index = empty_tbl[1], value = val} + end + local pos = love.math.random(1, #empty_tbl) + return {index = empty_tbl[pos], value = val} +end +get_best() +return core + + + diff --git a/custom.lua b/custom.lua new file mode 100644 index 0000000..2932710 --- /dev/null +++ b/custom.lua @@ -0,0 +1,11 @@ +Mod = { + sum = function(x, y) + return x + y + end +} + +function Mod.sayHello(name) + print("hello, " .. name) +end + +return Mod diff --git a/enemy.png b/enemy.png new file mode 100644 index 0000000..70a836b Binary files /dev/null and b/enemy.png differ diff --git a/enemyBullet.png b/enemyBullet.png new file mode 100644 index 0000000..9244a93 Binary files /dev/null and b/enemyBullet.png differ diff --git a/image.png b/image.png new file mode 100644 index 0000000..e41e2b8 Binary files /dev/null and b/image.png differ diff --git a/main.lua b/main.lua new file mode 100644 index 0000000..c710387 --- /dev/null +++ b/main.lua @@ -0,0 +1,318 @@ +-- main.lua (LÖVE框架的主文件) + +-- 初始化函数,只会在游戏开始时调用一次 +function love.load() + -- 初始化游戏状态和资源 + love.keyboard.setKeyRepeat(true) + windowsWith = 800 + windowsHeight = 600 + circleX = 400 + circleY = 550 + plane = { + x = 400, -- 初始X位置 + y = 500, -- 初始Y位置 + speed = 200, -- 移动速度(像素/秒) + angle = 0, -- 旋转角度 + img = nil -- 飞机图像 + } + plane.img = love.graphics.newImage("plan.png") + --plane['x'] = 400 + --plane['y'] = 500 + planeWidth = 10 + enemyWidth = 20 + bulletWidth = 10 + bulletHeight = 20 + --image = love.graphics.newImage("image.png") + local f = love.graphics.newFont(12) + love.graphics.setFont(f) + love.graphics.setColor(0,0,0,255) + love.graphics.setBackgroundColor(255,255,255) + clkX = 0 + clkY = 0 + stopX = false + stopY = false + bullet = {} + bulletImg = love.graphics.newImage("bullet.png") + text = '' + enemy = {} + enemy['x'] = 400 + enemy['y'] = -40 + enemy['hv'] = 100 + enemy['angle'] = 0 + enemyImg = love.graphics.newImage("enemy.png") + enemyWidth = 20 + currentTime = os.time() + enemyBullet = {} + enemyBullet['x'] = -40 + enemyBullet['y'] = -40 + enemyBulletImg = love.graphics.newImage("enemyBullet.png") + shootTime = os.time() + flag = 0 + hurtNumber = 30 + speed = 100 + boom = { + x = -40, + y = -40, + angle = 0, + time = 0, + img = nil + } + boom.img = love.graphics.newImage("boom.png") + boomTime = 0 + focus = true +end + +-- 更新函数,每一帧都会调用 +function love.update(dt) + if not focus then return end + love.graphics.print(plane['x'],plane['y']) + -- 更新游戏状态 + --circleX = circleX + (100 * dt) + if plane['x'] > windowsWith then + plane['x'] = 0 + elseif plane['x'] < 0 then + plane['x'] = windowsWith + end + + if plane['y'] > windowsHeight then + plane['y'] = 0 + elseif plane['y'] < 0 then + plane['y'] = windowsHeight + end + if love.keyboard.isDown('left') or love.keyboard.isDown('a') then + plane['x'] = plane['x'] - speed * dt + elseif love.keyboard.isDown('right') or love.keyboard.isDown('d') then + plane['x'] = plane['x'] + speed * dt + elseif love.keyboard.isDown('up') or love.keyboard.isDown('w') then + plane['y'] = plane['y'] - speed * dt + elseif love.keyboard.isDown('down') or love.keyboard.isDown('s') then + plane['y'] = plane['y'] + speed * dt + end + if clkX > 0 and math.abs(clkX - plane['x']) > 200 * dt then + if clkX > plane['x'] then + plane['x'] = plane['x'] + speed * dt + else + plane['x'] = plane['x'] - speed * dt + end + else + stopX = true + end + + + if clkY > 0 and math.abs(clkY - plane['y']) > 200 * dt then + if clkY > plane['y'] then + plane['y'] = plane['y'] + speed * dt + else + plane['y'] = plane['y'] - speed * dt + end + else + stopY = true + end + + if stopX and stopY then + clkX = -10 + clkY = -10 + end + time = os.time() + if enemy and time - currentTime > 3 then + enemy['y'] = enemy['y'] + speed * dt + end + if boom and boom.y > 0 then + boom.time = boom.time + 1 + if boom.time > 10 then + boom.x = -40 + boom.y = -40 + boom.time = 0 + end + end + addEnemyBullet() + updateBullet(dt) + collision() +end + +-- 绘制函数,每一帧都会调用 +function love.draw() + -- 绘制游戏画面 + --love.graphics.circle("fill", plane['x'], plane['y'], planeWidth, planeWidth) + love.graphics.draw( + plane.img, + plane.x, plane.y, -- 位置 + plane.angle,-- + math.pi/2, -- 角度(加π/2因为我们的三角形朝上) + 0.2, 0.2, -- 缩放 + plane.img:getWidth()/2, -- 原点X(居中) + plane.img:getHeight()/2 -- 原点Y(居中) + ) + if boom.x > 0 and os.time() > boomTime then + boom.x = -40 + boom.y = -40 + end + for i = 1, #bullet do + if bullet[i] then + if bullet[i]['y'] < 0 then + table.remove(bullet, i) + else + --love.graphics.rectangle("fill",bullet[i]['x'], bullet[i]['y'], bulletWidth,bulletHeight, 15) + love.graphics.draw( + bulletImg, + bullet[i].x, bullet[i].y, -- 位置 + 0, -- 角度(加π/2因为我们的三角形朝上) + 1, 1, -- 缩放 + bulletImg:getWidth()/2, -- 原点X(居中) + bulletImg:getHeight()/2 -- 原点Y(居中) + ) + end + end + end + if enemy then + if enemy['y'] > windowsHeight + 2 * enemyWidth or enemy['hv'] < 0 then + initEnemy() + else + --love.graphics.circle("fill", enemy['x'], enemy['y'], enemyWidth, enemyWidth) + love.graphics.draw( + enemyImg, + enemy.x, enemy.y, -- 位置 + enemy.angle + math.pi/2, -- 角度(加π/2因为我们的三角形朝上) + 1, 1, -- 缩放 + enemyImg:getWidth()/2, -- 原点X(居中) + enemyImg:getHeight()/2 -- 原点Y(居中) + ) + end + end + if enemyBullet then + if enemyBullet['y'] < windowsHeight + bulletHeight and enemyBullet['y'] > 0 then + love.graphics.draw( + enemyBulletImg, + enemyBullet.x, enemyBullet.y, -- 位置 + 0, -- 角度(加π/2因为我们的三角形朝上) + 1, 1, -- 缩放 + enemyBulletImg:getWidth()/2, -- 原点X(居中) + enemyBulletImg:getHeight()/2 -- 原点Y(居中) + ) + end + end + if boom and boom.y > 0 then + + love.graphics.draw( + boom.img, + boom.x, boom.y, -- 位置 + 0, -- 角度(加π/2因为我们的三角形朝上) + 1, 1, -- 缩放 + boom.img:getWidth()/2, -- 原点X(居中) + boom.img:getHeight()/2 -- 原点Y(居中) + ) + end + love.graphics.setColor(255,0,0) + love.graphics.print(text, 10, 20) +end + +function initEnemy() + enemy['y'] = -40 + enemy['hv'] = 100 + currentTime = os.time() + math.randomseed(os.time()) + enemy['x'] = math.random(enemyWidth / 2, windowsWith - enemyWidth / 2) +end + +-- 键盘按下事件处理函数 +function love.keypressed(key, scancode) + -- 处理键盘输入 + if key == 'space' then + bt = {} + bt['x'] = plane['x'] --- bulletWidth / 2 + bt['y'] = plane['y'] + table.insert(bullet, bt) + end +end + +function collision() + --子弹与敌方子弹碰撞消失, 子弹与地基碰撞消失, 血条减少 + for i = 1, #bullet do + if bullet[i] then + if bullet[i] and bullet[i]['x'] + bulletWidth > enemy['x'] - enemyWidth and bullet[i]['x'] < enemy['x'] + enemyWidth and enemy['y'] + enemyWidth >= bullet[i]['y'] then + enemy['hv'] = enemy['hv'] - 30 + boom.x = bullet[i].x + boom.y = bullet[i].y + planeWidth + boomTime = os.time() + table.remove(bullet,i) + if enemy['hv'] < 0 then + --enemy['y'] = -40 + --enemy.hv = 100 + initEnemy() + end + end + if bullet[i] and bullet[i]['x'] + bulletWidth > enemyBullet['x'] and bullet[i]['x'] < enemyBullet['x'] + bulletWidth and bullet[i]['y'] < enemyBullet['y'] + bulletHeight then + boom.x = bullet[i].x + boom.y = bullet[i].y + planeWidth + boomTime = os.time() + table.remove(bullet,i) + enemyBullet['y'] = -40 + shootTime = os.time() + end + end + end + --我方战机与敌机碰撞, 地基消失 + if math.abs(plane['x'] - enemy['x']) < planeWidth + enemyWidth and plane['y'] - planeWidth < enemy['y'] + enemyWidth then + boom.x = enemy.x + boom.y = enemy.y + planeWidth + boomTime = os.time() + initEnemy() + end + --敌方子弹碰撞我方战机, 敌方子弹消失 + if enemyBullet then + if ((enemyBullet['x'] >= plane.x and enemyBullet['x'] - plane['x'] < planeWidth) or (enemyBullet['x'] < plane['x'] and plane['x'] - enemyBullet['x'] < enemyWidth)) and enemyBullet['y'] + enemyWidth > plane['y'] - 10 then + shootTime = os.time() + boom.x = enemyBullet.x + boom.y = enemyBullet.y + planeWidth + boomTime = os.time() + enemyBullet['y'] = -40 + end + end +end + +function addEnemyBullet() + if enemy and enemy['y'] > 2 * enemyWidth and os.time() - shootTime > 1 and enemyBullet['y'] < 0 then + enemyBullet['x'] = enemy['x'] -- bulletWidth / 2 + enemyBullet['y'] = enemy['y'] + enemyWidth + --text = 'gege' .. enemyBullet['x'] .. ' ' .. enemy['x'] + end +end + +function love.keyreleased(key, scancode) + -- 处理键盘释放事件 +end + +function love.mousepressed(x, y, button, istouch) + if button == 1 then + stopX = false + stopY = false + clkX = x + clkY = y + end +end + +function love.focus(f) + if not f then + text = "LOST FOCUS" + focus = false + else + text ="GAINED FOCUS" + focus = true + end +end + +function love.quit() + print("Thanks for playing! Come back soon!") +end + +function updateBullet(dt) + for i = 1, #bullet do + bullet[i]['y'] = bullet[i]['y'] - speed * dt * 3 + end + if enemyBullet and enemyBullet['y'] > 0 then + enemyBullet['y'] = enemyBullet['y'] + speed * dt * 3 + if enemyBullet['y'] > windowsHeight + 2 * enemyWidth then + enemyBullet['y'] = -2 * enemyWidth + shootTime = os.time() + end + end +end \ No newline at end of file diff --git a/main11.lua b/main11.lua new file mode 100644 index 0000000..7a61b93 --- /dev/null +++ b/main11.lua @@ -0,0 +1,87 @@ +print(math.abs(-15)) + +print('hello world!') + +print('hello', 'world') + +print('hello' .. 'world') + +local a = 4 +local b = 3.12 +local c = 'hello' +local d = [[abcdefg]] + +print(a, b, c, d) +local one, two, three = 'one', 2, false + +print(one, two, three) + +GlobalVar = 23 +print(_G['GlobalVar']) + +local name = 'Qinga' +print('Hello, my name is ' .. name) +name = 'Sad' +print('this is a cool name, ' .. name) + +a = 'ddddddd' +b = 'ccccccc' +c = [[ + abdsde + adfadfd + fgfgfgfg +]] + print(a) + print(b) + print(c) + +local x = 'adfdfdfdf' +print(#x) +local num = 22 +local str = tostring(num) +print(type(num), type(str)) + +print('what is your name') +--local ans = io.read() +--print('Name:', ans) +local tbl = {'this', 2, 93, true, {'ok', 'cool'}} +for i = 1, #tbl do + print(tbl[i]) +end + +--io.output('myfile.txt') +--io.input('myfile.txt') +--local fileData = io.read('*all') +--print(fileData) +--io.close() + +--local file = io.open('myfile.txt', 'w') + +--if file ~= nil then +-- file:write('aaaaaaaa, bit') +-- file:close() +--else +-- print('can not open the file') +--end + +--local file = io.open('myfile.txt', 'r') +----if file ~= nil then +-- print(file:read('*all')) +-- file:close() +--else +-- print('can not open') +--end +local file = io.open('myfile.txt', 'a') +if file ~= nil then + print(file:write('\nAppended text')) + file:close() +else + print('can not ') +end + + + +local mod = require('custom') + +print(mod.sum(10, 4)) +mod.sayHello('junge') diff --git a/main2.lua b/main2.lua new file mode 100644 index 0000000..06c2de4 --- /dev/null +++ b/main2.lua @@ -0,0 +1,97 @@ +local core = require("core") +local block_pic = {} +local bk +local over_flag = false +local new_block = {flag = false} +local wH --window height +local wW --window weight +local bW --block width +local startpos = {} +local delay = 0 +function love.load() + love.window.setFullscreen() + wH = love.window.getHeight() + wW = love.window.getWidth() + bW = 0.8 * wH / 4 + bk = love.graphics.newImage("src/bk.jpg") + for i = 1, 11 do + block_pic[tostring(math.pow(2,i))] = love.graphics.newImage("src/"..tostring(math.pow(2,i))..".PNG") + end + love.graphics.setBackgroundColor(255, 255, 255) + love.graphics.setNewFont(24) + love.graphics.setColor(255, 255, 255) + core.initial() +end +local function draw_block(index, value) + local line = math.modf((index - 1)/4) + local row = (index - 1) % 4 + local pic_index = tostring(value) + love.graphics.draw(block_pic[pic_index], 0.1 * wH + row * bW, 0.1 * wH + line * bW, 0, bW/block_pic[pic_index]:getWidth(), bW/block_pic[pic_index]:getHeight()) +end +function love.draw() + local scorestr = "SCORE:\n"..core.score.."\nBEST:\n"..core.best + love.graphics.draw(bk, 0, 0, 0, wW/bk:getWidth(), wH/bk:getHeight()) + love.graphics.setColor(255, 255, 255) + love.graphics.rectangle("line", 0.1 * wH, 0.1 * wH, 0.8 * wH, 0.8 * wH) + for i = 1, 16 do + if core.block[i] then + draw_block(i, core.block[i]) + end + end + if new_block.flag then + if delay < 10 then delay = delay + 1 + else + draw_block(new_block.index, new_block.value) + core.block[new_block.index] = new_block.value + new_block.flag = false + delay = 0 + end + end + love.graphics.print(scorestr, wH, wH * 0.1) + if over_flag then + love.graphics.setColor(0, 0, 255) + love.graphics.rectangle("fill", 0.25 * wW, 0.25 * wH, 0.5 * wW, 0.5 * wH) + love.graphics.setColor(255,255,255) + love.graphics.print(scorestr, 0.45 * wW, 0.45 * wH) + end +end +function love.mousepressed(x, y, button) + if button == 'l' then + startpos.x = x + startpos.y = y + end +end +function love.mousereleased(x, y, button) + if button == 'l' then + if over_flag then + over_flag = false + core.initial() + return + end + local x_dis = x - startpos.x + local y_dis = y - startpos.y + local ret + if y_dis < 0 and math.abs(y_dis) > math.abs(x_dis) then + ret = core.up_move() + elseif y_dis > 0 and math.abs(y_dis) > math.abs(x_dis) then + ret = core.down_move() + elseif x_dis < 0 and math.abs(x_dis) > math.abs(y_dis) then + ret = core.left_move() + elseif x_dis > 0 and math.abs(x_dis) > math.abs(y_dis) then + ret = core.right_move() + end + if not ret then return end + new_block = core.new_block() + if not new_block then return end + new_block.flag = true + local testtbl = core.tblclone(core.block, 16) + testtbl[new_block.index] = new_block.value + if core.isfull(testtbl) then + if core.up_move(testtbl) or core.down_move(testtbl) or core.left_move(testtbl) or core.right_move(testtbl) then + return + end + core.set_best() + over_flag = true + end + end +end \ No newline at end of file diff --git a/myfile.txt b/myfile.txt new file mode 100644 index 0000000..38a9eb4 --- /dev/null +++ b/myfile.txt @@ -0,0 +1,7 @@ +aaaaaaaa, bit +dasfeasdf +asdsdfgf +222 +Appended text +Appended text +Appended text \ No newline at end of file diff --git a/plan.png b/plan.png new file mode 100644 index 0000000..2a0e02d Binary files /dev/null and b/plan.png differ