1. 程式人生 > >【Python遊戲編程--初步認識pygame】

【Python遊戲編程--初步認識pygame】

get() 傳遞 posit 進程 local spl 鋸齒 基礎 邊緣

一、pygame簡介

Pygame 是一組用來開發遊戲軟件的 Python 程序模塊,基於 SDL 庫的基礎上開發。允許你在 Python 程序中創建功能豐富的遊戲和多媒體程序,Pygame 是一個高可移植性的模塊可以支持多個操作系統。用它來開發小遊戲非常適合

二、pygame的使用

第一步是把pygame導入到Python程序中,

import pygame

然後需要引入pygame中所有常量

from pygame.locals import *

在經過初始化以後,我們就可以正常的使用pygame了

pygame.ini()

通常來說我們需要創建一個窗口,方便我們與程序就行交互,下面創建一個600 x 500的窗口

screen=pygame.display.set_mode((600,500))

1、打印字體

pygame支持使用pygame.font將文打印到窗口,要打印一個文本的話首先需要創建一個文本對象

myfont = pygame.font.Font(None,60)

這個文本是個重量級的進程,比較耗費時間,常用的做法是先在內存中創建文本對象,然後將文本當做一個圖像來渲染

white= 255,255,255
blue = 0,0,200
textImage = myfont.render("hello Pygame",True,white)

textImage對象可以使用screen.blit()來繪制,上面的render函數第一個參數是文本,第二個參數是抗鋸齒字體,第三個參數是一個顏色值(RGB值)

要繪制文本,通常的過程是清屏,繪制,刷新

screen.fill(blue)
screen.blit(textImage,(100,100))
pygame.display.update()

如果此時運行程序的話,會出現一個窗口一閃而過,為了讓他長時間顯示,需要把他放入一個循環中

import pygame
from pygame.locals import *
import sys
white = 255,255,255
blue = 0,0,200

pygame.init()
screen = pygame.display.set_mode((600,400))

myfont 
= pygame.font.Font(None,60) textImage = myfont.render("Hello World",True,white) while True: for event in pygame.event.get(): if event.type == pygame.QUIT: sys.exit() screen.fill(blue) screen.blit(textImage,(100,100)) pygame.display.update()

執行結果:

技術分享圖片

pygame除了能打印字體,仍然可以繪制各種圖像

2、繪制一個圓形

使用pygame.draw.cicle()方法,該方法需要傳遞圓的大小,顏色和參數

import pygame,sys

pygame.init()
screen = pygame.display.set_mode((600,400))

#設置圓形的位置和移動的速度變量
pos_x = 300
pos_y = 250
vel_x = 2
vel_y = 1

while True:
    for event in pygame.event.get():
        if event.type == pygame.QUIT:
            sys.exit()
    screen.fill((0, 0, 200))

    #移動圓形
    pos_x += vel_x
    pos_y += vel_y

    #讓圓形保持在窗口內
    if pos_x > 500 or pos_x < 0:
        vel_x = -vel_x
    if pos_y > 400 or pos_y <0:
        vel_y = -vel_y

    color = 255, 255, 0
    position = 300, 250
    radius = 80  # 半徑
    width = 0
    pos = pos_x,pos_y
    #print(pos)
    pygame.draw.circle(screen,color,pos,radius,width)

    pygame.display.update()

執行結果

技術分享圖片 技術分享圖片

3、繪制一個矩形

繪制一個可以移動的矩形,而非簡單的顯示在屏幕中間

首先需要設置pos_x,pos_y兩個變量來記錄矩形的位置信息,然後在創建一堆速度變量(vel_x,vel_y),在while循環中不斷變化矩形的位置,當矩形移動到屏幕邊緣的時候,將速度變量取返,這樣就可以產生碰撞的效果

from pygame.locals import *
import pygame
import sys

pygame.init()
screen = pygame.display.set_mode((600,400))
title = pygame.display.set_caption("Drawing Rectangles")

#記錄矩形的位置信息,記錄移動速度的位置信息
pos_x = 300
pos_y = 250
vel_x = 2
vel_y = 1

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

    screen.fill((0,0,200))

    #移動矩形
    pos_x += vel_x
    pos_y += vel_y

    #使矩形保持在窗口內
    if pos_x > 500 or pos_x <0:
        vel_x = -vel_x
    if pos_y >400 or pos_y < 0:
        vel_y = -vel_y

    #繪制矩形
    color = 255,255,0
    width = 0
    pos = pos_x,pos_y,100,100
    pygame.draw.rect(screen,color,pos,width)


    pygame.display.update()

執行結果:

技術分享圖片技術分享圖片

4、繪制線條

使用pygame.draw.line()方法,該方法需要傳遞起點和終點,還有線條的顏色和寬度

import pygame
import sys

pygame.init()
screen = pygame.display.set_mode((600,400))

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

    screen.fill((0,0,255))

    color = 255,255,0
    width = 5
    pygame.draw.line(screen,color,(100,100),(500,300),width)

    pygame.display.update()

執行結果:

技術分享圖片

5、繪制弧形

弧形是圓形的一部分,可以使用pygame.draw.arc方法繪制它,由於這個形狀相對比較復雜,需要用到的知識比前面的知識要多

首先,需要一個矩形來表示弧形的邊界(需提供矩形左上角的位置,寬度和高度)弧形就繪制在這個矩形當中

然後需要提供弧形的起始角度和結束角度,平常我們在生活中都是用度為單位衡量一個角度,但在幾何三角形中,通常使用的單位是弧度

將角度轉換成弧度的函數是math.redians()它包含在math庫中,因此使用之前一定要引入math庫

import math
import pygame
from pygame.locals import *
import sys

pygame.init()
screen = pygame.display.set_mode((600,500))
title = pygame.display.set_caption("Drawing Arcs")

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

    screen.fill((0,0,200))

    #繪制弧形
    color = 255,0,255
    position = 200,150,200,200
    start_angle = math.radians(0)
    stop_angle = math.radians(180)
    print(start_angle,stop_angle)
    width =8
    pygame.draw.arc(screen,color,position,start_angle,stop_angle,width)

    pygame.display.update()

執行結果:

技術分享圖片

【Python遊戲編程--初步認識pygame】