xxxxxxxxxx
261
let player;
let gravity;
let nextJumpTime;
let jumpIntervalMin = 1; // Minimum jump interval in seconds
let jumpIntervalMax = 3; // Maximum jump interval in seconds
let food; // Only one food object
let tongueLength = 0; // Initial tongue length
let tongueSpeed = 18; // Speed at which the tongue extends and retracts
let foodSpeed = 20; // Speed at which the food moves
let choice = 0;
let spriteSheet;
let spriteWidth; // Width of each sprite frame
let spriteHeight; // Height of each sprite frame
let totalFrames = 8; // Total number of frames in the sprite sheet
let currentFrame = 0; // Current frame being displayed
let frameDuration = 100; // Time (in milliseconds) per frame
let lastFrameTime = 0;
function preload() {
// Load the sprite sheet image
spriteSheet = loadImage('ToxicFrogGreenBlue_Idle.png');
}
function setup() {
createCanvas(400, 400);
player = new Player(createVector(width / 2, height));
gravity = createVector(0, 1);
setNextJumpTime();
setRandomFoodPosition();
spriteWidth = spriteSheet.width / totalFrames;
spriteHeight = spriteSheet.height;
}
function draw() {
background(220);
food.show();
food.update();
// Update and display the tongue
if (choice == 1) {
if (food && !food.isEaten) {
food.show(); // Display the food
if (
tongueLength <
dist(
player.position.x,
player.position.y,
food.position.x,
food.position.y
)
) {
player.extendTongue(); // Call the extendTongue method of the Player class
} else if (
tongueLength > 0 &&
tongueLength >=
dist(
player.position.x,
player.position.y,
food.position.x,
food.position.y
)
) {
food.isCaught = true;
player.retractTongue(); // Call the retractTongue method of the Player class
}
// Check for collision between player and food
if (
dist(
player.position.x,
player.position.y,
food.position.x,
food.position.y
) <
player.radius + food.radius
) {
food.isEaten = true; // Food is eaten
food.isCaught = false;
tongueLength = 0; // Retract the tongue when food is eaten
choice = 0;
}
}
}
player.update();
player.show();
if (millis() > nextJumpTime) {
bool = random(0, 10);
if (bool > 2) {
player.randomJump();
} else {
choice = 1;
}
setNextJumpTime();
}
}
function setNextJumpTime() {
// Set the next jump time to a random value between the specified interval
nextJumpTime =
millis() + random(jumpIntervalMin * 1000, jumpIntervalMax * 1000);
}
function mousePressed() {
// Create a new food object at a random position in the top half of the screen
food = new Food(mouseX, mouseY);
}
function setRandomFoodPosition() {
const x = random(width);
const y = random(height / 2); // Limit food to the top half of the screen
food = new Food(x, y);
}
class Player {
constructor(position) {
this.position = position;
this.velocity = createVector(0, 0);
this.radius = 25; // Increase the radius to make collision detection easier
this.color = color(0, 255, 0); // Green color
this.isJumping = false;
}
randomJump() {
if (!this.isJumping) {
// Random jump height (vertical velocity)
this.velocity.y = random(-20, -10);
// Random jump direction (horizontal velocity)
this.velocity.x = random(-10, 10);
// The player is now jumping
this.isJumping = true;
}
}
extendTongue() {
// Extend the tongue towards the food
tongueLength += tongueSpeed;
let direction = p5.Vector.sub(food.position, player.position).normalize();
let tongueEnd = p5.Vector.add(
player.position,
direction.copy().mult(tongueLength)
);
line(player.position.x, player.position.y, tongueEnd.x, tongueEnd.y);
}
retractTongue() {
// Retract the tongue
tongueLength -= tongueSpeed;
let direction = p5.Vector.sub(food.position, player.position).normalize();
let tongueEnd = p5.Vector.sub(
player.position,
direction.copy().mult(tongueLength)
);
line(
player.position.x,
player.position.y,
food.position.x,
food.position.y
);
if (tongueLength < 0) {
tongueLength = 0;
}
}
update() {
this.position.add(this.velocity);
this.velocity.add(gravity);
// Prevent falling below the screen
if (this.position.y > height) {
this.position.y = height;
this.velocity.y = 0;
this.isJumping = false; // The player is on the ground, so not jumping
}
if (this.position.x > width || this.position.x < 0) {
this.velocity.x = this.velocity.x * -1;
}
if (this.position.y > height || this.position.y < 0) {
this.velocity.y = this.velocity.y * -1;
}
// Stop horizontal movement when on the ground
if (!this.isJumping) {
this.velocity.x = 0;
}
}
show() {
fill(this.color); // Green color
// Calculate the x-coordinate for the current frame
let spriteX = currentFrame * spriteWidth;
// Display the current frame from the sprite sheet
image(spriteSheet, this.position.x-spriteHeight/1.5, this.position.y-spriteHeight*1.4, spriteWidth*2, spriteHeight*2, spriteX, 0, spriteWidth, spriteHeight);
// image(spriteSheet, this.position.x-this.radius, this.position.y-this.radius, 200,50, spriteX, 0, 200, 200);
// Update the frame
if (millis() > frameDuration + lastFrameTime) {
currentFrame = (currentFrame + 1) % totalFrames;
lastFrameTime = millis();
}
// ellipse(this.position.x, this.position.y, this.radius * 2);
}
}
class Food {
constructor(x, y) {
this.position = createVector(x, y);
this.velocity = createVector(x, y);
this.acceleration = createVector(x, y);
this.radius = 10;
this.isEaten = false;
this.isCaught = false;
this.color = color(0, 0, 0); // Red color
this.perlinOffset = random(1000); // Random offset for Perlin noise
}
show() {
if (!this.isEaten) {
fill(this.color);
ellipse(this.position.x, this.position.y, this.radius*0.7);
}
}
update() {
if (!this.isCaught) {
this.acceleration = p5.Vector.random2D();
//this.acceleration.mult(3);
//this.acceleration.mult(random(10));
if (this.position.x > width || this.position.x < 0) {
this.velocity.x = this.velocity.x * -1.4;
}
if (this.position.y > height || this.position.y < 0) {
this.velocity.y = this.velocity.y * -1.4;
}
let stepvar = random(1);
if (stepvar < 0.3) {
this.velocity.y -= 4;
} else if (stepvar < 0.5) {
this.velocity.y += 4;
} else if (stepvar < 0.75) {
this.velocity.x -= 4;
} else {
this.velocity.x += 4;
}
this.velocity.limit(10);
// Constrain the food's y-coordinate to the top half of the canvas
} else {
this.velocity = p5.Vector.sub(food.position, player.position)
.normalize()
.mult(-tongueSpeed);
}
this.position.add(this.velocity);
}
}