美文网首页
pygame初识-绘图

pygame初识-绘图

作者: 进击的鸭子 | 来源:发表于2017-07-01 01:43 被阅读0次

在上一章中已经向大家介绍了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()

屏幕快照 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()
屏幕快照 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()

屏幕快照 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()

屏幕快照 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()
屏幕快照 2017-07-01 上午1.42.48.png

相关文章

  • pygame初识-绘图

    在上一章中已经向大家介绍了pygame的开发基础,了解怎么安装pygame并且熟悉了一下它的窗口程序创建,接下来,...

  • Mac pygame踩坑(1)

    初次使用pygame实现绘图功能就踩坑 直接上代码 import pygame pygame.init() # 创...

  • pygame初识

    1.基础框架 Pygame 别人写好的第三方库 用pygame写一个游戏的最最基础的框架 如何下载pygame,下...

  • pygame绘图应用-Pie Game

    结合上一章应用pygame绘图的知识点,做一个简单的小游戏开发。Pie游戏,是一款非常简单的游戏,没有什么难度,但...

  • 表面对象和窗口(Surface Objects and The

    Surface对象是表示矩形2D图像。可以通过调用Pygame绘图函数更改Surface对象的像素,然后在屏幕上显...

  • 使用 CustomPaint 绘制基本图形

    前言 上一篇初识 Flutter 的绘图组件 — CustomPaint[https://www.jianshu....

  • 2018-09-04-pygame

    一、pygame基本操作 import pygame——导入pygame模块 pygame.init()——初始化...

  • Pygame入门--飞机大战案例

    Pygame的快速入门 #导入pygame模块 import pygame #游戏初始化 pygame.init(...

  • Day_10 异常与pygame

    异常捕获 pygame操作流程 pygame显示文字 pygame显示图片与图片操作 pygame基本显示

  • 从零开始学Sketch——进阶篇

    Sketch是一款矢量绘图应用,而矢量绘图无疑是目前进行网页、图标以及界面设计的最好方式。 在初识了Sketch的...

网友评论

      本文标题:pygame初识-绘图

      本文链接:https://www.haomeiwen.com/subject/opkwcxtx.html