This is a read-only snapshot of the ComputerCraft forums, taken in April 2020.
Exerro's profile picture

Ball game type thing

Started by Exerro, 29 October 2012 - 08:50 AM
Exerro #1
Posted 29 October 2012 - 09:50 AM
I got a bit bored making my new OS with click+colour support and decided to make a moving wallpaper. This then got a bit more advanced to the point where you could call it a game. The balls bounce off eachother and off the walls. There isnt really any point to it but check it out to see if you like it.
Screenshots:
Spoilerhttp://imgur.com/a/Itogk
Controls:
Spoiler-Add a ball…
–right click to add one going right
–left click to add one going left
–scroll up to change direction of new ball to up
–scroll down to change direction of new ball to down
–press a char key to change the icon of the added ball
-Change speed
–up arrow = +0.05
–down arrow = -0.05
–left arrow = -1
–right arrow = +1
-Pause
–space and space again to resume
-Delete
–click on "Del"
-Reset
–click on "Reset"
-Exit
–press backspace or enter (this was fine but now can cause java error, idk why.)
Code:
Spoiler

--this is a game
--copyright of BlueTide Programs.inc, Benedict Allen, and all members of BlueTide Programs.inc
--if you wish to use this program please contact Benedict Allen(awsumben13 on cc forums) and make sure you give the credit to BlueTide Programs
--usability functions
function clear(x,y)
if x == nil or y == nil then
  term.clear()
  term.setCursorPos(1,1)
else
  term.clear()
  term.setCursorPos(x,y)
end
end
function writePos(x,y,text)
term.setCursorPos(x,y)
term.write(text)
end
function clearPrint(x,y,text)
term.clear()
term.setCursorPos(x,y)
term.write(text)
end
function reset(file, x, y)
if fs.exists(file) then
  h = fs.open(file, "w")
  h.write("")
  h.close()
  term.setCursorPos(x,y)
  term.write("File reset")
  sleep(2)
else
  term.setCursorPos(x,y)
  term.write("file not found")
  sleep(2)
end
end
function tip(help)
while true do
  clear()
  print(help)
  writePos(1,17,"press enter to exit")
  event, key = os.pullEvent()
  if key == 28 then break
  end
end
end
function drawBox(x,y,z,n,clear)
if clear == false then
  for k = 1,n-y do
   for i = 1,z-x do
	writePos(x+i,y+k," ")
   end
  end
end
writePos(x,y," ")
term.setCursorPos(x+1,y)
for i = 1,(z-x)-1 do
  write(" ")
end
writePos(z,y," ")
term.setCursorPos(x+1,n)
for i = 1,(z-x)-1 do
  write(" ")
end
for i = 1,(n-y)-1 do
  writePos(x,y+i," ")
end
for i = 1,(n-y)-1 do
  writePos(z,y+i," ")
end
writePos(x,n," ")
writePos(z,n," ")
end
function split(str, pat)
   local t = {}
   local fpat = "(.-)" .. pat
   local last_end = 1
   local s, e, cap = str:find(fpat, 1)
   while s do
	  if s ~= 1 or cap ~= "" then
  table.insert(t,cap)
	  end
	  last_end = e+1
	  s, e, cap = str:find(fpat, last_end)
   end
   if last_end <= #str then
	  cap = str:sub(last_end)
	  table.insert(t, cap)
   end
   return t
end
function fill(x,y,z,n,colour)
for i = y,n do
  for k = x,z do
   term.setBackgroundColour(colour)
   writePos(k,i," ")
  end
