Skip to content

Commit 3a6ed80

Browse files
author
Alexei Bezborodov
committed
SVG
1 parent 665ab47 commit 3a6ed80

File tree

5 files changed

+292
-0
lines changed

5 files changed

+292
-0
lines changed

brython/draw_tree.html

+66
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,66 @@
1+
<!-- Copyright 2024 by Alexei Bezborodov <[email protected]> -->
2+
<!-- Общественное достояние (public domain) -->
3+
4+
<!DOCTYPE html>
5+
<html>
6+
7+
<head>
8+
<meta charset="UTF-8">
9+
<title>Ёлочка</title>
10+
<script type="text/javascript" src="https://cdn.jsdelivr.net/npm/[email protected]/brython.min.js"></script>
11+
<script type="text/javascript" src="https://cdn.jsdelivr.net/npm/[email protected]/brython_stdlib.js"></script>
12+
</head>
13+
<body onload="brython()">
14+
15+
<h1 class="text-center">Ёлочка</h1>
16+
<canvas id="draw-board" width="500" height="500"></canvas>
17+
18+
<script type="text/python">
19+
20+
from browser import document, html, window
21+
22+
canvas = document["draw-board"]
23+
ctx = canvas.getContext("2d")
24+
25+
# Чёрный экран
26+
ctx.fillStyle = "black"
27+
w = canvas.width # ширина
28+
h = canvas.height # высота
29+
ctx.fillRect(0, 0, w, w)
30+
31+
# Функция рисования линии
32+
def DrawLine(x1, y1, x2, y2, color, lineWidth):
33+
ctx.strokeStyle = color
34+
ctx.lineWidth = lineWidth
35+
ctx.beginPath()
36+
ctx.moveTo(x1, y1)
37+
ctx.lineTo(x2, y2)
38+
ctx.stroke()
39+
return
40+
41+
# Рисование ёлочки
42+
w2 = w/2
43+
c = 'green'
44+
lw = 10
45+
for j in range(2):
46+
x = w2
47+
y = 10
48+
znak = -1
49+
if j == 1:
50+
znak = 1
51+
for i in range(1,15):
52+
delta = 4 * i
53+
next_x = x + delta * znak
54+
next_y = y + delta
55+
DrawLine(x, y, next_x, next_y, c, lw)
56+
x = next_x - delta / 2 * znak
57+
y = next_y
58+
DrawLine(next_x, next_y, x, y, c, lw)
59+
60+
print('🎄')
61+
62+
</script>
63+
64+
</body>
65+
66+
</html>

brython/fractal_svg.html

