⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 game.js

📁 flash platform game with code
💻 JS
📖 第 1 页 / 共 2 页
字号:
// Below are constants used in the game
var namespace = "http://www.w3.org/2000/xlink/namespace/";

// monster information
var MONSTER_WIDTH = 61;
var MONSTER_HEIGHT = 45;
//var MONSTER_SCORE = 10;
var monsters_group = null;

// Laser Beam Setting
var LASER_WIDTH = 30.0;      // laser beam width
var LASER_HEIGHT = 5.0;     // laser beam height
var LASER_DX = 20.0;         // laser beam displacement in x axis
var LASER_DY = 0.0;          // laser beam displacement in y axis
var lasers_group = null;     // the SVG object that holds all the lasers
var laser_interval = 200.0;    // disble shooting period
var CAN_SHOOT = true;        // 'can shoot' flag
var laser_left = 20;		//only 20 shoots ata each level
var MOVING_RIGHT = "true";
var laser_left_text = null;

// Size of various things
var PLAYER_WIDTH = 46.0;
var PLAYER_HEIGHT = 46.0;

var SCREEN_WIDTH = 600.0;
var SCREEN_HEIGHT = 400.0;

// Player initial position
var PLAYER_INIT_X = 20;
var PLAYER_INIT_Y = 20;

// Motion Constant
var MOTION_NONE = 0;
var MOTION_LEFT = 1;
var MOTION_RIGHT = 2;

// Movement for horizontal direction
var DISPLACEMENT = 500.0;

// Movement for vertical direction
var JUMP_VELOCITY = 900.0;

// The physical quantities of the whole system
var TIME_INTERVAL = 50.0;
var GRAVITY = 5000.0;
var FLOOR_RESISTANCE = 0.5;

// SVG Root Document Node
var SVGDoc = null;

// Below are the SVG objects or style objects to be used in the functions

// The game_area group which has all the lines and player in it
var game_area = null;

// The player object which stores the properties
var player = null;

// The movement of the player (none, left or right) and jump of the player
var movement = MOTION_NONE;
var jump = false;

// Variable of the interval number
var interval = null;

// Time elapsed in the game
var time = 0;

// The zoom level of the screen
var zoom = 1.0;
var zoom_mode = false;


// Cheat mode on/off
var cheat = false;
var cheat_text = null;

//Score
var score = 0;
var m_score = 10;   //monster score
var s_score = 5;    //score pre remaining second
var score_text = null;

//Time
var total_time = 45;
var timer = null;
var time_text = null;

//exit
var exit_x = 550;
var exit_y = 35;

//level
var level = 1;
var level_text = null;

//game over background movement
var OVER_MOVE=0; // move to right
var dear = null;
var over_timer;

var bg_timer = null;

function game_over_move() {
	dead = SVGDoc.getElementById("dead");
	OVER_MOVE += 20;
	if (OVER_MOVE <= 620)
		dead.setAttribute("transform", "translate(" + OVER_MOVE + ", 0)");
	else {
		clearInterval(over_timer);
		game_over();
	}
	
}


// Collision detection (type by myself)
function collision_detection() {
    var cx, cy, x1, x2, y1, y2;
    cx = player.x;
    cy = player.y;
    if (!cheat) {
        for (i=0; i<monsters_group.childNodes.length; i++) {
            var monster = monsters_group.childNodes.item(i);
            x1 = parseFloat(monster.getAttribute("x"));
            y1 = parseFloat(monster.getAttribute("y"));
            x2 = x1 + MONSTER_WIDTH;
            y2 = y1 + MONSTER_HEIGHT;
            if (x1 <= cx && x2 >= cx && y1 <= cy && y2 >= cy) {
                clearInterval(interval);
				clearInterval(timer);
				play_sound("die_sound");
				over_timer = setInterval("game_over_move()", 50);
				break;
            }
        }
    }
    
    // colision for laser touching the monster
    for (k = 0; k < monsters_group.childNodes.length; k++) {
        var monster = monsters_group.childNodes.item(k);
        x1 = parseFloat(monster.getAttribute("x"));
        y1 = parseFloat(monster.getAttribute("y"));
        x2 = x1 + MONSTER_WIDTH;
        y2 = y1 + MONSTER_HEIGHT;
        
        for (L = 0; L < lasers_group.childNodes.length; L++) {
            var laser = lasers_group.childNodes.item(L);
            cx = parseFloat(laser.getAttribute("x"));
            cy = parseFloat(laser.getAttribute("y"));
            if (x1 <= cx && x2 >= cx && y1 <= cy && y2 >= cy) {
                lasers_group.removeChild(laser);
                monsters_group.removeChild(monster);
                if (!zoom_mode)
                    score += m_score;
                else score += m+score*2;
				score_text.setData(score);
                break;
            }
        }
	}
}


// Declaration of the player object
function Player() {
	this.object = SVGDoc.getElementById("player");

	this.width = PLAYER_WIDTH;
	this.height = PLAYER_HEIGHT;

	this.SetPosition(PLAYER_INIT_X, PLAYER_INIT_Y);

	this.vx = 0;
	this.vy = 0;
}

Player.prototype.SetPosition = function (x,y) {
	this.x = x;
	this.y = y;

	this.left = x - this.width / 2.0;
	this.right = x + this.width / 2.0;
	this.top = y - this.height / 2.0;
	this.bottom = y + this.height / 2.0;
}

// The load function for the whole SVG document
function Load(evt) {
	// Set the root node to the global variable
	SVGDoc = evt.getTarget().getOwnerDocument();
	
	// find the monsters_group
	monsters_group = SVGDoc.getElementById("monsters_group");
	
	// find the lasers_group
	lasers_group = SVGDoc.getElementById("lasers_group");

	// Preset all objects for later use
	game_area = SVGDoc.getElementById("game_area");
	player = new Player();
	
	// Remove text nodes in the 'platforms' group
	clean_up_groups("platforms_group");	
	
	//clearInterval(bg_timer);
	play_sound("background_sound");
}

// This function removes all text nodes under the groups that are used later
function clean_up_groups(id) {
	var object, next;

	var group = SVGDoc.getElementById(id);
	object = group.getFirstChild();
	while (object != null) {
		next = object.getNextSibling();
		if (object.nodeType == 3) group.removeChild(object);
		object = next;
	}
}

// This is the keydown handling function for the SVG
function KeyDown(evt) {
	
	switch (evt.getKeyCode()) {
		case "N".charCodeAt(0):
			movement = MOTION_LEFT;
			MOVING_RIGHT = false;
			//player.object.setAttribute
			//player.object.setAttribute("transform", "scale(1,-1)");
			break;
		case "M".charCodeAt(0):
			movement = MOTION_RIGHT;
			MOVING_RIGHT = true;
			//player.object.setAttribute("transform", "scale(1,-1)");
			break;
		case "Z".charCodeAt(0):
		    jump = true;
			play_sound("jump_sound");
		    break;
					
		// handle jump action	
		/*** Add your code here ***/	
		case 32: // space bar
		    if ((CAN_SHOOT) && (laser_left > 0))
		        shoot_a_laser();
		    break;
		
		// on/off cheat mode
		case "X".charCodeAt(0):
		    if (cheat) {
		        cheat = false;
				cheat_text.setData("~NO~");
			}
		    else {
				cheat = true;
				cheat_text.setData("~YES~");
			}
		
	}
}

// This is the keyup handling function for the SVG
function KeyUp(evt) {
	switch (evt.getKeyCode()) {
		case "N".charCodeAt(0):
			movement = MOTION_NONE;
			break;
		case "M".charCodeAt(0):
			movement = MOTION_NONE;
			break;
	}
}

// This function calculates the minimum distance of the player with a floor below it.
function minimum_floor_distance() {
	var object;
	var distance, floor;
	var floor_x1, floor_x2, floor_y, floor_width;

	floor = null;

	// go through all the platform's node
	var platforms = SVGDoc.getElementById("platforms_group");
	for (var i = 0; i < platforms.childNodes.length; i++) {
		object = platforms.childNodes.item(i);
		
		if ((object.getAttribute("type") == "disappearing") && (object.getAttribute("visibility") == "hidden")) {
			continue;
		}
		
		if (object.nodeName == "line") {
			floor_x1 = parseFloat(object.getAttribute("x1"));	// floor's left
			floor_x2 = parseFloat(object.getAttribute("x2"));	// floor's right
			floor_width = parseFloat(object.getStyle().getPropertyValue("stroke-width"));

			if (player.right>=floor_x1 && player.left<=floor_x2) {
				floor_y = parseFloat(object.getAttribute("y1"));
			
				distance = (floor_y - floor_width / 2.0) - player.bottom;

				if (distance >= 0) {
					if (floor == null || floor > distance)
						floor = distance;
				}
			}
		}
	}
	return floor;
}

// This function starts up the game by initializing the variables and setting the
// default SVG appearance
function game_start() {
	movement = MOTION_NONE;

	if (interval != null)
		clearInterval(interval);

	if (timer != null)
		clearInterval(timer);
	
	timer = setInterval("time_remaining()", 1000);
	interval = setInterval("game_play()", TIME_INTERVAL);
}

