Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- <html>
- <head></head>
- <body>
- <div id="marioField" style="position: relative;">
- <label id="score" style="position: absolute; top: 0px; left: 800px; font-size: 24px;">Score : 0</label>
- <img style="position: absolute; top: 200px;" id="mario" width="50" height="50" src="mario.jpg" />
- <img style="position: absolute; top: 50px; left: 800px;" class="coin" width="30" height="30" src="coin.gif" />
- <img style="position: absolute; top: 75px; left: 600px;" class="coin" width="30" height="30" src="coin.gif" />
- <img style="position: absolute; top: 75px; left: 400px;" class="coin" width="30" height="30" src="coin.gif" />
- <img style="position: absolute; top: 100px; left: 1000px;" class="coin" width="30" height="30" src="coin.gif" />
- <img style="position: absolute; top: 100px; left: 1200px;" class="coin" width="30" height="30" src="coin.gif" />
- <img style="position: absolute; top: 90px; left: 800px;" class="coin" width="30" height="30" src="coin.gif" />
- <img style="position: absolute; top: 130px; left: 800px;" class="coin" width="30" height="30" src="coin.gif" />
- <img style="position: absolute; top: 170px; left: 800px;" class="coin" width="30" height="30" src="coin.gif" />
- <img style="position: absolute; top: 150px; left: 900px;" class="coin" width="30" height="30" src="coin.gif" />
- <img style="position: absolute; top: 150px; left: 700px;" class="coin" width="30" height="30" src="coin.gif" />
- <img style="position: absolute; top: 150px; left: 600px;" class="coin" width="30" height="30" src="coin.gif" />
- <img style="position: absolute; top: 200px; left: 800px;" class="enemy" width="60" height="50" src="enemy.png" title="3" />
- <img style="position: absolute; top: 200px; left: 300px;" class="enemy" width="60" height="50" src="enemy.png" title="3" />
- <img style="position: absolute; top: 200px; left: 100px;" class="enemy" width="60" height="50" src="enemy.png" title="3" />
- <img style="position: absolute; top: 200px; left: 1500px;" class="enemy" width="60" height="50" src="enemy.png" title="3" />
- </div>
- <script type="text/javascript">
- //---- PlayGround -----
- var onAir = false;
- var running = false;
- var av = 5;
- var inertia = 10;
- var speed = 0;
- var vspeed = 0;
- var maxSpeed = 10;
- var left = 0;
- var gravity = 2;
- var score = 0;
- function Update() {
- if (running || onAir) {
- if (speed > maxSpeed) {
- speed = maxSpeed;
- }
- else if (speed < -maxSpeed) {
- speed = -maxSpeed;
- }
- }
- else {
- if (speed > 0.15)
- {
- speed -= inertia;
- }
- else if (speed < -0.15)
- {
- speed += inertia;
- }
- else
- {
- speed = 0;
- }
- }
- var fall = parseFloat($("#mario").css("top"));
- if (onAir) {
- vspeed += gravity;
- if (vspeed > 20) {
- vspeed = 20;
- }
- fall = vspeed + parseFloat($("#mario").css("top"));
- }
- if (parseFloat($("#mario").css("top")) > 200) {
- $("#mario").css({ top: 200 });
- if (onAir) {
- vspeed = 0;
- onAir = false;
- }
- } else {
- $("#mario").css({ top: fall });
- }
- $("#mario").css({ left: left += speed });
- if (parseFloat($("#mario").css("left")) < 0) {
- $("#mario").css({ left: 10 });
- running = false;
- } else if (parseFloat($("#mario").css("left")) + parseFloat($("#mario").css("width")) > window.screen.width) {
- $("#mario").css({ left: window.screen.width - parseFloat($("#mario").css("width") - 10) });
- running = false;
- }
- var py = parseFloat($("#mario").css("top"));
- var px = parseFloat($("#mario").css("left"));
- var pw = parseFloat($("#mario").prop("width"));
- var ph = parseFloat($("#mario").prop("height"));
- FireBallShot();
- CoinPickup(py, px, pw, ph);
- EnemyMove();
- EnemyEngage(py,px,pw,ph);
- }
- function FireBallShot() {
- $(".fireball").each(function () {
- $(this).css({ left: parseFloat($(this).css("left")) + 10 });
- var y = parseFloat($(this).css("top"));
- var x = parseFloat($(this).css("left"));
- var w = parseFloat($(this).prop("width"));
- var h = parseFloat($(this).prop("height"));
- if (EnemyEngage(y, x, w, h)) {
- $(this).remove();
- }
- if (parseFloat($(this).css("left")) > window.screen.width) {
- $(this).remove();
- }
- });
- }
- function EnemyMove() {
- $(".enemy").each(function () {
- if (parseFloat($(this).css("left")) < 0) {
- $(this).prop("title", 3);
- } else if (parseFloat($(this).css("left")) > window.screen.width - parseFloat($(this).css("width"))) {
- $(this).prop("title", -3);
- }
- $(this).css({ left: parseFloat($(this).css("left")) + parseFloat($(this).prop("title")) });
- });
- }
- function EnemyEngage(py,px,pw,ph) {
- $(".enemy").each(function () {
- var y = parseFloat($(this).css("top"));
- var x = parseFloat($(this).css("left"));
- var w = parseFloat($(this).prop("width"));
- var h = parseFloat($(this).prop("height"));
- if (py < (y + h) && (py + ph) > y && px < (x + w) && (px + pw) > x) {
- if ((py+ph) > (y + (w * 0.5))) {
- $("#score").text("Game Over");
- $("#mario").remove();
- } else {
- score++;
- $("#score").text("Score : " + score);
- $(this).remove();
- return true;
- }
- }
- });
- return false;
- }
- function CoinPickup(py,px,pw,ph) {
- $(".coin").each(function () {
- var y = parseFloat($(this).css("top"));
- var x = parseFloat($(this).css("left"));
- var w = parseFloat($(this).prop("width"));
- var h = parseFloat($(this).prop("height"));
- if (py < (y + h) && (py + ph) > y && px < (x + w) && (px + pw) > x) {
- score++;
- $("#score").text("Score : " + score);
- $(this).remove();
- }
- });
- }
- function SpawnEnemy() {
- var left = Math.floor(Math.random() * window.screen.width) + 400;
- var element = '<img style="position:absolute; top:200px; left:'+left+'px;" class="enemy" width="60" height="50" src="enemy.png" title="3"/>';
- $('#marioField').append(element);
- }
- function SpawnCoin() {
- var left = Math.floor(Math.random() * window.screen.width);
- var top = Math.floor(Math.random() * 200);
- var element = '<img style="position:absolute; top:' + top + 'px; left:' + left + 'px;" class="coin" width="30" height="30" src="coin.gif" />';
- $('#marioField').append(element);
- }
- function SpawnFireball() {
- var py = parseFloat($("#mario").css("top"));
- var px = parseFloat($("#mario").css("left"));
- var element = '<img style="position:absolute; top:' + py + 'px; left:' + px + 'px;" class="fireball" width="30" height="30" src="fireball.png" />';
- $('#marioField').append(element);
- }
- document.addEventListener('keypress', (e) => {
- if (e.code == "KeyA") speed -= av;
- else if (e.code == "KeyD") speed += av;
- running = true;
- });
- document.addEventListener('keyup', (e) => {
- if (e.code == "KeyW" && !onAir) {
- vspeed = -30;
- onAir = true;
- console.log(onAir + " jump");
- }
- running = false;
- });
- document.addEventListener('keydown', (e) => {
- if (e.code == "KeyE" && $('#mario') != null) {
- SpawnFireball();
- }
- });
- setInterval(Update, 10);
- setInterval(SpawnEnemy, 7200);
- setInterval(SpawnCoin, 3600);
- </script>
- </body>
- </html>
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement