-
Notifications
You must be signed in to change notification settings - Fork 1
/
game.back.js
86 lines (73 loc) · 2.54 KB
/
game.back.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
//load the AMD modules we need
require(['frozen/GameCore', 'frozen/ResourceManager', 'dojo/keys'], function(GameCore, ResourceManager, keys){
var speed = 10.0;
var obs_speed = 10;
locations = {}
locations["cat"] = {"x":100,"y":100}
locations["coin"] = {"x":Math.floor(Math.random()*1400),"y":Math.floor(Math.random()*800)}
//setup a ResourceManager to use in the game
var rm = new ResourceManager();
var backImg = rm.loadImage('images/background.png');
var nyan = rm.loadImage('images/eagle.gif');
var coin = rm.loadImage('images/bacon.gif');
//setup a GameCore instance
var game = new GameCore({
canvasId: 'canvas',
resourceManager: rm,
initInput: function(im){ //im = this.inputManager
//tells the input manager to listen for key events
im.addKeyAction(keys.LEFT_ARROW);
im.addKeyAction(keys.RIGHT_ARROW);
im.addKeyAction(keys.UP_ARROW);
im.addKeyAction(keys.DOWN_ARROW);
},
handleInput: function(im){
//just an example showing how to check for presses, could be done more effeciently
if(im.keyActions[keys.LEFT_ARROW].isPressed()){
locations["cat"]["x"] -= speed;
if (locations["cat"]["x"] > 1400) {
locations["cat"]["x"] = 1400;
}
}
if(im.keyActions[keys.RIGHT_ARROW].isPressed()){
locations["cat"]["x"] += speed;
if (locations["cat"]["x"] < 0) {
locations["cat"]["x"] = 0;
}
}
if(im.keyActions[keys.UP_ARROW].isPressed()){
locations["cat"]["y"] -= speed;
if (locations["cat"]["y"] > 800) {
locations["cat"]["y"] = 800;
}
}
if(im.keyActions[keys.DOWN_ARROW].isPressed()){
locations["cat"]["y"] += speed;
if (locations["cat"]["y"] < 0) {
locations["cat"]["y"] = 0;
}
}
},
update: function(millis){
locations['coin']['x'] -= obs_speed;
if (locations['coin']['x'] < -600) {
locations["coin"] = {"x":1600,"y":Math.floor(Math.random()*800)}
}
if ((locations['coin']['x'] - locations['cat']['x'] < 100) && (locations['coin']['y'] - locations['cat']['y'] < 100)) {
locations["coin"] = {"x":1600,"y":Math.floor(Math.random()*800)}
speed += 1;
obs_speed += 1;
}
console.log
},
draw: function(context){
context.drawImage(backImg, 0, 0, this.width, this.height);
context.drawImage(nyan, locations['cat']['x'], locations['cat']['y']);
context.drawImage(coin, locations['coin']['x'], locations['coin']['y']);
}
});
//if you want to take a look at the game object in dev tools
console.log(game);
//launch the game!
game.run();
});