|
This demo illustrates a number of Ucigame features, including
sound and sprite rotation.
The art in images/char9.png and
images/blocks1.png comes from
Ari Feldman's marvelous
SpriteLib
collection of sprites.
Keyboard controls:
|
|||||||||||||||||||||
|
FlippedOut.java
import ucigame.*;
public class FlippedOut extends Ucigame
{
Sprite cat;
Sprite block;
Sprite tiledBackground;
Sound violinSolo, pianoSolo;
Sound bump;
int catX, catY; // cat status variables
int catSpeed = 2;
int catRotation = 0;
boolean headedRight = true;
boolean upsideDown = false;
int blockRotation = 0;
public void setup()
{
window.size(250, 250);
window.title("Flipped Out");
framerate(30);
canvas.background(50, 150, 150);
cat = makeSprite(105, 60);
cat.addFrames(getImage("images/char9.png", 0),
0, 70, // cat has six frames
130, 70,
260, 70,
390, 70,
0, 203,
130, 203);
catX = 0;
catY = canvas.height()/2 - cat.height()/2;
cat.position(catX, catY);
cat.framerate(10);
block = makeSprite(32, 32);
block.addFrame(getImage("images/blocks1.png", 0), 240, 138);
block.position(200, 118);
tiledBackground = makeSprite(getImage("images/tilableBackground.png"), 250, 50);
tiledBackground.position(0, 200);
bump = getSound("sounds/Windows XP Battery Low.wav");
violinSolo = getSound("sounds/paganini-caprice13.wav");
pianoSolo = getSound("sounds/Schumann.mp3");
violinSolo.loop();
}
public void draw()
{
canvas.clear();
tiledBackground.draw();
// Move cat, reversing direction at edge
catX = catX + catSpeed;
if (catX + cat.width() >= canvas.width())
{
headedRight = false;
catSpeed = -catSpeed;
}
else if (catX <= 0)
{
headedRight = true;
catSpeed = -catSpeed; // change negative to positive
}
// if cat hits the top, flip it upside down
if (catY <= 0)
upsideDown = true;
// If cat is up in the air, move it down
if (catY < canvas.height() - cat.height())
catY = catY + 1;
else
upsideDown = false;
cat.position(catX, catY);
cat.checkIfCollidesWith(block);
if (cat.collided())
bump.play();
block.rotate(blockRotation);
block.draw();
if (!headedRight)
cat.flipHorizontal();
if (upsideDown)
cat.flipVertical();
cat.rotate(catRotation);
cat.draw();
}
// UP give cat a boost
// R rotate cat
// B rotate block
// SPACE restore cat and block orientation
// PAUSE stops the violin solo
// HOME starts the violin solo from the beginning with loop
// SHIFT starts the violin solo from the beginning, plays once
// 1 stops the piano solo
// 2 starts the piano solo from the beginning with loop
// 3 starts the piano solo from the beginning, plays once
public void onKeyPress()
{
if (keyboard.key() == keyboard.UP)
catY = catY - 20;
if (keyboard.key() == keyboard.R)
catRotation = catRotation + 5;
if (keyboard.key() == keyboard.B)
blockRotation = blockRotation + 5;
if (keyboard.key() == keyboard.SPACE)
{
catRotation = 0;
blockRotation = 0;
}
if (keyboard.key() == keyboard.PAUSE)
violinSolo.stop();
if (keyboard.key() == keyboard.HOME)
violinSolo.loop();
if (keyboard.key() == keyboard.SHIFT)
violinSolo.play();
if (keyboard.key() == keyboard.K1)
pianoSolo.stop();
if (keyboard.key() == keyboard.K2)
pianoSolo.loop();
if (keyboard.key() == keyboard.K3)
pianoSolo.play();
}
}
|
|||||||||||||||||||||