xxxxxxxxxx
226
// what are all of the things that are in this sketch?
// two sprites - Sprite class
// two background images - Background class
// state of game: win, lose, playmode Game class
class Sprite {
constructor(images, x, y) {
// images are already loaded in preload
// or decision to make: are the images preloaded?
// or does the constructor load them?
this.images = images;
this.index = 0;
// array of images
// index of the selected image
// do you need to scale the image?
this.x = 0;
this.y = 0;
}
show() {
// show the selected image
image(this.images[this.index], this.x, this.y);
}
update() {
// increment the index
this.index +=1;
if (this.index > this.images.length) {
this.index = 0;
}
}
}
// what are the incremental steps in getting this to work?
// get the images to switch
// then, figure out how to change the position of the sprite
let song;
let offset = 0;
let offset2 = 0;
let bottomX = 0;
let playSpeed = 1;
let topWidth = 440;
let topSpawn = 5.2;
let bottomWidth = 1228;
let bottomSpawn = 4;
let daylightBottom;
let daylightTop;
let mac1;
let mac2;
let trainer1;
let trainer2;
let macY = 0;
let macX = 0;
let macSpd = 4;
let trainX = 0;
let trainPos = 100 + trainX;
let trainSpd = .2;
let winCondition = bottomWidth * -bottomSpawn ;
let win = false;
let lose = false;
function bgScroll(topImg, bottomImg) {
image(winScreen, 0, 0);
offset = offset + .4 * playSpeed
offset2 = offset2 + .8 * playSpeed
for (var topX = topWidth * -topSpawn; topX <= topWidth * topSpawn; topX = topX + topWidth) {
//background top moving
image(topImg, topX + offset, -40);
}
for (let bottomX = 0; bottomX <= bottomWidth * bottomSpawn; bottomX = bottomX + bottomWidth) {
//background bottom moving
image(bottomImg, bottomX - bottomWidth * bottomSpawn + offset2, 95);
}
//how do I get win graphic and music to play when the background is done scrolling??!?!
if (bottomX >= bottomWidth * bottomSpawn){
print('you win');
}
}
// function winGame() {
// offset2 = offset2 + .8 * playSpeed
// push();
// for (var bottomX = bottomWidth * -bottomSpawn; bottomX <= bottomWidth * bottomSpawn; bottomX = bottomX + bottomWidth) {
// //background bottom moving
// }
// pop();
// }
function keyPressed() {
if (keyCode === DOWN_ARROW) {
macY = 5;
} else if (keyCode === UP_ARROW) {
macY = -5;
}
if (keyCode === RIGHT_ARROW) {
macX += macSpd;
} else if (keyCode === LEFT_ARROW) {
macX -= macSpd;
}
}
//Mac Movement
function macMove() {
if (macX > 110) {
macX = 0;
}
if (macX < -220) {
macX = 0;
}
let tintVal = 255;
if (frameCount % 60 > 30) {
image(mac2, 200 + macX, 120 + macY);
tintVal = 0;
//used tintVal to address overlap of images
}
push();
tint(255,255,255, tintVal);
image(mac1, 200+macX, 120+macY);
pop();
}
//Trainer Movement
function trainerMove() {
trainSpd = trainSpd + .0001;
if (macX < trainX){
trainX = trainX + trainSpd;
}
if (macX > trainX){
trainX = trainX - trainSpd;
}
if (macX == trainX){
trainX = 0;
}
image(trainer1, 100 + trainX, 120);
if (frameCount % 60 > 30) {
image(trainer2, 100 + trainX, 120);
}
}
function loseGame(){
//error with song??? how do i make this run once?
if (trainX < -180 || trainX > 180){
print('You Lose')
song.stop();
loseSong.play();
image(loseScreen, 0, 0);
}else{
loseSong.autoplay(false);
}
}
function preload() {
daylightBottom =loadImage('daylight_bottom.png');
daylightTop = loadImage('daylight_top.png');
nightBottom = loadImage('night_bottom.png');
nightTop = loadImage('night_top.png');
winScreen = loadImage('win.png');
mac1 = loadImage('mac1.png');
mac2 = loadImage('mac2.png');
trainer1 = loadImage('trainer1.png');
trainer2 = loadImage('trainer2.png');
loseScreen = loadImage ('lose.png');
song = loadSound('punchout.mp3');
winSong = loadSound ('won.mp3');
loseSong = createAudio ('lost.mp3');
}
function setup() {
createCanvas(300, 180);
}
function draw() {
background(0);
// bgScroll(daylightTop,daylightBottom);
bgScroll(nightTop, nightBottom);
loseGame();
// winGame();
macMove();
trainerMove();
}
function mousePressed() {
if (song.isPlaying()) {
// .isPlaying() returns a boolean
song.stop();
} else {
song.play();
}
}