• pygame系列_draw游戏画图


    说到画图,pygame提供了一些很有用的方法进行draw画图。

    '''
    pygame.draw.rect - draw a rectangle shape    draw a rectangle shape
    pygame.draw.polygon - draw a shape with any number of sides    draw a shape with any number of sides
    pygame.draw.circle - draw a circle around a point    draw a circle around a point
    pygame.draw.ellipse - draw a round shape inside a rectangle    draw a round shape inside a rectangle
    pygame.draw.arc - draw a partial section of an ellipse    draw a partial section of an ellipse
    pygame.draw.line - draw a straight line segment    draw a straight line segment
    pygame.draw.lines - draw multiple contiguous line segments    draw multiple contiguous line segments
    pygame.draw.aaline - draw fine antialiased lines    draw fine antialiased lines
    pygame.draw.aalines - pygame.draw.aalines(Surface, color, closed, pointlist, blend=1): return Rect
    '''
    1 pygame.draw.rect   #画一个矩形

    下面是我做的demo

    hongten_pygame

    有鼠标在窗口中点击的时候,系统会自动画出一个矩形,按键盘任意键,清屏

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

    代码部分:

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

     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' : 'hongtenzone@foxmail.com',
    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  #画圆

    demo:

    hongten_pygame

    当鼠标在窗口中移动的时候,单击鼠标,即可在窗口中产生一个随机圆,按下键盘任意键,清屏

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

    代码部分:

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

     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' : 'hongtenzone@foxmail.com',
    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   #画线

    demo:

    hongten_pygame

    鼠标在窗口中移动的时候,总是有一些线和鼠标汇聚,当鼠标被点击的时候,就会记录下此时的形状

    按下键盘任意键,清屏

    当然你也可以取消这个功能:

    1 RECORD = False   #取消记录鼠标轨迹

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

    代码部分:

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

     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' : 'hongtenzone@foxmail.com',
    10               'blog' : 'http://www.cnblogs.com/hongten',
    11               'Version' : '1.0'}
    12 
    13 pygame.init()
    14 
    15 SCREEN_WIDTH = 500
    16 SCREEN_HEIGHT = 500
    17 SCREEN_DEFAULT_SIZE = (SCREEN_WIDTH, SCREEN_HEIGHT)
    18 SCREEN_DEFAULT_COLOR = (0, 0 ,0)
    19 #record the mouse clicked points
    20 RECORD = True
    21 
    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 points
    30 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 yourself
    46             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()

    更多信息:http://pygame.org/docs/ref/draw.html

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

    More reading,and english is important.

    I'm Hongten

     

    大哥哥大姐姐,觉得有用打赏点哦!多多少少没关系,一分也是对我的支持和鼓励。谢谢。
    Hongten博客排名在100名以内。粉丝过千。
    Hongten出品,必是精品。

    E | hongtenzone@foxmail.com  B | http://www.cnblogs.com/hongten

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

  • 相关阅读:
    Linux之lsof命令
    lnmp一键安装的卸载
    MySQL密码忘了怎么办?MySQL重置root密码方法
    LNMP状态管理命令
    perl5 第一章 概述
    行政级别详解
    http://dl.fedoraproject.org/pub/epel/7/x86_64/,开源软件清单list
    Linux Centos 系统上安装BT客户端 Transmission
    在VPS上安裝BT軟體Transmission
    What is a good EPUB reader on Linux
  • 原文地址:https://www.cnblogs.com/hongten/p/hongten_pygame_draw_shape.html
Copyright © 2020-2023  润新知