+72
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,72 @@
1+
<!-- Copyright 2024 by Alexei Bezborodov <[email protected]> -->
2+
<!-- License: Public domain: http://unlicense.org/ -->
3+
<!-- Общественное достояние -->
4+
<!DOCTYPE html>
5+
<html>
6+
7+
<head>
8+
<meta charset="UTF-8">
9+
<title>Фрактал SVG</title>
10+
<script type="text/javascript" src="https://cdn.jsdelivr.net/npm/[email protected]/brython.min.js"></script>
11+
<script type="text/javascript" src="https://cdn.jsdelivr.net/npm/[email protected]/brython_stdlib.js"></script>
12+
</head>
13+
<body onload="brython()">
14+
15+
<h1 class="text-center">Фрактал SVG</h1>
16+
<p id="out_svg"></p>
17+
18+
<script type="text/python">
19+
20+
from browser import document, html, window
21+
22+
def Print2String(*args, **kwargs):
23+
newstr = ""
24+
for a in args:
25+
newstr += str(a) + ''
26+
return newstr
27+
28+
color_black = "black"
29+
color_test_rgb = "rgb(100,255,0)"
30+
31+
def SvgDrawRect(svg, x, y, width, height, color, line_color, line_width):
32+
svg += [Print2String("<rect x='",x,"' y='",y,"' width='", width,"' height='", height,"' style=\"fill:",color,";stroke-width:",line_width,";stroke:",line_color,"\" />")]
33+
34+
def SvgDrawLine(svg, x1, y1, x2, y2, color, width):
35+
svg += [Print2String("<line x1='",x1,"' y1='", y1,"' x2='",x2,"' y2='",y2,"' style=\"stroke:",color,";stroke-width:",width,"\" />")]
36+
37+
def SvgMake(svg, width, height):
38+
result = Print2String("<svg width='",width,"' height='",height,"' xmlns=\"http://www.w3.org/2000/svg\" xmlns:svg=\"http://www.w3.org/2000/svg\">")
39+
for c in svg:
40+
result += c
41+
result += "</svg>"
42+
return result
43+
44+
def SvgDrawSquare(svg, x1, y1, x2, y2, x3, y3, x4, y4, color, width):
45+
SvgDrawLine(svg, x1, y1, x2, y2, color, width)
46+
SvgDrawLine(svg, x2, y2, x3, y3, color, width)
47+
SvgDrawLine(svg, x3, y3, x4, y4, color, width)
48+
SvgDrawLine(svg, x1, y1, x4, y4, color, width)
49+
50+
def SvgDrawSquareFractal(svg, x1, y1, x2, y2, x3, y3, x4, y4, color, width, dep):
51+
if dep < 1:
52+
return
53+
SvgDrawSquare(svg, x1, y1, x2, y2, x3, y3, x4, y4, color, width)
54+
55+
koef = 0.15
56+
SvgDrawSquareFractal(svg, x1 + (x2 - x1) * koef, y1 + (y2 - y1) * koef, x2 + (x3 - x2) * koef, y2 + (y3 - y2) * koef, x3 + (x4 - x3) * koef, y3 + (y4 - y3) * koef, x4 + (x1 - x4) * koef, y4 + (y1 - y4) * koef, color, width, dep - 1)
57+
58+
59+
svg = []
60+
w = 500
61+
h = 500
62+
SvgDrawRect(svg, 0, 0, w, h, color_black, color_black, 1)
63+
64+
SvgDrawSquareFractal(svg, 0, 0, w, 0, w , h, 0, h, color_test_rgb, 3, 37)
65+
66+
document["out_svg"].innerHTML = SvgMake(svg, w, h)
67+
68+
</script>
69+
70+
</body>
71+
72+
</html>

rand_learn.txt

+39
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,39 @@
1+
s = "1 + 1 = 2"
2+
s = s.replace("1", "один")
3+
s = s.replace("2", "два")
4+
s = s.replace("3", "три")
5+
s = s.replace("4", "четыре")
6+
print(s)
7+
8+
9+
10+
exit()
11+
import random
12+
13+
a = 1323
14+
def rand():
15+
global a
16+
a = (a * 234 + 234235) % 1000
17+
return a
18+
19+
20+
for i in range(20):
21+
#print(random.randint(1, 10))
22+
print(rand())
23+
24+
25+
26+
exit()
27+
s = "@rand_int + @rand_int"
28+
29+
print(s.replace("@rand_int", str(random.randint(-15, 15)), 1))
30+
31+
exit()
32+
def full_replace(s):
33+
c = None
34+
while c != s:
35+
c = s
36+
s = s.replace("@rand_int", str(random.randint(-15, 15)), 1)
37+
return s
38+
39+
print(full_replace(s))

simple_pygame.py

