DevX Home    Today's Headlines   Articles Archive   Tip Bank   Forums   

Results 1 to 2 of 2
  1. #1
    Join Date
    Aug 2004

    Question Java (OO) n00b needs help with key input in snake game...


    First of, let me say that I only started playing with Java a couple of days ago.. I am pretty good with 'older' languages like Turbo Pascal, etc, but the OO stuff in Java is confusing the h*ll out of me..

    I decided to make a simple snake game to learn the language (probably a bit optimistic on this one...). I manage to get the game running, graphics are being drawn etc, but I cannot get any key input to register.. I am basically stuck here. I looked at other Snake games but their implementation is vastly different from mine, which could of course mean that my implementation is impossible to get to work in the first place, but I am trying to keep a positive mind so far

    Here is the code, I know I could write it shorter, but I tried to document thoroughly what I am trying to do..

    If anyone can explain how I can get the 'keyDown' to 'register' I would greatly appreciate it...

    import java.applet.Applet;
    import java.awt.*;
    import java.text.*;
    import java.lang.*;
    import java.util.*;
    import java.io.*;

    /*import java.awt.Graphics;*/
    /*import java.awt.Color;*/

    public class SimpleSnake12 extends Applet {
    /*public class SimpleSnake11 extends Applet implements Runnable {*/

    int[] xvalues = new int[10000]; /*array with all x values that the snake can have*/
    int[] yvalues = new int[10000]; /*array with all y values that the snake can have*/

    int move_number; /*the current move (each time the snake moves)*/
    int last_catch_number; /*the last time the snake 'ate' an item*/
    int top_snake_array_id; /* The highest array value. Since it starts with 0, 15 means it would be 16 pieces */
    int new_x; /*calculated based on direction, and what the previous head pos was*/
    int new_y; /*calculated based on direction, and what the previous head pos was*/
    int old_tail_x; /*calculated based on direction, and what the previous head pos was*/
    int old_tail_y; /*calculated based on direction, and what the previous head pos was*/
    int current_item_x; /*Reveals position of current item*/
    int current_item_y; /*Reveals position of current item*/
    String direction; /*what direction the snake is currently moving in*/
    boolean movement_type_move; /*what kind of movement type is necessary, either move snake or grow it*/
    Color background_color;
    Color snakehead_color;
    Color snakebody_color;
    Color item_color;
    boolean still_in_race;
    boolean key_return;

    public void init() {
    xvalues[0] = 50; /*middle of screen*/
    yvalues[0] = 50; /*middle of screen*/
    move_number = 0;
    top_snake_array_id = 1;
    last_catch_number = -4; /*to avoid snake from growing at first move*/
    direction = "left"; /* other options 'right','up','down'*/
    movement_type_move = true; /* true if move, false if grow/increase*/
    background_color = Color.white;
    snakehead_color = Color.blue;
    snakebody_color = Color.red;
    item_color = Color.green;
    current_item_x = 60; /*hardcoding first item location*/
    current_item_y = 70; /*hardcoding first item location*/


    still_in_race = true;


    public void start() {


    public void stop() {

    public void destroy() {
    System.out.println("Preparing to unload...");

    public void paint(Graphics g) {
    /*Setting up Playscreen*/
    g.setColor(background_color); /*frame color*/
    g.drawRect(0, 0, 499, 499); /*rectangle setup*/
    g.setColor(item_color); /*frame color*/
    g.fill3DRect(current_item_x*5, current_item_y*5, 4, 4, false); /*setting up first food*/

    /*Main Loop*/
    move_number += 1;

    /*Sleep Function*/
    catch (Exception e) { }

    /*What is new Snake Head Position?*/
    if (direction == "left") {
    new_x = xvalues[0] - 1;
    new_y = yvalues[0];}
    if (direction == "right") {
    new_x = xvalues[0] + 1;
    new_y = yvalues[0];}
    if (direction == "up") {
    new_x = xvalues[0];
    new_y = yvalues[0] + 1;}
    if (direction == "down") {
    new_x = xvalues[0];
    new_y = yvalues[0] - 1;}

    /*Determine whether to just move, or grow snake*/
    if (move_number - last_catch_number < 3){
    movement_type_move = false;}
    movement_type_move = true;}

    old_tail_x = xvalues[top_snake_array_id];
    old_tail_y = yvalues[top_snake_array_id];

    /*increasing array if growing*/
    if (movement_type_move = true)
    top_snake_array_id += 1;

    /*resorting array*/
    for (int arrcount=top_snake_array_id; arrcount>=1; arrcount--) {
    xvalues[arrcount] = xvalues[arrcount-1];
    yvalues[arrcount] = yvalues[arrcount-1];
    xvalues[0] = new_x;
    yvalues[0] = new_y;

    /*redraw snakehead*/
    g.drawOval(xvalues[0]*5, yvalues[0]*5, 4, 4);

    /*redraw old snakehead (now body)*/
    g.fill3DRect(xvalues[1]*5, yvalues[1]*5, 4, 4, false);

    /*remove tail*/
    if (movement_type_move = true) {
    g.fill3DRect(old_tail_x*5, old_tail_y*5, 4, 4, false);

    /* Check to see if any elements cause game to fail*/

    /* Does the snake hit itself?*/
    for (int arrcount=1; arrcount<=top_snake_array_id; arrcount++) {
    if (xvalues[arrcount] == new_x && yvalues[arrcount] == new_y){
    still_in_race = false;
    /* Does the snake hit the walls?*/
    if (new_x > 99 || new_y > 99 || new_x < 1 || new_y < 1)
    still_in_race = false;

    /* Does the snake eat an item?*/
    if (new_x == current_item_x && new_y == current_item_y){
    /*create new random item*/
    Random myRandom = new Random();
    current_item_x = myRandom.nextInt(98)+1;
    current_item_y = myRandom.nextInt(98)+1;
    g.fill3DRect(current_item_x*5, current_item_y*5, 4, 4, false);

    /*must loop the above, to ensure that no existing fields are being taken..*/
    last_catch_number = move_number;

    } while (still_in_race = true);
    g.drawString("GAME OVER", 30, 30);


    /*added to retrieve key presses, though I have no idea how the program will catch on and use it..*/
    public boolean keyDown(Event evt, int key) {
    if (key == Event.LEFT) {
    direction = "left";
    return true;}
    else if (key == Event.RIGHT) {
    direction = "right";
    return true;}
    else if (key == Event.UP) {
    direction = "up";
    return true;}
    else if (key == Event.DOWN) {
    direction = "down";
    return true;}
    return false;

  2. #2
    Join Date
    Jun 2004
    Add this to your init()

    addKeyListener(new KeyListener() {

    public void keyTyped(KeyEvent e) {
    // TODO Auto-generated method stub


    public void keyPressed(KeyEvent e) {

    public void keyReleased(KeyEvent e) {
    // TODO Auto-generated method stub



    change your keydown method signature to

    public boolean keyDown(KeyEvent e)

    acccess the key pressed from the event

    BTW, no need to System.out in destroy. It doesn't happen like C++ does. Gets called on garbage collection.

    Have Fun!

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
HTML5 Development Center
Latest Articles
Questions? Contact us.
Web Development
Latest Tips
Open Source

   Development Centers

   -- Android Development Center
   -- Cloud Development Project Center
   -- HTML5 Development Center
   -- Windows Mobile Development Center

We have made updates to our Privacy Policy to reflect the implementation of the General Data Protection Regulation.