Groups | Search | Server Info | Keyboard shortcuts | Login | Register [http] [https] [nntp] [nntps]
Groups > comp.lang.java.help > #2172
| Newsgroups | comp.lang.java.help |
|---|---|
| Date | 2012-10-24 18:31 -0700 |
| References | <a02ca455-c62d-4397-8673-26489e368630@googlegroups.com> |
| Message-ID | <cc60be5f-96fc-459f-b0ad-b23d825baf55@googlegroups.com> (permalink) |
| Subject | Re: Lunar Lander |
| From | Lew <lewbloch@gmail.com> |
sqwu...@gmail.com wrote:
> hello im [sic] 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
>
> import java.awt.*;
You should probably use Swing.
> import java.awt.event.*;
>
> public class lunarlander extends java.applet.Applet implements MouseListener, KeyListener
Java coding conventions call for type names to start with an upper-case letter and use camel case,
hence 'LunarLander'.
> {
> double x = 100, y = 40;
You're initializing 'double' variables with 'int' constants. Fortunately for you, Java does the
conversion for you in this case.
> double vx = 0.1, vy = 0.1;
>
> public lunarlander()
'LunarLander'
> {
> addMouseListener(this);
> addKeyListener(this);
> }
Where are your Javadoc comments?
> public void paint(Graphics g)
> {
> vy = vy + 0.1;
A convenient shortcut for this is 'vy += 0.1;'
> vx = vx + 0.001;
> vy = vy * 0.999;
> vx = vx * 0.999;
>
> if(y > 600 && vy > 0)
You should use curly braces for 'if' bodies and the like, for readability and to
avoid future bugs.
What are all these magic constants, 0.999 and 600 and the like? Maybe these are the
problem?
> vy = - vy;
> x = x + vx;
> y = y + vy;
>
> g.fillOval((int)x,(int)y,30,30);
>
> for(int i = 0; i < 100000; i ++)
>
> repaint();
'repaint()' inside 'paint()'?
> if(y < 100)
> g.drawString("Crash", 800, 800);
Indent *and* use curly braces.
> }
>
> public void keyReleased(KeyEvent ke){}
>
> public void keyPressed(KeyEvent ke)
> {
> if(ke.getKeyCode() == KeyEvent.VK_UP)
> vy-=vy+2.0;
Do you really mean to subtract (vy + 2.0) from vy? Why not just set 'vy = -2.0;' then?
> else if(ke.getKeyCode() == KeyEvent.VK_DOWN)
> vy-=vy-4.0;
'vy = 4.0;'
> else if(ke.getKeyCode() == KeyEvent.VK_LEFT)
> vx+=x-0.01;
> else if(ke.getKeyCode() == KeyEvent.VK_RIGHT)
> vx+=x-0.01;
Geez, use a 'switch()' already!
> }
>
> public void keyTyped(KeyEvent ke){}
>
> public void mouseExited(MouseEvent me){}
>
> public void mouseEntered(MouseEvent me){}
>
> public void mouseClicked(MouseEvent me){}
>
> public void mouseReleased(MouseEvent me){}
>
> public void mousePressed(MouseEvent me){}
>
> }
Check your formulas.
Don't use TAB characters to indent code on Usenet! Use spaces (max. of 4 per indent).
--
Lew
Back to comp.lang.java.help | Previous | Next — Previous in thread | Next in thread | Find similar | Unroll thread
Lunar Lander sqwundle@gmail.com - 2012-10-24 15:11 -0700
Re: Lunar Lander Knute Johnson <nospam@knutejohnson.com> - 2012-10-24 17:57 -0700
Re: Lunar Lander sqwundle@gmail.com - 2012-10-24 18:32 -0700
Re: Lunar Lander Knute Johnson <nospam@knutejohnson.com> - 2012-10-24 21:11 -0700
Re: Lunar Lander Lew <lewbloch@gmail.com> - 2012-10-25 01:05 -0700
Re: Lunar Lander Lew <lewbloch@gmail.com> - 2012-10-24 18:31 -0700
Re: Lunar Lander Daniel Pitts <newsgroup.nospam@virtualinfinity.net> - 2012-10-24 20:37 -0700
csiph-web