/    Sign up×
Community /Pin to ProfileBookmark

Help with Javascript / HTML programming !?

Hi!

I’m pretty new in javascript and also in HTML, so for my college assigment i have to do some agent modell simulation in Javascript language.

I manage to create object, add picture to the canvas and also create that those objects are moving. I also managed that object are turning into black if ‘black object’ in in touch with them.

But I want to be this as some sort of simulation of the real child game:
[url]https://abcwellbeing.wordpress.com/2…the-black-man/[/url]

We choose a hunter – the Black man. He stands on one of the lines and the other pupils stand on the other line which is on the other side of the playground. All the players, including the Black man, can run only towards the other side of the playground. They’re not allowed to return to the start line. If they do, they have to join the Black man and become his assistants.

Could anyone please help me to create that object would be moving just from one side to another and when they come to the other side stopped there?

Files (picture and html file) are on this link:

[url]https://drive.google.com/drive/folde…qz?usp=sharing[/url]

Whole code:

(sory for all comments in the code)

[code=html]
<!doctype html>
<meta charset=”utf-8″>

<html>
<head>
<title> Kdo se boji &#269;rnega moža ?? </title>
<style>
body

{
background-image: url(“parket1.png”);
background-repeat: no-repeat;
}
</style>
</head>

<body onload=”load();”> <!– ko se spletna stran odpre poženemo najprej funkcijo load –>
Opazujemo ljudi (rde&#269;a barva), ki se poskušajo izmakniti &#269;rnemu možu.
<br>
<canvas id=”cv1″ width=”400″ height=”350″ style=”border:2px solid #c36400″></canvas>

<script type=”text/javascript”>
“use strict”; // omogo&#269;imo delo z razredi (class)

var canvas, ctx; // ustvarimo dve novi spremenljivki za platno (canvas) in kontekst (ctx)
var agenti = []; // deklariramo polje, ki bo vsebovalo naše agente (Array je ozna&#269;en z [])
var &#269;as; // uvedemo spremenljivko, ki predstavlja &#269;as
var številoAgentov = 30; // dolo&#269;imo število agentov
var števecCrnih = 0; // števec Crnih agentov
var števecRde&#269;ih = 0; // števec rde&#269;ih agentov

class Agent {
constructor(x, y, xVel, yVel, barva) {
this.x = x; // dolo&#269;imo lastnost x pozicije agenta
this.y = y; // dolo&#269;imo lastnost y pozicije agenta
this.xVel = xVel; // dolo&#269;imo hitrost v smeri x
this.yVel = yVel; // dolo&#269;imo hitrost v smeri y
this.barva = barva; // dolo&#269;imo lastnost “barva”

}
update () { // &#269;lanska funkcija – ta funkcija je za vse agente enaka
this.x += this.xVel; // pozicija x se spremeni glede na hitrost v smeri x, t.j. xVel
this.y += this.yVel; // pozicija y se spremeni glede na hitrost v smeri y, t.j. yVel
if(this.x > canvas.width – 10 || this.x < 0) { // v primeru, da smo izven platna po x koordinati
this.xVel = – this.xVel; // dolo&#269;imo, da je xVel = -xVel (odboj od stene)
}
if(this.y > canvas.height – 10 || this.y < 0) { // v primeru, da smo izven platna po y koordinati
this.yVel = – this.yVel; // dolo&#269;imo, da je yVel = -yVel (odboj od stene)
}
// &#269;e je x manjši od 0 ga postavimo na 0 (da se agent ne potopi v steno)
if (this.x < 0) {this.x =0};
// &#269;e je y manjši od 0 ga postavimo na 0 (da se agent ne potopi v steno)
if (this.y < 0) {this.y =0};
// podobno na drugem koncu platna
if (this.x > canvas.width – 10) {this.x = canvas.width – 10};
if (this.y > canvas.height – 10) {this.y = canvas.height – 10};
}
}

// funkcija, ki preveri razdaljo vsakega agenta z vsakim agentom
// v primeru trka izvedemo funkcijo trk
function preveriBližinoAgentov() {
for (var i=0; i<številoAgentov; i++) {
var A = agenti[i];

for (var j=i+1; j<številoAgentov; j++) {
var B = agenti[j];

// izra&#269;unamo razlike koordinat
var dx = B.x – A.x;
var dy = B.y – A.y;
var dist = Math.sqrt(dx*dx + dy*dy);

// &#269;e je razlika v razdalji med agentoma manjša od 10 izvedemo trk

if (dist < 10) { // &#269;e je pogoj, da sta agenta dovolj blizu (trk) izpolnjen
izvediTrk(i, j); // izvedemo trk med agentoma z indeksoma i in j
}
}
}
}

// funkcija, ki izvede trk med agentoma z indeksoma i in j
function izvediTrk(indeksPrvega, indeksDrugega) {
var x1 = agenti[indeksPrvega].x;
var y1 = agenti[indeksPrvega].y;
var x2 = agenti[indeksDrugega].x;
var y2 = agenti[indeksDrugega].y;
var dx = x2 – x1;
var dy = y2 – y1;
var dist = Math.sqrt(dx*dx + dy*dy);
var razdaljaOdboja = 8; // spr. dolo&#269;a za koliko se agenta ob trku odbijeta

// izra&#269;unamo normalo razdalje
var normalaX = dx/dist;
var normalaY = dy/dist;

// sredinske to&#269;ke
var midpointX = (x1 + x2)/2;
var midpointY = (y1 + y2)/2;

// dolo&#269;imo nove pozicije
agenti[indeksPrvega].x = midpointX – normalaX * razdaljaOdboja;
agenti[indeksPrvega].y = midpointY – normalaY * razdaljaOdboja;
agenti[indeksDrugega].x = midpointX + normalaX * razdaljaOdboja;
agenti[indeksDrugega].y = midpointY + normalaY * razdaljaOdboja;

// zamenjamo hitrosti 1. in 2. agenta pri trku
var tempX = agenti[indeksPrvega].xVel;
var tempY = agenti[indeksPrvega].yVel;
agenti[indeksPrvega].xVel = agenti[indeksDrugega].xVel;
agenti[indeksPrvega].yVel = agenti[indeksDrugega].yVel;
agenti[indeksDrugega].xVel = tempX;
agenti[indeksDrugega].yVel = tempY;

// preverimo ali je kateri od agentov pri trku Crni, drugi rde&#269; in z Crnio barvo prebarvamo rde&#269;ega
// &#269;e je prvi rde&#269; (0) in drugi Crni (1)
if (agenti[indeksPrvega].barva == 0 && agenti[indeksDrugega].barva == 1) {
agenti[indeksPrvega].barva = 1; // prvega, ki je bil prej rde&#269; (0) prebarvamo na Crni (1)
}
// &#269;e je prvi Crni (1) in drugi rde&#269; (0)
if (agenti[indeksPrvega].barva == 1 && agenti[indeksDrugega].barva == 0) {
agenti[indeksDrugega].barva = 1; // drugega, ki je bil prej rde&#269; (0) prebarvamo na Crni (1)
}
}

function loop() {

števecRde&#269;ih = 0; // števec rde&#269;ih resetiramo
števecCrnih = 0; // števec Crnih resetiramo

ctx.clearRect(0, 0, canvas.width, canvas.height); // brišemo celotno platno

preveriBližinoAgentov (); // preverimo bližino agentov in izvedemo trk

for (var i=0; i<številoAgentov; i++) {
agenti[i].update(); // i-ti agent
if (agenti[i].barva == 1) {
ctx.fillStyle = “#000000”; // barvo nastavimo na Crnio
števecCrnih++; // pove&#269;amo števec Crnih za 1
}
else {
ctx.fillStyle = “#ff0000”; // sicer nastavimo rde&#269;o barvo
števecRde&#269;ih++;
}

ctx.fillRect(agenti[i].x, agenti[i].y, 10, 10); // na platno postavimo agenta na koordinati x,100 dim. 10×10
}

&#269;as++; // &#269;as pove&#269;amo za 1

setTimeout(loop, 5); // na 5ms kli&#269;emo funkcijo loop
}

function load() {
canvas = document.getElementById(“cv1”);
ctx = canvas.getContext(“2d”);

ctx.fillStyle = “#ff0000”; // dolo&#269;imo rde&#269;o barvo

for (var i=0; i<številoAgentov; i++) {
// agentom dolo&#269;imo x in y pozicijo ter x in y hitrost (xVel, yVel (Velocity)) ter barvo (0=rde&#269;a, 1=zel.)
agenti[i] = new Agent(Math.random() * (canvas.width -10), Math.random() * (canvas.height – 350), 1 * Math.random() – 0.5, 1 * Math.random() – 0.5, 0); // ustvarimo enega agenta, ki bo v polju “agenti” na mestu [0]
}

agenti[0].barva = 1; // le prvemu agentu, na mestu 0 v polju agentov “agenti” dolo&#269;im lastnost “barva” = Crnia

loop();
}

</script>

</body>

</html>[/code]

to post a comment
JavaScript

4 Comments(s)

Copy linkTweet thisAlerts:
@Khalid_AliDec 23.2017 — wrote this piece of code(almost 1 and half decade ago) for training purposes for learners, take a look. You will need to make very tiny change(instead of bringing the object back to start just stop)

[url=http://www.webapplikations.com/pages/html_js/document/MoveObjectWithArrowKey.html] Move Object by arrow keys on keyboard[/url]
Copy linkTweet thisAlerts:
@rootDec 24.2017 — and [B]<!doctype html>[/B] should be [B]<!DOCTYPE html>[/B] as older browsers will recognise [B]!DOCTYPE[/B] but not [B]!doctype[/B] and can then enter a quirks mode that means the browser doesn't have to render to the rules and you could get unexpected results, its only current HTML5 browsers that understand [B]!doctype[/B] and you will find that many examples of HTML5 use the uppercase [B]!DOCTYPE[/B] for this reason.
Copy linkTweet thisAlerts:
@kokilajpaDec 26.2017 — Its Really good question. Nowadays JavaScript rolling the world. learning angularjs really helpful for web development. it has advanced features.
Copy linkTweet thisAlerts:
@rootDec 26.2017 — Its Really good question. Nowadays JavaScript rolling the world. learning angularjs really helpful for web development. it has advanced features.[/QUOTE]

Learning JavaScript (as in how to actually program) is more valuable as a skill than learning a library / framework tool, understanding actual JavaScript is more beneficial in the long run.
×

Success!

Help @aljosa123 spread the word by sharing this article on Twitter...

Tweet This
Sign in
Forgot password?
Sign in with TwitchSign in with GithubCreate Account
about: ({
version: 0.1.9 BETA 5.24,
whats_new: community page,
up_next: more Davinci•003 tasks,
coming_soon: events calendar,
social: @webDeveloperHQ
});

legal: ({
terms: of use,
privacy: policy
});
changelog: (
version: 0.1.9,
notes: added community page

version: 0.1.8,
notes: added Davinci•003

version: 0.1.7,
notes: upvote answers to bounties

version: 0.1.6,
notes: article editor refresh
)...
recent_tips: (
tipper: @AriseFacilitySolutions09,
tipped: article
amount: 1000 SATS,

tipper: @Yussuf4331,
tipped: article
amount: 1000 SATS,

tipper: @darkwebsites540,
tipped: article
amount: 10 SATS,
)...