This repository has been archived by the owner on Feb 3, 2021. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
/
15.07.09 City Scroller.html
172 lines (163 loc) · 4.06 KB
/
15.07.09 City Scroller.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
157
158
159
160
161
162
163
164
165
166
167
168
169
170
<html>
<head>
<script src="http://cloud.github.com/downloads/processing-js/processing-js/processing-1.4.1.min.js"></script>
<script type="text/processing" data-processing-target="mycanvas">
//THIS IS WHERE yoU WILL WRITE YOUR CODE!
var WIDTH = 1355;
var HEIGHT = 750;
var buildings = [];
var hero;
var jumpCounter;
var jumpHappened;
var boxes = [];
sunrise = loadImage("sunrise.jpg");
class Building {
var height, width, xPos;
Building(h, w, x) {
height = h;
width = w;
xPos = x;
}
void drawBuilding() {
fill(0);
rect(xPos, HEIGHT - height, width, height);
}
void moveBuilding() {
xPos -= 5;
}
}
class Obstacle {
var xPos, yPos, boxNum;
Obstacle(bN) {
xPos = WIDTH;
yPos = HEIGHT - 80;
boxNum = bN;
}
void drawBoxes() {
fill(125);
for(var i = 0; i < boxNum; i++) {
rect(xPos, yPos-40*i, 40, 40);
}
}
void moveBoxes() {
xPos -= 6;
}
}
class Runner {
var yPos, xPos;
Runner() {
xPos = WIDTH/2;
yPos = HEIGHT - 100;
}
void drawRunner() {
//println("Gonna draw a runner");
noStroke();
fill(255);
ellipse(xPos, yPos, 40, 40);
ellipse(xPos, yPos - 35, 30, 30);
//println("finished draing head and shoulders");
rect(xPos - 20, yPos, 40, 40); //gets hung up here
//println("torso");
rect(xPos - 10, yPos + 40, 20, 20);
//println("feet");
}
void walk() {
//if(xPos =
}
void jump() {
if(keyPressed) {
if(key === 'w' || key === 'W') {
yPos -= 50;
jumpHappened = true;
}
}
}
void jumpDown(){
if(yPos < HEIGHT - 100) {
yPos += 50;
}
else {
jumpHappened = false;
jumpCounter = 0;
}
}
}
void setup() {
size(WIDTH, HEIGHT);
background(125);
fill(255);
PFont fontA = loadFont("courier");
textFont(fontA, 14);
buildings.push(new Building(random(200, HEIGHT - 100), random(100, 200), WIDTH));
hero = new Runner();
boxes.push(new Obstacle(1));
}
void draw(){
//background(34, 239, 120);
image(sunrise, 0, 0, WIDTH, HEIGHT);
fill(0);
//rect(0, HEIGHT - 150, WIDTH, 300);
for(var i = 0; i < buildings.length; i++) {
buildings[i].drawBuilding();
buildings[i].moveBuilding();
//println("I animated the building")
}
//removes far left building if off screen
if(buildings[buildings.length-1].xPos < -1 * buildings[buildings.length-1].width) {
buildings.pop(buildings[buildings.length - 1]);
//println("I removed a building.");
}
//creates new building on the right
if(buildings[0].xPos < WIDTH - 100) {
//creates randomness
if(random(100) % 100 < 50) {
buildings = splice(buildings, new Building(random(200, HEIGHT - 100), random(100, 200), WIDTH), 0);
//println("I added a building.");
}
}
//println("I am going to draw the runner");
fill(192);
rect(0, HEIGHT - 50, WIDTH, 100);
hero.drawRunner();
if(jumpHappened) {
jumpCounter += 1;
if(jumpCounter > 10) {
hero.jumpDown();
}
}
//else {
hero.jump();
//}
for(var i = 0; i < boxes.length; i++) {
boxes[i].drawBoxes();
boxes[i].moveBoxes();
//println("I animated the building")
}
//removes far left building if off screen
if(boxes[boxes.length-1].xPos < -1 * boxes[boxes.length-1].width) {
boxes.pop(boxes[boxes.length - 1]);
//println("I removed a building.");
}
//creates new building on the right
if(boxes[0].xPos < WIDTH - 500) {
//creates randomness
if(random(100) % 100 < 50) {
boxes = splice(boxes, new Obstacle(int(random(1,3))));
//println("I added a building.");
}
}
}
/*void keyPressed() {
println("key pressed");
//if(key === "w" || key === "W") {
println("about to jump");
hero.jump();
}
}*/
//DO NOT WRITE CODE BELOW THIS SCRIPT TAG
</script>
</head>
<body>
<canvas id="mycanvas"></canvas>
</body>
</html>