/* built with Studio Sketchpad:
* https://sketchpad.cc
*
* observe the evolution of this sketch:
* https://playgramming.sketchpad.cc/sp/pad/view/ro.GkJjoghz8Zs/rev.777
*
* authors:
* Devon Scott-Tunkin
* license (unless otherwise specified):
* creative commons attribution-share alike 3.0 license.
* https://creativecommons.org/licenses/by-sa/3.0/
*/
/* @pjs preload="/static/uploaded_resources/p.9705/sprites_map_claudius.png"; */
class Knight
{
PImage image;
PVector position;
int frameRow;
int frameColumn;
float frameTimer;
PVector velocity;
float speed;
}
int left = 0;
int right = 0;
int up = 0;
int down = 0;
Knight claudius = new Knight();
void setup() { // this is run once.
size(300, 300);
claudius.image = loadImage("/static/uploaded_resources/p.9705/sprites_map_claudius.png");
claudius.position = new PVector(100, 100);
claudius.frameTimer = 1.0;
claudius.frameRow = 0;
claudius.speed = 3;
}
void draw() { // this is run repeatedly.
background(255);
claudius.frameTimer += 0.1; // 0.1 is the framerate or speed of animation.
float velocityX = claudius.speed * (right - left);
claudius.position.x += velocityX;
float velocityY = claudius.speed * (down - up);
claudius.position.y += velocityY;
if (claudius.frameTimer >= 6)
{
claudius.frameTimer = 1;
}
if (velocityX == 0 && velocityY == 0) {
claudius.frameTimer = 0;
}
PImage frameImage = claudius.image.get((int)claudius.frameTimer * 32, claudius.frameRow * 64, 32, 64);
image(frameImage, claudius.position.x, claudius.position.y);
}
void keyPressed() {
if (keyCode == UP) {
claudius.frameRow = 2;
up = 1;
}
if (keyCode == DOWN) {
claudius.frameRow = 0;
down = 1;
}
if (keyCode == RIGHT) {
claudius.frameRow = 3;
right = 1;
}
if (keyCode == LEFT) {
claudius.frameRow = 1;
left = 1;
}
}
void keyReleased() {
if (keyCode == RIGHT) {
right = 0;
}
if (keyCode == LEFT) {
left = 0;
}
if (keyCode == UP) {
up = 0;
}
if (keyCode == DOWN) {
down = 0;
}
}