xxxxxxxxxx
168
let Player;
let BulletPool = [];
let Enemy;
let dodgeframe;
let i = 0;
let bgspeed = 1;
function setup() {
createCanvas(800, 450);
frameRate(60);
Player = new player();
Enemy = new EnemyObject();
}
function keyPressed(){
// print (keyCode);
if (keyCode === 65) {
Player.direction = "Left";
}
if (keyCode === 68) {
Player.direction = "Right";
}
if (keyCode === 87) {
Player.jump = true;
}
if (keyCode === 16) {
Player.dodge = true;
}
if (keyCode === 32) {
BulletPool[i] = new Bullet(Player.xpos + 25, Player.ypos + 25);
}
}
function draw() {
let frame = frameCount;
background(220);
if (Player.xpos + 25 < Enemy.hitboxXmax && Player.xpos + 25 > Enemy.hitboxXmin) {
// print ("inside x");
if (Player.dodge == true){
bgspeed = 0.1;
// print ("dodge");
dodgeframe = frame;
}
// if (Player.ypos - 25 < Enemy.hitboxYmax && Player.ypos - 25 > Enemy.hitboxYmin) {
// print ("inside y");
// }
}
if (frame > dodgeframe + 60) {
bgspeed = 1;
}
Player.display();
Player.move();
Enemy.display();
Enemy.move();
for (i=0; i<BulletPool.length; i++) {
BulletPool[i].display();
BulletPool[i].move();
}
if (keyIsPressed == false){
Player.direction = "Idle";
Player.jump = false;
}
}
class player{
constructor(){
this.xpos = 0;
this.ypos = 400;
this.xspeed = 5;
this.xbreak = 0.8;
this.jumpspeed = 10;
this.gravity = 2;
this.direction = "Idle";
this.jump = false;
this.lastjump = false;
this.dodge = false;
}
move() {
if (this.ypos <= 399) {
this.ypos -= this.jumpspeed;
this.jumpspeed -= this.gravity;
}
else {
this.ypos = 400;
this.jumpspeed = 30;
this.lastjump = false;
}
if (this.dodge == true) {
this.xspeed = 15;
}
else {
if (this.xspeed > 5){
this.xspeed -= this.xbreak;
}
else {this.xspeed = 5;}
}
this.dodge = false;
if (this.direction == "Right"){
this.xpos += this.xspeed;
}
else if (this.direction == "Left"){
this.xpos -= this.xspeed;
}
else {}
if (this.jump == true && this.lastjump == false) {
this.ypos = 399;
this.lastjump = true;
}
this.jump = false;
}
display() {
square(this.xpos, this.ypos, 50);
}
}
class EnemyObject{
constructor(){
this.ypos = 0;
this.speed = 0;
this.gravity = 0.6;
this.hitboxXmax = 675;
this.hitboxXmin = 550;
this.hitboxYmax = this.ypos+120;
this.hitboxYmin = this.ypos+40;
}
move(){
if (this.ypos < 800) {
this.speed += this.gravity*bgspeed;
this.ypos += this.speed*bgspeed;
}
else {
this.ypos = 0;
this.speed = 0;
}
}
display(){
rect(600, this.ypos, 25, 40);
}
}
class Bullet{
constructor(x,y){
this.xpos = x;
this.ypos = y;
this.speed = 15;
}
move(){
this.xpos += this.speed;
}
display(){
rect(this.xpos, this.ypos, 10, 3);
}
}