Jump to content


GhastTearz's Content

There have been 3 items by GhastTearz (Search limited from 10-February 22)


By content type

See this member's

Sort by                Order  

#278658 Easy Menu API

Posted by GhastTearz on 19 December 2018 - 01:35 PM in APIs and Utilities

Posted Image

MENU API Version 2.0.0

I never published version 1.0, but this version is a LOT better. I won't write too much here as most of the information
about this program is in the demo program. Create menu's with this program is really easy, you can type something like this:

os.loadAPI("menu")
local m = menu.new()
m:addScreen("MainMenu",
[[
This is the main menu.
You can't select this test cause there isn't a tag
Look at the games @games
]])
m:addScreen("games",
[[
Play a game of snake @snake
]])
m:addFunc("snake",  shell.run, "worm")
m:displayScreen("MainMenu")()

Try out the demo: pastebin run ZNYdQzaR
The API code is here: https://pastebin.com/B1N0C6Ua



#277169 Teletext and Character Tutorial

Posted by GhastTearz on 09 May 2018 - 05:13 AM in Tutorials

I couldn't find any info on the forums or on the wiki about the neat teletext characters so I decided to
dig around and figure it out.

Here is a table of every character escape code. You can use these in any string.
Posted Image


And here is the teletext characters:
Posted Image

If you look closely you may notice that there is a pattern. The pixels make up a 5 digit binary number. The digits are arranged like this:

  01
  23
  4X

which can be rearranged:

value:  32  16  8  4  2  1
digit:   X   4  3  2  1  0


So to get the right character you can just turn the pixels into a binary number and add 128 to get the corresponding character code.
There is one catch however: There is six pixels but you can only set 5 of the pixels. You can get around this by flipping the background
and foreground color to "invert" the pixels. It seems that if you want accurate pixel details you will have to stick to only two colors though :/

I wrote a short program that let's you create a grid of pixels and fill in points. Here it is:

width, height = term.getSize()

pixelWidth  = width * 2
pixelHeight = height * 3

pixels = {}

-- initialize the  pixel table
for x = 1, pixelWidth do
  pixels[x] = {}
  for y = 1, pixelHeight do
	pixels[x][y] = false
  end
end

fg = colors.white
bg = colors.black

term.setBackgroundColor(bg)
term.setTextColor(fg)
term.clear()

-- color is a boolean
function drawPoint(x, y, color)
  pixels[x][y] = color  

  --find which character the pixel falls into
  local charX = math.ceil(x / 2.0)
  local charY = math.ceil(y / 3.0)
  term.setCursorPos(charX, charY)

  -- Set charX and charY to be the top left pixel
  -- of the character
  charX = (charX - 1) * 2 + 1
  charY = (charY - 1) * 3 + 1


  --Convert the pixels into a 5 bit number
  local num =
	(pixels[charX][charY]	 and 1  or 0) +
	(pixels[charX+1][charY]   and 2  or 0) +
	(pixels[charX][charY+1]   and 4  or 0) +
	(pixels[charX+1][charY+1] and 8  or 0) +
	(pixels[charX][charY+2]   and 16 or 0)

  local char
  if pixels[charX+1][charY+2] == true then
	term.setBackgroundColor(fg)
	term.setTextColor(bg)
	-- 31 - num is equivilent to flipping all the bits of a 5 digit number
	char = 128 + (31 - num)
  else
	term.setBackgroundColor(bg)
	term.setTextColor(fg)
	char = 128 + num
  end

  loadstring("term.write(\"\\"..char.."\")")()
end

drawPoint(4, 4, true)
drawPoint(5, 4, true)
drawPoint(7, 4, true)
drawPoint(8, 4, true)
drawPoint(3, 5, true)
drawPoint(6, 5, true)
drawPoint(9, 5, true)
drawPoint(4, 6, true)
drawPoint(8, 6, true)
drawPoint(5, 7, true)
drawPoint(7, 7, true)
drawPoint(6, 8, true)
print()

This code will print out a heart, and with more functions you could make really cool graphics!
Posted Image



#277162 Brick Breaker

Posted by GhastTearz on 08 May 2018 - 09:15 PM in Games

Nice game, I was surprised how short the actual program was!

I wrote up some comments on your code which is here https://pastebin.com/yz04DK7e
and a modified version demonstrating what changes I'm talking about
can be found here https://pastebin.com/he0RFX0m

The modified version is still glitchy, but I hope you can learn something from it.

The biggest problem with your code is the drawing function. Computercraft can't draw fast
enough, and by drawing the whole screen every time you get a game that is very flickery.
For games it is much better to change things character by character instead of just
printing out the whole screen every time.

Some suggestions for improved versions would be starting the ball from a random direction
and in a random location, improving the brick breaking mechanics, and perhaps changing
the direction of the ball based on where the ball hits the paddle to give the player a little more control.