Jeremy Read

Smaller snake

June 22, 2008 on 4:51 pm | In Uncategorized | | Jeremy Read

Since we all love CompSci 101 so much, also as I’m testing out the source code highlighting plugin I present ’smaller snake’. You could write Snake a little smaller by storing head and apple in the same array as each other, or better yet with the snake. But I think this is just about as small as you can make it in java and still be ‘readable’. Apple probably shouldn’t be from fixed values or should be based on the container size, but I’m too lazy to fix it now.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
import java.util.*;
 
public class SnakeJPanel extends JPanel implements ActionListener, KeyListener, MouseListener  {
 
	private javax.swing.Timer t = new javax.swing.Timer(75,this);
	private LinkedList<Point> snake = new LinkedList(); 
	private Point apple = new Point( (int)(Math.random() * 45) * 10, (int)(Math.random() * 45) * 10);
	private Point dir = new Point(0,10);
 
	public SnakeJPanel() {
		t.start(); //To make stuff work.
		addMouseListener(this); 
		addKeyListener(this);
		snake.add(new Point(10,10));	
	}
 
	public void paintComponent(Graphics g) {
		super.paintComponent(g);
 
		g.fill3DRect(apple.x + 2,apple.y + 2, 5,5,true)	; //Draw apple
		for(int i = 0; i < snake.size(); i++){ //Draw snake and check self collision
			g.fill3DRect( snake.get(i).x,  snake.get(i).y , 10 , 10, true); 
			if( i != 0 && snake.get(i).x == snake.getFirst().x && snake.get(i).y == snake.getFirst().y){ t.stop(); }	//Check collision.	
		}
 
		if(snake.getFirst().x == apple.x && snake.getFirst().y == apple.y){ //Collision apple
			apple = new Point((int)(Math.random() * 45) * 10,(int)(Math.random() * 45) * 10);
			snake.add(snake.getLast());
		}
	}
 
	public void actionPerformed(ActionEvent e){ //Move snake
		snake.addFirst(new Point(snake.getFirst().x + dir.x, snake.getFirst().y + dir.y));
		snake.removeLast();
		repaint();
	}
	public void keyPressed(KeyEvent e){ //Change direction
		if(e.getKeyCode() == KeyEvent.VK_LEFT){dir = new Point(-10,0);}
		if(e.getKeyCode() == KeyEvent.VK_RIGHT){dir = new Point(10,0);}
		if(e.getKeyCode() == KeyEvent.VK_UP){dir = new Point(0,-10);}
		if(e.getKeyCode() == KeyEvent.VK_DOWN){dir = new Point(0,10);}
	}
 
	public void mousePressed(MouseEvent e){ //Java Hack
        super.requestFocus();
        repaint();
    }
    public void mouseClicked(MouseEvent e){}
    public void mouseEntered(MouseEvent e){}
    public void mouseExited(MouseEvent e){}
    public void mouseReleased(MouseEvent e){} 	
    public void keyReleased(KeyEvent e){} 
    public void keyTyped(KeyEvent e){}
}

2 Comments »

RSS feed for comments on this post. TrackBack URI

  1. [...] Tomorrow I should have XNA up as well. Then I’ll get to work on a simple tower defense game. Smaller Snake in [...]

    Pingback by Life Outside PLT1 » Smaller snake in C# — 4:55 pm — June 29, 2008 #

  2. [...] bit bigger than some of the others, but it’s using textures unlike the original smaller snake. 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 [...]

    Pingback by Life Outside PLT1 » XNA Smaller Snake — 9:48 pm — June 30, 2008 #

Leave a comment

You must be logged in to post a comment.

Powered by WordPress with Pool theme design by Borja Fernandez.
Entries and comments feeds. Valid XHTML and CSS. ^Top^