Commits

Anonymous committed 0305db9

pygame.draw.rect.

Comments (0)

Files changed (4)

examples_pygame/draw.py

+#!/usr/bin/env python
+
+'''
+'''
+
+__docformat__ = 'restructuredtext'
+__version__ = '$Id$'
+
+import random
+import sys
+
+import pygame
+from pygame.locals import *
+
+width, height = 640, 480
+max_vel = 1.0
+
+class Point(object):
+    __slots__ = ['pos', 'vel']
+
+    def __init__(self):
+        self.pos = [random.randrange(0, width), random.randrange(0, height)]
+        self.vel = [(random.random() - 0.5) * max_vel, 
+                    (random.random() - 0.5) * max_vel]
+
+class Shape(object):
+    def __init__(self, npoints):
+        self.points = []
+        for i in range(npoints):
+            self.points.append(Point())
+        self.color = (random.randint(0, 255),
+                      random.randint(0, 255), 
+                      random.randint(0, 255))
+
+    def get_points(self):
+        return [(int(p.pos[0]), int(p.pos[1])) for p in self.points]
+
+    def update(self, time):
+        for point in self.points:
+            point.pos[0] += point.vel[0] * time
+            point.pos[1] += point.vel[1] * time
+            if point.pos[0] >= width or point.pos[0] < 0:
+                point.vel[0] *= -1
+            if point.pos[1] >= height or point.pos[1] < 0:
+                point.vel[1] *= -1
+
+class Rectangle(Shape):
+    def __init__(self, width):
+        super(Rectangle, self).__init__(2)
+        self.width = width
+
+    def draw(self, surface):
+        r = Rect(self.get_points())
+        r.width -= r.left
+        r.height -= r.top
+        pygame.draw.rect(surface, self.color, r, self.width)
+
+class Line(Shape):
+    def __init__(self, width):
+        super(Line, self).__init__(2)
+        self.width = width
+
+    def draw(self, surface):
+        pygame.draw.line(surface, self.color, 
+                         self.get_points()[0],
+                         self.get_points()[1], self.width)
+
+class AntialiasLine(Shape):
+    def __init__(self):
+        super(AntialiasLine, self).__init__(2)
+
+    def draw(self, surface):
+        pygame.draw.aaline(surface, self.color, 
+                           self.get_points()[0],
+                           self.get_points()[1])
+
+class Polygon(Shape):
+    def __init__(self, width):
+        super(Polygon, self).__init__(random.randint(3, 5))
+        self.width = width
+
+    def draw(self, surface):
+        pygame.draw.polygon(surface, self.color, self.get_points(), self.width)
+
+class Ellipse(Shape):
+    def __init__(self, width):
+        super(Ellipse, self).__init__(2)
+        self.width = width
+
+    def draw(self, surface):
+        r = Rect(self.get_points())
+        r.width -= r.left
+        r.height -= r.top
+        r.normalize()
+        if self.width * 2 < r.width and self.width * 2 < r.height:
+            pygame.draw.ellipse(surface, self.color, r, self.width)
+
+if __name__ == '__main__':
+    pygame.init()
+
+    flags = 0
+    depth = 0
+    screen = pygame.display.set_mode((width, height), flags, depth)
+
+    shapes = [Rectangle(2)]
+
+    clock = pygame.time.Clock()
+    quit = False
+    paused = False
+    while not quit:
+        for event in pygame.event.get():
+            if event.type == QUIT:
+                quit = True
+            elif event.type == KEYDOWN:
+                if event.key == K_ESCAPE:
+                    quit = True
+                elif event.key == K_SPACE:
+                    paused = not paused
+                elif event.unicode == 'r':
+                    shapes.append(Rectangle(1))
+                elif event.unicode == 'R':
+                    shapes.append(Rectangle(0))
+                elif event.unicode == 'l':
+                    shapes.append(Line(1))
+                elif event.unicode == 'L':
+                    shapes.append(Line(4))
+                elif event.unicode == 'p':
+                    shapes.append(Polygon(1))
+                elif event.unicode == 'P':
+                    shapes.append(Polygon(0))
+                elif event.unicode == 'e':
+                    shapes.append(Ellipse(4))
+                elif event.unicode == 'E':
+                    shapes.append(Ellipse(0))
+                elif event.unicode == 'a':
+                    shapes.append(AntialiasLine())
+        time = clock.tick()
+        if not paused:
+            print >> sys.stderr, 'FPS %03.2f, % 3d shapes\r' % \
+                (clock.get_fps(), len(shapes)),
+
+            screen.fill((0, 0, 0))
+            for shape in shapes:
+                shape.update(time)
+                shape.draw(screen)
+            pygame.display.flip()
 __docformat__ = 'restructuredtext'
 __version__ = '$Id$'
 
+from copy import copy
+
+from SDL import *
+
+import pygame.base
+import pygame.rect
+
+def _get_color(color, surface):
+    rgba = pygame.base._rgba_from_obj(color)
+    if rgba:
+        color = SDL_MapRGBA(surface._surf.format, 
+                            rgba[0], rgba[1], rgba[2], rgba[3])
+    if type(color) not in (int, long):
+        raise 'invalid color argument'
+    return color
+
+def _get_rect(rect):
+    rect = copy(rect)
+    rect.normalize()
+    return rect._r
+
 def rect(surface, color, rect, width=0):
     '''Draw a rectangle shape.
 
     :rtype: `Rect`
     :return: Affected bounding box.
     '''    
+    color = _get_color(color, surface)
+    rect = _get_rect(rect)
+    if width == 0:
+        SDL_FillRect(surface._surf, rect, color)
+    else:
+        hw = width / 2
+        r = SDL_Rect(rect.x, rect.y - hw, rect.w, width)
+        SDL_FillRect(surface._surf, r, color)
+        r.y += rect.h
+        SDL_FillRect(surface._surf, r, color)
+        r.x -= hw
+        r.w = width
+        r.y = rect.y
+        r.h = rect.h
+        SDL_FillRect(surface._surf, r, color)
+        r.x += rect.w
+        SDL_FillRect(surface._surf, r, color)
+        # XXX returned clip rectangle wrong
+    return pygame.rect.Rect(rect)
 
 def polygon(surface, color, pointlist, width=0):
     '''Draw a shape with any number of sides.
         if self._r.h < 0:
             self._r.y += self._r.h
             self._r.h = -self._r.h
+        if isinstance(self._r, _RectProxy):
+            object.__setattr__(self, '_r', SDL.SDL_Rect(self._r.x,
+                                                        self._r.y,
+                                                        self._r.w,
+                                                        self._r.h))
 
     def contains(self, *other):
         other = _rect_from_object(other)._r
             self._fps_count = 0
             self._fps_tick = nowtime
         elif self._fps_count >= 10:
-            self._fps = self._fps_count / ((nowtime - self._fps_tick) / 1000.0)
+            t = (nowtime - self._fps_tick) / 1000.0
+            if t == 0.0:
+                self._fps = 0.0
+            else:
+                self._fps = self._fps_count / t
             self._fps_count = 0
             self._fps_tick = nowtime
         return self._time_passed