Win a copy of Cross-Platform Desktop Applications: Using Node, Electron, and NW.js this week in the JavaScript forum!
  • Post Reply Bookmark Topic Watch Topic
  • New Topic

Pong  RSS feed

 
James Vieira
Greenhorn
Posts: 3
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I was reviewing code to pong, i'm not sure i see how the game can read in the arrow keys while the ball is moving. How can the balls' x and y coordinate change while the program still waits to recieve an arrow key input to move the paddle?

Wouldn't this be running two programs at once?
[ September 08, 2004: Message edited by: James Vieira ]
 
Ernest Friedman-Hill
author and iconoclast
Sheriff
Posts: 24217
38
Chrome Eclipse IDE Mac OS X
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi James,

Welcome to JavaRanch!

It's not multiple programs, but multiple threads. On old-timey operating-systems of yesteryear, each process could execute only one instruction at a time (some OSs only would execute one such program at a time, as well!) These days, most OSs offer (either as a kernel feature, or as a library) the appearance of multiple concurrent instruction streams that can execute simultaneously within a single process. Each stream is called a thread. A thread has its own execution stack, but shares process memory with all the other threads in the application.

Java is a very thread-oriented language, and it's quite easy to write programs which appear to be doing multiple things at once.

Finally, I'll let you in on a secret: Pong, whether written in C or in Java, can easily be written in a single thread, as long as you can check the keyboard without blocking. In Java graphics, more or less everything happens on a single thread, but the keyboard is effectively polled -- there's no need to block and wait for a response. In C, you can generally do the same thing, although of course the details vary by platform.
 
James Vieira
Greenhorn
Posts: 3
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Howdy and thank you,

I'm only tought what is in the lesson plan, so this is extending my knowledge on my own time. Unfortunatly, self study is not as effective so this is going a little over my head. How exactly would you go about telling the computer to process the repaint while at the same time wait for keyboard input?

If this is more complicated than a line or two, maybe you could point me in the direction of a java trail so i can read up on it?

I appreciate it -James
 
Nick George
Ranch Hand
Posts: 815
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
take a lookie here:

Threads: Doing Two Things at Once
 
James Vieira
Greenhorn
Posts: 3
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thats what i was looking for Joe thanks, it's a little ahead of my current capabiilitys but hey, pong is ahead of my current capabilitys lol.

Quick question...is this trail saying that Timer can be used the same way as the javascript function "setTimeout("method()", miliseconds)"
or does java have thier own setTimeout?
 
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!