📕

Ch. 2 - Appendix

Appendix

Other Drawing methods:

Ellipse

Draws an ellipse (oval) on the screen. It is as if it draws the rectangle and then makes the curved Ellipse shape inside of it.

Form: pygame.draw.ellipse(screen, colour, vertices, width)

Variables:
Screen - the created screen where we are drawing the ellipse Color - RGB colour, can be predefined like RED in the example or defined in the statement as (red, green, blue) e.g. (100, 200, 155) Vertices - (x1, y1, w, h) where x1, y1 are the coordinates of the top left and w, h are the width and height of the rectangle in which the ellipse would fit within. Width - optional, does not have to be there, will fill in the ellipse if it is not. If it is, then width represents the thickness of the outside of the ellipse.

Examples:
pygame.draw.ellipse(screen, (255, 0, 0), (120,330, 200, 500)) pygame.draw.ellipse(screen, RED, (0,0, 200,500), 2)

Line

Draws a line on the screen

Form: pygame.draw.line(screen, colour, startCoordinates, endCoordinates, width)

Variables:
Screen - the created screen where we are drawing the line
Colour - RGB colour, can be predefined like RED in the example or defined in the statement as (red, green, blue) e.g. (100, 200, 155)
StartCoordinates - (x1, y1), where x1, y1 are the coordinates of the beginning of the line
EndCoordinates - (x2, y2), where x2, y2 are the coordinates of the end of the line
Width - optional, does not have to be there. If it is, then width represents the thickness of the line.

Example:
pygame.draw.line(screen, RED, (100, 200), (50, 350), 10)

Circle

Draws a circle on the screen

Form: pygame.draw.circle(screen, colour, pos, radius, width)

Variables:
Screen - the created screen where we are drawing the circle Colour - RGB colour, can be predefined like RED in the example or defined in the statement as (red, green, blue) e.g. (100, 200, 155) Pos- (x1, y1) where x1, y1 are the coordinates of the center of the circle Radius - the radius of the circle Width - optional, does not have to be there, will fill in the circle if it is not. If it is, then width represents the thickness of the outside of the circle

Examples:
pygame.draw.circle(screen, RED, (0, 0), 100) pygame.draw.circle(screen, RED, (0,0),100, 30)

Arc

Draws an arc on the screen

Form: pygame.draw.arc(screen, colour, vertices, startAngle, endAngle, width)

Variables:
Screen - the created screen where we are drawing the arc Colour - RGB colour, can be predefined like RED in the example or defined in the statement as (red, green, blue) e.g. (100, 200, 155) Vertices - (x1, y1, w, h) where x1, y1 are the coordinates of the top left and w,h are the height and width of the rectangle in which the arc would fit within. StartAngle - the angle (in radians) at which the arc would start (on the right is 0) EndAngle - the angle (in radians) at which the arc ends Width - optional, thickness to draw the outer edge

Examples:
pygame.draw.arc(screen,(100, 200, 155), (50, 50, 200, 200), 0, math.pi) pygame.draw.arc(screen,RED, (0, 0, 300, 250), math.pi / 2 , 3*math.pi/2) pygame.draw.arc(screen, BLUE, (100, 100, 200, 200), math.radians(30), math.radians(150))

Note:
  • To use math.pi, add import math at the top of your program
  • Use math.radians to convert from degrees to radians.
 
⚖ī¸
Copyright Š 2021 Code 4 Tomorrow. All rights reserved. The code in this course is licensed under the MIT License. If you would like to use content from any of our courses, you must obtain our explicit written permission and provide credit. Please contact classes@code4tomorrow.org for inquiries.