- <!doctype html>
- <html>
- <head>
- <meta charset="UTF-8" />
- <title>Canvas Test</title>
- </head>
- <body>
- <section>
- <div>
- <canvas id="canvas" width="320" height="480">
- This text is displayed if your browser does not support HTML5 Canvas.
- </canvas>
- </div>
- <script type="text/javascript">
- var canvas;
- canvas = document.getElementById('canvas');
- context2D = canvas.getContext('2d');
- //Create Shot class for when the player shoots
- function Shot (type) {
- this.x = 100;
- this.y = 200;
- this.img = "shot.jpg";
- // Move the shot up and draw it
- this.update = function() {
- this.x -= 1;
- context2D.drawImage(this.img, this.x, this.y);
- //ctx.drawImage(image, x, y);
- }
- }
- // Create player class
- function Player (type) {
- this.x = 100;
- this.y = 150;
- this.img = new Image();
- this.img.src = "player.jpg";
- this.shots = new Array();
- // When the player shoots
- this.create_shot = function() {
- this.s = new Shot();
- this.shots.push(this.s);
- }
- // For all the shots fired, draw them in thier record position from the array
- this.update_shots = function() {
- index = null
- for (index = 0; index < this.shots. length; index++) {
- //call the update for s.update?
- }
- }
- // Update all the shots and draw the player
- this.run = function() {
- this.update_shots();
- context2D.drawImage(this.img, this.x, this.y);
- }
- }
- function clear() {
- //ctx.clearRect(0, 0, WIDTH, HEIGHT);
- }
- function init() {
- canvas = document.getElementById("canvas");
- ctx = canvas.getContext("2d");
- return setInterval(draw, 10);
- }
- function doKeyDown(evt){
- switch (evt.keyCode) {
- case 97: /* Up arrow was pressed */
- p1.y -= 5;
- break;
- case 40: /* Down arrow was pressed */
- p1.y += 5;
- break;
- case 37: /* Left arrow was pressed */
- p1.x -= 5;
- break;
- case 39: /* Right arrow was pressed */
- p1.x += 5;
- break;
- }
- }
- //Where do I create the player?
- var p1 = new Player();
- function draw() {
- clear();
- p1.run();
- //ctx.drawImage(image, x, y);
- }
- init();
- window.addEventListener('keydown',doKeyDown,true);
- </script>
- </section>
- </body>
- </html>