-
Notifications
You must be signed in to change notification settings - Fork 1
/
CarGame.html
executable file
·156 lines (142 loc) · 5 KB
/
CarGame.html
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
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
<!doctype html>
<html lang="en">
<head>
<meta charset="UTF-8" />
<title>Phaser - CarGame</title>
<script type="text/javascript" src="js/phaser.min.js"></script>
<style type="text/css">
body {
margin: 0;
}
</style>
</head>
<body>
<section style="width: 100%; height:100%; display:flex; flex-direction: column; align-items: center ; justify-content: center;">
<h1 style="text-align: center">Phaser - Car Game</h1>
<div id="phaser-game" style="display:block;padding-top: 40px">
</div>
</section>
<script type="text/javascript">
var game = new Phaser.Game(800, 600, Phaser.AUTO, 'phaser-game', { preload: preload, create: create, update: update, render: render });
function preload() {
game.load.image('road', 'assets/road.png');
game.load.image('car', 'assets/car.png');
game.load.image('oil', 'assets/barrel.png');
game.load.image('block', 'assets/block.png');
}
var car;
var road;
var cursors;
var distance = 0;
var tileCounter = 1;
var oilGroup;
var blockGroup;
var timer;
var flag = true;
var firingTimer = 0;
var pos;
var num;
var score = 0;
var labelScore;
var noOil = false;
var noBlock = false;
function addOil(x, y) {
if (!noOil) {
var oil = oilGroup.getFirstExists(false);
if (oil != null) {
oil.reset(x, y);
oil.body.velocity.y = 600;
}
else {
noOil = true;
addBlock(x, y);
}
}
}
function addBlock(x, y) {
if (!noOil) {
var block = blockGroup.getFirstExists(false);
if (block != null) {
block.reset(x, y);
block.body.velocity.y = 600;
}
else {
noBlock = true;
addOil(x, y);
}
}
}
function addObj() {
pos = Math.floor(Math.random() * 3) + 1;
num = Math.floor(Math.random() * 2) + 1;
if (noOil && noBlock) {
game.pause();
}
if (num == 1) {
addOil(85 + 132 * pos, -100)
}
else {
addBlock(68 + 132 * pos, 0);
}
firingTimer = game.time.now + 2000;
}
function create() {
game.physics.startSystem(Phaser.Physics.ARCADE);
road = game.add.tileSprite(200, 0, 400, 600, 'road');
car = game.add.sprite(335, 305, 'car');
labelScore = game.add.text(20, 20, "0", { font: "30px Arial", fill: "#ffffff" });
game.physics.enable(car, Phaser.Physics.ARCADE);
game.physics.enable(road, Phaser.Physics.ARCADE);
oilGroup = game.add.group();
oilGroup.enableBody = true;
oilGroup.physicsBodyType = Phaser.Physics.ARCADE;
oilGroup.createMultiple(30, 'oil');
oilGroup.setAll('outOfBoundsKill', true);
oilGroup.setAll('checkWorldBounds', true);
blockGroup = game.add.group();
blockGroup.enableBody = true;
blockGroup.physicsBodyType = Phaser.Physics.ARCADE;
blockGroup.createMultiple(30, 'block');
blockGroup.setAll('outOfBoundsKill', true);
blockGroup.setAll('checkWorldBounds', true);
cursors = game.input.keyboard.createCursorKeys();
}
function incrementScore(car, oil) {
score = score + 10;
labelScore.text = score;
oil.kill();
}
function decrementScore(car, block) {
score = score - 10;
labelScore.text = score;
block.kill();
}
function update() {
game.physics.arcade.overlap(car, oilGroup, incrementScore, null, this);
game.physics.arcade.overlap(car, blockGroup, decrementScore, null, this);
road.tilePosition.y += 10;
distance = tileCounter * 600 + road.tilePosition.y;
if (road.tilePosition.y == 600) {
tileCounter = tileCounter + 1;
}
car.body.velocity.setTo(0, 0);
if (cursors.left.isDown) {
if (car.x > 212) {
car.body.velocity.x = -500;
}
}
else if (cursors.right.isDown) {
if (car.x < 475) {
car.body.velocity.x = 500;
}
}
if (game.time.now > firingTimer) {
addObj();
}
}
function render() {
// game.debug.text(" Distance::"+distance+" tilecounter::"+tilecounter+' Debugging Phaser::'+road.tilePosition.y, 20, 20, 'yellow', 'Segoe UI');
}
</script>
</body>
</html>