Advertisement
here2share

# js_arrowkeys_vertical.py

Nov 24th, 2019
249
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 2.71 KB | None | 0 0
  1. # js_arrowkeys_vertical.py
  2.  
  3. import tempfile
  4. import webbrowser
  5. import os
  6.  
  7. js_data = '''<!DOCTYPE html>
  8. <html>
  9. <head>
  10. <meta name="viewport" content="width=device-width, initial-scale=1.0"/>
  11. <style>
  12. canvas {
  13.    border:1px solid #d3d3d3;
  14.    background-color: #f1f1f1;
  15. }
  16. </style>
  17. </head>
  18. <body onload="startGame()">
  19. <script>
  20.  
  21. var myGamePiece;
  22.  
  23. function startGame() {
  24.    myGameArea.start();
  25.    myGamePiece = new component(30, 30, "red", 10, 120);
  26. }
  27.  
  28. var myGameArea = {
  29.    canvas : document.createElement("canvas"),
  30.    start : function() {
  31.        this.canvas.width = 480;
  32.        this.canvas.height = 270;
  33.        this.context = this.canvas.getContext("2d");
  34.        document.body.insertBefore(this.canvas, document.body.childNodes[0]);
  35.        this.interval = setInterval(updateGameArea, 20);
  36.        window.addEventListener('keydown', function (e) {
  37.            myGameArea.keys = (myGameArea.keys || []);
  38.            myGameArea.keys[e.keyCode] = (e.type == "keydown");
  39.        })
  40.        window.addEventListener('keyup', function (e) {
  41.            myGameArea.keys[e.keyCode] = (e.type == "keydown");            
  42.        })
  43.    },
  44.    clear : function(){
  45.        this.context.clearRect(0, 0, this.canvas.width, this.canvas.height);
  46.    }
  47. }
  48.  
  49. function component(width, height, color, x, y) {
  50.    this.gamearea = myGameArea;
  51.    this.width = width;
  52.    this.height = height;
  53.    this.speedX = 0;
  54.    this.speedY = 0;    
  55.    this.x = x;
  56.    this.y = y;    
  57.    this.update = function() {
  58.        ctx = myGameArea.context;
  59.        ctx.fillStyle = color;
  60.        ctx.fillRect(this.x, this.y, this.width, this.height);
  61.    }
  62.    this.newPos = function() {
  63.        this.x += this.speedX;
  64.        this.y += this.speedY;        
  65.    }    
  66. }
  67.  
  68. function updateGameArea() {
  69.    myGameArea.clear();
  70.    myGamePiece.speedX = 0;
  71.    myGamePiece.speedY = 0;    
  72.    if (myGameArea.keys && myGameArea.keys[37]) {myGamePiece.speedX = -1; }
  73.    if (myGameArea.keys && myGameArea.keys[39]) {myGamePiece.speedX = 1; }
  74.    if (myGameArea.keys && myGameArea.keys[38]) {myGamePiece.speedY = -1; }
  75.    if (myGameArea.keys && myGameArea.keys[40]) {myGamePiece.speedY = 1; }
  76.    myGamePiece.newPos();    
  77.    myGamePiece.update();
  78. }
  79. </script>
  80. <p>Make sure this window has focus, then use the arrow keys on you keyboard to move the red square.</p>
  81. <p>When pressing both the left and the down arrow, the red square will move both down and to the left.</p>
  82. </body>
  83. </html>
  84. '''
  85.  
  86. chrome_path = 'C:/Program Files (x86)/Google/Chrome/Application/chrome.exe %s'
  87.  
  88. tf = tempfile.mktemp(".html", "JSdemo_")
  89. print tf
  90. with open(tf, 'w') as temp:
  91.     temp.write(js_data)
  92. webbrowser.get(chrome_path).open(tf)
  93. os.remove(tf)
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement