Granny's Programming Pearls
"inside of every large program is a small program struggling to get out"
  • 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 all forums
this forum made possible by our volunteer staff, including ...
  • Campbell Ritchie
  • Devaka Cooray
  • Liutauras Vilda
  • Jeanne Boyarsky
  • Bear Bibeault
  • Paul Clapham
  • Knute Snortum
  • Rob Spoor
Saloon Keepers:
  • Tim Moores
  • Ron McLeod
  • Piet Souris
  • Stephan van Hulst
  • Carey Brown
  • Tim Holloway
  • Frits Walraven
  • Ganesh Patekar

Falling rocks in an applet?

Posts: 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
This is code for a game I am making where you are a character at the bottom of the screen dodging falling rocks. I was wondering how to add in the falling rocks. I think I need to use Timer and an ArrayList but I am not sure if that would work and not truly sure how to go about doing it. Any help or tips would be appreciated. Thanks!

Posts: 3323
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Welcome to the Ranch.

I would suggest you create a 'Rock' class that extends Component. The Rock Class needs to override paint() to draw a rock and also needs a method to move it to the next screen location. It may also be required to know how fast it is moving and what direction it is moving in (depending how complex your game is). Also you probably need some hit logic to know if it has touched another item such as another rock or the player.

You are correct in your idea of using a Timer and some form of collection to holds all your rocks. When the game is running you create instances of your Rock class for each rock that is currently falling and each time the timer fires you move the rocks to their next location and call repaint() to redraw the screen. In the paint method you draw the background and the player and then iterate over the collection of rocks and pass the current graphics object to each rock's paint method.
It is sorta covered in the JavaRanch Style Guide.
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!