|It is currently Thu Mar 28, 2024 2:53 pm Login / Join Codemiles

Java

C/C++

PHP

C#

HTML

CSS

ASP

Javascript

JQuery

AJAX

XSD

Python

Matlab

R Scripts

Weka


behumanizer
Permanent LinkPosted: Wed Aug 17, 2011 8:17 am 
Offline
Newbie
Newbie
USER_AVATAR

Joined: Wed Aug 17, 2011 8:09 am
Posts: 0
Blog: View Blog (1)
Program below is working i just don't know what to do for the algorithm on keyevents for right,left,up and down
The code like y+=10; is just my assumption for the snake movement.....
I believe below the path.reset() code has to be change since stack is being use...
Can anyone help me program to run correctly?


/***
* PROVIDE THE MISSING CODE in the keyPressed() method
* in order to move the 'snake' around the interface
* by pressing:
*
* --> cursor keys
* --> CTRL-Z to 'undo' last move
* --> CTRL-Y to 'redo' past/last move
*/

package midterms;

import java.awt.*;
import java.awt.event.*;
import java.awt.geom.*;
import javax.swing.*;
import java.util.*;
import javax.swing.border.*;



public class Snakes extends JPanel implements KeyListener {
final static long serialVersionUID = 1;
final private int WIDTH = 600;
final private int HEIGHT = 400;

LinkedList<Point> snake = new LinkedList<Point>();
Stack<Point> undo = new Stack<Point>();
Stack<Point> redo = new Stack<Point>();
int x = WIDTH/2;
int y = HEIGHT/2;

/***
* KeyListener method - keyPressed()
*
* Complete the missing code in this method
*/
public void keyPressed(KeyEvent ke) {
int code = ke.getKeyCode();

switch( code ) {
default: // exit the method, i.e. do NOT repaint
return;

case KeyEvent.VK_Z:
if (ke.isControlDown()){
undo.add(snake.pop());
}
repaint();
return; // exit the method, i.e. do NOT add new point(X,Y)

case KeyEvent.VK_Y:
if (ke.isControlDown()){
redo.add(snake.peek());
}// redo past/last move ...
break;

case KeyEvent.VK_UP:
y-=10;
// update x and/or y ...
break;

case KeyEvent.VK_DOWN:
y+=10;

// update x and/or y ...
break;

case KeyEvent.VK_LEFT:
x-=10;
// update x and/or y ...
break;

case KeyEvent.VK_RIGHT:
x+=10;
// update x and/or y ...
break;

}

// .. then add the new x,y coordinate to the list
snake.add( new Point(x,y) );
if(snake.size()==51){
snake.remove(0);
}

repaint();
}

/***
* just a constructor
*/
public Snakes() {
snake.add( new Point(x,y) );

this.setPreferredSize( new Dimension(600, 400) );
this.setBackground( Color.WHITE );
}


/***
* 'other' KeyListener methods
*/
public void keyTyped(KeyEvent ke) { /* NOT USED - DON'T REMOVE */ }
public void keyReleased(KeyEvent ke) { /* NOT USED - DON'T REMOVE */ }


/***
* paintComponent - overloaded JPanel method
*/
Path2D.Double path = new Path2D.Double();
public void paintComponent(Graphics g) {
super.paintComponent( g );

Graphics2D canvas = (Graphics2D)g;
Point p;

canvas.setRenderingHint( RenderingHints.KEY_ANTIALIASING, RenderingHints.VALUE_ANTIALIAS_ON );
canvas.setStroke( new BasicStroke(5.0f, BasicStroke.CAP_ROUND, BasicStroke.JOIN_ROUND) );

path.reset();

// (re)copy the points in 'snake' to create a 'path'
p = snake.get(0);
path.moveTo( p.x, p.y );
for(int i=1; i<snake.size(); i++) {
p = snake.get(i);
path.lineTo( p.x, p.y );
}

// draw the path
canvas.setColor( Color.BLUE );
canvas.draw( path );
}

/***
* main() - program execution starts here!
*/
public static void main(String[] args) {
Snakes myProgram = new Snakes();

JFrame frame = new JFrame("Snakes!");
frame.addKeyListener( myProgram );
frame.setContentPane( myProgram );
frame.pack();
frame.setDefaultCloseOperation( JFrame.EXIT_ON_CLOSE );
frame.setLocationRelativeTo( null );
frame.setResizable( false );
frame.setVisible( true );
}
}


Top
 Profile  
 


All times are UTC [ DST ]


cron






Powered by phpBB © 2000, 2002, 2005, 2007 phpBB Group
All copyrights reserved to codemiles.com 2007-2011
mileX v1.0 designed by codemiles team
Codemiles.com is a participant in the Amazon Services LLC Associates Program, an affiliate advertising program designed to provide a means for sites to earn advertising fees by advertising and linking to Amazon.com