Skip to content

Commit 14efebb

Browse files
committed
adding alternate version of sprite example
1 parent d2c19f0 commit 14efebb

4 files changed

Lines changed: 45 additions & 0 deletions

File tree

-3.68 KB
Binary file not shown.
Lines changed: 45 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,45 @@
1+
/**
2+
* Sprite2 (Teddy)
3+
* by James Patterson.
4+
*
5+
* Demonstrates loading and displaying a transparent GIF image.
6+
* This alternate version shows a sky image in the background.
7+
*/
8+
9+
PImage teddy;
10+
PImage sky;
11+
12+
float xpos;
13+
float ypos;
14+
float drag = 30.0;
15+
16+
void setup() {
17+
size(200, 200);
18+
teddy = loadImage("teddy.gif");
19+
sky = loadImage("sky.jpg");
20+
xpos = width/2;
21+
ypos = height/2;
22+
// resize the background image so that it fills the screen
23+
if (sky.width != width || sky.height != height) {
24+
sky.resize(width, height);
25+
}
26+
}
27+
28+
void draw() {
29+
background(sky);
30+
31+
float difx = mouseX - xpos-teddy.width/2;
32+
if (abs(difx) > 1) {
33+
xpos = xpos + difx/drag;
34+
xpos = constrain(xpos, 0, width-teddy.width);
35+
}
36+
37+
float dify = mouseY - ypos-teddy.height/2;
38+
if (abs(dify) > 1 ) {
39+
ypos = ypos + dify/drag;
40+
ypos = constrain(ypos, 0, height-teddy.height);
41+
}
42+
43+
// Display the sprite at the position xpos, ypos
44+
image(teddy, xpos, ypos);
45+
}
12.9 KB
Loading
746 Bytes
Loading

0 commit comments

Comments
 (0)