day10-pygame

1. 最小游戲框架

import pygame

  1. 初始化pygame
    pygame.init()

  2. 創(chuàng)建游戲窗口

set_mode((寬度,高度))
screen = pygame.display.set_mode((600, 400))
  1. 游戲循環(huán)
    while True:
        # 檢測事件
        for event in pygame.event.get():
            # 檢測窗口上的關(guān)閉按鈕是否被點(diǎn)擊
            if event.type == pygame.QUIT:
                # 退出游戲
                print("關(guān)閉按鈕被點(diǎn)擊  ")
                exit()

2. 顯示文字

import pygame
if __name__ == '__main__':
    pygame.init()
    screen = pygame.display.set_mode((700, 500))
    # 設(shè)置窗口的背景顏色
    screen.fill((255, 0, 0))
  1. 創(chuàng)建字體對象
    創(chuàng)建系統(tǒng)字體
    SysFont(name, size, bold=False, italic=False, constructor=None)
    name --> 字體名
    size --> 字體大小
    bold --> 加粗
    italic --> 傾斜
    font = pygame.font.SysFont('華文隸書', 50)

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

  2. 根據(jù)字體去創(chuàng)建顯示對象(文字)(找內(nèi)容)
    render(self, text, antialias, color, background=None)
    text --> 要顯示的文字內(nèi)容(str)
    antialias --> 是否平滑(布爾值)
    color --> 計(jì)算機(jī)三原色(紅、綠、藍(lán)),RGB顏色,值的范圍都是0-255,
    (255,0,0) --> 紅色 (0,255,0) --> 綠色 (0,0,255) --> 藍(lán)色
    (0,0,0) --> 黑色 (255,255,255) --> 白色 (x,x,x)-->灰色(三個(gè)值接近一樣)
    surface = font.render('hello, Python 嗨嘍,派森', True, (0, 255, 0))

  3. 將內(nèi)容添加到窗口上(畫到紙上)
    blit(需要顯示的對象, 顯示位置)
    需要顯示的對象 --> Surface類型的數(shù)據(jù)
    顯示位置 --> 坐標(biāo)(x, y)
    screen.blit(surface, (100, 100))

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

    # 游戲循環(huán)
    while True:
        for event in pygame.event.get():
            if event.type == pygame.QUIT:
                exit()

3. 顯示圖片

import pygame
if __name__ == '__main__':
    pygame.init()
    screen = pygame.display.set_mode((960, 540))
    screen.fill((255, 0, 0))
  1. 獲取圖片對象
    image = pygame.image.load('./image/2.jpg')
    a. 獲取圖片大小
    get_size()
    image_size = image.get_size()
    print(image_size)

b. 圖片縮放
transform:形變包含縮放、旋轉(zhuǎn)和平移
scale(縮放對象,新的大?。?-> 返回一個(gè)縮放后的新對象(圖片要變形)
new_image = pygame.transform.scale(image, (800, 500))
旋轉(zhuǎn):
rotate(旋轉(zhuǎn)對象,旋轉(zhuǎn)角度)---角度是0-360對應(yīng)的度數(shù)
image = pygame.transform.rotate(image, 180)
rotozoom(旋轉(zhuǎn)對象, 旋轉(zhuǎn)角度, 縮放比例)(圖片不變形)
image = pygame.transform.rotozoom(image, 180, 0.8)

  1. 將圖片對象渲染到窗口上
    screen.blit(image, (0, 0))
  2. 展示在屏幕上
    pygame.display.flip()
    while True:
        for event in pygame.event.get():
            if event.type == pygame.QUIT:
                exit()

4. 顯示圖形

import pygame
if __name__ == '__main__':
    pygame.init()
    screen = pygame.display.set_mode((800, 600))
    screen.fill((255, 0, 0))
  1. 畫直線
    line(Surface, color, start_pos, end_pos, width=1)
    Surface:畫在哪個(gè)地方
    color:線的顏色
    start_pos:起點(diǎn)
    end_pos:終點(diǎn)
    width:線的寬度
    pygame.draw.line(screen, (0, 255, 0), (60, 60), (300, 300), 5)

    lines(畫線的位置, 顏色, closed, 點(diǎn)的列表, width=1)
    pygame.draw.lines(screen, (0, 0, 255), True, [(2, 2), (15, 56), (240, 125), (400, 500)], 5)

    畫矩形
    pygame.draw.rect(screen, (255, 255, 0), (200, 300, 100, 100), 0)

  2. 畫曲線
    arc(Surface, color, Rect, start_angle, stop_angle, width=1)
    Rect --> (x, y, width, height)(矩形)(曲線在這個(gè)矩形范圍內(nèi)畫)
    start_angle:起始角度
    stop_angle:終止角度

    from math import pi
    pygame.draw.arc(screen, (0, 0, 0), (200, 200, 100, 200), pi+pi/4, 2*pi-pi/4)
  1. 畫圓
    circle(位置, color, 圓心位置, 半徑, width=0)
    width為0就填充,不為0就不填充
    import random
    pygame.draw.circle(screen,\
                       (random.randint(0, 255), random.randint(0, 255), random.randint(0, 255), random.randint(0, 255)),\
                       (100, 100), 100)

畫橢圓

    ellipse(Surface, color, Rect, width=0)
    pygame.draw.ellipse(screen, (0, 100, 0), (100, 300, 80, 200), 1)
    # 將內(nèi)容展示在屏幕上
    pygame.display.flip()

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

5. 動(dòng)畫原理

pygame產(chǎn)生動(dòng)畫的原理就是不斷地刷新圖像,比如我們用一個(gè)小圖片來移動(dòng),我們只要把每一次圖片放在不同位置然后反復(fù)刷新即可

import pygame
from time import sleep
if __name__ == '__main__':
    pygame.init()
    screen = pygame.display.set_mode((800, 600))
    screen.fill((0, 0, 255))

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

        x += 1
        y += 1
        sleep(0.03)
        screen.fill((0, 0, 255))
        pygame.draw.circle(screen, (255, 0, 0), (x, y), 80)
        pygame.display.flip()
最后編輯于
?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請聯(lián)系作者
【社區(qū)內(nèi)容提示】社區(qū)部分內(nèi)容疑似由AI輔助生成,瀏覽時(shí)請結(jié)合常識與多方信息審慎甄別。
平臺聲明:文章內(nèi)容(如有圖片或視頻亦包括在內(nèi))由作者上傳并發(fā)布,文章內(nèi)容僅代表作者本人觀點(diǎn),簡書系信息發(fā)布平臺,僅提供信息存儲(chǔ)服務(wù)。

友情鏈接更多精彩內(nèi)容