简体   繁体   English

存储ASCII艺术作品作为简单JavaScript游戏中的角色。 我可以将其存储在角色的对象中吗?

[英]Storing ASCII art for use as a character in a simple JavaScript game. Can I store it in the character's Object?

Making a simple JS game (all I can use is Jquery and vanilla JS) and would like to use art like this: 制作一个简单的JS游戏(我只能使用Jquery和Vanilla JS),并希望使用如下图样:

.-.
       |_:_|
      /(_Y_)\
     (_\/M\/_)
      /.'-'.\
     //|[_]|\\
     \\|   |//
      #|===|\#
     / |\ /I \
    /_ | | I _\
       |_|_|

and use Jquery to toss it around to different divs on my page. 并使用Jquery将其扔给我页面上的不同div。 How and where can I put this as to not lose that spacing but still be able to call it? 我该如何以及在何处放置它,以免失去该间距但仍可以调用它? When I have it in as a value i cant have multiple lines per value it would seem because sublime turns vader into a rainbow. 当我将其作为一个值输入时,每个值不能显示多行,因为崇高将vader变成彩虹。

Since SO seems to display your stuff correctly i just inspected what is the element and seems to be <pre> . 由于SO似乎正确显示了您的内容,因此我只是检查了元素是什么,并且似乎是<pre> So i created a var with the styling for recreating a darthvader like this: 因此,我使用样式创建了一个var,以重新创建这样的darthvader:

var darthvador = "        .-.<br>       |_:_|<br>      /(_Y_)\\<br>     (_\\/M\\/_)<br>      /.'-'.\\<br>     //|[_]|\\\\<br>     \\\\|   |//<br>      #|===|\\#<br>     / |\\ /I \\<br>    /_ | | I _\\<br>       |_|_| ";

<br> to make a linebreak. <br>进行换行。 and doubles all the \\ so it doesn't consider it as an escape character. 并将所有\\都加倍,因此它不会将其视为转义字符。

then i've made a div to hold the player: 然后我做了一个div来容纳玩家:

 <div id="player">

</div>

simple div didn't apply any style you can do what ever you like with it. simple div没有应用任何样式,您可以使用它来完成自己喜欢的任何事情。

<input type="button" value="addPlayer" onclick="addPlayer();"/> 

I made this button so you can add it when you want. 我做了这个按钮,以便您可以在需要时添加它。 You can make some logic to add it only once or just store it in player div and hide the button or what ever. 您可以制定一些逻辑,使其仅添加一次,或仅将其存储在player div中,然后隐藏按钮或其他内容。

And then add player simply creates a <pre> element and adds the darthvador player in it. 然后add player只需创建一个<pre>元素,然后在其中添加darthvador播放器。

function addPlayer(){
    document.getElementById("player").innerHTML = "<pre>" + darthvador +"</pre>";
}

css style to make pre look like a normal div. css样式使pre看起来像普通的div。 You can change the width or what ever if you need. 您可以更改宽度,也可以根据需要更改。

 pre{
    background-color: transparent;
    border: 0px;
}

hope this helps. 希望这可以帮助。

Then if you need to manipulate the player and toss it around other divs jQuery has a detach function to detach a div. 然后,如果您需要操纵播放器并将其扔向其他div,则jQuery具有detach功能来分离div。 And then you can attach if with after or appendTo or before . 然后可以将if附加在afterappendTobefore

PS: Sry i can't provide jsFiddle link i don't have access to all the website. PS:对不起,我无法提供jsFiddle链接,但我无法访问所有网站。 But it's just css html and js so you can easily rebuild it :p 但这只是css html和js,因此您可以轻松地重建它:p

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM