-
Notifications
You must be signed in to change notification settings - Fork 0
/
Snakes.dart
107 lines (99 loc) · 2.07 KB
/
Snakes.dart
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
#import('dart:html');
num posx=0,posy=0;
num x=0;
num flag=3,fl=0;
double bonusx,bonusy;
num score=0;
CanvasRenderingContext2D ctx;
void draw()
{
ctx.clearRect(0, 0, 450, 300);
ctx.fillStyle="#FF0000";
ctx.fillRect(posx*10,posy*10,10,10);
ctx.fillStyle="#FFFF00";
ctx.fillRect(bonusx*10,bonusy*10,10,10);
var ms = document.query("#msg");
ms.innerHTML="SCORE: "+score.toString();
}
void func()
{
switch(flag)
{
case 1:
posx--;
draw();
break;
case 2:
posx++;
draw();
break;
case 3:
posy++;
draw();
break;
case 4:
posy--;
draw();
break;
}
if(((posx==bonusx && posy==bonusy)) && fl==0)
{
bonusx = ((Math.random() * 100 ).floor() % 46);
bonusy=(Math.random() * 100 ).floor() % 31;
score+=10;
x+=5;
}
//document.on.keyDown
if(posx<0 || posx>=46 || posy<0 || posy>=31)
{
var msg_score = document.query("#msg");
fl=1;
msg_score.innerHTML = "Thank you for playing the game.<br /> Your Score : <b>"+score+"</b><br /><br /><input type='button' value='Play Again' id='play' />";
document.query("#play").on.click.add((Event e){
//window.location.reload();
print("hi");
});
document.query("#playArea").style.display = 'none';
window.clearInterval(value);
}
}
var value;
void main()
{
CanvasElement canvas = document.query("#playArea");
ctx = canvas.getContext("2d");
bonusx = ((Math.random() * 100 ).floor() % 46);
bonusy=(Math.random() * 100 ).floor() % 31;
value=window.setInterval(func,70-x);
if(fl==0)
{
document.on.keyDown.add((Event e){
KeyboardEvent ke=e;
//print(ke.keyIdentifier);
if (ke.keyIdentifier=="U+0061" ||ke.keyIdentifier=="U+0041")
{
posx--;
draw();
flag=1;
}
else if (ke.keyIdentifier=="U+0064" ||ke.keyIdentifier=="U+0044")
{
posx++;
draw();
flag=2;
}
else if (ke.keyIdentifier=="U+0053" ||ke.keyIdentifier=="U+0073")
{
posy++;
draw();
flag=3;
}
else if (ke.keyIdentifier=="U+0077" ||ke.keyIdentifier=="U+0057")
{
posy--;
draw();
flag=4;
}
});
}
}