Groups | Search | Server Info | Keyboard shortcuts | Login | Register [http] [https] [nntp] [nntps]


Groups > comp.lang.java.help > #2170 > unrolled thread

Lunar Lander

Started bysqwundle@gmail.com
First post2012-10-24 15:11 -0700
Last post2012-10-24 20:37 -0700
Articles 7 — 4 participants

Back to article view | Back to comp.lang.java.help


Contents

  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

#2170 — Lunar Lander

Fromsqwundle@gmail.com
Date2012-10-24 15:11 -0700
SubjectLunar Lander
Message-ID<a02ca455-c62d-4397-8673-26489e368630@googlegroups.com>
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 

import java.awt.*;
import java.awt.event.*;

public class lunarlander extends java.applet.Applet implements MouseListener, KeyListener
{
		double x = 100, y = 40;
		double vx = 0.1, vy = 0.1;


		public lunarlander()
		{
			addMouseListener(this);
			addKeyListener(this);
		}
		public void paint(Graphics g)
		{
			vy = vy + 0.1;
			vx = vx + 0.001;
			vy = vy * 0.999;
			vx = vx * 0.999;
			if(y > 600 && vy > 0)
				vy = - vy;
			x = x + vx;
			y = y + vy;
			g.fillOval((int)x,(int)y,30,30);
			for(int i = 0; i < 100000; i ++)
			repaint();
			if(y < 100)
			g.drawString("Crash", 800, 800);
		}
		public void keyReleased(KeyEvent ke){}
		public void keyPressed(KeyEvent ke)
		{
			if(ke.getKeyCode() == KeyEvent.VK_UP)
				vy-=vy+2.0;
			else if(ke.getKeyCode() == KeyEvent.VK_DOWN)
				vy-=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;
		}
		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){}
}

[toc] | [next] | [standalone]


#2171

FromKnute Johnson <nospam@knutejohnson.com>
Date2012-10-24 17:57 -0700
Message-ID<k6a2po$ulm$1@dont-email.me>
In reply to#2170
On 10/24/2012 3:11 PM, sqwundle@gmail.com 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
>
> import java.awt.*;
> import java.awt.event.*;
>
> public class lunarlander extends java.applet.Applet implements MouseListener, KeyListener
> {
> 		double x = 100, y = 40;
> 		double vx = 0.1, vy = 0.1;
>
>
> 		public lunarlander()
> 		{
> 			addMouseListener(this);
> 			addKeyListener(this);
> 		}
> 		public void paint(Graphics g)
> 		{
> 			vy = vy + 0.1;
> 			vx = vx + 0.001;
> 			vy = vy * 0.999;
> 			vx = vx * 0.999;
> 			if(y > 600 && vy > 0)
> 				vy = - vy;
> 			x = x + vx;
> 			y = y + vy;
> 			g.fillOval((int)x,(int)y,30,30);
> 			for(int i = 0; i < 100000; i ++)
> 			repaint();
> 			if(y < 100)
> 			g.drawString("Crash", 800, 800);
> 		}
> 		public void keyReleased(KeyEvent ke){}
> 		public void keyPressed(KeyEvent ke)
> 		{
> 			if(ke.getKeyCode() == KeyEvent.VK_UP)
> 				vy-=vy+2.0;
> 			else if(ke.getKeyCode() == KeyEvent.VK_DOWN)
> 				vy-=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;
> 		}
> 		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){}
> }
>

Take a look at my Asteroids game code;

http://rabbitbrush.frazmtn.com/asteroids.html

-- 

Knute Johnson

[toc] | [prev] | [next] | [standalone]


#2173

Fromsqwundle@gmail.com
Date2012-10-24 18:32 -0700
Message-ID<0a94edb9-529b-4df8-8b89-97778cacbd15@googlegroups.com>
In reply to#2171
On Wednesday, October 24, 2012 8:57:28 PM UTC-4, Knute Johnson wrote:
> On 10/24/2012 3:11 PM, sqwundle@gmail.com 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
> 
> >
> 
> > import java.awt.*;
> 
> > import java.awt.event.*;
> 
> >
> 
> > public class lunarlander extends java.applet.Applet implements MouseListener, KeyListener
> 
> > {
> 
> > 		double x = 100, y = 40;
> 
> > 		double vx = 0.1, vy = 0.1;
> 
> >
> 
> >
> 
> > 		public lunarlander()
> 
> > 		{
> 
> > 			addMouseListener(this);
> 
> > 			addKeyListener(this);
> 
> > 		}
> 
> > 		public void paint(Graphics g)
> 
> > 		{
> 
> > 			vy = vy + 0.1;
> 
> > 			vx = vx + 0.001;
> 
> > 			vy = vy * 0.999;
> 
> > 			vx = vx * 0.999;
> 
> > 			if(y > 600 && vy > 0)
> 
> > 				vy = - vy;
> 
> > 			x = x + vx;
> 
> > 			y = y + vy;
> 
> > 			g.fillOval((int)x,(int)y,30,30);
> 
> > 			for(int i = 0; i < 100000; i ++)
> 
> > 			repaint();
> 
> > 			if(y < 100)
> 
> > 			g.drawString("Crash", 800, 800);
> 
> > 		}
> 
> > 		public void keyReleased(KeyEvent ke){}
> 
> > 		public void keyPressed(KeyEvent ke)
> 
> > 		{
> 
> > 			if(ke.getKeyCode() == KeyEvent.VK_UP)
> 
> > 				vy-=vy+2.0;
> 
> > 			else if(ke.getKeyCode() == KeyEvent.VK_DOWN)
> 
> > 				vy-=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;
> 
> > 		}
> 
> > 		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){}
> 
> > }
> 
> >
> 
> 
> 
> Take a look at my Asteroids game code;
> 
> 
> 
> http://rabbitbrush.frazmtn.com/asteroids.html
> 
> 
> 
> -- 
> 
> 
> 
> Knute Johnson

im a beginner at java so reading long code like that is confusing. what exactly do i need to fix my problem

[toc] | [prev] | [next] | [standalone]


#2175

FromKnute Johnson <nospam@knutejohnson.com>
Date2012-10-24 21:11 -0700
Message-ID<k6ae4v$f9j$1@dont-email.me>
In reply to#2173
On 10/24/2012 6:32 PM, sqwundle@gmail.com wrote:
> On Wednesday, October 24, 2012 8:57:28 PM UTC-4, Knute Johnson wrote:
>> On 10/24/2012 3:11 PM, sqwundle@gmail.com 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
>>
>>>
>>
>>> import java.awt.*;
>>
>>> import java.awt.event.*;
>>
>>>
>>
>>> public class lunarlander extends java.applet.Applet implements MouseListener, KeyListener
>>
>>> {
>>
>>> 		double x = 100, y = 40;
>>
>>> 		double vx = 0.1, vy = 0.1;
>>
>>>
>>
>>>
>>
>>> 		public lunarlander()
>>
>>> 		{
>>
>>> 			addMouseListener(this);
>>
>>> 			addKeyListener(this);
>>
>>> 		}
>>
>>> 		public void paint(Graphics g)
>>
>>> 		{
>>
>>> 			vy = vy + 0.1;
>>
>>> 			vx = vx + 0.001;
>>
>>> 			vy = vy * 0.999;
>>
>>> 			vx = vx * 0.999;
>>
>>> 			if(y > 600 && vy > 0)
>>
>>> 				vy = - vy;
>>
>>> 			x = x + vx;
>>
>>> 			y = y + vy;
>>
>>> 			g.fillOval((int)x,(int)y,30,30);
>>
>>> 			for(int i = 0; i < 100000; i ++)
>>
>>> 			repaint();
>>
>>> 			if(y < 100)
>>
>>> 			g.drawString("Crash", 800, 800);
>>
>>> 		}
>>
>>> 		public void keyReleased(KeyEvent ke){}
>>
>>> 		public void keyPressed(KeyEvent ke)
>>
>>> 		{
>>
>>> 			if(ke.getKeyCode() == KeyEvent.VK_UP)
>>
>>> 				vy-=vy+2.0;
>>
>>> 			else if(ke.getKeyCode() == KeyEvent.VK_DOWN)
>>
>>> 				vy-=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;
>>
>>> 		}
>>
>>> 		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){}
>>
>>> }
>>
>>>
>>
>>
>>
>> Take a look at my Asteroids game code;
>>
>>
>>
>> http://rabbitbrush.frazmtn.com/asteroids.html
>>
>>
>>
>> --
>>
>>
>>
>> Knute Johnson
>
> im a beginner at java so reading long code like that is confusing. what exactly do i need to fix my problem
>

You've got a lot of problems.  You need a more workable structure for 
your drawing code.  Calling repaint() from your paint() method is 
problematic.  You need a Thread or a Timer that calls repaint() after 
you change your velocities.  Lew's suggestion to use Swing would make 
things easier and probably make it work better tool.

The javax.swing.Timer class is very easy to use for your animation loop. 
  You can use the arrow keys to increment or decrement your velocities. 
  Then in each actionPerformed() of the Timer, redraw your lander's world.

Here is some simpler code for you to emulate;

import java.awt.*;
import java.awt.event.*;
import javax.swing.*;

public class test1 extends JPanel implements ActionListener {
     private volatile int x = 200, y = 150;
     private volatile int dx = 0, dy = 0;

     private final Timer timer;

     public test1() {
         setPreferredSize(new Dimension(400,300));

         addKeyListener(new KeyAdapter() {
             public void keyPressed(KeyEvent ke) {
                 int code = ke.getKeyCode();
                 switch (code) {
                     case KeyEvent.VK_UP:
                         dy -= 1;
                         break;
                     case KeyEvent.VK_DOWN:
                         dy += 1;
                         break;
                     case KeyEvent.VK_LEFT:
                         dx -= 1;
                         break;
                     case KeyEvent.VK_RIGHT:
                         dx += 1;
                         break;
                 }
             }
         });

         timer = new Timer(40,this);  // 25 frames per second
     }

     public void start() {
         timer.start();
     }

     public void actionPerformed(ActionEvent ae) {
         x += dx;
         y += dy;
         repaint();
     }

     public void paintComponent(Graphics g) {
         g.setColor(getBackground());
         g.fillRect(0,0,getWidth(),getHeight());
         g.setColor(Color.RED);
         g.drawString(String.format("X:%04d Y:%04d dX:%04d 
dY:%04d",x,y,dx,dy),
          10,10);
         g.fillOval(x-2,y-2,5,5);
     }

     public static void main(String[] args) {
         EventQueue.invokeLater(new Runnable() {
             public void run() {
                 test1 t1 = new test1();
                 JFrame f = new JFrame("test1");
                 f.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
                 f.add(t1,BorderLayout.CENTER);
                 f.pack();
                 f.setVisible(true);
                 t1.start();
                 t1.requestFocusInWindow();
             }
         });
     }
}



-- 

Knute Johnson

[toc] | [prev] | [next] | [standalone]


#2176

FromLew <lewbloch@gmail.com>
Date2012-10-25 01:05 -0700
Message-ID<31988b9d-9d7c-41a5-96cf-535787dc2f83@googlegroups.com>
In reply to#2173
sqwundle@ wrote:
> Knute Johnson wrote:
>> sqwundle@ 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
> 
> [snip]
> 
>> Take a look at my Asteroids game code;
>> http://rabbitbrush.frazmtn.com/asteroids.html
...
> 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 
invocations.

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.

Twice.

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 
comments.

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 
lap.

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 
longer.

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.

-- 
Lew

[toc] | [prev] | [next] | [standalone]


#2172

FromLew <lewbloch@gmail.com>
Date2012-10-24 18:31 -0700
Message-ID<cc60be5f-96fc-459f-b0ad-b23d825baf55@googlegroups.com>
In reply to#2170
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

[toc] | [prev] | [next] | [standalone]


#2174

FromDaniel Pitts <newsgroup.nospam@virtualinfinity.net>
Date2012-10-24 20:37 -0700
Message-ID<zt2is.1522$lD4.1163@newsfe24.iad>
In reply to#2170
On 10/24/12 3:11 PM, sqwundle@gmail.com 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
[snip]
> if(ke.getKeyCode() == KeyEvent.VK_UP)
>    vy-=vy+2.0;

Think carefully what this line says.

"vy-=vy+2.0"

the "a-=b" can be written instead as a = a - (b).  In your case, this 
would result in "vy = vy - (vy + 2.0)"  vy-(vy+2.0) is always -2.0

You have similar problems on your other key codes.

I suggest that problem be fixed, which I'll leave as an exercise for the 
reader.

[toc] | [prev] | [standalone]


Back to top | Article view | comp.lang.java.help


csiph-web