pygame 图形
1 #!/usr/bin/python 2 # -*- coding: UTF-8 -*- 3 # python 27 4 5 import pygame, sys, random 6 from pygame.color import THECOLORS 7 pygame.init() 8 screen = pygame.display.set_mode([640, 480]) 9 screen.fill([255,255,255]) 10 for i in range(50): 11 w = random.randint(1, 250) 12 h = random.randint(0, 100) 13 t = random.randint(0, 400) 14 l = random.randint(0, 500) 15 o = random.randint(1, 2) 16 color_name = random.choice(THECOLORS.keys()) 17 color = THECOLORS[color_name] 18 pygame.draw.rect(screen, color, [w, h, t, l], o) 19 pygame.display.flip() 20 running = True 21 while running: 22 for event in pygame.event.get(): 23 if event.type == pygame.QUIT: 24 running = False 25 pygame.quit()
1 #!/usr/bin/python 2 # -*- coding: UTF-8 -*- 3 # python 27 4 5 import pygame, sys 6 import math 7 pygame.init() 8 screen = pygame.display.set_mode([640,480]) 9 screen.fill([225, 225, 225]) 10 plotpoints = [] 11 for x in range(0, 640):# 从左到右循环(x=0到639) 12 y = int(math.sin(x/640.0 * 4 * math.pi) * 200 + 240)# 计算每个点的y坐标 13 plotpoints.append([x, y])# 将各个点添加到列表 14 pygame.draw.lines(screen, [0, 0, 0],False, plotpoints, 2)# 用draw.lines()函数画出整条曲线 15 pygame.display.flip() 16 running = True 17 while running: 18 for event in pygame.event.get(): 19 if event.type == pygame.QUIT: 20 running = False 21 pygame.quit()
1 #!/usr/bin/python 2 # -*- coding: UTF-8 -*- 3 # python 27 4 5 import pygame, sys 6 pygame.init() 7 8 # 各个点的坐标 9 dota = [[221, 432], [22, 331], [133, 342], [141, 310], 10 [51, 230], [74, 217], [58, 153], [114, 164], 11 [123, 135], [176, 190], [159, 77],[193, 93], 12 [230, 28], [267, 93], [301, 77], [284, 190], 13 [327, 135], [336, 164],[402, 153], [386, 217], 14 [409, 230], [319, 310], [327, 342], [233, 331], 15 [237, 432]] 16 17 screen = pygame.display.set_mode([640, 480]) 18 screen.fill([255, 255, 255]) 19 pygame.draw.lines(screen, [255, 0, 0],True, dota, 2)# 连接每个点 close=True 20 pygame.display.flip() 21 running = True 22 while running: 23 for event in pygame.event.get(): 24 if event.type == pygame.QUIT: 25 running = False 26 pygame.quit()
如果想改变一个像素的颜色,可以不使用 draw 函数,而利用 Surface.set_at() 方法访问一个表面上的单个像素。你要指出希望设置哪个像素,以及设置成什么颜色:
Surface.set_at([x, y], [0, 0, 0]).
还可以用 Surface.set_at() 方法检查一个像素设置为什么颜色。只需要传入你想要检查的那个像素的坐标,比如: pixel_color = screen.get_at([320, 240]) 。screen是表面的名字。