Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
Found in an email attachment from 2006.
import java.applet.AudioClip;
import java.awt.Color;
import java.awt.Font;
import java.awt.FontMetrics;
import java.awt.Graphics;
import java.awt.Image;
import java.awt.Point;
import java.awt.Rectangle;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.KeyAdapter;
import java.awt.event.KeyEvent;
import java.awt.geom.Point2D;
import java.awt.image.BufferedImage;
import java.net.MalformedURLException;
import java.net.URL;
import java.util.ArrayList;
import javax.swing.ImageIcon;
import javax.swing.JApplet;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JOptionPane;
import javax.swing.Timer;
/*
* Snake.java
*
* Created on November 16, 2006, 6:35 PM
*
* To change this template, choose Tools | Template Manager
* and open the template in the editor.
*/
/**
*
* @author steven
*/
public class Snake extends JApplet{
public static BufferedImage toBuffered(Image i,int w,int h){
BufferedImage tmp = new BufferedImage(w,h,1);
tmp.getGraphics().drawImage(i,0,0,null);
return tmp;
}
Image[] backgrounds = new Image[12];
FontMetrics font = null;
float color = 0;
int dir = 0;
int speed = 1;
public static final int UP = 3 , DOWN = 1 , LEFT = 2 , RIGHT = 0 , SNAKESIZE = 10;
static int length = 2;
int fSize = 24;
int frame = 0;
int quoteNum=0;
int points = 0;
static ArrayList<Point> tail = new ArrayList<Point>();
static ArrayList<Letter> letters = new ArrayList<Letter>();
BufferedImage [] bodyImg = new BufferedImage[8];// 0 and 5 aren't used
BufferedImage [] headImg= new BufferedImage[4];
BufferedImage [] tailImg= new BufferedImage[4];
BufferedImage [] bonusImg= new BufferedImage[2];
String[] quotes = {"Quote 1 ","Quote 2" ,"Quote 3"};
String[] auths = {"Auth 1","Auth 2","Auth 3"};
BufferedImage bi = new BufferedImage(500,500,1);
boolean active= false;
Image background;
Point2D.Double bgShift=new Point2D.Double(0,0);
double bgDir = 0;
Color bg = new Color(0,128,255);
static AudioClip bgMusic = null;
Bonus a = new Bonus(true,(int)(Math.random()*500/SNAKESIZE),(int)(Math.random()*500/SNAKESIZE));
Bonus b = new Bonus(false,(int)(Math.random()*500/SNAKESIZE),(int)(Math.random()*500/SNAKESIZE));
public int getDir(int sIdx,int eIdx){
int xs = tail.get(sIdx).x - tail.get(eIdx).x;
int ys = tail.get(sIdx).y - tail.get(eIdx).y;
if(xs >0)return RIGHT;
else if(xs <0)return LEFT;
else if(ys <0)return UP;
else return DOWN;
}
public class Bonus{
boolean good;
int x , y;
boolean active = true;
public Bonus(boolean b , int i , int i2){
x=i;y=i2;
good = b;
}
public boolean contains(Point p){
if(p.x == x && p.y==y)return true;
return false;
}
public boolean active(){
return active;
}
public void use(){
if(!active)return;
active=false;
if(good)length=2*length/3+1;
else length+=length/2;
}
}
public class Letter{
char c;
int x , y;
boolean alive = true;
int deadTime = 0;
int r , g , b;
public Letter(char ch , int i , int i2){
c = ch;
x = i;
y = i2;
}
public void move(){
// x+=Math.random()>.5?1:-1;
// y+=Math.random()>.5?1:-1;
x%=500;
y%=500;
}
public boolean contains(Point p){
if(new Rectangle((p.x)*SNAKESIZE,p.y*SNAKESIZE,SNAKESIZE,SNAKESIZE).intersects(
new Rectangle(x-SNAKESIZE/2,y-font.getHeight()-SNAKESIZE/2,font.charWidth(c)+SNAKESIZE,font.getHeight()+SNAKESIZE))) return true;
return false;
}
public void paint(Graphics g){
if(!alive){
g.setColor(new Color(r,this.g,b,255-deadTime*5));
g.fillOval(x-deadTime,y-deadTime,deadTime*2,deadTime*2);
deadTime++;
if(deadTime==51){
letters.remove(this);
}
}else{
g.setColor(new Color(
(float)(.5+.5*Math.sin((x+y+frame)/10.0)),
(float)(.5+.5*Math.sin((x+y+frame)/10.0)),
(float)(.5+.5*Math.sin((x+y+frame)/10.0))
));
g.drawString(""+c,x,y);
}
}
public void kill(){
if(alive){
color+=.1;
color=Math.min(1,color);
points+=(int)(100*color);
if(color<.5){
g = (int)((color)/.5*256);
r = 255;
b = 0;
}else{
b = (int)((color-.5)/.5*255);
r = 255;
g = 255;
}
alive = false;
length++;
for(int i = 0 ; i < letters.size() ; i++)
if(letters.get(i).alive ) return;
active = false;
setString(quotes[++quoteNum],auths[quoteNum]);
}
}
}
public static double fix(double angle){
while(angle<0)angle+=Math.PI*2;
while(angle>Math.PI*2)angle-=Math.PI*2;
return angle;
}
public static double arcDistance(double a1 , double a2){
a1 = fix(a1);
a2 = fix(a2);
if(a1==a2)return 0;
double ang1= Math.toDegrees(a1);//350
double ang2= Math.toDegrees(a2);//20
double a1q = 0;
if(ang1<90) a1q = 0;
else if (ang1<180) a1q = 1;
else if (ang1<270) a1q = 2;
else a1q = 3;
double a2q = 0;
if(ang2<90) a2q = 0;
else if (ang2<180) a2q = 1;
else if (ang2<270) a2q = 2;
else a2q = 3;
if(a1q==a2q) return Math.toRadians(ang2-ang1);
if(Math.abs(a1q-a2q)==1) return a2-a1;
if(Math.abs(a1q-a2q)==3){
if(ang1<ang2){
return -Math.toRadians((360-ang2)+ang1);
}else
return Math.toRadians((360-ang1)+ang2);
}
double dist = ang2 - ang1;
if(Math.abs(dist)<=180) return Math.toRadians(dist);
return Math.toRadians(180-(dist-180));
}
boolean crazyLetters;
boolean movingBackground;
boolean colorLetters;
public void init(){
try {
BufferedImage input;
do{
input = toBuffered(getImage(new URL(this.getCodeBase() + "Snake.png")),40,40);
}while(input.getRGB(0,0)==-16777216);
for(int i = 0 ; i < 4 ; i++){
headImg[i]=input.getSubimage(SNAKESIZE*3,i*SNAKESIZE,SNAKESIZE,SNAKESIZE);
tailImg[i]=input.getSubimage(0,i*SNAKESIZE,SNAKESIZE,SNAKESIZE);
bodyImg[i]=input.getSubimage(SNAKESIZE*2,i*SNAKESIZE,SNAKESIZE,SNAKESIZE);
bodyImg[i+4]=input.getSubimage(SNAKESIZE,i*SNAKESIZE,SNAKESIZE,SNAKESIZE);
}
bonusImg[0] = bodyImg[2];
bonusImg[1] = bodyImg[3];
for(int i = 0 ; i < 12 ; i++)
backgrounds[i] = getImage(new URL(this.getCodeBase() + (i +".jpg")));
} catch (MalformedURLException ex) {
JOptionPane.showMessageDialog(null,ex.toString());
ex.printStackTrace();
}
setString("","W");
new Timer(10000,new ActionListener(){
public void actionPerformed(ActionEvent ae){
a = new Bonus(true,(int)(Math.random()*500/SNAKESIZE),(int)(Math.random()*500/SNAKESIZE));
b = new Bonus(false,(int)(Math.random()*500/SNAKESIZE),(int)(Math.random()*500/SNAKESIZE));
}
}).start();
setSize(500,500);
new Timer(50,new ActionListener(){public void actionPerformed(ActionEvent ae){repaint();}}).start();
addKeyListener(new KeyAdapter(){
public void keyPressed(KeyEvent ke){
if(!active)bgMusic.play();
switch(ke.getKeyCode()){
case KeyEvent.VK_UP: case KeyEvent.VK_W: dir = UP; break;
case KeyEvent.VK_DOWN: case KeyEvent.VK_S: dir = DOWN; break;
case KeyEvent.VK_LEFT: case KeyEvent.VK_A: dir = LEFT; break;
case KeyEvent.VK_RIGHT: case KeyEvent.VK_D: dir = RIGHT; break;
}
active = true;
}
});
new Timer(50,new ActionListener(){
public void actionPerformed(ActionEvent ae){
if(active){
if(dir == UP) bgDir+=arcDistance(bgDir,Math.PI/2)/10.0;
if(dir == DOWN) bgDir+=arcDistance(bgDir,3*Math.PI/2)/10.0;
if(dir == LEFT) bgDir+=arcDistance(bgDir,Math.PI)/10.0;
if(dir == RIGHT) bgDir+=arcDistance(bgDir,0)/10.0; }else bgDir+=.1;
bgShift.x+=2.5*Math.cos(bgDir);
bgShift.y-=2.5*Math.sin(bgDir);
bgShift.x+=background.getWidth(null);
bgShift.x%=background.getWidth(null);
bgShift.y+=background.getHeight(null);
bgShift.y%=background.getHeight(null);
}
}).start();
}
public void setString(String quote,String auth){
if(bgMusic!=null)bgMusic.stop();
bgMusic = getAudioClip(getCodeBase(),"music"+(int)(Math.random()*2%2)+".wav");
int img = (int)(Math.random()*12);
background = backgrounds[img];
getAudioClip(getCodeBase(), "level.wav").play();
quoteNum--;
quote="I came here tonight because when you realize you want to spend the rest of your life with somebody, you want the rest of your life to start as soon as possible.";
auth ="Billy Crystal";
while(letters.size()!=0)letters.remove(0);
String[] words = quote.split(" ");
int x = 50;
int y = 50;
for(int i = 0 ; i < words.length ; i++) {
x += fSize*2;
if(x + words[i].length()*fSize>450){
x = 50;
y += fSize*1;
}
for(int l = 0 ; l < words[i].length() ; l++){
letters.add(new Letter(words[i].charAt(l),x,y));
x+=fSize;
}
}
y+=60;
x=0;
words = auth.split(" ");
for(int i = 0 ; i < words.length ; i++) {
x += fSize*3;
if(x + words[i].length()*fSize>450){
x = 50;
y += fSize*2;
}
for(int l = 0 ; l < words[i].length() ; l++){
letters.add(new Letter(words[i].charAt(l),x,y));
x+=fSize;
}
}
tail.clear();
tail.add(new Point(250/SNAKESIZE-1,500/SNAKESIZE));
tail.add(new Point(250/SNAKESIZE-1,500/SNAKESIZE-1));
}
public void paint(Graphics g2){
frame++;
color-=.02;
color=Math.max(0,color);
if(font==null)font = g2.getFontMetrics();
Graphics g = bi.getGraphics();
g.setFont(new Font("",0,fSize));
for(int x = -background.getWidth(null)+(int)bgShift.x ; x <500 ; x+=background.getWidth(null))
for(int y = -background.getHeight(null)+(int)bgShift.y; y <500 ; y+=background.getHeight(null))
g.drawImage(background,x,y,null);
int gr = 0, bl=0,re=0;
if(color<.5){
gr = (int)((color)/.5*256);
re = 255;
bl = 0;
}else{
bl = (int)((color-.5)/.5*255);
re = 255;
gr = 255;
}
g.setColor(new Color(re,gr,bl,128));
g.fillRect(0,0,100+(int)(color*400),fSize);
g.setColor(Color.black);
g.drawString("Points:"+points,0,fSize);
if(a.active())g.drawImage(bonusImg[1],a.x*SNAKESIZE,a.y*SNAKESIZE,null);
if(b.active())g.drawImage(bonusImg[0],b.x*SNAKESIZE,b.y*SNAKESIZE,null);
for(int i = 0 ; i < letters.size() ; i++){
Letter l = letters.get(i);
l.paint(g);
if(active)l.move();
}
if(active){
Point last = new Point(tail.get(tail.size()-1));
if(dir == UP)last.y-=speed;
if(dir == DOWN)last.y+=speed;
if(dir == LEFT)last.x-=speed;
if(dir == RIGHT)last.x+=speed;
last.x+=500/SNAKESIZE;
last.y+=500/SNAKESIZE;
last.x%=500/SNAKESIZE;
last.y%=500/SNAKESIZE;
if(tail.size()>length){tail.remove(0);}
if(tail.size()>length){tail.remove(0);}
tail.add(last);
paintSnake(g);
for(int i = 0 ; i < letters.size() ; i++){
if(a.contains(last)) a.use();
if(b.contains(last)) b.use();
if(letters.get(i).contains(last)){
letters.get(i).kill();
}
}
}else {
paintSnake(g);
}
/*
g.setColor(Color.white);
Point p = new Point(tail.get(tail.size()-1));
g.drawRect((p.x)*SNAKESIZE,p.y*SNAKESIZE,SNAKESIZE,SNAKESIZE);
for(int i = 0 ; i < letters.size() ; i++){
Letter l = letters.get(i);
Rectangle2D r = g.getFontMetrics().getStringBounds(""+l.c,g);
g.drawRect(l.x+(int)r.getX(),l.y+(int)r.getY(),(int)r.getWidth(),(int)r.getHeight());
}
/**/
g2.drawImage(bi,0,0,null);
}
public void paintSnake(Graphics g){
for(int i = 0 ; i < tail.size() ; i++){
if(i==0){
int dir =getDir(1,0);
g.drawImage(tailImg[dir],SNAKESIZE*tail.get(i).x,SNAKESIZE*tail.get(i).y,null);
}else if (i+1 == tail.size()){
int dir =getDir(i , i-1);
g.drawImage(headImg[dir],SNAKESIZE*tail.get(i).x,SNAKESIZE*tail.get(i).y,null);
}else{
int d1 = getDir(i-1 , i);
int d2 = getDir(i , i+1);
if((d1==RIGHT && d2==RIGHT) || (d1==LEFT && d2==LEFT))
g.drawImage(bodyImg[0],SNAKESIZE*tail.get(i).x,SNAKESIZE*tail.get(i).y,null);
else if((d1==UP && d2==UP) || (d1==DOWN && d2==DOWN))
g.drawImage(bodyImg[1],SNAKESIZE*tail.get(i).x,SNAKESIZE*tail.get(i).y,null);
else if((d2==LEFT && d1== DOWN)|| (d1==RIGHT && d2 == UP))
g.drawImage(bodyImg[7],SNAKESIZE*tail.get(i).x,SNAKESIZE*tail.get(i).y,null);
else if((d1==LEFT && d2== UP) || (d1==DOWN && d2==RIGHT))
g.drawImage(bodyImg[4],SNAKESIZE*tail.get(i).x,SNAKESIZE*tail.get(i).y,null);
else if((d1==UP && d2== RIGHT) || (d2==DOWN && d1==LEFT))
g.drawImage(bodyImg[5],SNAKESIZE*tail.get(i).x,SNAKESIZE*tail.get(i).y,null);
else if((d1==UP && d2== LEFT) || (d2==DOWN && d1==RIGHT))
g.drawImage(bodyImg[6],SNAKESIZE*tail.get(i).x,SNAKESIZE*tail.get(i).y,null);
g.setColor(Color.black);
g.drawString(d1+":"+d2,100,100);
}
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment