-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathgame - Backup.js
139 lines (125 loc) · 5.59 KB
/
game - Backup.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
var PLAYGROUND_WIDTH = 1380;
var PLAYGROUND_HEIGHT = 700;
var REFRESH_RATE = 15;
var farParallaxSpeed = 1;
var closeParallaxSpeed = 3;
var enemyHeight = 60;
var enemyWidth = 120;
var enemySpawnRate = 1000;
var shipHealth = 5;
var enemyHealth = 2;
var playerStamina = 10;
function Enemy(node, value){
this.speed = 5;
this.node = node;
this.update = function(){
this.node.x(-this.speed, true);
};
};
var playerHeight = 86;
var playerWidth = 151;
function Player(){
};
var missileSpeed = 25;
var symbolIndex = 0;
var background1 = new $.gQ.Animation({imageURL: "https://upload.wikimedia.org/wikipedia/commons/f/f3/Galaxy_history_revealed_by_the_Hubble_Space_Telescope_%28GOODS-ERS2%29.jpg"});
var background2 = new $.gQ.Animation({imageURL: "https://upload.wikimedia.org/wikipedia/commons/f/f3/Galaxy_history_revealed_by_the_Hubble_Space_Telescope_%28GOODS-ERS2%29.jpg"});
var background3 = new $.gQ.Animation({imageURL: "https://upload.wikimedia.org/wikipedia/commons/f/f3/Galaxy_history_revealed_by_the_Hubble_Space_Telescope_%28GOODS-ERS2%29.jpg"});
var background4 = new $.gQ.Animation({imageURL: "https://upload.wikimedia.org/wikipedia/commons/f/f3/Galaxy_history_revealed_by_the_Hubble_Space_Telescope_%28GOODS-ERS2%29.jpg"});
$("#playground").playground({height: PLAYGROUND_HEIGHT, width: PLAYGROUND_WIDTH, keyTracker: true});
$.playground().addGroup("background", {width: PLAYGROUND_WIDTH, height: PLAYGROUND_HEIGHT})
.addSprite("background1", {animation: background1, width: PLAYGROUND_WIDTH, height: PLAYGROUND_HEIGHT})
.addSprite("background2", {animation: background2, width: PLAYGROUND_WIDTH, height: PLAYGROUND_HEIGHT, posx: PLAYGROUND_WIDTH})
.addSprite("background3", {animation: background3, width: PLAYGROUND_WIDTH, height: PLAYGROUND_HEIGHT})
.addSprite("background4", {animation: background4, width: PLAYGROUND_WIDTH, height: PLAYGROUND_HEIGHT, posx: PLAYGROUND_WIDTH})
.end()
.addGroup("enemies", {width: PLAYGROUND_WIDTH, height: PLAYGROUND_HEIGHT})
.end()
.addGroup("player", {posx: 0, posy: PLAYGROUND_HEIGHT/2, width: playerWidth, height: playerHeight})
.addSprite("playerBody",{animation: '', posx: 0, posy: 0, width: playerWidth, height: playerHeight})
.end()
.addGroup("playerMissileLayer",{width: PLAYGROUND_WIDTH, height: PLAYGROUND_HEIGHT}).end()
$("#player")[0].player = new Player();
$.playground().registerCallback(function(){
$("#background1").x(($("#background1").x() - farParallaxSpeed - PLAYGROUND_WIDTH) % (-2 * PLAYGROUND_WIDTH) + PLAYGROUND_WIDTH);
$("#background2").x(($("#background2").x() - farParallaxSpeed - PLAYGROUND_WIDTH) % (-2 * PLAYGROUND_WIDTH) + PLAYGROUND_WIDTH);
$("#background3").x(($("#background3").x() - closeParallaxSpeed - PLAYGROUND_WIDTH) % (-2 * PLAYGROUND_WIDTH) + PLAYGROUND_WIDTH);
$("#background4").x(($("#background4").x() - closeParallaxSpeed - PLAYGROUND_WIDTH) % (-2 * PLAYGROUND_WIDTH) + PLAYGROUND_WIDTH);
$(".enemy").each(function(){
this.enemy.update();
if(($(this).x()+ enemyWidth) < 0){
$(this).remove();
} else {
var collided = $(this).collision("#playerBody,."+$.gQ.groupCssClass);
if(collided.length > 0){
$("#player")[0].player.value += $(this)[0].enemy.value;
$("#player")[0].player.number = $(this)[0].enemy.value;
$("#player .value").html($("#player")[0].player.value);
$("#player .number").html($("#player")[0].player.number);
$(this).remove();
}
}
});
$(".playerMissiles").each(function(){
var posx = $(this).x();
if(posx > PLAYGROUND_WIDTH){
$(this).remove();
}else{
$(this).x(missileSpeed, true);
var collided = $(this).collision(".enemy,."+$.gQ.groupCssClass);
if(collided.length > 0){
collided.each(function(){
//var possible_value = Math.round(eval($(this)[0].enemy.value + " " + symbols[symbolIndex] + " " + $('#player')[0].player.number));
})
$(this).remove();
};
};
});
if(jQuery.gameQuery.keyTracker[37]){
var nextpos = $("#player").x()-5;
if(nextpos > 0){
$("#player").x(nextpos);
}
}
if(jQuery.gameQuery.keyTracker[39]){
var nextpos = $("#player").x()+5;
if(nextpos < PLAYGROUND_WIDTH - playerWidth){
$("#player").x(nextpos);
}
}
if(jQuery.gameQuery.keyTracker[38]){
var nextpos = $("#player").y()-5;
if(nextpos > 0){
$("#player").y(nextpos);
}
}
if(jQuery.gameQuery.keyTracker[40]){
var nextpos = $("#player").y()+5;
if(nextpos < PLAYGROUND_HEIGHT - playerHeight){
$("#player").y(nextpos);
}
}
}, REFRESH_RATE);
$.playground().registerCallback(function(){
var enemyValue = Math.ceil(Math.random()*21) - 11;
var name = "enemy_"+(new Date).getTime();
$("#enemies").addSprite(name, {animation: '', posx: PLAYGROUND_WIDTH, posy: Math.random()*PLAYGROUND_HEIGHT*0.9,width: enemyWidth, height: enemyHeight});
var enemyElement = $("#"+name);
enemyElement.addClass("enemy");
enemyElement[0].enemy = new Enemy(enemyElement, enemyValue);
enemyElement.text(enemyValue);
}, enemySpawnRate);
$(document).keydown(function(e){
if(e.keyCode === 32){
var playerposx = $("#player").x();
var playerposy = $("#player").y();
var name = "playerMissile_"+(new Date()).getTime();
$("#playerMissileLayer").addSprite(name, {posx: playerposx + playerWidth, posy: playerposy, width: playerWidth/2,height: playerHeight/2});
$("#"+name).addClass("playerMissiles");
$("#"+name).html("<div>"+$("#player")[0].player.number+"</div>");
} else if(e.keyCode === 70){
symbolIndex = (symbolIndex+1)%symbols.length;
$("#player .symbol").text(symbols[symbolIndex]);
};
});
$.playground().startGame();