Could someone show me how to implement a movement code for the snake? package Fi
ID: 3626793 • Letter: C
Question
Could someone show me how to implement a movement code for the snake?package FinalProject;
import java.awt.BasicStroke;
import java.awt.Color;
import java.awt.Graphics;
import java.awt.Graphics2D;
import java.awt.Paint;
import java.awt.Shape;
import javax.swing.JFrame;
import javax.swing.JPanel;
import javax.swing.Timer;
public class Board extends JFrame{
public Board(){
}
public void paint(Graphics g) {
Graphics2D g2d = (Graphics2D)g;
g2d.setStroke(new BasicStroke (3));
g2d.setColor(Color.red);
g2d.drawRect(49, 49, 501, 401);
g2d.setColor(Color.black);
g2d.fillRect(50, 50, 500, 400);
g2d.drawRect(580, 50, 100, 40);
for(int i = 300; i <550; i+=10)
DrawSnake(g2d, i, 250);
}
public static void DrawSnake(Graphics2D g2, int x, int y){
g2.setColor(Color.GREEN);
g2.fillOval(x,y, 10, 10);
System.out.println("DRAWING DA SNAKE");
}
public static void main(String[] args) {
// TODO Auto-generated method stub
Board f = new Board();
f.setTitle("Snake");
f.setSize(700, 500);
f.setVisible(true);
f.setResizable(false);
}
}
Explanation / Answer
Look at this link you can find a source code for a simple snake implementation. the function public void keyPressed(final KeyEvent e) listens to key strokes and move the snake on the Panel. http://www.koders.com/java/fidC9A7B46A2D1F7F47AD338B1609DA4465F320DDF8.aspx?s=304
Related Questions
Hire Me For All Your Tutoring Needs
Integrity-first tutoring: clear explanations, guidance, and feedback.
Drop an Email at
drjack9650@gmail.com
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.