+85
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,85 @@
1+
2+
# https://proglib.io/p/samouchitel-po-python-dlya-nachinayushchih-chast-21-osnovy-razrabotki-igr-na-pygame-2023-05-29
3+
4+
import pygame
5+
import random
6+
7+
pygame.init()
8+
screen_width = 640
9+
screen_height = 480
10+
screen = pygame.display.set_mode((screen_width, screen_height))
11+
12+
# цвета окружностей
13+
white = (255, 255, 255)
14+
red = (255, 0, 0)
15+
green = (0, 255, 0)
16+
blue = (0, 0, 255)
17+
black = (0, 0, 0)
18+
yellow = (255, 255, 0)
19+
20+
# цвет, скорость, начальная позиция окружности
21+
circle_radius = 30
22+
circle_speed = 3
23+
circle_color = random.choice([red, green, blue, yellow, white])
24+
circle_pos = [screen_width//2, -circle_radius]
25+
circle_landed = False
26+
27+
# список приземлившихся окружностей и их позиций
28+
landed_circles = []
29+
30+
while True:
31+
for event in pygame.event.get():
32+
if event.type == pygame.QUIT:
33+
pygame.quit()
34+
quit()
35+
36+
# если окружность не приземлилась
37+
if not circle_landed:
38+
# меняем направление по нажатию клавиши
39+
keys = pygame.key.get_pressed()
40+
if keys[pygame.K_LEFT]:
41+
circle_pos[0] -= circle_speed
42+
if keys[pygame.K_RIGHT]:
43+
circle_pos[0] += circle_speed
44+
45+
# проверяем, столкнулась ли окружность с другой приземлившейся окружностью
46+
for landed_circle in landed_circles:
47+
landed_rect = pygame.Rect(landed_circle[0]-circle_radius, landed_circle[1]-circle_radius, circle_radius*2, circle_radius*2)
48+
falling_rect = pygame.Rect(circle_pos[0]-circle_radius, circle_pos[1]-circle_radius, circle_radius*2, circle_radius*2)
49+
if landed_rect.colliderect(falling_rect):
50+
circle_landed = True
51+
collision_x = circle_pos[0]
52+
collision_y = landed_circle[1] - circle_radius*2
53+
landed_circles.append((collision_x, collision_y, circle_color))
54+
break
55+
56+
# если окружность не столкнулась с другой приземлившейся окружностью
57+
if not circle_landed:
58+
# окружность движется вниз
59+
circle_pos[1] += circle_speed
60+
61+
# проверяем, достигла ли окружность дна
62+
if circle_pos[1] + circle_radius > screen_height:
63+
circle_pos[1] = screen_height - circle_radius
64+
circle_landed = True
65+
# добавляем окружность и ее позицию в список приземлившихся окружностей
66+
landed_circles.append((circle_pos[0], circle_pos[1], circle_color))
67+
68+
if circle_landed:
69+
# если окружность приземлилась, задаем параметры новой
70+
circle_pos = [screen_width//2, -circle_radius]
71+
circle_color = random.choice([red, green, blue, yellow, white])
72+
circle_landed = False
73+
74+
# рисуем окружности
75+
screen.fill(black)
76+
for landed_circle in landed_circles:
77+
pygame.draw.circle(screen, landed_circle[2], (landed_circle[0], landed_circle[1]), circle_radius)
78+
pygame.draw.circle(screen, circle_color, circle_pos, circle_radius)
79+
pygame.display.update()
80+
81+
# частота обновления экрана
82+
pygame.time.Clock().tick(60)
83+
84+
85+

svg_example.txt

+30
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,30 @@
1+
<svg width="1000" height="1000">
2+
<title>Attribute Animation with SMIL</title>
3+
<rect x="0" y="0" width="100" height="100" stroke="black" stroke-width="1" >
4+
<set
5+
attributeName="y"
6+
to="500"
7+
begin="1s"
8+
/>
9+
<set
10+
attributeName="y"
11+
to="300"
12+
begin="5s"
13+
/>
14+
</rect>
15+
</svg>
16+
17+
18+
<svg width="1000" height="1000">
19+
<title>Attribute Animation with SMIL</title>
20+
<rect x="0" y="0" width="100" height="100" stroke="black" stroke-width="1" >
21+
<animate
22+
attributeName="y"
23+
from="0"
24+
to="500"
25+
begin="0s"
26+
dur="5s"
27+
repeatCount="indefinite" />
28+
</rect>
29+
</svg>
30+

0 commit comments

Comments
 (0)