summary refs log tree commit diff stats
path: root/tests/closure/t1641.nim
Commit message (Expand)AuthorAgeFilesLines
* Add a test-case for #1641Daniil Yarancev2017-10-161-0/+20
at <elioat@tilde.institute> 2023-04-30 18:44:20 -0400 committer elioat <elioat@tilde.institute> 2023-04-30 18:44:20 -0400 *' href='/elioat/tour/commit/js/canvas/game.js?id=c5d26c499634c280696eba1c985e00259732ffa2'>c5d26c4 ^
c5d26c4 ^
c5d26c4 ^


569669d ^
a0c4292 ^
49cd686 ^
c5d26c4 ^

09b87ef ^

569669d ^
c5d26c4 ^
569669d ^

c5d26c4 ^

569669d ^


c5d26c4 ^
49cd686 ^


c5d26c4 ^




49cd686 ^
c5d26c4 ^



49cd686 ^
c5d26c4 ^



49cd686 ^
c5d26c4 ^



49cd686 ^
c5d26c4 ^


49cd686 ^
c5d26c4 ^


49cd686 ^
c5d26c4 ^



49cd686 ^
c5d26c4 ^

a0c4292 ^
c5d26c4 ^

a0c4292 ^
49cd686 ^













a0c4292 ^
49cd686 ^
569669d ^

c5d26c4 ^
a0c4292 ^
c5d26c4 ^



09e4cd1 ^
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
                                                                    
                                                     

                    
                                    


                                                
 
                                  
                

        

             
           
                   

                            

  


                                    
                     


                                                                                                                                              




                                                 
                              



                      
                              



                   
                              



                     
                              


                
                               


                
                                     



            
 

                     
                                

                                                   
                                                         













                                 
         
                               

                                                                               
 
               



                                  
                                
// configure the canvas -- our playspace...magic circle and what not
const canvas = document.getElementById("gameCanvas");
canvas.width = 512;
canvas.height = 512;
const ctx = canvas.getContext("2d");
ctx.fillStyle = "#e0f8cf";
ctx.fillRect(0, 0, canvas.width, canvas.height);


// our noble avatar into playspace
const player = {
  x: 50,
  y: 50,
  width: 16,
  height: 16,
  step: 10,
  color: "#65ff00",
  colorAlt: "pink",
  spriteUrl: "chickadee.svg"
};

const playerSprite = new Image();
playerSprite.src = player.spriteUrl;

// add keyboard input
// FIXME: this input is blocking, allowing for only 1 key to be read at a time
// this means that if you wanna do 2 things at 1 time you cannot.
// For more info, <https://medium.com/@dovern42/handling-multiple-key-presses-at-once-in-vanilla-javascript-for-game-controllers-6dcacae931b7>
document.addEventListener("keydown", (event) => {
  switch (event.code) {
    case "ArrowLeft":
    case "KeyH":
    case "KeyA":
      player.x -= player.step;
      break;
    case "ArrowRight":
    case "KeyL":
    case "KeyD":
      player.x += player.step;
      break;
    case "ArrowUp":
    case "KeyK":
    case "KeyW":
      player.y -= player.step;
      break;
    case "ArrowDown":
    case "KeyJ":
    case "KeyS":
      player.y += player.step;
      break;
    case "KeyZ":
    case "KeyN":
      player.color = "#65ff00";
      break;
    case "KeyX":
    case "KeyM":
      player.color = player.colorAlt;
      break;
  }
});


// game loop
function gameLoop() {
  // clear the canvas every tick
  ctx.clearRect(0, 0, canvas.width, canvas.height);

  // looping canvas space so you can't wander out of view
  // this feels like a hack...
  if (player.x < 0) {
    player.x = canvas.width - 1
  }
  if (player.x > canvas.width) {
    player.x = 1
  }
  if (player.y < 0) {
    player.y = canvas.height - 1
  }
  if (player.y > canvas.height) {
    player.y = 1
  }

  // draw
  ctx.fillStyle = player.color;
  ctx.fillRect(player.x, player.y, player.width, player.height);  
  ctx.drawImage(playerSprite, player.x, player.y, player.width, player.height);

  // next frame
  requestAnimationFrame(gameLoop);
}

// start the game loop
requestAnimationFrame(gameLoop);