Python游戲編程-初步認識pygame


Python游戲編程-初步認識pygame

一、總結

一句話總結:

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

 

 

二、【Python游戲編程01--初步認識pygame】

轉自或參考:【Python游戲編程01--初步認識pygame】 - 塵封~~ - 博客園
https://www.cnblogs.com/frankruby/p/10597192.html

一、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將文打印到窗口,要打印一個文本的話首先需要創建一個文本對象,即:創建字體,None:默認字體,60:字體大小

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

這個文本是個重量級的進程,比較耗費時間,常用的做法是先在內存中創建文本對象,然后將文本當做一個圖像來渲染,即:創建一個可以使用screen.blit()繪制的平面

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

textImage對象可以使用screen.blit()來繪制,上面的render函數第一個參數是文本,第二個參數是抗鋸齒字體,第三個參數是一個顏色值(RGB值)
要繪制文本,通常的過程是清屏,繪制,刷新,即:進行繪制

screen.blit()需要兩個參數,繪制的對象及其(左上角頂點)坐標

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()方法,該方法需要傳遞圓的大小,顏色和參數

  color    (0,0,0)給定顏色
    radius圓半徑
    position     (0,0)給定圓心坐標
    width線條寬度

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循環中不斷變化矩形的位置,當矩形移動到屏幕邊緣的時候,將速度變量取返,這樣就可以產生碰撞的效果

 position      (pos_x,pos_y,100,100)給定左上角頂點的坐標、長和寬

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()方法,該方法需要傳遞起點和終點,還有線條的顏色和寬度

(0,0)(100,100)負責給定線段的兩個端點

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庫

 

#x,y表示弧形所在的圓的圓心坐標,radius表示半徑

start_angle起始角度 指向正右側的半徑開始逆時針旋轉就是0到360
end_angle結束角度

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

執行結果:

 二、實踐:繪制大餅游戲(當按下1,2,3,4相應的按鈕時,就會在程序中繪制相應的餅塊,當整個餅塊繪制完成以后,顏色變成亮綠色)

 

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

pygame.init()
screen = pygame.display.set_mode((600,600))
title = pygame.display.set_caption("The Pie Game")

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

color = 200,80,60
width = 4
x = 300
y = 250
radius = 200
position = x-radius,y-radius,radius*2,radius*2


#設置按鍵1,2,3,4變量
piece1 = False
piece2 = False
piece3 = False
piece4 = False


while True:
    for event in pygame.event.get():
        if event.type == pygame.QUIT:
            sys.exit()
        elif event.type == KEYUP:
            if event.key == pygame.K_ESCAPE:
                sys.exit()
            elif event.key == pygame.K_1:
                piece1 = True
            elif event.key == pygame.K_2:
                piece2 = True
            elif event.key == pygame.K_3:
                piece3 = True
            elif event.key == pygame.K_4:
                piece4 = True
    #清屏
    screen.fill((0,0,200))

    #繪制4個數字
    textImage1 = myfont.render("1",True,color)
    text1=screen.blit(textImage1,(x+radius/2-20,y-radius/2))
    #print("text1==",text1)
    textImage2 = myfont.render("2",True,color)
    text2=screen.blit(textImage2,(x-radius/2,y-radius/2))
    #print("text2==", text2)
    textImage3 = myfont.render("3",True,color)
    text3=screen.blit(textImage3,(x-radius/2,y+radius/2-20))
    #print("text3==", text3)
    textImage4 = myfont.render("4",True,color)
    text4=screen.blit(textImage4,(x+radius/2-20,y+radius/2-20))
    #print("text4==", text4)

    #判斷是否繪制餅
    if piece1:
        start_angle = math.radians(0)
        end_angle = math.radians(90)
        arc1=pygame.draw.arc(screen,color,position,start_angle,end_angle,width)
        print("arc1==",arc1)
        line1=pygame.draw.line(screen,color,(x,y),(x,y-radius),width)
        print("line1==",line1)
        line2=pygame.draw.line(screen,color,(x,y),(x+radius,y),width)
        print("line2==",line2)
    if piece2:
        start_angle = math.radians(90)
        end_angle = math.radians(180)
        pygame.draw.arc(screen,color,position,start_angle,end_angle,width)
        pygame.draw.line(screen,color,(x,y),(x,y-radius),width)
        pygame.draw.line(screen,color,(x,y),(x-radius,y),width)
    if piece3:
        start_angle = math.radians(180)
        end_angle = math.radians(270)
        pygame.draw.arc(screen,color,position,start_angle,end_angle,width)
        pygame.draw.line(screen,color,(x,y),(x-radius,y),width)
        pygame.draw.line(screen,color,(x,y),(x,y+radius),width)
    if piece4:
        start_angle = math.radians(270)
        end_angle = math.radians(360)
        pygame.draw.arc(screen,color,position,start_angle,end_angle,width)
        pygame.draw.line(screen,color,(x,y),(x,y+radius),width)
        pygame.draw.line(screen,color,(x,y),(x,y+radius),width)

    #是否4個餅都繪制完成
    if piece1 and piece2 and piece3 and piece4:
        color = 0,255,0

    pygame.display.update()

執行結果

三、繪制橢圓

采用pygame.draw.ellipse()函數

 

 


免責聲明!

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



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