Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #176 from Prabhnith/patch-2
Example rewritten in Python Mode : Example->Basic->Objects->MultipleCons
- Loading branch information
Showing
1 changed file
with
51 additions
and
0 deletions.
There are no files selected for viewing
51 changes: 51 additions & 0 deletions
51
mode/examples/Basics/Objects/MultipleConstructors/MultipleConstructors.pyde
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,51 @@ | ||
''' | ||
Multiple Constructors | ||
A class can have multiple constructors that assign the fields in different ways. | ||
Sometimes it's beneficial to specify every aspect of an object's data by assigning | ||
parameters to the fields, but other times it might be appropriate to define only | ||
one or a few. | ||
Advanced example written in Python Mode by: Prabhjot Singh (NITH) | ||
Original example in Java Mode: Example->Basic->Objects->MultipleConstructors | ||
''' | ||
def setup(): | ||
size(640, 360) | ||
smooth(4) | ||
noLoop() | ||
|
||
ellipseMode(CENTER) | ||
strokeWeight(2.5) | ||
stroke(0) | ||
fill('#FFFF00') | ||
|
||
global spots | ||
spots = Spot(), Spot(radius=58), Spot(x=120, y=70),\ | ||
Spot(width / 2, height / 2, 120) | ||
|
||
|
||
def draw(): | ||
background(0300) | ||
for sp in spots: | ||
sp.display() | ||
|
||
|
||
class Spot: | ||
|
||
def __init__(self, x=None, y=None, radius=40): | ||
if x is None: | ||
self.x = width / 4 | ||
else: | ||
self.x = x | ||
|
||
if y is None: | ||
self.y = height / 2 | ||
else: | ||
self.y = y | ||
|
||
self.radius = radius | ||
self.diam = radius * 2 | ||
|
||
|
||
def display(self): | ||
ellipse(self.x, self.y, self.radius, self.diam) |