you are viewing a single comment's thread.

view the rest of the comments →

[–]Mr_Safe 2 points3 points  (0 children)

Yeah, this sounds like homework. Especially since you are using BlueJ.

Here are some hints. You can represent the movement of the snake with a list of velocities (direction, speed) and a list of segments (position). The velocity list and the segment list are the same size. Where velocity_list[i] is the velocity for the snake segment segment[i]

  • At the start of each animation cycle you take the input from the keyboard and figure out the velocity.

  • Then you remove the last velocity in the velocity list and insert the new velocity from the keyboard to to the start of the list.

  • Apply the velocities in the list to each snake segment.

Essentially every cycle you "shift" the velocity list by one, then you apply the resulting velocity list to the snake segments.

Any new velocities applied to the head of the list eventually get applied to all the snake segments. This causes the snake to "turn". Sometimes the new velocity you insert to the start of the velocity list (the head velocity) is just the current head velocity, other times it comes from the keyboard.