Hello,

I am trying to make a simple drawing program. This program should, in theory, have a 1 pixel cursor, and move around the graph screen using the arrow keys. It should place a pixel when you press 2nd, and remove it when you press Del.

However, instead, it makes a single pixel which just moves around the screen, and no keys do anything (except the on key, of course (well, not "of course" if you have onblock)).

The code is here:


Code:
ClrDraw
Pxl-On(1,1)
Pxl-Off(1,1)
1→A
1→B
1→C
1→D
1→E
Repeat 0
getKey→K
If K=21
1→E
If K=23
0→E
If K=24
B-1→B
If K=26
B+1→B
If K=25
A-1→A
If K=34
A+1→A
If E=1
Then
Pxl-On(C,D)
Pxl-On(A,B)
Else
If A≠C≠D
Then
Pxl-Off(C,D)
Pxl-On(A,B)
End
End
A→C
B→D
0→E
End


It's probably a silly mistake. Can you please help me find it?

Thanks!

Code:
If A≠C≠D


If you're trying to say "A≠C and C≠D", the above doesn't do that; TI-BASIC does not have chained comparisons like, say, Python's. I think your code was corrupted by TI-Connect and is really "A≠C or B≠D" or something, so that's probably not the problem.

It looks like every time through the loop, you're setting (C,D), the old position, to (A,B), the new position at the end. Also, E is set to 0. Therefore, to place a pixel, two things must happen in one iteration of the loop:

* The cursor moves to a new pixel (one of the arrow keys is pressed)
* The pixel turns on (2nd is pressed)

But since you're only calling getKey once, only one keypress can be registered per loop. So the cursor can never move off of a pixel while turning it on. I think you can fix this issue by not setting E to 0 at the end of the loop body.
lirtosiast wrote:

Code:
If A≠C≠D


If you're trying to say "A≠C and C≠D", the above doesn't do that; TI-BASIC does not have chained comparisons like, say, Python's. I think your code was corrupted by TI-Connect and is really "A≠C or B≠D" or something, so that's probably not the problem.


You're right. I just noticed that, and yes, in the original, it said "A≠C or B≠D."

lirtosiast wrote:
It looks like every time through the loop, you're setting (C,D), the old position, to (A,B), the new position at the end. Also, E is set to 0. Therefore, to place a pixel, two things must happen in one iteration of the loop:

* The cursor moves to a new pixel (one of the arrow keys is pressed)
* The pixel turns on (2nd is pressed)

But since you're only calling getKey once, only one keypress can be registered per loop. So the cursor can never move off of a pixel while turning it on. I think you can fix this issue by not setting E to 0 at the end of the loop body.


If I'm understanding you correctly, you are saying that I should remove the 0→E at the end. However, this means that E will always equal 1 until Del is pressed, so a pixel will always be placed when the arrow keys are pressed. This creates a continuous line instead of a single pixel. Unfortunately, none are what I'm looking for 😁 .

If you need me to send a screenshot, I'd be happy to do so (and learn how to at the same time 🙂 )!

Thanks for your help, lirtosiast!

Code:

Pxl-On(1,1)
Pxl-Off(1,1)


Also, this code at the very beginning of your program seems redundant. Why is it there?
It is to bring the user to the graph screen.
fluzz wrote:
It is to bring the user to the graph screen.

Do that with DispGraph. Is faster than turning on and off a pixel.

Here is some pseudocode that might work; it changes not much from your stuff:

Code:
1->A
1->B
1->C
1->D
Repeat 0
  [update A and B with getKey]
  If A!=C or B!=D
  Then
    Pxl-Off(C,D
    A->C
    B->D
    Pxl-On(C,D
  End
End

Don't use E or whatever.

It first updates A and B, so A and B hold the new location of the pixel, while C and D the old location. Now, if you pressed an arrow (A!=C or B!=D), it first erases the old pixel, updates the location to the new pixel, and redraw it.

I hope this helps! 🙂
Also, to make exiting the program easier, you can change your repeat command to this:


Code:

Repeat K=45
    getkey->K
    code
End


So, if you press Clear (keycode 45) it exits the loop. 😉
This should work, inspired from my text sprite and spritesheet programs 🙂

Code:
1->A:1->B
Repeat K=45
   Pxl-Change(A,B
   getKey->K
   Pxl-Change(A,B
   If K=34 or 2>abs(K-25:Then
      A-(K=25)(A>0)+(K=34)(A<62->A
      B-(K=24)(B>0)+(K=26)(B<94->B
   End
   If K=21
      Pxl-On(A,B
   
   If K=23
      Pxl-Off(A,B
End
Although the cursor switches pretty fast so you can't see it well...
  
Register to Join the Conversation
Have your own thoughts to add to this or any other topic? Want to ask a question, offer a suggestion, share your own programs and projects, upload a file to the file archives, get help with calculator and computer programming, or simply chat with like-minded coders and tech and calculator enthusiasts via the site-wide AJAX SAX widget? Registration for a free Cemetech account only takes a minute.

» Go to Registration page
Page 1 of 1
» All times are UTC - 5 Hours
 
You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum
You cannot vote in polls in this forum

 

Advertisement