Advertisement
here2share

# js_keyrotate.py

Nov 24th, 2019
293
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 2.95 KB | None | 0 0
  1. # js_keyrotate.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.  
  20. <script>
  21. var myGamePiece;
  22.  
  23. function startGame() {
  24.    myGamePiece = new component(30, 30, "red", 225, 225);
  25.    myGameArea.start();
  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.frameNo = 0;
  36.        this.interval = setInterval(updateGameArea, 20);
  37.        window.addEventListener('keydown', function (e) {
  38.            e.preventDefault();
  39.            myGameArea.keys = (myGameArea.keys || []);
  40.            myGameArea.keys[e.keyCode] = (e.type == "keydown");
  41.        })
  42.        window.addEventListener('keyup', function (e) {
  43.            myGameArea.keys[e.keyCode] = (e.type == "keydown");
  44.        })
  45.    },
  46.    stop : function() {
  47.        clearInterval(this.interval);
  48.    },    
  49.    clear : function() {
  50.        this.context.clearRect(0, 0, this.canvas.width, this.canvas.height);
  51.    }
  52. }
  53.  
  54. function component(width, height, color, x, y, type) {
  55.  
  56.    this.type = type;
  57.    this.width = width;
  58.    this.height = height;
  59.    this.speed = 0;
  60.    this.angle = 0;
  61.    this.moveAngle = 0;
  62.    this.x = x;
  63.    this.y = y;    
  64.    this.update = function() {
  65.        ctx = myGameArea.context;
  66.        ctx.save();
  67.        ctx.translate(this.x, this.y);
  68.        ctx.rotate(this.angle);
  69.        ctx.fillStyle = color;
  70.        ctx.fillRect(this.width / -2, this.height / -2, this.width, this.height);
  71.        ctx.restore();    
  72.    }
  73.    this.newPos = function() {
  74.        this.angle += this.moveAngle * Math.PI / 180;
  75.        this.x += this.speed * Math.sin(this.angle);
  76.        this.y -= this.speed * Math.cos(this.angle);
  77.    }
  78. }
  79.  
  80. function updateGameArea() {
  81.    myGameArea.clear();
  82.    myGamePiece.moveAngle = 0;
  83.    myGamePiece.speed = 0;
  84.    if (myGameArea.keys && myGameArea.keys[37]) {myGamePiece.moveAngle = -1; }
  85.    if (myGameArea.keys && myGameArea.keys[39]) {myGamePiece.moveAngle = 1; }
  86.    if (myGameArea.keys && myGameArea.keys[38]) {myGamePiece.speed= 1; }
  87.    if (myGameArea.keys && myGameArea.keys[40]) {myGamePiece.speed= -1; }
  88.    myGamePiece.newPos();
  89.    myGamePiece.update();
  90. }
  91. </script>
  92.  
  93. <p>Make sure the gamearea has focus, and use the arrow keys to move the red square around.</p>
  94.  
  95. </body>
  96. </html>
  97. '''
  98.  
  99. chrome_path = 'C:/Program Files (x86)/Google/Chrome/Application/chrome.exe %s'
  100.  
  101. tf = tempfile.mktemp(".html", "JSdemo_")
  102. print tf
  103. with open(tf, 'w') as temp:
  104.     temp.write(js_data)
  105. webbrowser.get(chrome_path).open(tf)
  106. os.remove(tf)
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement