Advertisement
Guest User

snake za sasho

a guest
Dec 15th, 2019
96
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.88 KB | None | 0 0
  1. // Creating variables
  2. var myX = 0, myY = 0,snakeX=[],snakeY=[],snakesize= 4
  3. var points=0
  4.  
  5.  
  6. for(var i=0;i < snakesize;++i){
  7. snakeX[i]=15
  8. snakeY[i]=15+i
  9.  
  10. }
  11. var lastpressed = 38 ,counter=0
  12. var apppleX=Math.floor(Math.random()*20)
  13. var apppleY=Math.floor(Math.random()*20)
  14.  
  15. function update() {
  16. if(counter%20==0){
  17. for(var i=snakesize-1;i>0;--i){
  18. snakeX[i]=snakeX[i-1]
  19. snakeY[i]=snakeY[i-1]
  20.  
  21. }
  22. if(lastpressed == key_up){
  23. snakeY[0]=snakeY[0]-1
  24.  
  25. }if(lastpressed == key_down){
  26. snakeY[0]=snakeY[0]+1
  27.  
  28. }if(lastpressed == key_right){
  29. snakeX[0]=snakeX[0]+1
  30.  
  31. }if(lastpressed == key_left){
  32. snakeX[0]=snakeX[0]-1
  33.  
  34. }
  35. if(areColliding(snakeX[0]*30,snakeY[0]*30,29,29,apppleX*30,apppleY*30,29,29)){
  36. apppleX=Math.floor(Math.random()*20)
  37. apppleY=Math.floor(Math.random()*20)
  38. snakesize++
  39. ++points
  40.  
  41.  
  42. }
  43.  
  44.  
  45.  
  46. }
  47.  
  48. counter++
  49.  
  50. }
  51.  
  52.  
  53. function draw() {
  54. // This is how you draw a rectangle
  55.  
  56.  
  57.  
  58.  
  59.  
  60.  
  61. for(var i=0;i<30;++i){
  62. for(var j=0;j<30;++j) {
  63. context.fillStyle='black'
  64. context.fillRect(j*30,i*30,29,29)
  65.  
  66.  
  67. }
  68. }
  69. context.fillStyle='red'
  70. for(var i=0;i<snakesize;++i){
  71.  
  72. context.fillRect(snakeX[i]*30,snakeY[i]*30,29,29)
  73. }
  74. context.fillStyle='green'
  75. context.fillRect(apppleX*30,apppleY*30,29,29)
  76. };
  77.  
  78. function keyup(key) {
  79. // Show the pressed keycode in the console
  80. console.log("Pressed", key);
  81.  
  82. if(lastpressed==key_up && key==key_down){}
  83. else if(lastpressed==key_down && key==key_up){}
  84. else if(lastpressed==key_right && key==key_left){}
  85. else if(lastpressed==key_left && key==key_right){}
  86. else{
  87. lastpressed=key
  88. }
  89. };
  90.  
  91. function mouseup() {
  92. // Show coordinates of mouse on click
  93. console.log("Mouse clicked at", mouseX, mouseY);
  94. };
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement