forked from filipporaciti/AppleSnake-p5js
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathsketch.js
86 lines (65 loc) · 2.1 KB
/
sketch.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
let canvasWidth = side*20
let canvasHeight = side*20
const timeInterval = 200
let canvasY = 135
let score = 0
let press = false
let touchMov = ''
let gameInterval
function setup() {
while(window.innerHeight < canvasHeight+canvasY || window.innerWidth < canvasWidth){
side -= 1
canvasHeight = side*20
canvasWidth = side*20
}
let canvas = createCanvas(canvasWidth, canvasHeight);
canvas.position(windowWidth/2-width/2, canvasY)
snake = new Snake(canvasWidth/2,canvasHeight)
gameInterval = setInterval(() => snake.moveInterval(), timeInterval)
}
function draw() {
background(0)
snake.show()
}
function controls(){
if(keyIsPressed == true || touchMov != ''){
if ((key == "ArrowLeft" || key == "a" || touchMov == 'l') && !press && snake.scelta != 'r') {
snake.move('l')
}
if ((key == "ArrowRight" || key == "d" || touchMov == 'r') && !press && snake.scelta != 'l') {
snake.move('r')
}
if ((key == "ArrowDown" || key == "s" || touchMov == 'b') && !press && snake.scelta != 'u') {
snake.move('b')
}
if ((key == "ArrowUp" || key == "w" || touchMov == 'u') && !press && snake.scelta != 'b') {
snake.move('u')
}
press = true
touchMov = ''
}else{
press = false
}
}
function touchStarted() {
touchMov = ''
startX = mouseX
startY = mouseY
}
function touchEnded() {
if(startX < mouseX && ((mouseX-startX) > Math.pow(Math.pow(mouseY-startY, 2), 0.5))){
touchMov = 'r'
}else if(startX >= mouseX && ((startX-mouseX) > Math.pow(Math.pow(mouseY-startY, 2), 0.5))){
touchMov = 'l'
}else if(startY < mouseY && ((mouseY-startY) > Math.pow(Math.pow(mouseX-startX, 2), 0.5))){
touchMov = 'b'
}else if(startY >= mouseY && ((startY-mouseY) > Math.pow(Math.pow(mouseX-startX, 2), 0.5))){
touchMov = 'u'
}
controls()
}
function windowResized() {
let canvas = createCanvas(canvasWidth, canvasHeight);
canvas.position(windowWidth/2-width/2, canvasY)
setCanvasSize()
}