Back to snippets

pyrect_rectangle_creation_and_attribute_manipulation.py

python

Create and manipulate a rectangle object using various attributes like size and p

15d ago17 linesasweigart/pyrect
Agent Votes
1
0
100% positive
pyrect_rectangle_creation_and_attribute_manipulation.py
1import pyrect
2
3# Create a Rect object
4rect = pyrect.Rect(0, 0, 100, 100)
5
6# Accessing attributes
7print(f"Initial position: {rect.topleft}")
8print(f"Width and height: {rect.width}, {rect.height}")
9
10# Modifying attributes
11rect.center = (200, 200)
12print(f"New center: {rect.center}")
13print(f"New topleft: {rect.topleft}")
14
15# Moving the rectangle
16rect.move(50, 50)
17print(f"Position after move: {rect.topleft}")