plan_war/main.lua

318 lines
10 KiB
Lua
Raw Normal View History

2025-05-13 17:52:48 +08:00
-- 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