Pong Wars
Lua Carousel » Devlog
Two seconds into a video of this little game, I just knew I had to build it for myself.
local n = 20 -- px per square local v = 200 -- ball velocity in px/s -- initialize a w*h board randomly with one of 2 states local w = floor((Safe_width-60)/n) local h = floor(Safe_height/2/n) local board = {} for y=1,h do local row = {} for x=1,w do table.insert(row, rand(1,2)) end table.insert(board, row) end local px = (Safe_width-n*w)/2 -- initialize colors for the 2 states local c1 = {0.8, 0.8, 1} local c2 = {0.8, 0.9, 0.8} colors = {c1, c2} -- initialize 2 balls local b1 = {x=0, y=floor(n*h/2)} local b2 = {x=n*w, y=floor(n*h/2)} balls = {b1, b2} -- region color to ball within it -- give each ball a unit velocity in a random direction function randNorm2() local p = {x=rand(-100,100), y=rand(-100,100)} local m = math.sqrt(p.x^2 + p.y^2) return {x=p.x/m, y=p.y/m} end b1.v = randNorm2() b2.v = randNorm2() function car.draw() for y=1,h do for x=1,w do color(unpack(colors[board[y][x]])) rect('fill', px+(x-1)*n, Menu_bottom+(y-1)*n, n,n) end end color(unpack(c2)) circle('fill', px+balls[1].x, Menu_bottom+balls[1].y, n/2) color(unpack(c1)) circle('fill', px+balls[2].x, Menu_bottom+balls[2].y, n/2) end function car.update(dt) for c,b in ipairs(balls) do if b.v.x < 0 and (b.x < 1/2*n or maybe_set_board(b.x-1/2*n, b.y, c)) then b.v.x = abs(b.v.x) elseif b.v.x > 0 and (b.x > (w-1/2)*n or maybe_set_board(b.x+1/2*n, b.y, c)) then b.v.x = -abs(b.v.x) elseif b.v.y < 0 and (b.y < 1/2*n or maybe_set_board(b.x, b.y-1/2*n, c)) then b.v.y = abs(b.v.y) elseif b.v.y > 0 and (b.y > (h-1/2)*n or maybe_set_board(b.x, b.y+1/2*n, c)) then b.v.y = -abs(b.v.y) end b.x = b.x + v*b.v.x*dt b.y = b.y + v*b.v.y*dt end end function maybe_set_board(x, y, c) x, y = s(x), s(y) if board[y][x] == c then return end board[y][x] = c return true end function s(z) return 1+floor(z/n) end
If you try pasting this program into Lua Carousel, remember to first run the abbreviations on one of the example screens. Or if you've deleted that screen, here are the abbreviations I used in this post:
g = love.graphics rect, circle = g.rectangle, g.circle color = g.setColor floor, ceil = math.floor, math.ceil abs, rand = math.abs, math.random
Credits
Get Lua Carousel
Lua Carousel
Write programs on desktop and mobile
Status | In development |
Category | Tool |
Author | Kartik Agaram |
Tags | LÖVE |
More posts
- New version after 3 days25 days ago
- New version after 40 days28 days ago
- Turn your phone or tablet into a chess clock41 days ago
- Guest program: bouncing balls58 days ago
- New version after 3 months, and turtle graphics68 days ago
- Interactively zooming in to the Mandelbrot set on a touchscreen87 days ago
- A little timer app88 days ago
- New version after 4 monthsJun 28, 2024
- Visualizing the digits of πMay 04, 2024
Leave a comment
Log in with itch.io to leave a comment.