pygame

基本流程

import pygame
#1. 初始化pygame
    pygame.init()

    #2. 創建游戲窗口
    #set_mode(寬度,高度)
    screen = pygame.display.set_mode((600,400))

    #3. 游戲循環
    while True:
        #檢測事件
        for event in pygame.event.get():
            #檢測關閉按鈕是否被點擊
            if event.type == pygame.QUIT:
                #退出游戲
                print('關閉按鈕被點擊!')
                exit()

文本顯示

"""__author__ == Jefferson"""
import pygame


if __name__ == '__main__':
    pygame.init()
    screen = pygame.display.set_mode((700,400))
    #設置窗口顏色
    screen.fill((38,54,64))
    #1. 創建字體對象
    '''
    創建系統字體
    SysFont(name, size, bold=False, italic=False)
    name:字體名
    size:字體大小
    bold:加粗
    italic:傾斜
    font = pygame.font.SysFont('times',50)
    '''

    '''
    創建自定義字體
    Font(字體文件路徑,字體大小)
    '''
    font = pygame.font.Font('./font/aa.ttf',300)

    #2. 根據字體創建顯示對象
    '''
    render(text, antialias, color, background=None)
    text: 要顯示的文字內容
    antialias: 是否平滑
    color: 文字顏色 RGB 值的范圍0~255
    紅色: (255,0,0)
    綠色: (0,255,0)
    藍色: (0,0,255)
    '''
    surface = font.render('流批!',True,(0,216,255))
    '''
    3. 將內容添加到窗口上
    blit(需要顯示的對象, 顯示位置)
    需要顯示的對象: Surface類型的數據
    顯示位置: 坐標(x,y)
    '''
    screen.blit(surface,(0,50))

    '''
    4. 將窗口上的內容展示出來(將畫有文字的紙貼出來)
    '''
    pygame.display.flip()



    while True:
        for event in pygame.event.get():
            if event.type == pygame.QUIT:
                exit()

顯示圖片

"""__author__ == Jefferson"""
import pygame

if __name__ == '__main__':
    pygame.init()
    screen = pygame.display.set_mode((690,685))
    screen = pygame.display.set_mode((405, 810))
    screen.fill((255,255,255))
    #1. 獲取圖片對象
    image = pygame.image.load('./image/2.jpg')
    image_dog = pygame.image.load('./image/1.jpg')
    #獲取圖片大小
    image_size = image.get_size()
    print(image_size)
    '''
    形變:
    transform: 形變包喊縮放,旋轉和平移
    scale(縮放對象,新的大小): 返回一個縮放后的新對象
    new_image = pygame.transform.scale(image,(50,50))
    rotate(旋轉對象,旋轉角度)
    new_image = pygame.transform.rotate(image,180)
    '''
    image_dog = pygame.transform.rotozoom(image_dog,0,0.6)


    #2. 將圖片對象渲染到窗口上
    screen.blit(image_dog,(0,0))
    screen.blit(image,(0,400))
    #3. 展示到屏幕上
    pygame.display.flip()


    while True:
        for event in pygame.event.get():
            if event.type==pygame.QUIT:
                exit()

顯示圖形

"""__author__ == Jefferson"""
import pygame

if __name__ == '__main__':
    pygame.init()
    screen = pygame.display.set_mode((600,400))
    screen.fill((255,255,255))

    '''
    1. 畫直線
    line(Surface,color,start_pos,end_pos,width=1)
    Surface: 畫在哪個地方
    color: 線的顏色
    start_pos: 起點
    end_pos: 終點
    width: 線的寬度
    '''
    pygame.draw.line(screen,(255,0,0),(78,59),(100,100),2)
    pygame.draw.line(screen,(0,255,0),(0,50),(130,100),2)
    #lines(畫線的位置, 顏色, closed, 點的列表, width)
    pygame.draw.lines(screen,(0,216,255),True,[(10,10),(50,50),(60,15)],5)

    '''
    畫矩形
    rect(位置, 顏色, (x,y,width,height),with)
    '''



    '''
    2. 畫曲線
    arc(Surface,color,Rect,start_angle,stop_angle,width)
    Rect: (x,y,width,height)矩形
    start_angle:
    stop_angle:
    
    
    '''
    from math import pi
    pygame.draw.arc(screen,(0,0,0),(0,0,200,200),pi/2,pi)


    '''
    3. 畫圓
    circle(位置,顏色,圓心位置,半徑,width=0)
    
    '''
    import random
    pygame.draw.circle(screen,(random.randint(0,255),random.randint(0,255),random.randint(0,255)),(400,200),100)


    '''
    4. 畫橢圓
    ellipse(Surface,color,Rect,width=0)
    '''
    pygame.draw.ellipse(screen,(0,100,255),(100,300,200,80),1)



    pygame.display.flip()

    while True:
        for event in pygame.event.get():
            if event.type == pygame.QUIT:
                exit()


動畫原理

通過不停的重置窗口畫面實現動畫效果

import pygame
if __name__ == '__main__':
    pygame.init()
    screen = pygame.display.set_mode((600,400))
    screen.fill((255,255,255))
    x=0
    y=0

    while True:
        for event in pygame.event.get():
            if event.type == pygame.QUIT:
                exit()
        x+=1
        y+=1
        screen.fill((255,255,255))
        pygame.draw.circle(screen,(255,0,0),(x,y),80)
        pygame.display.flip()
?著作權歸作者所有,轉載或內容合作請聯系作者
平臺聲明:文章內容(如有圖片或視頻亦包括在內)由作者上傳并發布,文章內容僅代表作者本人觀點,簡書系信息發布平臺,僅提供信息存儲服務。

推薦閱讀更多精彩內容