Archive for the 'Uncategorized' Category

Quick Note On Text Wrapping

Monday, November 9th, 2009

So while working on my Text Adventure the problem of each word in a list being on a separate line when using printf or simply calling a list got annoying, so after a quick bit of looking using

(write "lots and lots of text") 

will automatically “wrap” text into the appropriate number of lines, at least on windows 7. Just thought I would share in case this was frustrating anyone else.. also if this doesn’t work on other operating systems if you could let me know that would be great.

Wiggley critters in frame-based animation

Thursday, November 5th, 2009

Here’s a version of the frame-based animation idea that I presented in class (and in a previous post) that stores the information a little differently and also shows one way to make characters cycle through several appearances: critters.

-Lee

Saving a jpeg of your graphics

Wednesday, November 4th, 2009

If you are using my “better-draw” code (with the variable bm defined as a bitmap%) then you can save an image with a call like this:

(send bm save-file "myImage.jpg" 'jpeg 100)

Other formats are available too. The 100 is a quality level. Check the documentation of save-file (method of bitmap%) for more details.

-Lee

Simple frame-based animation

Tuesday, November 3rd, 2009

Here’s the code from class today, with a simple example of frame-based animation.

-Lee

List recursion code from class

Thursday, October 29th, 2009

Here is the list recursion code from class today.

Using the git DVCS with code-immersion

Thursday, October 29th, 2009

Note: This is totally unnecessary but potentially convenient in most situations. The only situation in which you really need to understand git is if you’d like to contribute code to the code-immersion package – which I’d love it if you did.

As you all probably know, code-immersion is hosted on a service called GitHub. This, contrary to potential expectations, is not simply a funny-sounding name for yet another web service. Rather, it’s a¬† funny-sounding name with some level of reason for yet another web service. The name comes from the distributed version control system (DVCS) the site is rooted in, called git. If you haven’t done much work with programming, you may not know what a version control system is; essentially, it’s a place to keep all the code so that it’s convenient for any number of programmers to grab and play with, without breaking anything permanently. It keeps track of changes and history and makes it easy to get, play with, and update code.

You can read about git at its own site, or ask me about it (in comments or by email or in person); the point of this post is really to say that, should you be interested, code-immersion is hosted in a git repository; if you have git, you can get it (clone, in git terminology) from git://github.com/ianmcorvidae/code-immersion.git and play with it in the usual ways (other than pushing back to github Рto do that, talk to me or read up on how github itself  works!).

Simple file I/O

Wednesday, October 28th, 2009

If you want to read things from files into Scheme, or to write things to files from Scheme, there are a lot of built in procedures to use and a lot of approaches you could follow, but the simplest idea is to read and write single Scheme objects, which could be arbitrarily big things if you use nested lists. Here’s an example:

(define data
  '((here are some symbols (and also "a string"))
    ((2009 10 20 11 34)
     "Numbers in the car of the list of which I'm the cadr.")
    (more stuff)))

(define save-data
  (lambda ()
    (with-output-to-file "myData.txt"
      (lambda ()
        (print data))
      #:exists 'replace)))

(define read-data
  (lambda ()
    (with-input-from-file "myData.txt"
      (lambda ()
        (set! data (read))))))

Turtle graphics and list manipulation

Monday, October 26th, 2009

I’ve written a version of “turtle graphics” that is simpler and different than the versions built in to PLT Scheme, with the intention that it serve as a base for some list manipulation exercises. We will work with this in class soon. The code is linked here.

-Lee

Logic Function

Sunday, October 25th, 2009

Here’s the function I tried to show in class; I had put in a couple last minute fixes that actually ended up making it break more often, but now as far as I can tell it corresponds with sentential logic exactly, except that for coding reasons it requires parentheses around negated clauses, which sentential does not. The function is based on the more rigorous definition of sentential, which requires parentheses inserted with every connective (in practice, logicians often leave out the outermost parentheses).


;Determines if the argument is a well-formed formula of sentential logic with basic letters P-S.
;Example calls: (wff? 'P) (wff? '(P v Q)) (wff? '(~ (~ (P & (Q (R v S))))) all return #t
; (wff? 23) (wff? '(P Q v)) (wff? '(P v Q v R)) return #f


(define letters '(P Q R S))
(define connectives '(v & => ))
(define negated '(~P ~Q ~R ~S))


(define wff?
(lambda (formula)
(let ((letter? (lambda (a)
;asks if the argument is a basic letter of sentential
(if (or (member a letters)
(member a negated))
#t
#f))))
(cond
;these conditions check syntactic rules
((letter? formula)
#t)
((list? formula)
(cond
((equal? (cdr formula) '())
#f)
((equal? (cddr formula) '())
#f)
((if (or (and (and (wff? (car formula)) (and (wff? (third formula)) (eq? (cdddr formula) '())))
(member (second formula) connectives))
(and (equal? (car formula) '~) (and (wff? (second formula)) (eq? (cddr formula) '()))))
#t
#f))
(else #f)))
(else #f)))))

Animating several things at once

Saturday, October 24th, 2009

Now that several of you are making animated characters I’ve received a lot of questions about how to make several characters move at the same time. There are several ways to approach this, but one of the simplest and most popular is to think in terms of complete frames. The idea is to break up the animation process as follows:

loop for each time step:
  erase the whole screen
  draw everything where it should be for this time step
  delay for a bit, to let the viewer see the current frame
  change the positions of everything for the next time step

This means you have to keep track of the positions of everything somewhere (maybe in global variables), that you have to rewrite your character-drawing code to draw a single instance of the character in a single position (which should be easy), and that you have to separate out the code for changing the position of your character from step to step.

That main loop, by the way could be an iterative loop (maybe created with “for” or “do” or “while“) or it could be implemented with a recursive call.

Note that this “erase the whole screen” idea doesn’t play very nicely with the idea of having all of our characters animate at the same time on the same screen in class. That’s okay for now, but with a little more effort you could change your system to do this:

erase the whole screen
loop for each time step:
  draw everything where it should be for this time step
  delay for a bit, to let the viewer see the current frame
  erase all of the individual characters, based on where things are this time step
  change the positions of everything for the next time step

If you do this then you could later change your calls to “ellipse,” “rectangle,” “line,” etc. to calls to “send-code” that tell the server to call ellipse, rectangle, line, etc. And since you’d only be erasing the place where your last drawing was made there wouldn’t be any conflicts except when things actually overlap.