Day10-GUI

作者: 晓晓的忍儿 | 来源:发表于2018-07-28 09:47 被阅读0次

1.窗口创建三个必须操作

1)1.初始化pygame

pygame.inin()

2)创建游戏窗口

set_mode(  (宽度(像素),高度(像素))  )
screen=pygame.display.set_mode((窗口长度,窗口宽))

3)游戏循环


while True:
    #必做:检测事件
    for event in pygame.event.get():
        # 检测窗口上的关闭按钮是否被点击
         if event.type==pygame.QUIT  #检测游戏退出事件
                # 退出
                exit()

2 显示字体

1)设置背景

fill((颜色))

2)创建字体对象

  • 创建系统字体 SyFont()
    SysFont(name, size, bold=0, italic=0, constructor=None):
  • name:字体名字
  • size:字体大小
  • bold:加粗
  • italic:倾斜
# font=pygame.font.SysFont('宋体',12) #有错
    # font=pygame.font.SysFont('Times',22)

4)创建自定义字体

Font(字体文件路径,字体大小)

font=pygame.font.Font('./font/bit.ttf',33)

5)根据字体去创建(文字)显示对象

def render(self,text,antialias,color,background=None):

  • self :系统自己赋值
  • text:要显示的文字内容
  • antialias:是否平滑
  • color:字体颜色(计算机三原色(红、绿、蓝)),RGB颜色
    值的范围都是0-255
    (255,0,0)->红
    (0,255,0)->绿
    (0,255,0)->蓝
    (0,0,0)->黑
    (255,255,255)->白
    (X,X,X)->灰色
surface=font.render('汉字,hello',True,(0,255,0))
    print(type(surface))

6)将内容添加到窗口上(渲染)

blit(需要显示的对象,显示位置)
需要显示的对象-->Surface类型的数据
显示位置-->坐标(x,y)

 screen.blit(surface,(100,100))

7)将窗口上的内容展示出来

pygame.display.flip()

import pygame
if __name__ == '__main__':
    pygame.init()
 screen=pygame.display.set_mode((600,400))
    # 设置背景
    screen.fill((255,255,255))
    # 1.创建字体对象
    # font=pygame.font.SysFont('宋体',12) #有错
    font=pygame.font.SysFont('Times',22)
    font=pygame.font.Font('./font/bit.ttf',33)
    #2.根据字体去创建(文字)显示对象
    surface=font.render('汉字,hello',True,(0,255,0))
    print(type(surface))
    # 将内容添加到窗口
    screen.blit(surface,(100,100)
    pygame.display.flip()
    while True:
        for event in pygame.event.get():
            if event.type==pygame.QUIT:
                exit()

3.显示图片

1)获取图片

pygame.image.load(地址)

  • 获取图片大小
    get_size()
  • 缩放图片
    *transform:形变包含缩放、平移和旋转

scale(缩放对象,新的大小)-->返回一个缩放后的新对象

rotate(旋转对象,旋转角度)
rotozoom(旋转对象,旋转角度,缩放比例)

import  pygame
if __name__ == '__main__':
    #初始化
    pygame.init()
    #创建窗口
    screen=pygame.display.set_mode((700,400))
    #添加背景
    screen.fill((255,255,255))
    #获取图像
    images=pygame.image.load('./image/fubo.jpg')
    #获取图片大小
    images_size=images.get_size()
    print(images_size)
    #图片缩放
    new_images=pygame.transform.scale(images,(400,300))
    #旋转
    images=pygame.transform.rotata(images,90)
    #缩放旋转
    images=pygame.transform.rotozoom(images,270,0.4)
    # 将图片对象渲染到窗口上
    screen.blit(images,(0,0))
    # screen.blit(new_images,(200,200))
    #展示在屏幕上
    pygame.display.flip()
    while True:
        for event in pygame.event.get():
            if event.type==pygame.QUIT:
                exit()

4)图像显示

1)画直线

line(Surface, color,start_pos,end_pos,width=1):
surface->画在哪个地方
color->线的颜色
start_pos->起点
end_pos->起点
width->线的宽度
lines(画线的位置,颜色,closed,点的列表,width=1)

pygame.draw.line(screen,(255,0,0),(78,59),(100,100),5)

2)画矩形

rect(位置,颜色,(0,0,宽度,长度),width=1)

pygame.draw.rect(screen,(0,255,0),(200,200,100,100),1)

3)画曲线

arc(Surface,color,Rect,start_angle,stop_angle,width=1)
Rect->(x,y,width,height)矩形
start_angle:
stop_angle:

pygame.draw.arc(screen,(0,0,0),(200,200,200,100),math.pi,math.pi*2,2)

4)画圆

circle(位置,颜色,圆心位置,半径,width=0)
import random
    pygame.draw.circle(screen,(random.randint(0,255),random.randint(0,255),\
                               random.randint(0,255)),(random.randint(0,255),random.randint(0,255)),50,0)

5)画椭圆 ellipse(surface,color,rect,width=0)

pygame.draw.ellipse(screen,(0,234,123),(400,200,240,300),1)
import pygame
import math
if __name__ == '__main__':
    screen=pygame.display.set_mode((800,500))
    screen.fill((234,215,123))
    
    #画直线
    
    pygame.draw.line(screen,(255,0,0),(78,59),(100,100),5)
    pygame.draw.line(screen, (255, 0, 0), (110, 100), (128, 59), 5)
    pygame.draw.line(screen, (255, 0, 0), (78, 62), (128, 62), 5)
    pygame.draw.lines(screen,(0,0,255),True,[(10,10),(45,67),(10,210)],2)
    #画矩形
   
    pygame.draw.rect(screen,(0,255,0),(200,200,100,100),1)
 
     #画曲线
    
    pygame.draw.arc(screen,(0,0,0),(200,200,200,100),math.pi,math.pi*2,2)
    #画圆
    import random
    pygame.draw.circle(screen,(random.randint(0,255),random.randint(0,255),\              random.randint(0,255)),(random.randint(0,255),random.randint(0,255)),50,0)
    #画椭圆
    pygame.draw.ellipse(screen,(0,234,123),(400,200,240,300),1)
    pygame.display.flip()
    while True:
        for event in pygame.event.get():
            if event.type==pygame.QUIT:
                exit()

相关文章

  • Day10-GUI

    1.窗口创建三个必须操作 1)1.初始化pygame 2)创建游戏窗口 3)游戏循环 2 显示字体 1)设置背景 ...

网友评论

      本文标题:Day10-GUI

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