Day10-GUI

2018-07-28  本文已影响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)创建字体对象

# 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):

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(地址)

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()
上一篇 下一篇

猜你喜欢

热点阅读