Skip to content

Commit 6cc40e3

Browse files
author
Austin Putman, austinfromboston & Cooper Quintin, cooperq
committed
set player on platform at start; player falls; configurable jump accelleration and graavity; game_loop is pausable;
1 parent 495df27 commit 6cc40e3

File tree

9 files changed

+55
-18
lines changed

9 files changed

+55
-18
lines changed

.gitignore

+1
Original file line numberDiff line numberDiff line change
@@ -1,2 +1,3 @@
11
**swp
22
.DS_Store
3+
.idea

client/index.html

+1-1
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,7 @@
1313
<script type="text/javascript">
1414
$(function(){
1515
game = new Game();
16-
player = new Player( {name: prompt('What is your name?') });
16+
player = new Player( {name: prompt('What is your name?'), position: {x:400 , y: 460}});
1717
});
1818
</script>
1919
</head>

client/js/avatar.js

+4-2
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@ Avatar = function(options){
66
right: false
77
};
88
this.velocity = {x:0, y:0};
9-
this.position = {x:0,y:0};
9+
this.position = options.position || {x:0, y:0};
1010
this.dom_element = $('<div id="avatar-'+this.id+'" class="avatar"><div class="avatar-name">'+this.name+'</div></div>')
1111
};
1212

@@ -20,12 +20,14 @@ Avatar.prototype = {
2020
},
2121

2222
accelerate_up: function(){
23-
this.velocity.y++;
23+
this.velocity.y += AVATAR_JUMP_ACCEL;
2424
},
2525

2626
update_position : function(){
2727
if(this.move.left) this.accelerate_left();
2828
if(this.move.right) this.accelerate_right();
29+
//deal with gravity
30+
this.velocity.y += GRAVITY;
2931

3032
this.position.x += this.velocity.x;
3133
this.position.y += this.velocity.y;

client/js/config.js

+2
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,7 @@
11
ONE_GAME_TICK = 50;
2+
GRAVITY=.3;
23
AVATAR_RUN_ACCEL = 3;
4+
AVATAR_JUMP_ACCEL = -10;
35
AVATAR_FRICTION = 0.5;
46

57
Config = {

client/js/game.js

+7-2
Original file line numberDiff line numberDiff line change
@@ -17,13 +17,18 @@ var Game = function() {
1717
self.build_display = function(){
1818
//load level
1919
$('#level-container').html(self.current_level.html);
20-
self.next_tick();
20+
//self.next_tick();
2121
};
2222
self.next_tick = function(){
2323
self.update_sprites();
2424
self.refresh_display();
2525
};
26-
setInterval(function() { self.next_tick(); }, ONE_GAME_TICK);
26+
27+
var loopTimer = setInterval(function() { self.next_tick(); }, ONE_GAME_TICK);
28+
29+
self.pause = function() {
30+
clearInterval(loopTimer);
31+
};
2732

2833
return self;
2934
}

client/js/player.js

+2-1
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,9 @@
11
var Player = function( options ) {
22
options = options || {};
33
options.name = options.name || "anonymous";
4+
45

5-
this.avatar = new Avatar({name:options.name});
6+
this.avatar = new Avatar(options);
67

78
this.initialize_keyboard_bindings();
89

client/spec/avatar_spec.js

+16-6
Original file line numberDiff line numberDiff line change
@@ -1,14 +1,24 @@
11
describe('Avatar', function() {
22
var player, avatar;
3+
beforeEach(function() {
4+
game = new Game();
5+
player = new Player('fred');
6+
avatar = player.avatar;
7+
});
38

49
describe('accelerate_up', function() {
5-
beforeEach(function() {
6-
player = new Player('fred');
7-
avatar = player.avatar;
8-
});
9-
it('should add 1 to the velocity.y', function() {
10+
it('should subtract 1 from the velocity.y', function() {
1011
avatar.accelerate_up();
11-
expect(avatar.velocity.y).toEqual(1);
12+
expect(avatar.velocity.y).toEqual(AVATAR_JUMP_ACCEL);
13+
});
14+
});
15+
describe('gravity', function() {
16+
it("every game tick should add to velocity.y", function() {
17+
expect(avatar.velocity.y).toEqual(0);
18+
jasmine.Clock.tick(ONE_GAME_TICK);
19+
expect(avatar.velocity.y).toEqual(GRAVITY);
20+
jasmine.Clock.tick(ONE_GAME_TICK);
21+
expect(avatar.velocity.y).toEqual(GRAVITY * 2);
1222
});
1323
});
1424
});

client/spec/game_spec.js

+12
Original file line numberDiff line numberDiff line change
@@ -42,13 +42,25 @@ describe("Game", function() {
4242
expect(game.next_tick.calls.length).toBe(4);
4343
});
4444

45+
it('should be possible to stop the game loop', function() {
46+
spyOn(game, 'next_tick').andCallThrough();
47+
jasmine.Clock.tick(ONE_GAME_TICK );
48+
jasmine.Clock.tick(ONE_GAME_TICK );
49+
expect(game.next_tick.calls.length).toBe(2);
50+
game.pause();
51+
jasmine.Clock.tick(ONE_GAME_TICK );
52+
jasmine.Clock.tick(ONE_GAME_TICK );
53+
expect(game.next_tick.calls.length).toBe(2);
54+
});
55+
4556

4657
describe('on next_tick', function(){
4758
describe('refresh_display',function(){
4859
describe("moves the .avatar div", function(){
4960
beforeEach(function(){
5061
game = new Game();
5162
player = new Player();
63+
game.next_tick();
5264
});
5365

5466
it('should move left when player presses left', function(){

client/spec/player_spec.js

+10-6
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,12 @@ describe("Player", function() {
2323
it('should start in position 0', function() {
2424
expect(player.avatar.position.x).toEqual(0);
2525
});
26+
27+
it("should be possible to define the initial position", function() {
28+
player = new Player({position: {x:360, y:550}});
29+
expect(player.avatar.position.x).toEqual(360);
30+
expect(player.avatar.position.y).toEqual(550);
31+
});
2632

2733
it("should display the player's name in #hello div", function() {
2834
$('#jasmine_content').html('<div id="hello"></div>');
@@ -70,20 +76,18 @@ describe("Player", function() {
7076

7177
});
7278

73-
describe("on_up_arrow", function() {
79+
describe("on up arrow", function() {
7480
beforeEach(function() {
75-
var event = jQuery.Event('keydown');
76-
event.keyCode = Config.key_codes.up;
77-
$game_container.trigger(event);
81+
simulate_up_key_press();
7882
});
7983

8084
it('should change velocity on keydown', function(){
81-
expect(player.avatar.velocity.y).toEqual(1);
85+
expect(player.avatar.velocity.y).toEqual(AVATAR_JUMP_ACCEL);
8286
});
8387

8488
it('should move up', function(){
8589
game.next_tick();
86-
expect(player.avatar.position.y).toEqual(1);
90+
expect(player.avatar.position.y).toEqual(AVATAR_JUMP_ACCEL + GRAVITY);
8791
});
8892

8993
});

0 commit comments

Comments
 (0)