• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

Pong

 
Greenhorn
Posts: 3
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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 ]
 
author and iconoclast
Posts: 24207
46
Mac OS X Eclipse IDE Chrome
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
 
Ranch Hand
Posts: 815
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
    Number of slices to send:
    Optional 'thank-you' note:
  • 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?
 
reply
    Bookmark Topic Watch Topic
  • New Topic