赞
踩
1.绘制矩形,使用 rect() 方法:
pygame.draw.rect()
rect(Surface, color, Rect, width = 0)
在 Surface对象上绘制一个矩形,color 表示边框的颜色。Rect 参数指定矩形的位置和尺寸。width 参数指定边框的宽度,如果设置为 0 则表示填充该矩形。
例如:
- import pygame
- import sys
- from pygame.locals import *
-
- pygame.init()
-
- WHITE = (255, 255, 255)
- BLACK = (0, 0, 0)
-
- size = width, height = 640,200
- screen = pygame.display.set_mode(size)
- pygame.display.set_caption("Python Demo")
-
- clock = pygame.time.Clock()
-
- while True:
- for event in pygame.event.get():
- if event.type == QUIT:
- sys.exit()
-
- screen.fill(WHITE)
-
- pygame.draw.rect(screen, BLACK, (50, 50, 150, 50), 0)
- pygame.draw.rect(screen, BLACK, (250, 50, 150, 50), 1)
- pygame.draw.rect(screen, BLACK, (450, 50, 150, 50), 10)
-
- pygame.display.flip()
-
- clock.tick(10)
运行效果:
2.绘制多边形,使用polygon()方法:
pygame.draw.polygon()
polygon(Surface, color, pointlist, width=0)
在Surface对象上绘制一个多边形,pointlist 参数指定多边形的各个顶点。width 参数指定边框的宽度,如果设置为 0 则表示填充该矩形。
例如:
- import pygame
- import sys
- from pygame.locals import *
-
- pygame.init()
-
- WHITE = (255, 255, 255)
- BLACK = (0, 0, 0)
- GREEN = (0, 255, 0)
-
- points = [(200, 75), (300, 25), (400, 75), (450, 25), (450, 125), (400, 75), (300, 125)]
-
- size = width, height = 640, 200
- screen = pygame.display.set_mode(size)
- pygame.display.set_caption("Python Demo")
-
- clock = pygame.time.Clock()
-
- while True:
- for event in pygame.event.get():
- if event.type == QUIT:
- sys.exit()
-
- screen.fill(WHITE)
-
- pygame.draw.polygon(screen, GREEN, points, 0)
-
- pygame.display.flip()
-
- clock.tick(10)
运行效果:
3.根据圆心和半径绘制圆形:
pygame.draw.circle()
circle(Surface, color, pos, radius, width=0)
在 Surface对象上绘制一个圆形,pos 参数指定圆心的位置,radius 参数指定圆的半径。width 参数指定边框的宽度,如果设置为 0 则表示填充该矩形。
注:
event.button == 1:#表示鼠标左键
event.button == 2:#表示鼠标中间键
event.button == 3:#表示鼠标右键
event.button == 4:#表示滚轮向上
event.button == 5:#表示滚轮向下
例如:
- import pygame
- import sys
- from pygame.locals import *
-
- pygame.init()
-
- WHITE = (255, 255, 255)
- BLACK = (0, 0, 0)
- GREEN = (0, 255, 0)
- RED = (255, 0, 0)
- BLUE = (0, 0, 255)
-
- size = width, height = 640, 480
- screen = pygame.display.set_mode(size)
- pygame.display.set_caption("Python Demo")
-
- position = size[0]//2, size[1]//2
- moving = False
-
- clock = pygame.time.Clock()
-
- while True:
- for event in pygame.event.get():
- if event.type == QUIT:
- sys.exit()
-
- if event.type == MOUSEBUTTONDOWN:
- if event.button == 1:#表示鼠标左键
- moving = True
-
- if event.type == MOUSEBUTTONUP:
- if event.button == 1:
- moving = False
-
- if moving:
- position = pygame.mouse.get_pos()
-
- screen.fill(WHITE)
-
- pygame.draw.circle(screen, RED, position, 25, 1)
- pygame.draw.circle(screen, GREEN, position, 75, 1)
- pygame.draw.circle(screen, BLUE, position, 125, 1)
-
- pygame.display.flip()
-
- clock.tick(120)
运行效果:
4.根据限定矩形绘制一个椭圆形:
pygame.draw.ellipse()
ellipse(Surface, color, Rect, width=0)
在 Surface 对象上绘制一个椭圆形,Rect 参数指定椭圆外围的限定矩形(当此矩形为正方形时,得到的也是圆形)。width 参数指定边框的宽度,如果设置为 0 则表示填充该矩形。
例如:
- import pygame
- import sys
- from pygame.locals import *
-
- pygame.init()
-
- WHITE = (255, 255, 255)
- BLACK = (0, 0, 0)
- GREEN = (0, 255, 0)
- RED = (255, 0, 0)
- BLUE = (0, 0, 255)
-
- size = width, height = 640, 300
- screen = pygame.display.set_mode(size)
- pygame.display.set_caption("Python Demo")
-
- clock = pygame.time.Clock()
-
- while True:
- for event in pygame.event.get():
- if event.type == QUIT:
- sys.exit()
-
- screen.fill(WHITE)
-
- pygame.draw.ellipse(screen, RED, (220, 50, 200, 200), 0)#圆
- pygame.draw.ellipse(screen, BLACK, (100, 100, 440, 100), 1)
-
-
- pygame.display.flip()
-
- clock.tick(10)
运行效果(语句先后执行决定了谁覆盖谁):
5.绘制弧线(圆和椭圆的一部分):
pygame.draw.arc()
arc(Surface, color, Rect, start_angle, stop_angle, width=1)
在 Surface 对象上绘制一条弧线,Rect 参数指定弧线所在的椭圆外围的限定矩形。两个 angle 参数指定弧线的开始和结束位置。width 参数指定边框的宽度。
例如:
- import pygame
- import sys
- import math
- from pygame.locals import *
-
- pygame.init()
-
- WHITE = (255, 255, 255)
- BLACK = (0, 0, 0)
- GREEN = (0, 255, 0)
- RED = (255, 0, 0)
- BLUE = (0, 0, 255)
-
- size = width, height = 640, 300
- screen = pygame.display.set_mode(size)
- pygame.display.set_caption("Python Demo")
-
- clock = pygame.time.Clock()
-
- while True:
- for event in pygame.event.get():
- if event.type == QUIT:
- sys.exit()
-
- screen.fill(WHITE)
-
- pygame.draw.arc(screen, BLACK, (100, 100, 440, 100), 0, math.pi, 1)
- pygame.draw.arc(screen, BLACK, (220, 50, 200, 200), math.pi, math.pi * 2, 1)
-
- pygame.display.flip()
-
- clock.tick(10)
运行效果:
6.绘制线段:
pygame.draw.line()
pygame.draw.lines()
line(Surface, color, start_pos, end_pos, width=1)
在 Surface 对象上绘制一条线段。两端以方形结束。
lines(Surface, color, closed, pointlist, width=1)
在 Surface 对象上绘制一系列连续的线段。pointlist 参数是一系列短点。如果 closed 参数设置为 True,则绘制首尾相连。
另:绘制抗锯齿的线段:
aaline() 和 aalines() 是绘制抗锯齿的线段。最后一个参数不是width 而是 blend,指定是否通过绘制混合背景的阴影来实现抗锯齿功能,由于没有 width 选项,所以这两个方法只能绘制唯一像素宽度值的线段。
pygame.draw.aaline()
aaline(Surface, color, startpos, endpos, blend=1)
在 Surface 对象上绘制一条抗锯齿的线段。blend 参数指定是否通过绘制混合背景的阴影来实现抗锯齿功能。该函数的结束位置允许使用浮点数。
pygame.draw.aalines()
aalines(Surface, color, closed, pointlist, blend=1)
在 Surface 对象上绘制一系列连续的线段(抗锯齿),如果 closed 参数为 True,则首尾相连。blend 参数指定是否通过绘制混合背景的阴影来实现抗锯齿功能。该函数的结束位置允许使用浮点数。
例如:
- import pygame
- import sys
- from pygame.locals import *
-
- pygame.init()
-
- WHITE = (255, 255, 255)
- BLACK = (0, 0, 0)
- GREEN = (0, 255, 0)
-
- points = [(200, 75), (300, 25), (400, 75), (450, 25), (450, 125), (400, 75), (300, 125)]
-
- size = width, height = 640, 480
- screen = pygame.display.set_mode(size)
- pygame.display.set_caption("Python Demo")
-
- clock = pygame.time.Clock()
-
- while True:
- for event in pygame.event.get():
- if event.type == QUIT:
- sys.exit()
-
- screen.fill(WHITE)
-
- pygame.draw.lines(screen, GREEN, 1, points, 1)#此处最后一个参数为0的时候,不会填充,而是线段消失,与多边形绘制不同
- pygame.draw.line(screen, BLACK, (100, 200), (540, 250), 1)
- pygame.draw.aaline(screen, BLACK, (100, 250), (540, 300), 1)
- pygame.draw.aaline(screen, BLACK, (100, 300), (540, 350), 0)
-
- pygame.display.flip()
-
- clock.tick(10)
运行效果:
Copyright © 2003-2013 www.wpsshop.cn 版权所有,并保留所有权利。