pygame初识-绘图

在上一章中已经向大家介绍了pygame的开发基础,了解怎么安装pygame并且熟悉了一下它的窗口程序创建,接下来,要带大家走近的是怎么利用pygame游戏库来实现并绘制基本的图形文本。

1、 打印文本

#font
import pygame,sys
from pygame.locals import *
pygame.init()
screen = pygame.display.set_mode((600,500))
pygame.display.set_caption("MyFont")
myfont = pygame.font.Font(None,60)
white = 255,255,255
blue = 0,0,255
textImage = myfont.render("Hello pygame",True,white)

while True:
    for event in pygame.event.get():
        if event.type in (QUIT,KEYDOWN):
            sys.exit()
    screen.fill(blue)
    screen.blit(textImage,(100,100))
    pygame.display.update()

pygame初识-绘图_第1张图片
屏幕快照 2017-07-01 上午1.38.32.png

2、画线

#draw line
import pygame
from pygame.locals import *
import sys

pygame.init()
screen = pygame.display.set_mode((600,500))
pygame.display.set_caption("Drawing Line")
while True:
    for event in pygame.event.get():
        if event.type in (QUIT,KEYDOWN):
            sys.exit()

    screen.fill((0,0,255))

    #draw line
    color = 100,255,200
    width = 4
    pygame.draw.line(screen,color,(0,0),(200,200),width)

    pygame.display.update()
pygame初识-绘图_第2张图片
屏幕快照 2017-07-01 上午1.32.28.png

3、画弧

#draw arc
import pygame
import math
from pygame.locals import *
import sys

pygame.init()
screen = pygame.display.set_mode((600,500))
pygame.display.set_caption("Drawing Arc")

while True:
    for event in pygame.event.get():
        if event in (QUIT,KEYDOWN):
            sys.exit()

    screen.fill((0,0,200))

    #draw arc
    color = 255,0,255
    position = 100,100,150,150
    start_angle = math.radians(0)
    end_angle = math.radians(270)
    width = 4
    pygame.draw.arc(screen,color,position,start_angle,end_angle,width)

    pygame.display.update()

pygame初识-绘图_第3张图片
屏幕快照 2017-07-01 上午1.30.44.png

4、画圆

#draw circle size/color/position
import pygame
import sys
from pygame.locals import *

pygame.init()
screen = pygame.display.set_mode((600,500))
fill_color = 0,0,255 
pygame.display.set_caption("Drawing Circles")

while True:
    for event in pygame.event.get():
        if event.type in (QUIT,KEYDOWN):
            sys.exit()

    screen.fill(fill_color)
    #draw circle
    color = 255,255,0
    position = 300,200
    radius = 100
    width = 2
    pygame.draw.circle(screen,color,position,radius,width)
    pygame.display.update()

pygame初识-绘图_第4张图片
屏幕快照 2017-07-01 上午1.26.56.png

5、画矩形

import pygame
import sys
from pygame.locals import *

pygame.init()
screen = pygame.display.set_mode((600,500))

pos_x = 250
pos_y = 200

while True:
    for event in pygame.event.get():
        if event.type in (QUIT,KEYDOWN):
            sys.exit()
    screen.fill((0,0,200))

    #draw the rectangle
    color = 255,255,0
    width = 0 #solid dill
    pos = pos_x,pos_y,100,100
    pygame.draw.rect(screen,color,pos,width)

    #refresh
    pygame.display.update()
pygame初识-绘图_第5张图片
屏幕快照 2017-07-01 上午1.42.48.png

你可能感兴趣的:(pygame初识-绘图)