Toggle navigation
Explora
(current)
Aprende
Crea
Retos
×
Aspectos básicos
void main() {...}
for ( int i = 0 ; i < N ; i++ ) {...}
while (condición) {...}
do {...} while (condición);
if (condición) {...}
if (condición) {...} else {...}
switch (valor) {...}
Mostrar y pedir datos
print()
println()
readInteger()
readDouble()
readChar()
readString()
Funciones matemáticas
abs(n)
log(n)
sqrt(n)
pow(b,e)
floor(n)
ceil(n)
round(n)
sin(n)
cos(n)
tan(n)
asin(n)
acos(n)
atan(n)
random(n)
Funciones gráficas
point(x,y)
line(x1,y1,x2,y2)
ellipse(x,y,w,h)
rect(x,y,w,h)
triangle(x1,y1,x2,y2,x3,y3)
text(msg,x,y)
textWidth(msg)
textSize(n)
background(r,g,b,a)
strokeWeight(n)
stroke(r,g,b,a)
noStroke()
fill(r,g,b,a)
noFill()
image(url, x,y,w,h)
Nuevo
Ayuda
Probar
...
int textfont[][][] = { {{1, 1, 1}, // A {1, 0, 1}, {1, 1, 1}, {1, 0, 1}}, {{1, 1, 0}, // B {1, 1, 1}, {1, 0, 1}, {1, 1, 0}}, {{1, 1, 1}, // C {1, 0, 0}, {1, 0, 0}, {1, 1, 1}}, {{1, 1, 0}, // D {1, 0, 1}, {1, 0, 1}, {1, 1, 0}}, {{1, 1, 1}, // E {1, 1, 0}, {1, 0, 0}, {1, 1, 1}}, {{1, 1, 1}, // F {1, 0, 0}, {1, 1, 0}, {1, 0, 0}}, {{1, 1, 1}, // G {1, 0, 0}, {1, 0, 1}, {1, 1, 1}}, {{1, 0, 1}, // H {1, 0, 1}, {1, 1, 1}, {1, 0, 1}}, {{1, 1, 1}, // I {0, 1, 0}, {0, 1, 0}, {1, 1, 1}}, {{1, 1, 1}, // J {0, 0, 1}, {1, 0, 1}, {0, 1, 0}}, {{1, 0, 1}, // K {1, 1, 0}, {1, 0, 1}, {1, 0, 1}}, {{1, 0, 0}, // L {1, 0, 0}, {1, 0, 0}, {1, 1, 1}}, {{1, 0, 1}, // M {1, 1, 1}, {1, 0, 1}, {1, 0, 1}}, {{1, 0, 1}, // N {1, 1, 1}, {1, 1, 1}, {1, 0, 1}}, {{1, 1, 1}, // O {1, 0, 1}, {1, 0, 1}, {1, 1, 1}}, {{1, 1, 1}, // P {1, 0, 1}, {1, 1, 1}, {1, 0, 0}}, {{1, 1, 1}, // Q {1, 0, 1}, {1, 1, 1}, {0, 0, 1}}, {{1, 1, 1}, // R {1, 0, 1}, {1, 1, 0}, {1, 0, 1}}, {{0, 1, 1}, // S {1, 1, 0}, {0, 0, 1}, {1, 1, 0}}, {{1, 1, 1}, // T {0, 1, 0}, {0, 1, 0}, {0, 1, 0}}, {{1, 0, 1}, // U {1, 0, 1}, {1, 0, 1}, {1, 1, 1}}, {{1, 0, 1}, // V {1, 0, 1}, {1, 0, 1}, {0, 1, 0}}, {{1, 0, 1}, // W {1, 0, 1}, {1, 1, 1}, {1, 1, 1}}, {{1, 0, 1}, // X {0, 1, 0}, {1, 0, 1}, {1, 0, 1}}, {{1, 0, 1}, // Y {1, 0, 1}, {0, 1, 0}, {0, 1, 0}}, {{1, 1, 1}, // Z {0, 1, 0}, {1, 0, 0}, {1, 1, 1}}, {{0, 0, 0}, // Espacio 26 {0, 0, 0}, {0, 0, 0}, {0, 0, 0}}, {{0, 0, 0}, // . 27 {0, 0, 0}, {0, 0, 0}, {1, 0, 0}}, {{0, 0, 0}, // , 28 {0, 0, 0}, {0, 1, 0}, {1, 1, 0}}, {{0, 1, 0}, // ! 29 {0, 1, 0}, {0, 0, 0}, {0, 1, 0}}, {{1, 1, 1}, // ? 30 {0, 1, 1}, {0, 0, 0}, {0, 1, 0}}, {{0, 1, 0}, // ¡ 31 {0, 0, 0}, {0, 1, 0}, {0, 1, 0}}, {{0, 1, 0}, // ¿ 32 {0, 0, 0}, {1, 1, 0}, {1, 1, 1}}, {{1, 1, 1}, // Ñ 33 {0, 0, 0}, {1, 1, 1}, {1, 0, 1}}, {{0, 0, 0}, // : 34 {1, 0, 0}, {0, 0, 0}, {1, 0, 0}} }; int intAt(String txt, int ind) { // Es necesario para la funcion ptext return (int)(charAt(txt, ind)); } void ptext(String txt, int x, int y, int fontsize, double vibration) { // Muestra texto int ox = 0; for (int i = 0; i < sizeOf(txt); i++) { int ind = 26; switch (intAt(txt, i)) { case 32: ind = 26; break; case 46: ind = 27; break; case 44: ind = 28; break; case 33: ind = 29; break; case 63: ind = 30; break; case 161: ind = 31; break; case 191: ind = 32; break; case 241: case 209: ind = 33; break; case 58: ind = 34; break; default: if (intAt(txt, i) >= 65 && intAt(txt, i) <= 90) { ind = intAt(txt, i) - 65; } else if (intAt(txt, i) >= 97 && intAt(txt, i) <= 122) { ind = intAt(txt, i) - 97; } else { ind = 26; } break; } for (int ux = 0; ux < 3; ux++) { for (int uy = 0; uy < 4; uy++) { int v = textfont[ind][uy][ux]; if (v == 1) { rect(random(-vibration, vibration) + x + ((ox+ux)*fontsize), random(-vibration, vibration) + y + (uy*fontsize), fontsize, fontsize); } } } ox += 4; } } int maps[][] = { {1,1,1,1,1,1,1,1}, {2,0,0,0,0,0,0,1}, {1,1,1,1,1,1,0,1}, {1,0,0,0,0,0,0,1}, {1,0,1,1,1,1,1,1}, {1,0,0,1,0,0,0,1}, {1,1,0,0,0,1,0,1}, {1,1,1,1,1,1,1,1}}; int mapW = 8; int mapH = 8; double range = 1; double scale = 0.04; double angle = -(PI/3); double distPrecission = 2; double scale3d = 100; double cameraX = 260; double cameraY = 260; double speed = 10; double pts[] = new double[range]; boolean phone = false; void main() { animate(predraw); } void predraw() { background(24); fill(255); noStroke(); ptext("Raycasting", 40, 40, 6, 1); ptext("Movil", 120, 200, 4, 1); ptext("Ordenador", 90, 250, 4, 1); if (mouseX >= 120 && mouseX <= 200 && mouseY >= 200 && mouseY <= 216) { phone = true; animate(draw); } if (mouseX >= 90 && mouseX <= 230 && mouseY >= 250 && mouseY <= 266) { if (mousePressed) { phone = false; animate(draw); } } } double dist(double x1, double y1, double x2, double y2) { return sqrt(pow(x1-x2, 2) + pow(y1-y2, 2)); } double map(double v, double in_min, double in_max, double out_min, double out_max) { return (v - in_min) * (out_max - out_min) / (in_max - in_min) + out_min; } double constrain(double v, double min, double max) { return (v
max?max:(v
0) { akk = akk || inrect(ux*(320/mapW), uy*(320/mapH), 320/mapW, 320/mapH, cameraX, cameraY); } } } if (akk) { cameraX -= cos(angle)*speed; cameraY -= sin(angle)*speed; cameraX = constrain(cameraX, 0, 319); cameraY = constrain(cameraY, 0, 319); } } void goBackward() { speed = -speed; goForward(); speed = -speed; } boolean mp = false; int pmouseX = 0; int pmouseY = 0; boolean inRect(int x, int y, int w, int h) { int x1 = x; int x2 = x+w; int y1 = y; int y2 = y+h; if (mouseX >= x1 && mouseX <= x2 && mouseY >= y1 && mouseY <= y2) { return true; } else { return false; } } boolean inrect(int x, int y, int w, int h, double ox, double oy) { int x1 = x; int x2 = x+w; int y1 = y; int y2 = y+h; if (ox >= x1 && ox <= x2 && oy >= y1 && oy <= y2) { return true; } else { return false; } } int textures[][][] = {{ {1, 1, 1, 1, 1, 1, 1, 1}, {1, 1, 1, 1, 1, 1, 1, 1}, {1, 1, 1, 1, 1, 1, 1, 1}, {1, 1, 1, 1, 1, 1, 1, 1}, {1, 1, 1, 1, 1, 1, 1, 1}, {1, 1, 1, 1, 1, 1, 1, 1}, {1, 1, 1, 1, 1, 1, 1, 1}, {1, 1, 1, 1, 1, 1, 1, 1}}, { {0, 0, 0, 0, 0, 0, 0, 0}, {0, 0, 0, 0, 0, 0, 0, 0}, {0, 0, 1, 1, 1, 1, 0, 0}, {0, 0, 1, 0, 0, 1, 0, 0}, {0, 0, 1, 0, 0, 1, 0, 0}, {0, 0, 1, 1, 1, 1, 0, 0}, {0, 0, 0, 0, 0, 0, 0, 0}, {0, 0, 0, 0, 0, 0, 0, 0} }}; boolean auto = false; void textureLine(int x, int y1, int y2, int base, double br, int text) { for (int i = y1; i < y2; i++) { mstroke(textures[text][round(map(i, y1, y2, 0, 7))][round(x/20)%8]*br); rect(x-10, (base+i)-10, 20, 20); } } void draw() { background(0); stroke(255); strokeWeight(0); double r; double x = 0; if (!phone) { mp = mousePressed; } if (auto && phone) { goRight(); goForward(); } if (!mp) { //angle = map(mouseX, 0, 320, 0, PI*2); for (double a = angle-(range/2); a < angle+(range/2); a += scale) { //r = dist(constrain(160+(cos(a)*240), 0, 320), constrain(160+(sin(a)*240), 0, 320), 160, 160); r = 80; // Distance perception vr = 500; // Viewing range double ax; double ay; double i; boolean ci = true; for (i = 0; i < vr && ci; i += distPrecission) { ax = cameraX+(cos(a)*i); ay = cameraY+(sin(a)*i); for (int ux = 0; ux < mapW; ux++) { for (int uy = 0; uy < mapH; uy++) { if (maps[ux][uy] > 0) { if (inrect(ux*(320/mapW), uy*(320/mapH), 320/mapW, 320/mapH, ax, ay)) { //mstroke(((maps[ux][uy]*25.5)/i)*brightness); ci = false; if (!mp) { int ay1 = -round(((r/i)*scale3d)/2); int ay2 = round(((r/i)*scale3d)/2); line(x, ay1, x, ay2); textureLine(round(x), ay1, ay2, 160, ((255.0)/i)*brightness, maps[ux][uy]-1); a = (angle+(range/2)) - 0.01; } x = map(a, angle-(range/2), angle+(range/2), 0, 320); } else { if (!mp) { stroke(0); } } } } } } } } if (mp) { background(0); for (int ux = 0; ux < mapW; ux++) { for (int uy = 0; uy < mapH; uy++) { stroke(0); strokeWeight(1); fill(round(maps[ux][uy]*25.5)); rect(ux*(320/mapW), uy*(320/mapH), 320/mapW, 320/mapH); fill(255, 0, 0); ellipse(cameraX, cameraY, 20, 20); stroke(255); line(cameraX, cameraY, cameraX+(cos(angle)*10), cameraY+(sin(angle)*10)); } } } if (keyPressed && !phone) { if (key == "a") { goLeft(); } else if (key == "d") { goRight(); } else if (key == "w") { goForward(); } else if (key == "s") { goBackward(); } else if (key == "e") { goRight(); goForward(); } } if (phone) { speed = 20; strokeWeight(3); fill(191, 191, 191, 0.5); stroke(255, 255, 255, 0.5); rect(280, 280, 30, 30); rect(240, 280, 30, 30); rect(200, 280, 30, 30); rect(240, 240, 30, 30); if (mp) { fill(191, 0, 0, 0.5); stroke(255, 0, 0, 0.5); } rect(10, 10, 65, 30); fill(191, 191, 191, 0.5); stroke(255, 255, 255, 0.5); if (auto) { fill(191, 0, 0, 0.5); stroke(255, 0, 0, 0.5); } rect(85, 10, 78, 30); fill(255, 255, 255, 0.8); noStroke(); ptext("D", 290, 287, 4, 0); ptext("S", 250, 287, 4, 0); ptext("A", 210, 287, 4, 0); ptext("W", 250, 247, 4, 0); ptext("MAP", 20, 17, 4, 0); ptext("AUTO", 95, 17, 4, 0); noFill(); if (mouseX != pmouseX || mouseY != pmouseY) { if (inRect(280, 280, 30, 30)) { goRight(); } else if (inRect(200, 280, 30, 30)) { goLeft(); } else if (inRect(240, 240, 30, 30)) { goForward(); } else if (inRect(240, 280, 30, 30)) { goBackward(); } else if (inRect(10, 10, 65, 30)) { mp = !mp; } else if (inRect(85, 10, 78, 30)) { auto = !auto; } } } pmouseX = mouseX; pmouseY = mouseY; }
Canvas not supported.
Programado por
segfaultdev
0 votos
407 descargas
106 usos