Pygame图形
Pygame的座标原点(0,0)点位于左上角,X轴自左向右,Y轴自上向下,单位为像素。
pygame.draw.line(Surface, color, start_pos, end_pos, width)此方法用于绘制一条线段。
pygame.draw.rect(Surface, color, Rect)此方法用于绘制一个矩形
pygame.draw.rect(Surface, color, Rect, width)此方法用于绘制一个矩形框
import pygame
import sys
#from pygame.locals import *
pygame.init() # 初始化pygame,为使用硬件做准备
pygame.display.set_caption("Drawing") # 设置窗口标题
screen=pygame.display.set_mode((400,300))
BLACK = ( 0, 0, 0)
WHITE = (255, 255, 255)
RED = (255, 0, 0)
GREEN = ( 0, 255, 0)
BLUE = ( 0, 0, 255)
screen.fill(WHITE)
# 绘制一条线
pygame.draw.line(screen, GREEN, [0, 0], [150,30], 10)
# 绘制一个矩形
pygame.draw.rect(screen, BLACK, [150, 10, 100, 100],15)
while True: # 游戏主循环
for event in pygame.event.get():
if event.type == pygame.QUIT: # 接收到退出时间后退出程序
pygame.quit()
sys.exit()
pygame.display.update()