// This function updates the position and motion of the player in the system
function game_play() {
	var x, y;
	var player_distance, floor_distance;
	var t;
	var platform_found = false;
	var platforms = SVGDoc.getElementById("platforms_group");
		
	/*** Update player position and velocity ***/

	x = player.x;
	y = player.y;

	if (!platform_found)
		floor_distance = minimum_floor_distance();
	else
		floor_distance = 0;
		

	if (floor_distance == null)
		floor_distance = SCREEN_HEIGHT - player.bottom;
	
	t = TIME_INTERVAL / 1000.0;
	
	player_distance = player.vy * t + 0.5 * GRAVITY * t * t;		// s = u*t + 1/2*a*t^2
	if (player_distance > floor_distance) {		// if the player is on the game_area
		player_distance = floor_distance;
		player.vy = 0;
		floor_distance = 0;
	}
	else {
		player.vy += GRAVITY * t;		// update velocity dv / t = a
	}

	// if jump, give a upward velocity
	/*** Add your code here ***/
    if (jump) {
        if (floor_distance == 0)
            player.vy = -JUMP_VELOCITY;
        jump = false;
    }

	y += player_distance;

	if (floor_distance == 0) {
		switch (movement) {
			case MOTION_LEFT:
				player.vx = -DISPLACEMENT;
				break;
			case MOTION_RIGHT:
				player.vx = DISPLACEMENT;
				break;
		}

		player_distance = player.vx * t;
		player.vx -= player.vx * FLOOR_RESISTANCE;
	}
	else {
		player_distance = player.vx * t;
	}

	x += player_distance;

	// Check that whether the player is out of bounds

	if ((x - player.width / 2.0) < 0) x = player.width / 2.0;
	if ((x + player.width / 2.0) > SCREEN_WIDTH) x = SCREEN_WIDTH - player.width / 2.0;

	if ((y - player.height / 2.0) < 0) y = player.height / 2.0;
	if ((y + player.height / 2.0) > SCREEN_HEIGHT) y = SCREEN_HEIGHT - player.height / 2.0;

	// Set the location back to the player object (before update the screen)
	player.SetPosition(x,y);
	
	move_laser();
    collision_detection();
	update_screen();
	next_level();
	move_monster();
	move_platform();
}

//functions for control the laser
function shoot_a_laser () {
    var laser, x, y;
    CAN_SHOOT = false;
	
	    setTimeout("CAN_SHOOT = true", laser_interval);
		play_sound("laser_sound");
	    laser = SVGDoc.createElement("use");
		if (MOVING_RIGHT)
			x = player.right - LASER_WIDTH;
		else 	x = player.left - LASER_WIDTH;
	    y = player.top + (PLAYER_HEIGHT - LASER_HEIGHT)/2.0;
	    
	    laser.setAttribute("x", x);
	    laser.setAttribute("y", y);
		laser.setAttribute("direction", MOVING_RIGHT);
	    laser.setAttributeNS(namespace, "xlink:href", "#laser");
	    
	    lasers_group.appendChild(laser);
		laser_left--;
		
		leaer_left_text = SVGDoc.getElementById("laser_left").childNodes.item(0);
		leaer_left_text.setData(laser_left);		
}

//move the laser
function move_laser() {
    for (i=0; i<lasers_group.childNodes.length; i++) {
        var laser = lasers_group.childNodes.item(i);
		var x = parseFloat(laser.getAttribute("x"));
		var dir = laser.getAttribute("direction");
		screen_y = parseFloat(laser.getAttribute("y")) + LASER_DY;
		
		if  ((screen_y > SCREEN_HEIGHT) 
			|| (screen_y < LASER_HEIGHT * -1))
			lasers_group.removeChild(laser);
		
		if (dir == "true") { // move to right
			//alert("here");
			x = x + LASER_DX;
			if (x > SCREEN_WIDTH) 
				lasers_group.removeChild(laser);
			else  laser.setAttribute("x", x);
		}
		else { //move to left
			x = x - LASER_DX;
			if (x < 0)
				lasers_group.removeChild(laser);
			else  laser.setAttribute("x", x);
		}            
    }
}

// This function updates the position of the player's SVG object and
// set the appropriate translation of the game screen relative to the
// the position of the player
function update_screen() {
	
	/*** transform the player  ***/
	if (MOVING_RIGHT)
		player.object.setAttribute("transform",
		"translate(" + player.x + "," + player.y + ") scale(-1,1)");
	else
		player.object.setAttribute("transform",
		"translate(" + player.x + "," + player.y + ") scale(1,1)");
		
		//setAttribute("transform", "translate("+PLAYER_WIDTH+", 0) scale(-1, 1)");

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -