首页 > 代码库 > pygame系列_draw游戏画图
pygame系列_draw游戏画图
说到画图,pygame提供了一些很有用的方法进行draw画图。
‘‘‘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 #画一个矩形
下面是我做的demo
有鼠标在窗口中点击的时候,系统会自动画出一个矩形,按键盘任意键,清屏
=================================================
代码部分:
=================================================
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:
当鼠标在窗口中移动的时候,单击鼠标,即可在窗口中产生一个随机圆,按下键盘任意键,清屏
==================================================
代码部分:
==================================================
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:
鼠标在窗口中移动的时候,总是有一些线和鼠标汇聚,当鼠标被点击的时候,就会记录下此时的形状
按下键盘任意键,清屏
当然你也可以取消这个功能:
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 = 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()
更多信息:http://pygame.org/docs/ref/draw.html
pygame系列_draw游戏画图
声明:以上内容来自用户投稿及互联网公开渠道收集整理发布,本网站不拥有所有权,未作人工编辑处理,也不承担相关法律责任,若内容有误或涉及侵权可进行投诉: 投诉/举报 工作人员会在5个工作日内联系你,一经查实,本站将立刻删除涉嫌侵权内容。