Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- let dataServer;
- let pubKey = 'pub-c-78d9ee8d-b27d-463a-a05d-528b91cb6a56';
- let subKey = 'sub-c-7dcb509c-f6af-11ea-a5ac-ca9fd24ed40e';
- let background;
- // Define global variables
- // SPR Header means image
- let spr_raspberry;
- let spr_leaf;
- let spr_enemy_head;
- let spr_enemy_body;
- let spr_local_head;
- let spr_local_body;
- let spr_gameover;
- let spr_snail_d;
- let spr_snail_r;
- let spr_ant_u;
- let spr_ant_d;
- let spr_ant_r;
- let spr_ant_l;
- let spr_won;
- let bite1;
- let bite2;
- let death;
- // Lists for the various items and animals in the game as NPCs
- let ants = [];
- let items = [];
- let snails = [];
- let dir = undefined;
- // Generate a unique UUID
- let UUID = PubNub.generateUUID();
- let dataToSend = {
- player: undefined,
- req: undefined
- }
- let dataReceived = [];
- let oPlayer = undefined;
- let channelName = "game";
- let cell_size = 32;
- // Define canvas size
- let game_width = 700;
- let game_height = 700;
- class Item{ // Item class (What the caterpillar eats)
- constructor() {
- this.type = Math.floor(Math.random() * 4); // Determine if the image will be a leaf or a raspberry
- this.x = Math.floor(Math.random() * game_width/cell_size) * cell_size - 8; // Spawn at random place
- this.y = Math.floor(Math.random() * game_height/cell_size) * cell_size - 8;
- };
- }
- class Mob{ // Mob parent class (Snails and Ants)
- constructor() {
- // Spawn at random point
- this.x = Math.floor(Math.random() * game_width/cell_size);
- this.y = Math.floor(Math.random() * game_height/cell_size);
- // Spawn off screen and set beginning direction
- if(this.x > this.y){
- this.y = 0;
- this.dir = 1;
- }else{
- this.x = 0;
- this.dir = 3;
- }
- }
- }
- class Snail extends Mob{ // Snail class
- constructor() {
- super(); // Inherit the parent class constructor
- }
- move(){ // Used to move the snail in a constant direction at 0.5 the speed of the player
- if (this.dir === 1){
- this.y += 0.5;
- }else if (this.dir === 3){
- this.x += 0.5;
- }
- }
- }
- class Ants extends Mob{ // Ant class
- constructor() {
- super(); // Inherit the parent class constructor
- this.spaces_moved = 0; // Used to determine whether the ant will change direction
- // Move to the opposite side of the screen to start
- if (this.x === 0){
- this.x = game_width / cell_size;
- this.dir = 2;
- }else if(this.y === 0){
- this.y = game_height / cell_size;
- this.dir = 1;
- }
- // Determine how many ants in the line
- this.size = Math.floor(Math.random() * 7) + 6;
- this.pos = [];
- this.pos_dir = [];
- // Generate the list of positions for the ants and the direction they're facing
- for (let i = 0; i < this.size; i++){
- if (this.dir === 3) {
- this.pos.push([this.x + i, this.y]);
- }else{
- this.pos.push([this.x, this.y + i]);
- }
- this.pos_dir.push(this.dir);
- }
- }
- move(){ // Move the ants
- let old_dir;
- // If you've moved more than 6 spaces, you can now change your direction
- if(this.spaces_moved > 6){
- if(Math.floor(Math.random() * 3) === 2){ // 1/3 chance to change direction
- old_dir = this.dir;
- while(true) { // Check that they didn't double back on themselves
- this.dir = Math.floor(Math.random() * 4);
- if(old_dir !== 1 && this.dir === 0){
- // W
- this.dir = 0;
- break;
- }
- else if(old_dir !== 0 && this.dir === 1){
- // S
- this.dir = 1;
- break;
- }
- else if(old_dir !== 3 && this.dir === 2){
- // A
- this.dir = 2;
- break;
- }
- else if(old_dir !== 2 && this.dir === 3){
- // D
- this.dir = 3;
- break;
- }
- }
- this.spaces_moved = 0; // Need to move 6 more spaces because the direction has been changed
- }
- }
- // Move the last ant to the beginning of the line to give the impression that the ants are moving in a line
- this.pos[this.pos.length - 1][0] = this.pos[0][0];
- this.pos[this.pos.length - 1][1] = this.pos[0][1];
- switch (this.dir){
- case 0:
- // UP
- this.pos[this.pos.length - 1][1] -= 1;
- break;
- case 1:
- // DOWN
- this.pos[this.pos.length - 1][1] += 1;
- break;
- case 2:
- // LEFT
- this.pos[this.pos.length -1][0] -= 1;
- break;
- case 3:
- // RIGHT
- this.pos[this.pos.length - 1][0] += 1;
- break;
- case undefined:
- return;
- }
- // Change the ants positions
- this.pos_dir[this.pos_dir.length - 1] = this.dir;
- let holder2 = this.pos_dir[this.pos_dir.length - 1];
- this.pos_dir.pop();
- this.pos_dir.unshift(holder2);
- let holder = this.pos[this.pos.length -1];
- this.pos.pop();
- this.pos.unshift(holder);
- this.spaces_moved ++;
- }
- }
- function setup() // Initialize the game
- {
- // Load in all of the images and sounds
- spr_leaf = loadImage("Assets/Images/leaf.png");
- spr_raspberry = loadImage("Assets/Images/raspberry.png");
- background = loadImage("Assets/Images/bg.png");
- spr_local_body = loadImage("Assets/Images/l_body1.png");
- spr_gameover = loadImage("Assets/Images/gameover.png");
- spr_local_head = loadImage("Assets/Images/l_head.png");
- spr_enemy_body = loadImage("Assets/Images/e_body.png");
- spr_enemy_head = loadImage("Assets/Images/e_head.png");
- spr_won = loadImage("Assets/Images/youwon.png");
- spr_ant_u = loadImage("Assets/Images/ant.png");
- spr_ant_d = loadImage("Assets/Images/ant_d.png");
- spr_ant_r = loadImage("Assets/Images/ant_r.png");
- spr_ant_l = loadImage("Assets/Images/ant_l.png");
- spr_snail_d = loadImage("Assets/Images/snail_d.png");
- spr_snail_r = loadImage("Assets/Images/snail_r.png");
- background_song = new Audio("Assets/Sounds/FrenchToast.wav");
- bite1 = new Audio("Assets/Sounds/Bite1.wav");
- bite2 = new Audio("Assets/Sounds/GrapeBite.wav");
- death = new Audio("Assets/Sounds/DyingSound.wav");
- // Set the framerate for the game and initialize the canvas
- frameRate(7);
- createCanvas(game_width, game_height);
- // Player object
- local_player = {
- won: undefined,
- steps: 0,
- colour: color(0, 255, 0),
- pos: [[5, 5], [4, 5], [3, 5]],
- id: UUID,
- dir: undefined
- };
- // Connect to the PubNub Server
- dataServer = new PubNub({
- publish_key : pubKey,
- subscribe_key : subKey,
- ssl: true,
- uuid: UUID
- });
- dataServer.addListener({ message: readIncoming });
- dataServer.subscribe({channels: [channelName]});
- sendTheMessage(local_player, true);
- ant = new Ants();
- }
- function move(p){ // Move the player
- // Same general code as the ants except it doesn't change the direction of each individual piece so is much simpler
- positions = p.pos;
- // Move the back of the player to the front
- positions[positions.length - 1][0] = positions[0][0];
- positions[positions.length - 1][1] = positions[0][1];
- switch (p.dir){ // Move based on the direction
- case 0:
- // UP
- positions[positions.length - 1][1] -= 1;
- break;
- case 1:
- // DOWN
- positions[positions.length - 1][1] += 1;
- break;
- case 2:
- // LEFT
- positions[positions.length -1][0] -= 1;
- break;
- case 3:
- // RIGHT
- positions[positions.length - 1][0] += 1;
- break;
- case undefined:
- return;
- }
- // Swap the position of the last index to the beginning and shift everything down one
- holder = positions[positions.length -1];
- positions.pop();
- positions.unshift(holder);
- p.pos = positions;
- }
- function spawn_item(){ // Spawns a new item for the players to eat
- nItem = new Item();
- items.push(nItem);
- sendTheMessage(local_player, false); // Sends the item to the other player
- }
- function collision(p1, p2){ // Checks for collisions between the two players, the player and the snails, ants and edge of screen
- switch (p1.dir){ // Gets the position of the next move
- case 0:
- // UP
- d = -1;
- r = 0;
- break;
- case 1:
- // DOWN
- d = 1;
- r = 0;
- break;
- case 2:
- // LEFT
- r = -1;
- d = 0;
- break;
- case 3:
- // RIGHT
- r = 1
- d = 0;
- break;
- case undefined:
- r = 0;
- d = 0;
- }
- // If the other player has been loaded in, check if your head has hit them
- if(p2 !== undefined) {
- for (let i = 0; i < p2.pos.length; i++) {
- if ((p1.pos[0][0] + r === p2.pos[i][0]) && (p1.pos[0][1] + d === p2.pos[i][1])) {
- return true;
- }
- }
- }
- // Check for each snail to see if you hit them
- for(i =0; i < snails.length; i++) {
- if (p1.pos[0][0] + r === Math.floor(snails[i].x) && p1.pos[0][1] + d === Math.floor(snails[i].y)){
- return true;
- }
- }
- // Check for each ant to see if you hit them
- for(e=0; e < ants.length; e++){
- for(i = 0; i < ants[e].pos.length; i++){
- if(p1.pos[0][0] + r === Math.floor(ants[e].pos[i][0]) && p1.pos[0][1] + d === Math.floor(ants[e].pos[i][1])){
- return true;
- }
- }
- }
- // Check if you're off screen. If all are false you did not hit anything.
- return p1.pos[0][0] + r > game_width / cell_size + 1 || p1.pos[0][0] + r < -1 || p1.pos[0][1] + d > game_height / cell_size + 1 || p1.pos[0][1] < 0;
- }
- function checkWon(p){
- // Checks if you have won by reaching a length of 20, and if not then checks how 'hungry' you are and removes length from you if
- // You're too hungry
- if (p.pos.length > 15){
- p.won = true;
- }
- if(p.pos.length <= 1){
- local_player.won = false;
- }
- if(p.steps > 20){
- p.pos.pop();
- p.steps = 0;
- }
- }
- function draw() { // Main function
- // 1 in 40 chance to spawn a new snail
- if(Math.floor(Math.random()*40) === 2){
- snails.push(new Snail());
- }
- // 1 in 60 chance to spawn a new ant
- if(Math.floor(Math.random()*60) === 2){
- ants.push(new Ants());
- }
- // Draw the background
- image(background,0,0,700,700);
- // Draw each snail depending on their direction
- for (i = 0; i < snails.length; i++){
- if(snails[i].dir === 1){
- image(spr_snail_d, snails[i].x * cell_size, snails[i].y * cell_size, 48, 48);
- }else{
- image(spr_snail_r, snails[i].x * cell_size, snails[i].y * cell_size, 48, 48);
- }
- // Move the snails
- snails[i].move();
- }
- // Draw each segment of the ant line depending on the direction that they are facing and moving in
- for (e = 0; e < ants.length; e++) {
- ants[e].move(); // Move the ants
- for (i = 0; i < ants[e].size; i++) {
- switch (ants[e].pos_dir[i]){
- case 0:
- image(spr_ant_u, ants[e].pos[i][0] * cell_size, ants[e].pos[i][1] * cell_size, cell_size, cell_size);
- break;
- case 1:
- image(spr_ant_d, ants[e].pos[i][0] * cell_size, ants[e].pos[i][1] * cell_size, cell_size, cell_size);
- break;
- case 2:
- image(spr_ant_l, ants[e].pos[i][0] * cell_size, ants[e].pos[i][1] * cell_size, cell_size, cell_size);
- break;
- case 3:
- image(spr_ant_r, ants[e].pos[i][0] * cell_size, ants[e].pos[i][1] * cell_size, cell_size, cell_size);
- break;
- }
- }
- }
- // Play the background music
- if(!(background_song.duration > 0 && !background_song.paused)){
- background_song.play();
- }
- checkWon(local_player); // Check if the player has won, or if they need to be shortened
- local_player.steps ++; // Increase number of spaces walked
- // Draw each of the items and check if the player has eaten them
- if(items.length > 0) {
- if(items[0].type === 0){
- image(spr_raspberry, items[0].x, items[0].y, cell_size*1.5, cell_size*1.5);
- }else{
- image(spr_leaf, items[0].x, items[0].y, cell_size*1.5, cell_size*1.5);
- }
- switch (local_player.dir){
- case 0:
- // UP
- d = -1;
- r = 0;
- break;
- case 1:
- // DOWN
- d = 1;
- r = 0;
- break;
- case 2:
- // LEFT
- r = -1;
- d = 0;
- break;
- case 3:
- // RIGHT
- r = 1
- d = 0;
- break;
- case undefined:
- r = 0;
- d = 0;
- }
- if ((local_player.pos[0][0] + r === (items[0].x + 8) / cell_size) && (local_player.pos[0][1] + d === (items[0].y + 8) / cell_size)) {
- // If the player has eaten the item, the player gains one length at the point the item was, steps and reset and a new item is spawned
- local_player.pos.push([local_player.pos[0][0] + r, local_player.pos[0][1] + d, Math.floor(Math.random()*4)]);
- local_player.steps = 0;
- if(items[0].type === 0){
- //Berry
- bite1.play();
- }else{
- // Leaf
- bite2.play();
- }
- items.shift();
- spawn_item();
- }
- }else{
- spawn_item(); // If there are no items, spawn one
- }
- // Local player
- // Move the local player
- move(local_player);
- image(spr_local_head, local_player.pos[0][0] * cell_size - 8, local_player.pos[0][1] * cell_size - 8, cell_size*1.5, cell_size*1.5); // Draw the player head
- // Draw the player body
- for (i = 1; i < local_player.pos.length; i ++){
- image(spr_local_body, local_player.pos[i][0] * cell_size - 8, local_player.pos[i][1] * cell_size - 8, cell_size*1.5, cell_size*1.5);
- }
- // Check for collisions, if there is one you have lost.
- if (collision(local_player, oPlayer) && local_player.dir !== undefined) {
- local_player.won = false;
- }
- if(local_player.won == false){
- image(spr_gameover, 0, 0, 700, 700);
- sendTheMessage(local_player, false); // Tell the opponent they have won
- background_song.pause();
- death.play();
- noLoop(); // Stop the game
- }
- // Other player
- if (dataReceived[0] !== undefined) {
- // Update the player from information received from the other client
- oPlayer = dataReceived[0].message.dataToSend.player;
- move(oPlayer); // Move the other player locally to reduce messages sent
- // Draw the other players head
- image(spr_enemy_head, oPlayer.pos[0][0] * cell_size - 8, oPlayer.pos[0][1] * cell_size - 8, cell_size*1.5, cell_size*1.5);
- for (let i = 1; i < oPlayer.pos.length; i++) {
- // Draw the other players body
- image(spr_enemy_body, oPlayer.pos[i][0] * cell_size - 8, oPlayer.pos[i][1] * cell_size - 8, cell_size*1.5, cell_size*1.5);
- }
- }
- // If you have won, stop the game and draw the win screen
- if(local_player.won === true){
- image(spr_won, 0, 0, 700, 700);
- noLoop();
- }
- }
- function sendTheMessage(player, r) { // Sends information to the opponent
- dataToSend.player = player; // Local player object
- dataToSend.req = r; // Whether or not to request the opponent to send information back to you
- if(items.length > 0) {
- dataToSend.item = items[0]; // Items to send
- }
- dataServer.publish(
- {
- channel: channelName,
- message:
- {
- dataToSend
- }
- });
- }
- function readIncoming(inMessage) { // Read the messages sent to you from the opponent
- console.log(inMessage);
- if (inMessage.message.dataToSend.player.id === UUID){ // If it is a message from yourself, ignore it
- return
- }
- if (inMessage.message.dataToSend.req === true) { // If the other player wants your information, send it
- sendTheMessage(local_player, false);
- }
- exists = false; // check if the player that connected is already in the game
- for (i = 0; i < dataReceived.length; i++) {
- if (inMessage.message.dataToSend.player.id === dataReceived[i].message.dataToSend.player.id) {
- dataReceived[i] = inMessage; // Overwrite the current player information with the new information
- exists = true
- }
- }
- if (!exists){
- dataReceived.push(inMessage); // Makes sure not to overwrite the current player with the new player connection
- }
- if(inMessage.message.dataToSend.player.won === false){
- local_player.won = true; // If the other player has lost, you have won
- }
- items[0] = inMessage.message.dataToSend.item; // Update the current item on the screen if the other player has eaten it
- console.log(dataReceived); // Log the data received
- }
- function keyPressed() { // Used for movement
- if(keyCode === 87 && dir !== 1){
- // W
- dir = 0;
- }
- else if(keyCode === 83 && dir !== 0){
- // S
- dir = 1;
- }
- else if(keyCode === 65 && dir !== 3){
- // A
- dir = 2;
- }
- else if(keyCode === 68 && dir !== 2){
- // D
- dir = 3;
- }
- local_player.dir = dir; // Change the players direction
- sendTheMessage(local_player, false); // Update the other player that you have changed direction
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement