|
#1
|
|||
|
|||
|
I am currently trying to make a paint program but I can't seem to figure out how to draw a line by tapping one point then tapping another point and have a line drawn between the two coordinates.
Heres what I have so far Code:
cblk = color.new(0,0,0);
cwht = color.new(255,255,255);
cred = color.new(255,0,0);
screen.fillrect(0,0,400,240,blk);
function makeButton(x,y,width,height,bordercolor,bgcolor,textsize,textcolor,string)
screen.fillrect(x,y,width,height,bgcolor);
screen.drawrect(x,y,x+width,y+height,bordercolor);
text.color(textcolor);
text.size(textsize);
text.draw(x,y,string);
end;
function get_pts()
while true do
if (control.read() == 1) and (control.isTouch() == 1) then
if (touch.click() == 1) then
x1,y1 = touch.pos();
screen.drawpixel(x1,y1,cwht);
return x1,y1;
end;
end;
end;
if (control.read() == 1) and (control.isTouch() == 1) then
if (touch.click() == 1) then
x2,y2 = touch.pos();
end;
end;
end;
function draw_line(x1,y1,x2,y2,linecolor)
get_pts
screen.drawline(
screen.update();
end;
function draw_dot(x,y)
makeButton(0,0,53,40,cred,blk,20,cred," Exit");
makeButton(0,41,53,40,cred,cblk,18,cred,"Clear");
makeButton(0,82,53,40,cred,cblk,20,cred," Line");
screen.drawpixel(x,y,cwht);
screen.update();
end;
draw_dot(0,0);
while true do
if (control.read() == 1) and (control.isTouch() == 1) then
if (touch.move() == 1) then
x,y = touch.pos();
draw_dot(x,y);
end;
if (touch.click() == 1) then
x,y = touch.pos();
if (y < 40) and (x < 50) then
break;
end;
end;
if (touch.click() == 1) then
x,y = touch.pos();
if (y > 40) and (y < 80) and (x < 50) then
screen.fillrect(0,0,400,240,cblk);
screen.update();
do_out(0,0);
end;
end;
if (touch.click() == 1) then
x,y = touch.pos();
if (y > 81) and (y < 122) and (x < 50) then
draw_line();
end;
end;
end;
os.sleep(1);
end;
If there is a way to interrupt a while loop I could make it all work eventually but I can't seem to find that info. If somebody can point me in the right direction or give me any help it would be great. |
|
|
|||
|
|
|
#2
|
||||
|
||||
|
instead of putting
Code:
while true do e.g. Code:
looping = 1; while looping == 1 do if control.read() == 1 then if (control.isButton() == 1) then if (button.home() == 1) and (button.click() == 1) then looping = 0; end; end; end; end; I havn't had a look through the rest of your code, but it doesn't look too bad on first glance. |
|
#3
|
|||
|
|||
|
Thanks a lot
I was thinking of doing that but had no idea how.I have one question about the code you gave me with the "endgame = 0;" line. Shouldn't that be "looping = 0;" instead. |
|
#4
|
||||
|
||||
|
Hi.
It's hard for me to jump into a unfinished code, and to see what's missing vs. what's wrong. but any way :-) You call this function from draw_line but you don't use the returned x1,y1 and the function never gets to the bottom part? Code:
function get_pts()
while true do
if (control.read() == 1) and (control.isTouch() == 1) then
if (touch.click() == 1) then
x1,y1 = touch.pos();
screen.drawpixel(x1,y1,cwht);
return x1,y1;
end;
end;
end; --loop end ?
if (control.read() == 1) and (control.isTouch() == 1) then
if (touch.click() == 1) then
x2,y2 = touch.pos();
end;
end;
end;
|
|
#5
|
||||
|
||||
|
Quote:
|
|
#6
|
|||
|
|||
|
Yeah I just fixed that with what bzdbbb gave me and that should fix my main problem
Code:
cblk = color.new(0,0,0);
cwht = color.new(255,255,255);
cred = color.new(255,0,0);
screen.fillrect(0,0,400,240,blk);
function makeButton(x,y,width,height,bordercolor,bgcolor,textsize,textcolor,string)
screen.fillrect(x,y,width,height,bgcolor);
screen.drawrect(x,y,x+width,y+height,bordercolor);
text.color(textcolor);
text.size(textsize);
text.draw(x,y,string);
end;
function draw_line()
getpta = 1
while getpta == 1 do
if (control.read() == 1) and (control.isTouch() == 1) then
if (touch.click() == 1) then
x1,y1 = touch.pos();
screen.drawpixel(x1,y1,cwht);
screen.update();
getpta = 0;
end;
end;
end;
getptb = 1
while getptb == 1 do
if (control.read() == 1) and (control.isTouch() == 1) then
if (touch.click() == 1) then
x2,y2 = touch.pos();
screen.drawline(x1,y1,x2,y2,cwht);
screen.update();
getptb = 0;
end;
end;
end;
end;
function draw_dot(x,y)
makeButton(0,0,53,40,cred,blk,20,cred," Exit");
makeButton(0,41,53,40,cred,cblk,18,cred,"Clear");
makeButton(0,82,53,40,cred,cblk,20,cred," Line");
screen.drawpixel(x,y,cwht);
screen.update();
end;
draw_dot(0,0);
while true do
if (control.read() == 1) and (control.isTouch() == 1) then
if (touch.move() == 1) then
x,y = touch.pos();
draw_dot(x,y);
end;
if (touch.click() == 1) then
x,y = touch.pos();
if (y < 40) and (x < 50) then
break;
end;
end;
if (touch.click() == 1) then
x,y = touch.pos();
if (y > 40) and (y < 80) and (x < 50) then
screen.fillrect(0,0,400,240,cblk);
screen.update();
draw_dot(0,0);
end;
end;
if (touch.click() == 1) then
x,y = touch.pos();
if (y > 81) and (y < 122) and (x < 50) then
draw_line();
end;
end;
end;
os.sleep(1);
end;
|
|
#7
|
|||
|
|||
|
Quote:
|
|
#8
|
|||
|
|||
|
Hmmm.... I tried running it and it said "Error Occured!"
So... um what did I do wrong or what is it that I need to add. |
|
#9
|
||||
|
||||
|
Quote:
Color blk ? function draw_dot(x,y) makeButton(0,0,53,40,cred,blk,20,cred," Exit"); |
|
#10
|
||||
|
||||
|
function makeButton(x,y,width,height,bordercolor,bgcolor,te xtsize,textcolor,string)
string is a reserved keyword use strings eg. Recommend using the SCiTE editor that comes with Lua 5.1 for windows or any other Lua highlight enable editor. Jan_DK Last edited by Jan_DK; 01-13-2010 at 04:13 PM. |
|
#11
|
|||
|
|||
|
Thanks.
I think i fixed all the spelling mistakes now. Here's the updated code Code:
cblk = color.new(0,0,0);
cwht = color.new(255,255,255);
cred = color.new(255,0,0);
screen.fillrect(0,0,400,240,cblk);
function makeButton(x,y,width,height,bordercolor,bgcolor,textsize,textcolor,string)
screen.fillrect(x,y,width,height,bgcolor);
screen.drawrect(x,y,x+width,y+height,bordercolor);
text.color(textcolor);
text.size(textsize);
text.draw(x,y,string);
end;
function draw_line()
getpta = 1;
while getpta == 1 do
if (control.read() == 1) and (control.isTouch() == 1) then
if (touch.click() == 1) then
x1,y1 = touch.pos();
screen.drawpixel(x1,y1,cwht);
screen.update();
getpta = 0;
end;
end;
end;
getptb = 1;
while getptb == 1 do
if (control.read() == 1) and (control.isTouch() == 1) then
if (touch.click() == 1) then
x2,y2 = touch.pos();
screen.drawline(x1,y1,x2,y2,cwht);
screen.update();
getptb = 0;
end;
end;
end;
end;
function draw_dot(x,y)
makeButton(0,0,53,40,cred,cblk,20,cred," Exit");
makeButton(0,41,53,40,cred,cblk,18,cred,"Clear");
makeButton(0,82,53,40,cred,cblk,20,cred," Line");
screen.drawpixel(x,y,cwht);
screen.update();
end;
draw_dot(0,0);
while true do
if (control.read() == 1) and (control.isTouch() == 1) then
if (touch.move() == 1) then
x,y = touch.pos();
draw_dot(x,y);
end;
if (touch.click() == 1) then
x,y = touch.pos();
if (y < 40) and (x < 50) then
break;
end;
end;
if (touch.click() == 1) then
x,y = touch.pos();
if (y > 40) and (y < 80) and (x < 50) then
screen.fillrect(0,0,400,240,cblk);
screen.update();
draw_dot(0,0);
end;
end;
if (touch.click() == 1) then
x,y = touch.pos();
if (y > 81) and (y < 122) and (x < 50) then
draw_line();
end;
end;
end;
os.sleep(1);
end;
|
|
#12
|
|||
|
|||
|
Quote:
Code:
cblk = color.new(0,0,0);
cwht = color.new(255,255,255);
cred = color.new(255,0,0);
screen.fillrect(0,0,400,240,cblk);
function makeButton(x,y,width,height,bordercolor,bgcolor,textsize,textcolor, strings)
screen.fillrect(x,y,width,height,bgcolor);
screen.drawrect(x,y,x+width,y+height,bordercolor);
text.color(textcolor);
text.size(textsize);
text.draw(x,y,strings);
end;
function draw_line()
getpta = 1;
while getpta == 1 do
if (control.read() == 1) and (control.isTouch() == 1) then
if (touch.click() == 1) then
x1,y1 = touch.pos();
screen.drawpixel(x1,y1,cwht);
screen.update();
getpta = 0;
end;
end;
end;
getptb = 1;
while getptb == 1 do
if (control.read() == 1) and (control.isTouch() == 1) then
if (touch.click() == 1) then
x2,y2 = touch.pos();
screen.drawline(x1,y1,x2,y2,cwht);
screen.update();
getptb = 0;
end;
end;
end;
end;
function draw_dot(x,y)
makeButton(0,0,53,40,cred,cblk,20,cred," Exit");
makeButton(0,41,53,40,cred,cblk,18,cred,"Clear");
makeButton(0,82,53,40,cred,cblk,20,cred," Line");
screen.drawpixel(x,y,cwht);
screen.update();
end;
draw_dot(0,0);
while true do
if (control.read() == 1) and (control.isTouch() == 1) then
if (touch.move() == 1) then
x,y = touch.pos();
draw_dot(x,y);
end;
if (touch.click() == 1) then
x,y = touch.pos();
if (y < 40) and (x < 50) then
break;
end;
end;
if (touch.click() == 1) then
x,y = touch.pos();
if (y > 40) and (y < 80) and (x < 50) then
screen.fillrect(0,0,400,240,cblk);
screen.update();
draw_dot(0,0);
end;
end;
if (touch.click() == 1) then
x,y = touch.pos();
if (y > 81) and (y < 122) and (x < 50) then
draw_line();
end;
end;
end;
os.sleep(1);
end;
|
|
#13
|
|||
|
|||
|
GREAT it runs now but it crashed when I touched the line button
|
|
#14
|
||||
|
||||
|
Try commenting out each function and seeing if it runs without that function. Locate the exact problem and then working out what's wrong with it. If you can't see what wrong after that then it must be a logical error rather than a syntax error. Add a "text.draw" line and get the zen to draw ALL the variables on the screen and check they do what you want them too. Make sure to put in "tostring()" with numerical values.
And comment your code so when it's easy to see what does what when you look over it. Try all that and you should find the problem.. |
|
#15
|
|||
|
|||
|
Thanks for the help I'll try everything you mentioned. Although I don't exactly know how to add a "text.draw"
|
|
#16
|
||||
|
||||
|
I also recommend using audio.beep's to debug like this:
function draw_line() audio.beep(700,400); --code audio.beep(1700,400); end; I only hear the first beep then it crash. Jan_DK |
|
#17
|
||||
|
||||
|
Hehe after unplugging 30 times and trying, your code works !
it dos not crash, it just go into a loop where it apparently don't read control input without os.sleep(10) function. Add those os.sleep(10) and it works, one click on the line button, 2nd. on screen to set first point 3rd. on screen to draw the line. Code:
function draw_line()
getpta = 1;
while getpta == 1 do
os.sleep(10);
if (control.read() == 1) and (control.isTouch() == 1) then
if (touch.click() == 1) then
x1,y1 = touch.pos();
screen.drawpixel(x1,y1,cwht);
screen.update();
getpta = 0;
end;
end;
end;
getptb = 1;
while getptb == 1 do
os.sleep(10);
if (control.read() == 1) and (control.isTouch() == 1) then
if (touch.click() == 1) then
x2,y2 = touch.pos();
screen.drawline(x1,y1,x2,y2,cwht);
screen.update();
getptb = 0;
end;
end;
end;
end;
|
|
#18
|
||||
|
||||
|
After discovering that os.sleep() don't take milliseconds but units of 10ms
i wanted to change the os.sleep(1) = wait 10ms to os.wait(1) = wait 1 ms in the Starfield screensaver to speed it up, i got the speed-up, but after changing that, I could not exit the apps. only reset works, so I guess that the control event thingy in the X-Fi2 needs at least 10ms to work, even the power off button. Jan_DK |
|
#19
|
|||
|
|||
|
Example use "If then else", your control loop used the same calls a bit too much:
Code:
clBlack = color.new(0,0,0);
clWhite = color.new(255,255,255);
clRed = color.new(255,0,0);
screen.fillrect(0,0,400,240,clBlack)
function makeButton(x,y,width,height,bordercolor,bgcolor,textsize,textcolor,strings)
screen.fillrect(x,y,width,height,bgcolor);
screen.drawrect(x,y,x + width,y + height,bordercolor);
text.color(textcolor);
text.size(textsize);
text.draw(x,y,strings);
end;
function draw_line()
local getpta = 1;
while getpta == 1 do
if (control.read() == 1) and (control.isTouch() == 1) then
if (touch.click() == 1) then
x1, y1 = touch.pos();
screen.drawpixel(x1,y1,clWhite);
screen.update();
getpta = 0;
end;
end;
os.sleep(4);
end;
local getptb = 1;
while getptb == 1 do
if (control.read() == 1) and (control.isTouch() == 1) then
if (touch.click() == 1) then
x2, y2 = touch.pos();
screen.drawline(x1,y1,x2,y2,clWhite);
screen.update();
getptb = 0;
end;
end;
os.sleep(4);
end;
end;
function draw_dot(x,y)
makeButton(0,0,53,40,clRed,clBlack,20,clRed," Exit");
makeButton(0,41,53,40,clRed,clBlack,18,clRed,"Clear");
makeButton(0,82,53,40,clRed,clBlack,20,clRed," Line");
screen.drawpixel(x,y,clWhite);
screen.update();
end;
draw_dot(0,0);
while true do
if (control.read() == 1) and (control.isTouch() == 1) then
if (touch.move() == 1) then
x,y = touch.pos();
draw_dot(x,y);
elseif (touch.click() == 1) then
x,y = touch.pos();
if (y < 40) and (x < 50) then
break;
elseif (y > 40) and (y < 80) and (x < 50) then
screen.fillrect(0,0,400,240,clBlack);
screen.update();
draw_dot(x,y);
elseif (y > 81) and (y < 122) and (x < 50) then
draw_line();
end;
end;
end;
os.sleep(4);
end;
|
|
#20
|
|||
|
|||
|
Thank you so much! This just made my day
![]() I eventually will add a more colors and if somebody figures out how to save a screenshot I will be able to add a save function |
![]() |
«
Previous Thread
|
Next Thread
»
| Thread Tools | Search this Thread |
| Display Modes | |
|
|
All times are GMT -5. The time now is 06:06 PM.











I was thinking of doing that but had no idea how.
Linear Mode
