Skip to content

Commit

Permalink
feat: add Pt and Gradient to readme
Browse files Browse the repository at this point in the history
  • Loading branch information
ShashkovS committed Nov 12, 2023
1 parent b0850ce commit 35b02e9
Show file tree
Hide file tree
Showing 31 changed files with 228 additions and 24 deletions.
94 changes: 92 additions & 2 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -66,6 +66,71 @@ while True:
```


# Point class which acts like 2-d vector and Turtle both

<img alt="transparent.png" src="https://raw.githubusercontent.com/ShashkovS/drawzero/master/docs/ex_05.png" width="50%">

```python
from drawzero import *

# just coordinates
A = Pt(100, 100)
B = Pt(300, 150)
line(C.red, A, B)

# A point which acts as 2-d vector and as a Turtle
# Pt(x=0.0, y=0.0, *, heading=0.0)
#
# Provides (for a, b — points, k number):
# * arithmetic
# a+b — vector addition
# a-b — vector subtraction
# k*a and a*k — multiplication with scalar
# abs — absolute value of a
# +a, -a

# arithmetic
A = Pt(100, 200)
dx = Pt(50, 5)
for i in range(10):
filled_circle(C.blue, A + dx * i, radius=10)

# * turtle-style movement
# forward — Move the point forward by the specified distance.
# backward — Move the point backward by distance.
# right — Turn point right by angle degrees.
# left — Turn point left by angle degrees.
# goto — Move point to an absolute position.
# rotate_around — Rotate around given point by angle degrees.
# move_towards — Move towards the given point by the specified distance.
# reset, home — Move point to the origin - coordinates (0,0), set heading=0
# setx — Set the point's first coordinate to x
# sety — Set the point's second coordinate to y
# setheading — Set the point's heading

A = Pt(100, 300)
B = Pt(1000, 400)
for i in range(10):
filled_circle(C.green2, A, radius=10)
A.move_towards(50, B)

A = Pt(100, 400)
for i in range(10):
filled_circle(C.magenta, A, radius=10)
A.left(10).forward(30)

# * information
# position — Return the point's current location (x,y), as a tuple.
# x, xcor — Return the point's x coordinate.
# y, ycor — Return the point's y coordinate
# heading — Return the point's heading
# distance — Return the distance between points
# towards — Return the angle towards point
# * deep copy
# copy — a clone of point
```



# Transparency

Expand Down Expand Up @@ -124,6 +189,31 @@ circle((0, 255, 0, 50), (100, 110), 50, line_width=10) # <-- RGBA
</details>


# Color gradients

<img alt="transparent.png" src="https://raw.githubusercontent.com/ShashkovS/drawzero/master/docs/ex_13.png" width="50%">

```python
from drawzero import *


scale1 = Gradient([C.black, C.white], 0, 1000)
for x in range(0, 1000, 10):
filled_rect(scale1(x), (x, 0), 10, 100)
text(C.white, 'scale1 = Gradient([C.black, C.white], 0, 1000)', (50, 100), 48, '<^')

scale2 = Gradient([C.green, C.yellow, C.magenta, C.red], 0, 1000)
for x in range(0, 1000, 10):
filled_rect(scale2(x), (x, 200), 10, 100)
text(C.white, 'scale2 = Gradient([C.green, C.yellow, C.magenta, C.red], 0, 1000)', (50, 300), 32, '<^')

scale3 = Gradient([C.white, C.black, C.red, C.black, C.white], 200, 800)
for x in range(0, 1000, 10):
filled_rect(scale3(x), (x, 400), 10, 100)
text(C.white, 'scale3 = Gradient([C.white, C.black, C.red, C.black, C.white], 200, 800)', (50, 500), 32, '<^')
```



# Keyboard and mouse events

Expand Down Expand Up @@ -180,10 +270,10 @@ Or run the following program:
```python
import os, sys
python = sys.executable
user = '--user' if 'venv' not in python else ''
user = '--user' if 'venv' not in python and 'virtualenvs' not in python else ''
cmd = f'"{python}" -m pip install drawzero --upgrade {user}'
print(cmd)
os.system(cmd)
from drawzero import *
```

# [Contributing](CONTRIBUTING.md)
Binary file added docs/ex_00.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added docs/ex_01.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added docs/ex_02.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added docs/ex_03.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added docs/ex_04.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added docs/ex_05.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added docs/ex_06.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added docs/ex_10.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added docs/ex_13.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
2 changes: 1 addition & 1 deletion requirements.txt
Original file line number Diff line number Diff line change
@@ -1 +1 @@
pygame>=2.0.0
pygame>=2.0.1
2 changes: 1 addition & 1 deletion src/drawzero/__about__.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@
"__copyright__",
]

__version__ = '0.4.3'
__version__ = '0.4.4'
__author__ = "Sergey Shashkov"
__license__ = "MIT"
__copyright__ = "Copyright 2020- Sergey Shashkov"
2 changes: 2 additions & 0 deletions src/drawzero/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,8 @@
__all__ = [
# spec
'__author__', '__copyright__', '__version__',
#
'run',
# simple shapes
'line', 'arc', 'circle', 'rect', 'ellipse', 'rect_rotated', 'polygon',
# filled shapes
Expand Down
32 changes: 15 additions & 17 deletions src/drawzero/examples/00_hello_world.py
Original file line number Diff line number Diff line change
@@ -1,22 +1,20 @@
# Just import everything
# Импортируем всё
from drawzero import *
#
# # Red rectangle with upper left corner at (50, 150) and width = 900, height = 700
# # Красный прямоугольник с левым верхнем углом в точке(50, 150), шириной 900 и высотой 700
# rect('red', (50, 150), 900, 700)
#
# # Straight orange line from (100, 500) to (900, 500)
# # Оранжевая прямая линия из точки (100, 500) в точку (900, 500)
# line('orange', (100, 500), (900, 500))
#
# # Centered text
# # Центрированный текст
# text('green', 'Hello world!', (500, 250), fontsize=72)
# text('blue', 'Привет, мир!', (500, 750), fontsize=72)

from drawzero import *
# Red rectangle with upper left corner at (50, 150) and width = 900, height = 700
# Красный прямоугольник с левым верхнем углом в точке(50, 150), шириной 900 и высотой 700
rect('red', (50, 150), 900, 700)

# Straight orange line from (100, 500) to (900, 500)
# Оранжевая прямая линия из точки (100, 500) в точку (900, 500)
line('orange', (100, 500), (900, 500))

# Centered text
# Центрированный текст
text('green', 'Hello world!', (500, 250), fontsize=72)
text('blue', 'Привет, мир!', (500, 750), fontsize=72)

# help(line)
set_lang('en')
line('red', 300, 400, 1, 2, )
# Run loop
# Запуск
run()
57 changes: 57 additions & 0 deletions src/drawzero/examples/05_points.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,57 @@
from drawzero import *

# just coordinates
A = Pt(100, 100)
B = Pt(300, 150)
line(C.red, A, B)

# A point which acts as 2-d vector and as a Turtle
# Pt(x=0.0, y=0.0, *, heading=0.0)
#
# Provides (for a, b — points, k number):
# * arithmetic
# a+b — vector addition
# a-b — vector subtraction
# k*a and a*k — multiplication with scalar
# abs — absolute value of a
# +a, -a

# arithmetic
A = Pt(100, 200)
dx = Pt(50, 5)
for i in range(10):
filled_circle(C.blue, A + dx * i, radius=10)

# * turtle-style movement
# forward — Move the point forward by the specified distance.
# backward — Move the point backward by distance.
# right — Turn point right by angle degrees.
# left — Turn point left by angle degrees.
# goto — Move point to an absolute position.
# rotate_around — Rotate around given point by angle degrees.
# move_towards — Move towards the given point by the specified distance.
# reset, home — Move point to the origin - coordinates (0,0), set heading=0
# setx — Set the point's first coordinate to x
# sety — Set the point's second coordinate to y
# setheading — Set the point's heading

A = Pt(100, 300)
B = Pt(1000, 400)
for i in range(10):
filled_circle(C.green2, A, radius=10)
A.move_towards(50, B)

A = Pt(100, 400)
for i in range(10):
filled_circle(C.magenta, A, radius=10)
A.left(10).forward(30)

# * information
# position — Return the point's current location (x,y), as a tuple.
# x, xcor — Return the point's x coordinate.
# y, ycor — Return the point's y coordinate
# heading — Return the point's heading
# distance — Return the distance between points
# towards — Return the angle towards point
# * deep copy
# copy — a clone of point
File renamed without changes.
File renamed without changes.
17 changes: 17 additions & 0 deletions src/drawzero/examples/13_gradients.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
from drawzero import *


scale1 = Gradient([C.black, C.white], 0, 1000)
for x in range(0, 1000, 10):
filled_rect(scale1(x), (x, 0), 10, 100)
text(C.white, 'scale1 = Gradient([C.black, C.white], 0, 1000)', (50, 100), 48, '<^')

scale2 = Gradient([C.green, C.yellow, C.magenta, C.red], 0, 1000)
for x in range(0, 1000, 10):
filled_rect(scale2(x), (x, 200), 10, 100)
text(C.white, 'scale2 = Gradient([C.green, C.yellow, C.magenta, C.red], 0, 1000)', (50, 300), 32, '<^')

scale3 = Gradient([C.white, C.black, C.red, C.black, C.white], 200, 800)
for x in range(0, 1000, 10):
filled_rect(scale3(x), (x, 400), 10, 100)
text(C.white, 'scale3 = Gradient([C.white, C.black, C.red, C.black, C.white], 200, 800)', (50, 500), 32, '<^')
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@

circles = []
tick()
scale = Gradient([C.gray10, C.white], 100, 500)
scale = Gradient([C.gray10, C.blue, C.orange], 100, 500)
while True:
x, y = mouse_pos()
circles.append([x, y, 100])
Expand All @@ -12,7 +12,7 @@
if r > 1000:
circles.pop(i)
else:
circle(scale(r), (x, y), r, line_width=1)
circle(scale(r), (x, y), r, line_width=3)
circles[i][2] += 10
fps()
tick()
Original file line number Diff line number Diff line change
Expand Up @@ -59,7 +59,7 @@ def draw_stars(stars):
screen_x = 500 + star.x / y
screen_y = 500 + star.z / y
screen_r = star.r / y
filled_circle(star.color, (screen_x, screen_y), screen_r)
filled_circle(star.color, (screen_x, screen_y), screen_r*2)
text('white', 'Press WASD or QE to move', (500, 5), 48, '.^')


Expand Down
File renamed without changes.
File renamed without changes.
40 changes: 40 additions & 0 deletions src/drawzero/examples/99_errors.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,40 @@
import os
import traceback
import sys

os.environ['EJUDGE_MODE'] = 'true'

from drawzero import *

filled_circle('boo')
# line('nocolor', (100, 100), ('asdf',))
circle('red', (100, 100), 'asd', line_width=0)


class ExceptionPrinter:
def __enter__(self):
pass

def __exit__(self, exc_type, exc_value, exc_traceback):
if exc_traceback:
traceback_len = 0
cur = exc_traceback
while cur:
traceback_len += 1
cur = cur.tb_next
print(f'Traceback is {traceback_len} deep')
print(''.join(traceback.format_tb(exc_traceback)), end='')
print(exc_value)
return True


# import drawzero
# print(help(drawzero))
# exit()
#
#
s = ExceptionPrinter()

with s: line('nocolor', (100, 100), (200, 200))
with s: line(C.red, (100, 200), (200, 'baz'))
with s: rect('violet', (100, 200, 300, 'baz'))

0 comments on commit 35b02e9

Please sign in to comment.