博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
love2d角度,方向以及旋转
阅读量:4577 次
发布时间:2019-06-08

本文共 1886 字,大约阅读时间需要 6 分钟。

新建程序入口main.lua

function love.load()  tank = {      x = 400,      y = 300,      w = 60,      h = 100,      speed = 1,      rot = 0,      cannon = {          w = 10,          h = 50,          radius = 20        }    }        target ={      x = 0,      y = 0      }endfunction keyControl()  local down = love.keyboard.isDown  if down("a") then    tank.rot = tank.rot - 0.1  elseif down("d") then    tank.rot = tank.rot + 0.1  elseif down("w") then    tank.x = tank.x + tank.speed * math.sin(tank.rot)    tank.y = tank.y - tank.speed * math.cos(tank.rot)  elseif down("s") then    tank.x = tank.x - tank.speed * math.sin(tank.rot)    tank.y = tank.y + tank.speed * math.cos(tank.rot)  endendfunction getRot(x1, y1, x2, y2)  if x1==x2 and y1==y2 then     return 0   end  local angle = math.atan((x2-x1)/(y2-y1))  if y1-y2 < 0 then    angle=angle-math.pi   end  if angle > 0 then    angle = angle - math.pi*2  end  return -angleendfunction mouseControl()  target.x, target.y  = love.mouse.getPosition()  local rot = getRot(target.x, target.y, tank.x, tank.y)  tank.cannon.rot = rotendfunction love.update(dt)  keyControl()  mouseControl()endfunction love.draw()  --车身  love.graphics.push()  love.graphics.translate(tank.x, tank.y)  love.graphics.rotate(tank.rot)  love.graphics.setColor(128,128,128)  love.graphics.rectangle("fill", -tank.w/2, -tank.h/2, tank.w, tank.h)  love.graphics.pop()    --炮塔  love.graphics.push()  love.graphics.translate(tank.x, tank.y)  love.graphics.rotate(tank.cannon.rot)  love.graphics.setColor(0, 255, 0)  love.graphics.circle("fill", 0, 0, tank.cannon.radius)  love.graphics.setColor(0, 255, 255)  love.graphics.rectangle("fill", -tank.cannon.w/2, 0, tank.cannon.w, tank.cannon.h)  love.graphics.pop()    --激光  love.graphics.setColor(255,0,0)  love.graphics.line(tank.x, tank.y, target.x, target.y)end

 

运行效果

转载于:https://www.cnblogs.com/JimmyCode/p/7421217.html

你可能感兴趣的文章
cenos 7 安装php7
查看>>
扩展RadioButtonListFor和CheckBoxListFor
查看>>
<ul></ul>标签
查看>>
IOS KVO没有在delloc中移除导致奔溃
查看>>
关于python内open函数encoding编码问题
查看>>
C# 基础知识复习(七)---继承
查看>>
『飞行路线 分层图最短路』
查看>>
.net 中句柄的理解
查看>>
性能指标
查看>>
(详细)JAVA使用JDBC连接MySQL数据库(2)- MySQL Connectors
查看>>
Eclipse: How to Navigating Java call stack in Eclipse
查看>>
多行超高 溢出省略
查看>>
C#获取项目程序及运行路径的方法
查看>>
Android核心分析之十七电话系统之rilD
查看>>
HDFS详解
查看>>
Java 基本I/O的学习总结(一 是什么)
查看>>
Alcatraz的安装和使用
查看>>
bzoj 3533 [Sdoi2014]向量集 线段树+凸包+三分(+动态开数组) 好题
查看>>
T-SQL 簡易小數處理
查看>>
Java工程师必备书单
查看>>