Skip to content

Commit aaef446

Browse files
author
Sarah Sicard
committed
added code for squirrels and sample code
1 parent d769455 commit aaef446

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

100 files changed

+2563
-1
lines changed

BasicGraphics

-1
This file was deleted.

sample_code/basicflyer/Falcon.java

+59
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,59 @@
1+
/*
2+
* To change this license header, choose License Headers in Project Properties.
3+
* To change this template file, choose Tools | Templates
4+
* and open the template in the editor.
5+
*/
6+
package basicflyer;
7+
8+
import basicgraphics.Sprite;
9+
import basicgraphics.SpriteCollisionEvent;
10+
import basicgraphics.SpriteComponent;
11+
import basicgraphics.images.Picture;
12+
import java.awt.Dimension;
13+
import java.io.IOException;
14+
15+
/**
16+
*
17+
* @author sbrandt
18+
*/
19+
public class Falcon extends Sprite {
20+
public Picture initialPic;
21+
public void init(SpriteComponent sc) throws IOException {
22+
initialPic = new Picture("mfalcon.png");
23+
setPicture(initialPic);
24+
Dimension d = sc.getSize();
25+
setX(d.width/2);
26+
setY(d.height/2);
27+
setVelX(Math.cos(heading));
28+
setVelY(Math.sin(heading));
29+
this.sc = sc;
30+
sc.addSprite(this);
31+
}
32+
SpriteComponent sc;
33+
double heading = 0;
34+
boolean update;
35+
@Override
36+
public void preMove() {
37+
if(update) {
38+
setVelX(Math.cos(heading));
39+
setVelY(Math.sin(heading));
40+
setPicture(initialPic.rotate(heading));
41+
update = false;
42+
}
43+
}
44+
@Override
45+
public void processEvent(SpriteCollisionEvent se) {
46+
if (se.xlo) {
47+
setX(sc.getSize().width-getWidth());
48+
}
49+
if (se.xhi) {
50+
setX(0);
51+
}
52+
if (se.ylo) {
53+
setY(sc.getSize().height-getHeight());
54+
}
55+
if (se.yhi) {
56+
setY(0);
57+
}
58+
}
59+
}

sample_code/basicflyer/Flyer.java

+54
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,54 @@
1+
/*
2+
* To change this license header, choose License Headers in Project Properties.
3+
* To change this template file, choose Tools | Templates
4+
* and open the template in the editor.
5+
*/
6+
package basicflyer;
7+
8+
import basicgraphics.BasicFrame;
9+
import basicgraphics.SpriteComponent;
10+
import java.awt.Dimension;
11+
import java.awt.event.KeyAdapter;
12+
import java.awt.event.KeyEvent;
13+
import java.awt.event.MouseAdapter;
14+
import java.awt.event.MouseEvent;
15+
import java.awt.event.MouseListener;
16+
import java.io.IOException;
17+
18+
/**
19+
*
20+
* @author sbrandt
21+
*/
22+
public class Flyer {
23+
public static void main(String[] args) throws IOException {
24+
BasicFrame bf = new BasicFrame("Flyer");
25+
SpriteComponent sc = new SpriteComponent();
26+
sc.setPreferredSize(new Dimension(800,400));
27+
bf.add("Flyer",sc,0,0,1,1);
28+
bf.show();
29+
final Falcon f = new Falcon();
30+
final double INCR = Math.PI*2/100.0;
31+
bf.addKeyListener(new KeyAdapter() {
32+
@Override
33+
public void keyPressed(KeyEvent ke) {
34+
if(ke.getKeyCode() == KeyEvent.VK_RIGHT) {
35+
f.heading += INCR;
36+
f.update = true;
37+
} else if(ke.getKeyCode() == KeyEvent.VK_LEFT) {
38+
f.heading -= INCR;
39+
f.update = true;
40+
} else if(ke.getKeyChar() == ' ') {
41+
Plasma pl = new Plasma();
42+
pl.setVelX(f.getVelX()*2);
43+
pl.setVelY(f.getVelY()*2);
44+
pl.setX(f.getX()+f.getWidth()*3./7.);
45+
pl.setY(f.getY()+f.getHeight()*3./6.5);
46+
pl.init(sc);
47+
sc.addSprite(pl);
48+
}
49+
}
50+
});
51+
f.init(sc);
52+
sc.start(0,10);
53+
}
54+
}

sample_code/basicflyer/Plasma.java

+42
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,42 @@
1+
/*
2+
* To change this license header, choose License Headers in Project Properties.
3+
* To change this template file, choose Tools | Templates
4+
* and open the template in the editor.
5+
*/
6+
package basicflyer;
7+
8+
import basicgraphics.BasicFrame;
9+
import basicgraphics.Sprite;
10+
import basicgraphics.SpriteCollisionEvent;
11+
import basicgraphics.SpriteComponent;
12+
import basicgraphics.images.Picture;
13+
import java.awt.Color;
14+
import java.awt.Graphics;
15+
import java.awt.Image;
16+
17+
/**
18+
*
19+
* @author sbrandt
20+
*/
21+
class Plasma extends Sprite {
22+
23+
static Picture makeBall(Color color,int size) {
24+
Image im = BasicFrame.createImage(size, size);
25+
Graphics g = im.getGraphics();
26+
g.setColor(color);
27+
g.fillOval(0, 0, size, size);
28+
return new Picture(im);
29+
}
30+
31+
public void init(SpriteComponent sc) {
32+
setPicture(makeBall(Color.red,10));
33+
}
34+
35+
@Override
36+
public void processEvent(SpriteCollisionEvent se) {
37+
if(se.sprite2 != null) {
38+
} else {
39+
setActive(false);
40+
}
41+
}
42+
}

0 commit comments

Comments
 (0)