-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathsketch.js
More file actions
135 lines (117 loc) · 3.46 KB
/
sketch.js
File metadata and controls
135 lines (117 loc) · 3.46 KB
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
var CURRENT_SHAPE
function setup() {
window.canvas = createCanvas(200,400)
window.canvas.parent("canvas")
this.DROPSPEED = 15
this.SCORE = 0
this.HOLDING = undefined
this.HOLDING_COOLDOWN = false
this.DROPSPEED_MAX = this.DROPSPEED // For resetting the timer
this.SHAPE_LIST = []
this.BAG = []
initShapes()
fillBag()
spawnShape()
}
function draw() {
background(185)
frameRate(25)
drawGrid()
drawShapes()
if (this.DROPSPEED === 0) {
CURRENT_SHAPE.moveDown()
this.DROPSPEED = this.DROPSPEED_MAX
}
this.DROPSPEED--
document.getElementById("score").innerHTML = "SCORE: " + this.SCORE
document.getElementById("next").innerHTML = "NEXT: " + this.BAG[0].name
if (this.HOLDING !== undefined) {
document.getElementById("holding").innerHTML = "HOLDING: " + this.HOLDING.shapeID.name
}
}
function getShapeList() {
return this.SHAPE_LIST
}
function keyPressed() {
if (keyCode === 38 || keyCode === 88) { // UP ARROW or X | Rotate Right
this.CURRENT_SHAPE.rotateShape()
} else if (keyCode === 17 || keyCode === 89) { // CTRL or Y | Rotate Left
this.CURRENT_SHAPE.rotateShape(0)
} else if (keyCode === 40) { // DOWN ARROW | Move Down
this.CURRENT_SHAPE.moveDown()
} else if (keyCode === 32) { // SPACE | Hard Drop (currently Spawning shape)
this.CURRENT_SHAPE.hardDrop()
} else if (keyCode === 67 || keyCode === 16) { // SHIFT or C | Hold Piece
if (!this.HOLDING_COOLDOWN) {
if (this.HOLDING === undefined) {
hold()
} else {
releaseHolding()
}
this.HOLDING_COOLDOWN = true
}
} else if (keyCode === 37) { // LEFT ARROW | Move Left
this.CURRENT_SHAPE.moveLeft()
} else if (keyCode === 39) { // RIGHT ARROW | Move Right
this.CURRENT_SHAPE.moveRight()
}
}
function hold() {
this.HOLDING = new Shape(this.CURRENT_SHAPE.shapeID, createVector(5, -2))
let index = this.SHAPE_LIST.indexOf(this.CURRENT_SHAPE)
this.SHAPE_LIST.splice(index, 1)
spawnShape()
}
function drawShapes() {
for (let shape of this.SHAPE_LIST) {
shape.draw()
}
}
function drawGrid() {
let gridsize = 20
push()
stroke(0)
for (let i = 0; i < window.canvas.width; i++) {
if (i % gridsize === 0) {
line(i,0,i,window.canvas.height)
}
}
for (let i = 0; i < window.canvas.height; i++) {
if (i % gridsize === 0) {
line(0,i,window.canvas.width,i)
}
}
pop()
}
// Simple Random shape spawn
function spawnShape() {
let shape = new Shape(this.BAG[0], createVector(5, -2))
this.SHAPE_LIST.push(shape)
this.CURRENT_SHAPE = shape
this.BAG.shift()
if (this.BAG.length === 0) {
fillBag()
}
}
function releaseHolding() {
let old = this.CURRENT_SHAPE
this.SHAPE_LIST.push(this.HOLDING)
this.CURRENT_SHAPE = this.HOLDING
this.HOLDING = new Shape(old.shapeID, createVector(5, -2))
let index = this.SHAPE_LIST.indexOf(old)
this.SHAPE_LIST.splice(index, 1)
}
function fillBag() {
shapes = [iShape, oShape, tShape, zShape, sShape, lShape, jShape]
for (let i = 0; i < 7; i++) {
index = Math.floor(Math.random() * shapes.length)
this.BAG.push(shapes[index])
shapes.splice(index,1)
}
}
function score() {
this.SCORE++
}
function resetHoldingCooldown() {
this.HOLDING_COOLDOWN = false
}