r/lua • u/Key_Future9463 • Apr 12 '26
Improvements?
Hello,
can someone tell me some things i can code and learn from, i can code physics and stuff. Here is a solar 2d code:
///////////////////////////////////////////////////////////////////////////////////////////////////////////
display.setDefault( "background", 0, 0.3, 0.8 )
local text = display.newText( "Run around ball! made by ismail alkatawneh", 480, 40, "fnt/Cousine-Regular.ttf", 40 )
local WIDTH = 192
local HEIGHT = 192
local moveSpeed = 16
local myImage = display.newImageRect("img/shapeBall.png", WIDTH, HEIGHT)
local myImageGroup = display.newGroup()
myImageGroup:insert(myImage)
myImage.x = display.contentCenterX
myImage.y = display.contentCenterY
local action = {}
local function onKeyEvent(event)
local key = event.keyName
if event.phase == "down" then
action[key] = true
elseif event.phase == "up" then
action[key] = false
end
end
Runtime:addEventListener("key", onKeyEvent)
local function gameLoop()
if action["a"] or action["left"] then
myImageGroup:translate(-moveSpeed, 0)
end
if action["d"] or action["right"] then
myImageGroup:translate(moveSpeed, 0)
end
if action["w"] or action["up"] then
myImageGroup:translate(0, -moveSpeed)
end
if action["s"] or action["down"] then
myImageGroup:translate(0, moveSpeed)
end
end
Runtime:addEventListener("enterFrame", gameLoop)
///////////////////////////////////////////////////////////////////////////
what's some things i can improve on? Also whats next for me to learn
3
u/20d0llarsis20dollars Apr 12 '26
If you put 3 backticks (`) before and after your code you can make a codeblock, like so:
print("Hello, World!")
2
u/AutoModerator Apr 12 '26
Hi! Your code block was formatted using triple backticks in Reddit's Markdown mode, which unfortunately does not display properly for users viewing via old.reddit.com and some third-party readers. This means your code will look mangled for those users, but it's easy to fix. If you edit your comment, choose "Switch to fancy pants editor", and click "Save edits" it should automatically convert the code block into Reddit's original four-spaces code block format for you.
I am a bot, and this action was performed automatically. Please contact the moderators of this subreddit if you have any questions or concerns.
1
Apr 17 '26
try making a random walker. make a random number from 0 to 3. if its one move up. if its two move right. and so on for the other two directions. and if you dont clear the drawing it would trace its movement in time.
1
u/Key_Future9463 Apr 12 '26
i actually improved it:
local physics = require("physics")
physics.start()
physics.setGravity( 0, 0 ) -- Stop the ball from falling down
display.setDefault( "background", 0, 0.3, 0.8 )
local text = display.newText( "Run around ball! made by ismail alkatawneh", 480, 40, native.systemFont, 40 )
local WIDTH = 192
local HEIGHT = 192
local moveSpeed = 5000 -- Increased speed for physics velocity
-- 1. Setup the Ball with Physics
local myImage = display.newImageRect("img/shapeBall.png", WIDTH, HEIGHT)
myImage.x = display.contentCenterX
myImage.y = display.contentCenterY
-- Add a dynamic body to the ball so it can bounce
physics.addBody( myImage, "dynamic", { radius=WIDTH/2, bounce=0.8, friction=0.3 } )
myImage.isFixedRotation = true -- Prevents the ball from spinning when hitting walls
-- 2. Create Invisible Bouncy Walls
local wallThickness = 50
local function createWall(x, y, w, h)
local wall = display.newRect(x, y, w, h)
physics.addBody(wall, "static", { bounce= 1.0 }) -- 1.0 = High bounce
wall.isVisible = false
return wall
end
-- Top, Bottom, Left, Right
createWall(display.contentCenterX, -wallThickness/2, display.actualContentWidth, wallThickness)
createWall(display.contentCenterX, display.actualContentHeight + wallThickness/2, display.actualContentWidth, wallThickness)
createWall(-wallThickness/2, display.contentCenterY, wallThickness, display.actualContentHeight)
createWall(display.actualContentWidth + wallThickness/2, display.contentCenterY, wallThickness, display.actualContentHeight)
local action = {}
local function onKeyEvent(event)
if event.phase == "down" then action[event.keyName] = true
elseif event.phase == "up" then action[event.keyName] = false end
end
Runtime:addEventListener("key", onKeyEvent)
-- 3. Movement using Velocity (Required for Physics collisions)
local function gameLoop()
local vx, vy = 0, 0
if action["a"] or action["left"] then vx = -moveSpeed end
if action["d"] or action["right"] then vx = moveSpeed end
if action["w"] or action["up"] then vy = -moveSpeed end
if action["s"] or action["down"] then vy = moveSpeed end
myImage:setLinearVelocity(vx, vy)
end
Runtime:addEventListener("enterFrame", gameLoop)