Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- import pygame
- from pygame.locals import *
- # Originally common.py
- objs = []
- # Originally configs.py
- h = 500
- w = 300
- color_depth = 24
- obj_width = 1
- # Originally constants.py
- # Colors
- WHITE = (255, 255, 255)
- BLACK = (0, 0, 0)
- RED = (255, 0, 0)
- GRAY = (186, 186, 186)
- # Mouse
- MAIN_BUTTON = 1
- MIDDLE_BUTTON = 2
- SECONDARY_BUTTON = 3
- DRAG_AND_DROP = 2
- # Originally contoller.py
- def mouse(event):
- """Processes mouse events"""
- global clickPos
- global releasePos
- if event.type == MOUSEBUTTONUP:
- releasePos = pygame.mouse.get_pos()
- if event.button == MAIN_BUTTON:
- # Notice we are not handling drag and drops and considering
- # everything as a simple click.
- obj = collideAll(releasePos)
- if obj:
- obj.text = 'Hello World' if not obj.text else obj.text
- return MAIN_BUTTON
- elif event.button == MIDDLE_BUTTON:
- Circle(list(releasePos), 50)
- else:
- return False
- clock = pygame.time.Clock()
- def mainLoop():
- clock.tick(10)
- screen.fill(WHITE)
- for obj in objs:
- obj.draw()
- pygame.display.update()
- for event in pygame.event.get():
- mouse(event)
- if event.type == pygame.QUIT:
- pygame.quit()
- return None
- return True
- # Originally objects.py
- class Object:
- """Superclass for all objects.
- position - tuple
- Pair of screen coordinates.
- dimensions - list or array
- Pair of floats or integers representing width and height.
- """
- global objs
- def __init__(self, position, dimensions, color=BLACK):
- self.dimensions = dimensions
- self.position = position
- objs.append(self)
- def draw(self):
- """Should be defined in the subclass."""
- raise RuntimeError('draw() not defined for '+str(self))
- class Circle(Object):
- """
- radius - integer
- density - float or integer
- """
- def __init__(self, position, radius, density=1, color=BLACK, text=None):
- Object.__init__(self, position, [radius, radius], color)
- self.color = color
- self.radius = radius
- self.text = text
- self.draw()
- def draw(self):
- pygame.draw.circle(
- screen,
- self.color,
- self.position,
- self.radius,
- obj_width
- )
- sendMsg(self.text, (self.position[0]-self.dimensions[0], self.position[1]-15))
- # Originally graphics.py
- pygame.init()
- # The 0 below stands for "no flags"
- screen = pygame.display.set_mode((w, h), 0, color_depth)
- def sendMsg(msg, pos, font='monospace', fontsize=15, color=BLACK):
- myfont = pygame.font.SysFont(font, fontsize)
- label = myfont.render(msg, 1, color)
- screen.blit(label, pos)
- pygame.display.update()
- def collideObject(position, obj):
- if ((position[0] >= obj.position[0]-obj.dimensions[0]) and (
- position[0] <= obj.position[0]+obj.dimensions[0])) and (
- (position[1] >= obj.position[1]-obj.dimensions[1]) and (
- position[1] <= obj.position[1]+obj.dimensions[1])):
- return obj
- else:
- return None
- def collideAll(position, except_=None):
- for obj in objs:
- if not obj == except_:
- returnValue = collideObject(position, obj)
- if returnValue:
- return returnValue
- return None
- screen.fill(WHITE)
- # Originally __init__.py
- try:
- while mainLoop():
- pass
- except (KeyboardInterrupt, SystemExit):
- pass
- raise SystemExit
Advertisement
Add Comment
Please, Sign In to add comment