pygame模塊的簡介


pygame模塊概覽

 

 

 

pygame的詳細使用

  • 初始化pygame
pygame.init()
  • 創建窗口
# set_mode((width, height)),單位是像素
screen = pygame.display.set_mode((1200, 800))
  • 設置填充顏色

顏色值(是由計算機的三原色組成–>紅、綠、藍),簡稱RGB。我們能使用的所有的顏色都可以通過RGB值來表示。
RGB值的表示方式:
a. (red值, green值, blue值)值都是數字:0-255。(255, 0, 0)–>紅色;(255, 255, 255)–>白色
b.十六進制的RGB值。#ff0000 --> 紅色;#000000 -->黑色;#00ff00 --> 綠色


# 設置填充顏色為灰色
screen.fill((230, 230, 230))

進行鼠標與窗口的互動

  • 打開窗口的固定格式
import pygame

def main():
    # 初始化
    pygame.int=it()
    #創建窗口
    screen = pygame.display.set_mode((600, 400))
    
    pygame.display.flip()
  • 與鍵盤鼠標相關的操作
## 所有與鼠標鍵盤相關的操作一定要放在循環里。


while True:
    # 獲取事件
    for event in pygame.event.get():
        # 1.點關閉按鈕對應的事件
        if event.type == pygame.QUIT:
            exit()

        # 2.鼠標按下對應的事件
        if event.type == pygame.MOUSEBUTTONDOWN:
            # 獲取鼠標按下的坐標
            print(event.pos)
            print('鼠標按下')

        if event.type == pygame.MOUSEBUTTONUP:
            # 獲取鼠標按下后彈起的坐標
            print(event.pos)
            print('鼠標按下彈起')

        if event.type == pygame.MOUSEMOTION:
            # 鼠標移動過程中對應的點的坐標
            # print(event.pos)
            # print('鼠標移動')
            pass

        # 3.鍵盤相關的事件
        if event.type == pygame.KEYDOWN:
            print(chr(event.key))
            print('鍵盤按鈕按下'if chr(event.key) == 'ē':
                print('')

        if event.type == pygame.KEYUP:
            print(chr(event.key))
            print('鍵盤按鈕按下彈起來')

#這句話與模塊首的main函數相對應,是用來測試這個模塊是否被調用的。專業程序員一般都會寫這個函數,用於對模塊進行規范。
if __name__ == '__main__':
    main()

顯示圖片

  • 顯示圖片的固定格式
import pygame
# 初始化
pygame.init()

# 創建窗口
screen = pygame.display.set_mode((600, 400))
print(type(screen))
screen.fill((255, 255, 255))
  • 創建一個圖片對象(surface)
image = pygame.image.load('images/ship.bmp')
  • 獲取圖片大小
格式:(width, height) = 圖片對象.get_size()


size = image.get_size()
print(size)
  • 對圖片進行縮放
將指定的圖片,變成指定大小,返回一個新的圖片對象。
格式:pygame.transform.scale(圖片對象, (wigth, height))


image1 = pygame.transform.scale(image, (600, 400))
  • 對圖片進行旋轉
格式:pygame.transform.rotozoom(圖片對象, 旋轉角度, 縮放倍數)


image3 = pygame.transform.rotozoom(image, 0, 2)
  • 將圖片對象添加到窗口對象中
格式:screen.blit(圖片對象, 坐標)
坐標可以是圖片對象的外接矩形


creen.blit(image3, (0, 0))
  • 顯示在屏幕上
# 不再是 pygame.display.flip()
pygame.display.update()

顯示文字

  • 創建文字對象
pygame.font.SysFont(系統字體名, 字體大小)
pygame.font.Font(字體文件路徑, 字體大小)



font1 = pygame.font.SysFont('Times', 20)
font2 = pygame.font.Font('files/aa.ttf', 40)
  • 根據文體創建文字對象
格式:font.render(文字, 是否平滑, 文字顏色)


title = '根據字體創建文字對象'
text = font.render(title, True, (255, 0, 0))
  • 將文字對象添加到窗口上
screen.blit(text, (100, 0))

顯示圖形

  • 畫線
第一種:pygame.draw.line(外觀對象, 線顏色, 起點, 終點, 線寬)
第二種:pygame.draw.lines(外觀對象, 線顏色, 是否閉合, 點的列表, 線寬)
第三個參數:False --> 不閉合;True --> 閉合


pygame.draw.lines(screen, (255, 0, 0), True, [(300, 50), (150, 180), (200, 120), (330, 200)], 3)
  • 畫矩形
格式:pygame.draw.rect(外觀對象, color, Rect, width)
Rect:位置及大小 --> (x, y, width, height)
width:為0就會填充矩形

pygame.draw.rect(screen, (0, 255, 0), (10, 10, 200, 150), 0)
  • 畫弧線
格式:pygame.draw.arc(外觀對象, 顏色, 范圍, 起始角度, 終止角度, 線寬


from math import pi
pygame.draw.arc(screen, (255, 255, 0), (10, 200, 200, 200), pi/2, pi, 3)

顯示動態效果

import pygame

    def main():
        # 游戲初始化
        pygame.init()
        screen = pygame.display.set_mode((600, 400))
        screen.fill((255, 255, 255))
        pygame.display.flip()


        x = 100
        y = 100

        names = ['張三', '李四', '王五', '駱昊', '王海飛', '肖世榮']
        index = 0

        # 游戲循環
        while True:
            # 事件獲取
            for event in pygame.event.get():
                if event.type == pygame.QUIT:
                    exit()


            pygame.time.delay(100)
            # screen.fill((255, 255, 255))
            pygame.draw.rect(screen, (255, 255, 0),(200, 200, 100, 50))
            # 獲取文字
            font = pygame.font.Font('./files/aa.ttf', 30)
            text = font.render(names[index], True, (0, 0, 0))
            screen.blit(text, (200, 200))
            pygame.display.flip()

            # 獲取下一個文字下標
            index += 1
            if index >= len(names):
                index = 0

    if __name__ == '__main__':
        main()
# 延遲操作

# delay(時間) ,時間單位是毫秒
pygame.time.delay(100) # 覆蓋之前畫的內容
screen.fill((255, 255, 255)) # 畫圓
pygame.draw.circle(screen, (0, 255, 0), (x, y), 50) x += 5 y += 5 pygame.display.flip()

制作一個按鈕

import pygame

    def creat_button(screen, bg_color, text_color):
        # 清空前面畫的
        pygame.draw.rect(screen, (255, 255, 255), (100, 100, 150, 70))
        # 畫矩形
        pygame.draw.rect(screen, bg_color,(100, 100, 150, 70))

        # 畫文字
        font = pygame.font.Font('./files/aa.ttf', 30)
        text = font.render('開 始', True, text_color)
        screen.blit(text, (140, 120))

        pygame.display.flip()

    def main():
        # 游戲界面初始化
        pygame.init()
        screen = pygame.display.set_mode((600, 400))
        screen.fill((255, 255, 255))

        # 顯示一個按鈕
        creat_button(screen, (0, 255, 0), (100, 100, 100))

        pygame.display.flip()

        while True:
            # 獲取事件
            for event in pygame.event.get():
                # 1.點關閉按鈕對應的事件
                if event.type == pygame.QUIT:
                    exit()

                # 2.鼠標按下對應的事件(確定反應范圍)
                if event.type == pygame.MOUSEBUTTONDOWN:
                    x, y = event.pos
                    if 100<x<100+150 and 100<y<100+70:
                        creat_button(screen, (0, 150, 0), (255, 0, 0))

                if event.type == pygame.MOUSEBUTTONUP:
                    creat_button(screen, (0, 255, 0), (100, 100, 100))

                if event.type == pygame.MOUSEMOTION:
                    # 鼠標移動過程中對應的點的坐標
                    # print(event.pos)
                    # print('鼠標移動')
                    pass


    if __name__ == '__main__':
        main()

 

  


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM