The goal of this tutorial is to use python classes, inheritance, and draw positional axes for rectangles.
I used two simple python scripts to solve these issues.
This python script named
testing_axes.py I create two classes named Point_XY and Columns_XY:
import random
#
class Point_XY:
def __init__(self):
self.x = random.randrange(400)
self.y = random.randrange(400)
self.dx = random.randrange(50)+10
self.dy = random.randrange(50)+10
def __repr__(self):
return "" % (self.x, self.y)
def __str__(self):
return "From str method of Point_XY: a is %s, b is %s" % (self.x, self.y)
#
class Columns_XY(Point_XY):
def __init__(self):
column = []
p = Point_XY()
self.column = (p.x, p.y, p.dx, p.dy)
print(column)
def __repr__(self,p):
return "" % (self.column)
def __str__(self):
return "From str method of Columns_XY :%s>" % (self.column)
'''
if __name__ == "__main__":
Columns_XY()
pass
'''
With this script named
pygame_testing_axes.py I draw columns and axes:
import sys
import pygame
from pygame.locals import *
from testing_axes import *
# define a square for each column
squares = []
# create column position x,y and rect size dx, dy
def create_columns_rect(n):
for i in range(n):
col = Columns_XY()
#print(col.column)
squares.append(col.column)
#print(squares)
return squares
def main():
# init PyGame
pygame.init()
# set size of screen
size_screen = (640,480)
# create display
screen_display = pygame.display.set_mode(size_screen,0,32)
# set color white
color_white = (255,255,255)
# set color blue
color_blue = (0,0,255)
# fill screen with a white color
screen_display.fill(color_white)
# set the numar of columns
nr_col = 5
# create columns from classes
cols = create_columns_rect(nr_col)
# use each column
for (px,py,dx,dy) in cols:
# print positions and size of rectangle
print (px,py,dx,dy)
# draw column rectangle to display with color and position and size
# screen_display , color_blue , px,py,dx,dy
pygame.draw.rect(screen_display,color_blue,(px,py,dx,dy))
# create a color for axes
color_col = (random.randrange(255),random.randrange(255),random.randrange(255))
# draw axes for x and y with the color
pygame.draw.line(screen_display, color_col, ( px , 0) , ( px , py + dy) )
pygame.draw.line(screen_display, color_col, ( 0 , py) , ( px + dx , py) )
# is workings get events
while True:
for event in pygame.event.get():
if event.type==QUIT:
pygame.quit()
sys.exit()
pygame.display.update()
# run main
if __name__ == "__main__":
main()
pass
This is result of the running python script
pygame_testing_axes.py: