Advertisement

[java] Help with my first game

Started by May 20, 2000 06:50 PM
1 comment, last by jojor512 24 years, 7 months ago
Hello. For my first Java game, I''m making a WORM-like game. I''m using rectangles as the Worm and its food. My problem is that I can''t get the program to respond to keyboard input. this input would be used to control the directionn of the worm. Any suggestions would be great on how I could improve this design and implement keyboard input. Heres what I have so far: import java.awt.*; import java.applet.*; import java.awt.event.*; import java.util.*; public class TrivialApplet extends Applet { int posX = 10; int posY = 20; boolean run = true; public void init() { setBackground( Color.white ); resize(420,420); } public void paint( Graphics g ) { while( run == true ) { g.setColor( Color.white ); g.fillRect(posX,posY, 10,3); posX = posX + 1; g.setColor( Color.black ); g.fillRect(posX,posY, 10,3); try { Thread.sleep(10); } catch ( Exception e ) { break; } }
Since this is your first game I would really recommend getting one of the books available on Java game programming. I keep a list of the ones I know about on the FAQ:

http://games.cpbinc.com/faq/

In particular I would recommend ''Java Game Programming for Dummies''. It covers a lot of the basics in a step-by-step format and has pretty good source code included.
Advertisement
http://java.sun.com/docs/books/tutorial/uiswing/events/intro.html
I was gonna tell you myself, but sun just does such good a job of it. I couldn''t find event listeners in the faq.

This topic is closed to new replies.

Advertisement