end
end
function reader(x,y,limit)
t = {}
k = ""
variable = 0
while true do
  variable = 0
  event, p1, p2, p3 = os.pullEvent()
  if event == "click" then
   getItemSel()
   break
  elseif p1 == 15 and screenSel == 2 then
   screenSel = 1
   t = {}
   break
  elseif event == "rednet_message" then
   receive()
  elseif event == "char" then
   table.insert(t,p1)
  elseif p1 == 28 then
   break
  elseif p1 == 14 then
   table.remove(t,#t)
  end
  for i = x-1,x+limit do
   term.setBackgroundColour(colours.lightBlue)
   writePos(i,y," ")
  end
  if #t < limit then
   for i = 1,#t do
	writePos((x-1)+i,y,t[i])
	term.setCursorBlink(true)
   end
  else
   for i = #t-limit,#t do
	variable = variable+1
	writePos(((x-1)+variable)-1,y,t[i])
	term.setCursorBlink(true)
   end
  end
end
for i = 1,#t do
  k = k..t[i]
end
return k
end
function defaultColours()
term.setBackgroundColour(colours.white)
term.setTextColour(colours.black)
end
function sleep(time)
while true do
  if time ~= nil then
   os.startTimer(time)
  end
  event, key = os.pullEvent()
  if time == nil then
   break
  elseif event == "timer" then
   break
  end
end
end
--end
--programs functions
function drawScreen()
clear()
term.setBackgroundColour(colours.black)
drawBox(1,1,51,17)
drawBox(1,17,51,19)
defaultColours()
for i = 1,#items do
  writePos(items[i].xPos,items[i].yPos,items[i].toPrint)
  --writePos(1,i,items[i].xPos.."-"..items[i].yPos.." - - - "..items[i].yChange)
end
writePos(2,18,"Speed = "..11-time.." - Number of items = "..#items)
writePos(37,18,"- Del - Reset")
end
function move()
for i = 1,#items do
  for k = 1,#items do
   if items[i].xPos == items[k].xPos-1 and (items[i].yPos == items[k].yPos or items[i].yPos == items[k].yPos+1 or items[i].yPos == items[k].yPos-1) then
	if items[i].xChange == 1 then
	 items[i].xChange = -1
	end
   end
   if items[i].xPos == items[k].xPos+1 and (items[i].yPos == items[k].yPos or items[i].yPos == items[k].yPos+1 or items[i].yPos == items[k].yPos-1) then
	if items[i].xChange == -1 then
	 items[i].xChange = 1
	end
   end
   if items[i].yPos == items[k].yPos-1 and (items[i].xPos == items[k].xPos or items[i].xPos == items[k].xPos-1 or items[i].xPos == items[k].xPos+1) then
	if items[i].yChange == 1 then
	 items[i].yChange = -1
	end
   end
   if items[i].yPos == items[k].yPos+1 and (items[i].xPos == items[k].xPos or items[i].xPos == items[k].xPos-1 or items[i].xPos == items[k].xPos+1) then
	if items[i].yChange == -1 then
	 items[i].yChange = 1
	end
   end
  end
end
for i = 1,#items do
  if items[i].xPos <= 2 and items[i].xChange < 0 then
   items[i].xChange = tonumber(string.sub(tostring(items[i].xChange),2,3))	  
  elseif items[i].xPos >= 50 and items[i].xChange > 0 then
   items[i].xChange = items[i].xChange-(2*items[i].xChange)
  end
  if items[i].yPos <= 2 and items[i].yChange < 0 then
   items[i].yChange = tonumber(string.sub(tostring(items[i].yChange),2,3))	  
  elseif items[i].yPos >= 16 and items[i].yChange > 0 then
   items[i].yChange = items[i].yChange-(2*items[i].yChange)
  end
end
for i = 1,#items do
  items[i].xPos = items[i].xPos+items[i].xChange
  items[i].yPos = items[i].yPos+items[i].yChange
end
end
function runApp()
while loopBreaker ~= true do
  for i = 1,#items do
   items[i].height = 15-items[i].yPos
  end
  drawScreen()
  move()
  if timerStarted == false then
   os.startTimer(time)
   timerStarted = true
  end
  while true do
   drawScreen()
   event, p1, p2, p3 = os.pullEvent()
   if event == "key" and (p1 == 28 or p1 == 14) then
	loopBreaker = true
	break
   elseif event == "char" then
	icon = p1
   elseif event == "mouse_click" and #items < itemLimit and p1 == 2 and p3 < 16 and p3 > 1 and p2 > 1 and p2 < 51 then
	items[#items+1] = {toPrint = icon;xPos = p2;yPos = p3;xChange = 1;yChange = dir; height = 15-p3}
   elseif event == "mouse_click" and #items < itemLimit and p1 == 1 and p3 < 16 and p3 > 1 and p2 > 1 and p2 < 51 then
	items[#items+1] = {toPrint = icon;xPos = p2;yPos = p3;xChange = -1;yChange = dir;height = 15-p3}
   elseif event == "mouse_click" and p3 == 18 and p2 > 38 and p2 < 42 and #items > 1 then
	items[#items] = nil
	break
   elseif event == "mouse_click" and p3 == 18 and p2 > 44 and p2 < 50 then
	for i = 2,#items do
	 items[i] = nil
	end
	break
   elseif event == "mouse_scroll" and p1 == -1 then
	dir = -1
   elseif event == "mouse_scroll" and p1 == 1 then
	dir = 1
   elseif p1 == 205 and time > 1.04 then
	time = time-1
   elseif p1 == 203 and time < 9.01 then
	time = time+1
   elseif p1 == 200 and time > 0.09 then
	time = time-0.05
   elseif p1 == 208 and time < 9.96 then
	time = time+0.05
   elseif p1 == 208 and time < 0.09 then
	time = 0.0
   elseif p1 == 205 and time < 1.05 then
	time = 0.0
   elseif p1 == 203 and time > 9 then
	time = 10
   elseif p1 == 200 and time > 9.95 then
	time = 10
   elseif p1 == 15 then
	time = 1
	timerStarted = false
	break
   elseif event == "timer" then
	timerStarted = false
	break
   elseif p1 == 57 then
	while true do
	 for i = 1,#items do
	  writePos(items[i].xPos-1,items[i].yPos,"["..items[i].toPrint.."]")
	 end
	 event, key = os.pullEvent("key")
	 if key == 57 then
	  timerStarted = false
	  break
	 end
	end
	break
   end
  end
end
end
--end
--variables
dir = 1
time = 0
icon = "O"
itemLimit = 30
loopBreaker = false
timerStarted = false
items = {
{toPrint = "O";
  xPos = 2;
  yPos = 2;
  xChange = 1;
  yChange = 1;
  p = true
};
{toPrint = "O";
  xPos = 3;
  yPos = 2;
  xChange = 1;
  yChange = 1;
  p = true
};
{toPrint = "O";
  xPos = 4;
  yPos = 2;
  xChange = 1;
  yChange = 1;
  p = true
};
}
--end
--code
defaultColours()
runApp()
--end
Hope you have fun :P/>/>
download below:
[attachment=632:file.zip]
jag #2
Posted 29 October 2012 - 12:08 PM
"Pictures: Coming soon!"
Meh… :/