Pygame series _ draw game drawing

Source: Internet
Author: User

Speaking of drawing, pygame provides some useful methods.DrawDrawing.

‘‘‘pygame.draw.rect - draw a rectangle shape    draw a rectangle shapepygame.draw.polygon - draw a shape with any number of sides    draw a shape with any number of sidespygame.draw.circle - draw a circle around a point    draw a circle around a pointpygame.draw.ellipse - draw a round shape inside a rectangle    draw a round shape inside a rectanglepygame.draw.arc - draw a partial section of an ellipse    draw a partial section of an ellipsepygame.draw.line - draw a straight line segment    draw a straight line segmentpygame.draw.lines - draw multiple contiguous line segments    draw multiple contiguous line segmentspygame.draw.aaline - draw fine antialiased lines    draw fine antialiased linespygame.draw.aalines - pygame.draw.aalines(Surface, color, closed, pointlist, blend=1): return Rect‘‘‘
1 pygame. Draw. rect # Draw a rectangle

Below is my demo

When you click in a window, the system will automatically draw a rectangle, press any key on the keyboard, clear the screen

========================================================== ==========

Code Section:

========================================================== ==========

 1 #pygame draw 2  3 import pygame 4 from pygame.locals import * 5 from sys import exit 6 from random import * 7  8 __author__ = {‘name‘ : ‘Hongten‘, 9               ‘mail‘ : ‘[email protected]‘,10               ‘blog‘ : ‘http://www.cnblogs.com/hongten‘,11               ‘Version‘ : ‘1.0‘}12 13 pygame.init()14 15 SCREEN_DEFAULT_SIZE = (500, 500)16 SCREEN_DEFAULT_COLOR = (0, 0 ,0)17 18 screen = pygame.display.set_mode(SCREEN_DEFAULT_SIZE, 0, 32)19 screen.fill(SCREEN_DEFAULT_COLOR)20 21 while 1:22     for event in pygame.event.get():23         if event.type ==  QUIT:24             exit()25         elif event.type == KEYDOWN:26             screen.fill(SCREEN_DEFAULT_COLOR)27         elif event.type == MOUSEBUTTONDOWN:28             rect_color = (randint(0, 255), randint(0, 255), randint(0, 255))29             rect_pos = (randint(0, 500), randint(0, 500))30             rect_pos_end = (500 - randint(rect_pos[0], 500), 500 - randint(rect_pos[1], 500))31             pygame.draw.rect(screen, rect_color, Rect(rect_pos, rect_pos_end))32     pygame.display.update()
1 pygame. Draw. Circle # Circle

Demo:

When the mouse moves in the window, click the mouse to generate a random circle in the window, press any key on the keyboard, clear the screen

========================================================== ============

Code Section:

========================================================== ============

 1 #pygame draw 2  3 import pygame 4 from pygame.locals import * 5 from sys import exit 6 from random import * 7  8 __author__ = {‘name‘ : ‘Hongten‘, 9               ‘mail‘ : ‘[email protected]‘,10               ‘blog‘ : ‘http://www.cnblogs.com/hongten‘,11               ‘Version‘ : ‘1.0‘}12 13 pygame.init()14 15 SCREEN_DEFAULT_SIZE = (500, 500)16 SCREEN_DEFAULT_COLOR = (0, 0 ,0)17 18 screen = pygame.display.set_mode(SCREEN_DEFAULT_SIZE, 0, 32)19 screen.fill(SCREEN_DEFAULT_COLOR)20 21 while 1:22     for event in pygame.event.get():23         if event.type ==  QUIT:24             exit()25         elif event.type == KEYDOWN:26             screen.fill(SCREEN_DEFAULT_COLOR)27         elif event.type == MOUSEBUTTONDOWN:28             c_color = (randint(0, 255), randint(0, 255), randint(0, 255))29             c_pos = (randint(0, 500), randint(0, 500))30             c_r = randint(10, 100)31             pygame.draw.circle(screen, c_color, c_pos, c_r)32     pygame.display.update()
1 pygame. Draw. Line # Draw line

Demo:

When the mouse moves in the window, there are always some lines and mouse convergence. When the mouse is clicked, it will record the shape at this time.

Press any key on the keyboard to clear the screen

You can also cancel this function:

1 record = false # cancel recording the mouse track

========================================================== ============

Code Section:

========================================================== ============

 1 #pygame draw 2  3 import pygame 4 from pygame.locals import * 5 from sys import exit 6 from random import * 7  8 __author__ = {‘name‘ : ‘Hongten‘, 9               ‘mail‘ : ‘[email protected]‘,10               ‘blog‘ : ‘http://www.cnblogs.com/hongten‘,11               ‘Version‘ : ‘1.0‘}12 13 pygame.init()14 15 SCREEN_WIDTH = 50016 SCREEN_HEIGHT = 50017 SCREEN_DEFAULT_SIZE = (SCREEN_WIDTH, SCREEN_HEIGHT)18 SCREEN_DEFAULT_COLOR = (0, 0 ,0)19 #record the mouse clicked points20 RECORD = True21 22 screen = pygame.display.set_mode(SCREEN_DEFAULT_SIZE, 0, 32)23 screen.fill(SCREEN_DEFAULT_COLOR)24 25 def draw_lines(screen, line_color, points, mouse_pos):26     for point in points:27         pygame.draw.line(screen, line_color, point, mouse_pos)28 ps = []29 #you can add other points30 points = [(0, 0), (250, 0), (500, 0),31           (0, 250),(0, 500),(250, 500),32           (500, 250),(500, 500)]33 34 35 while 1:36     for event in pygame.event.get():37         if event.type ==  QUIT:38             exit()39         elif event.type == KEYDOWN:40             screen.fill(SCREEN_DEFAULT_COLOR)41         elif event.type == MOUSEMOTION:42             screen.fill(SCREEN_DEFAULT_COLOR)43             line_color = (randint(0, 255), randint(0, 255), randint(0, 255))44             draw_lines(screen, line_color, points, pygame.mouse.get_pos())45             #record the mouse clicked points depend on yourself46             if not RECORD:47                 ps = []48             for c_p in ps:49                 draw_lines(screen, c_p[0], points, c_p[1])  50         elif event.type == MOUSEBUTTONDOWN:51             x, y = pygame.mouse.get_pos()52             line_color = (randint(0, 255), randint(0, 255), randint(0, 255))53             draw_lines(screen, line_color, points, (x, y))54             ps.append((line_color, (x, y)))55             56     pygame.display.update()

More information: http://pygame.org/docs/ref/draw.html

Pygame series _ draw game drawing

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.