Re: Lunar Lander
Knute Johnson wrote:
hello im making a lunar lander program but im having trouble with the
accleration and when you click the left and right arrows the ball goes
flying can anyone help fix my code
Take a look at my Asteroids game code;
im a beginner at java so reading long code like that is confusing.
what exactly do i need to fix my problem
This is a normal situation at beginning stages. Key to your success
will be patience.
To read long code, first get a good editor like various windowed versions
of vi or emacs, or any IDE that does Java, and set your preferences to show
methods in boldface and type names (classes and interfaces) in a notable
distinguishing color or boldface.
You can do more than that, but that's up to you.
Copy and paste the code into your nifty editor.
Start by reading only the type declarations and method declarations and
Ignore variables, logic not directly talking to method invocations and
other fluff. Do read comments, of course.
Read the whole code just comments, types and methods.
In my answer to your question upthread I read the whole code you posted.
Good job, by the way, posting your code. Excellent way to show your difficulty.
But I skipped some details, like understanding your specific velocity deltas.
Daniel and Knute gave you the foundation for a really good solution.
I figure they read your whole code, too, judging from the detail of their
So at least three people have read your whole code that you posted,
expecting and hoping that people would read it, as we did.
And code known to have a bug or two in it but without specific knowledge
of where to look, or why certain decisions (like delta values chosen),
and the like, now that if any does, deserves to be called confusing.
But it wasn't bad code, really. It had the errors Daniel pointed out, and
the architectural flaws Knute showed you how to fix, but that just shows
you that programmers accepted your start and showed you how to go the next
So fix those minus-equals operators and restructure your code Knute's way.
You have to make some deep changes,and learning it differently from
reading Knute's examples will nearly certainly take at least four times
Here's what you'll learn, if you are patient with your confusion and just
go through the code, and use it - I mean compile and freaking run it. You
don't have to worry about the compiler or JVM getting confusd.
- How to separate GUI and computation work into the EDT
(look it up, "Java Swing EDT Event Dispatch Thread")
and worker threads, without having to master threads first;
- How to hook GUI components together;
- How to name types, variables and methods and such;
- How to draw custom thingies into a GUI without screwing up the GUI;
- How to deal with timing;
- How to deal with timing;
- Event-driven programming;
- How to make self-documenting (Knuth's "literate") code;
- How to structure a program compactly and completely and logically;
and far too much more to mention here.
Now how you gonna learn all that any quicker than the week or so Knute's
stuff'll teach you?
Remember - actually run it.
From the command line, e.g.,
$ java com.knutejohnson.games.asteroids.Asteroids &
and from any IDE or other way you like, as well.