Pages

Se afișează postările cu eticheta gfxdraw. Afișați toate postările
Se afișează postările cu eticheta gfxdraw. Afișați toate postările

sâmbătă, 29 iulie 2017

PyGame : The experimental gfxdraw.

This is a tutorial about gfxdraw how to use.
Note: this is an EXPERIMENTAL feature for PyGame ( meaning this API may change, or disappear in later pygame releases).
First about this gfxdraw we can use this:
  • pygame module for drawing shapes
  • pygame.gfxdraw.pixel — place a pixel
  • pygame.gfxdraw.hline — draw a horizontal line
  • pygame.gfxdraw.vline — draw a vertical line
  • pygame.gfxdraw.rectangle — draw a rectangle
  • pygame.gfxdraw.box — draw a box
  • pygame.gfxdraw.line — draw a line
  • pygame.gfxdraw.circle — draw a circle
  • pygame.gfxdraw.arc — draw an arc
  • pygame.gfxdraw.aacircle — draw an anti-aliased circle
  • pygame.gfxdraw.filled_circle — draw a filled circle
  • pygame.gfxdraw.ellipse — draw an ellipse
  • pygame.gfxdraw.aaellipse — draw an anti-aliased ellipse
  • pygame.gfxdraw.filled_ellipse — draw a filled ellipse
  • pygame.gfxdraw.pie — draw a pie
  • pygame.gfxdraw.trigon — draw a triangle
  • pygame.gfxdraw.aatrigon — draw an anti-aliased triangle
  • pygame.gfxdraw.filled_trigon — draw a filled trigon
  • pygame.gfxdraw.polygon — draw a polygon
  • pygame.gfxdraw.aapolygon — draw an anti-aliased polygon
  • pygame.gfxdraw.filled_polygon — draw a filled polygon
  • pygame.gfxdraw.textured_polygon — draw a textured polygon
  • pygame.gfxdraw.bezier — draw a Bézier curve
Nou about the example I will use into this tutorial. Is a very simple class with an anti-aliased ellipse.
This class used pygame.gfxdraw.aaellipse , see the doc:
pygame.gfxdraw.aaellipse()
    draw an anti-aliased ellipse
      aaellipse(surface, x, y, rx, ry, color) -> None
      Draws anti aliased edges of an ellipse onto a surface.
#!/usr/bin/python

import random
import math

import pygame
from pygame.locals import *
from pygame import *
from pygame import gfxdraw

#create a math function
def linear_interp(a, b, x):
 return a*(1-x) + b*x

#a class python to show on display
class Screen():
 def __init__(self):
  pygame.init()

  self.display_flags = DOUBLEBUF
  rect = self.width, self.height = 640, 480

  if pygame.display.mode_ok(rect, self.display_flags):
   self.screen = pygame.display.set_mode(rect, self.display_flags)

 def shell_print(self,text):
  print text

 def aaellipse(self,surface, x, y, rx, ry, color):
  pygame.gfxdraw.ellipse(surface, x, y, rx, ry, color)

show = Screen()

run = 1
clock = pygame.time.Clock()

show.shell_print("test")
show.aaellipse(show.screen, x=300, y=200, rx=100, ry=60, color=(8, 8, 160))

while run:
 events = pygame.event.get()

 for event in events:
  if event.type == QUIT:
   run = 0

 pygame.display.flip()

 clock.tick(60)
To understand the class methods I used two: shell_print and aaellipse.
The shell_print is used just to print a input text to python shell.
The aaellipse take all inputs for the default pygame.gfxdraw.ellipse.
The result is a simple blue ellipse at 300 and 200 points and sized with 100 and 60.
Another aspect to note is use of the surface into show.aaellipse : show.screen.
This means that the drawing screen is used and not another defined surface.
Of course we can change Screen class to help us - decorators, other defining surfaces, initializations...