• 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

Serpentine

 
Greenhorn
Posts: 1
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Dear fellow Java-Programmers,

for my Programming-Course I have to come up with a Java-Implementation of the old C64 Classic "Serpentine", also known as "Snake". To put it in a nutshell, the player controls a snake with two keys, turning the snake right or left at a 90° angle. The snake itself goes forward automatically and cannot be stopped at any time. The goal of the game is to eat 10 apples just by running over them. If you have accomplished that, you advance to the next level, which might have a kind of obstacle (a wall) somewhere in the middle. If you steer the snake into a wall or into itself, it dies and loses one life.
The catch is that the snake, which is relatively small at the beginning, grows with each apple it eats. Thus, it gets harder with any apple the snake has eaten to avoid running into your own body.

So far for the game. I would like to know what your general approach to this programming task would be. What kind of Java-Object would you use for the snake itself, which one for the walls, which for the apples? How would you manage the collision detection etc.

I know these are not questions which can be answered in one or two sentences, but nevertheless, it would be just great if some of you could give me any hints or advice.

Thank you very much in advance,
best regards.
Sven Leinert
 
Bartender
Posts: 5465
212
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
hi Sven,

the easiest approach I can think of is to use a JLabel[][] array, a panel with a gridLayout of the same size, in which you place
your labels.
Your labels can be easily filled as necessary, with some background color representing an empty field, and filled with whatever
icons to present the different objects.
The snake I would represent by an object with an arraylist of coordinates, and a field indicating the direction of movement.
And of course a timer that defines the speed.
If the snake takes an apple, you would put another pair of coordinates to your arraylist. And after moving the snake, you would check
whether the head stumbles onto something (apple, wall, itself, whatever).
And of course you need a KeyListener, and an additional panel for the scores and other control elements.

Disadvantage would be that the unit of movement is the size of your labels, so it is not a continuous movement. I cannot remember
the original play very well, so I don't know if that would be any problem. You could set a snake by coloring your labels, or you could
use some nice icons for that (in which case you probably need an icon for each of the four directions).

Well, something like this.

Greetings,
Piet
 
A day job? In an office? My worst nightmare! Comfort me tiny ad!
a bit of art, as a gift, the permaculture playing cards
https://gardener-gift.com
reply
    Bookmark Topic Watch Topic
  • New Topic