src/GameCourt.java
/**
     * This method is called every time the timer defined in the constructor triggers.
     */
This method is called every time the timer defined in the constructor triggers.
This method is called every time the timer defined in the constructor triggers .
void tick() {
        if (playing) {
            Iterator<Snakeunit> iter = snake.iterator();
            iter.next();
            int x = 0;
            int y = 0;
            for (Snakeunit a : snake) {
                if (first) {
                    x = a.getPx();
                    a.setPx(x + SQUARE_VELOCITY);
                } else {
                    if (a.equals(head)) {
                        x = a.getPx();
                        y = a.getPy();
                        a.move();
                    } else {
                        int t = a.getPx();
                        int v = a.getPy();
                        a.setPx(x);
                        a.setPy(y);
                        x = t;
                        y = v;
                    }
                }
            }
            first = false;
            for (int i = 0; i < food.length; i++) {
                Food a = new Food(food[i][0], food[i][1], COURT_WIDTH, COURT_HEIGHT);
                if (head.intersects(a)) {
                    food = createFood(3);
                    this.scorecount++;
                    if (scorecount > highscore) {
                        status.setText("New High Score!");
                    }
                    score.setText("Score: " + scorecount);
                    snakelength++;
                    int[] val = getDetails();
                    Snakeunit tail = new Snakeunit(val[0], val[1], 
                            COURT_WIDTH, COURT_HEIGHT, Color.GREEN);
                    snake.add(tail);      
                }
            }
            for (Snakeunit b : snake) {
                if (b.equals(head)) {
                    continue;
                }
                if (head.intersects(b)) {
                    hitWall = true;
                }
            }
            if (hitWall) {
                playing = false;
                Writer a = new Writer(name.getText(), scorecount);
                a.scoreWriter();
                int rs = JOptionPane.showInternalConfirmDialog(null,
                        "Game Over! Would you like to play again?", "Game Over",
                        JOptionPane.YES_NO_OPTION, JOptionPane.INFORMATION_MESSAGE);                
                if (rs == JOptionPane.YES_OPTION) {
                    reset();
                } else {
                    System.exit(0);
                }
            }
            if (head.hitWall()) {
                hitWall = true;
            }
            repaint();
        }
    }
void tick ( ) { if ( playing ) { Iterator < Snakeunit > iter = snake . iterator ( ) ; iter . next ( ) ; int x = 0 ; int y = 0 ; for ( Snakeunit a : snake ) { if ( first ) { x = a . getPx ( ) ; a . setPx ( x + SQUARE_VELOCITY ) ; } else { if ( a . equals ( head ) ) { x = a . getPx ( ) ; y = a . getPy ( ) ; a . move ( ) ; } else { int t = a . getPx ( ) ; int v = a . getPy ( ) ; a . setPx ( x ) ; a . setPy ( y ) ; x = t ; y = v ; } } } first = false ; for ( int i = 0 ; i < food . length ; i ++ ) { Food a = new Food ( food [ i ] [ 0 ] , food [ i ] [ 1 ] , COURT_WIDTH , COURT_HEIGHT ) ; if ( head . intersects ( a ) ) { food = createFood ( 3 ) ; this . scorecount ++ ; if ( scorecount > highscore ) { status . setText ( "New High Score!" ) ; } score . setText ( "Score: " + scorecount ) ; snakelength ++ ; int [ ] val = getDetails ( ) ; Snakeunit tail = new Snakeunit ( val [ 0 ] , val [ 1 ] , COURT_WIDTH , COURT_HEIGHT , Color . GREEN ) ; snake . add ( tail ) ; } } for ( Snakeunit b : snake ) { if ( b . equals ( head ) ) { continue ; } if ( head . intersects ( b ) ) { hitWall = true ; } } if ( hitWall ) { playing = false ; Writer a = new Writer ( name . getText ( ) , scorecount ) ; a . scoreWriter ( ) ; int rs = JOptionPane . showInternalConfirmDialog ( null , "Game Over! Would you like to play again?" , "Game Over" , JOptionPane . YES_NO_OPTION , JOptionPane . INFORMATION_MESSAGE ) ; if ( rs == JOptionPane . YES_OPTION ) { reset ( ) ; } else { System . exit ( 0 ) ; } } if ( head . hitWall ( ) ) { hitWall = true ; } repaint ( ) ; } }
This method is called every time the timer defined in the constructor triggers.
This method is called every time the timer defined in the constructor triggers .
// advance the square and snitch in their current direction. // update the display