《Python游戲編程入門》
這些文章負責整理在這本書中的知識點、注意事項和課后習題的嘗試實現。
並且對每一個章節給出的最終實例進行分析和注釋。
初識pygame:pie游戲
pygame游戲庫使得如下功能成為可能:繪制圖形、獲取用戶輸入、執行動畫
以及使用定時器讓游戲按照穩定的幀速率運行。
使用pygame庫;
以一定字體打印文本;
使用循環來重復動作;
繪制圓、矩形、線條和戶型;
創建pie游戲;
從哪里獲得pygame庫:http://www.pygame.org/download.shtml
我現在使用的Python2.7和pygame1.9
書中使用的環境是Python3.2和pygame1.9
現在不在Python3的環境下安裝上pip工具導致環境無法一致
pygame庫的初始化工作:
import pygame
from pygame.locals import *
pygame.init()
創建一個(600,500)大小的屏幕
screen=pygame.display.set_mode((600,500))
screen同時被賦值為<Surface(600x500x32 SW)>
這是一個有用的值,所以用screen變量存儲。
打印文本
1、創建字體對象
myfont=pygame.font.Font(None,60)
None:使用默認字體
60:字體大小
2、創建一個可以使用screen.blit()繪制的平面
textimage=myfont.render("Hello Python",True,(255,255,255))
render需要三個參數,需要被顯示的字符串、是否抗鋸齒True/False、顏色
3、將textimage交給screen.blit()進行繪制
screen.blit(textimage,(100,100))
screen.blit()需要兩個參數,繪制的對象及其(左上角頂點)坐標
背景填充
screen.fill((0,0,0))
screen.fill()需要給出背景顏色
刷新顯示
screen.display.update()
一般配合while循環使用
while循環
通過while循環可以進行事件處理和持續的屏幕刷新
while True:
for event in pygame.event.get():
if event.type in (QUIT,KEYDOWN):
sys.exit()
screen.display.update()
繪制圓形
pygame.draw.circle(screen,color,position,radius,width)
color (0,0,0)給定顏色
radius圓半徑
position (0,0)給定圓心坐標
width線條寬度
繪制矩形
pygame.draw.rect(screen,color,position,width)
position (pos_x,pos_y,100,100)給定左上角頂點的坐標、長和寬
繪制線條
pygame.draw.line(screen,color,(0,0),(100,100),width)
(0,0)(100,100)負責給定線段的兩個端點
繪制弧形
start_angle=math.radians(0)
end_angle=math.radians(90)
position=x-radius,y-radius,radius*2,radius*2
#x,y表示弧形所在的圓的圓心坐標,radius表示半徑
pygame.draw.arc(screen,color,position,start_angle,end_angle,width)
start_angle起始角度 指向正右側的半徑開始逆時針旋轉就是0到360
end_angle結束角度
兩段值得學習的示例
1、繪制移動矩形
#!/usr/bin/python
import sys
import random
from random import randint
import pygame
from pygame import *
pygame.init()
screen=pygame.display.set_mode((600,500))
pygame.display.set_caption("Drawing Rectangles")
pos_x=300
pos_y=250
vel_x=2
vel_y=1
color=100,100,100
while True:
for event in pygame.event.get():
if event.type in (QUIT,KEYDOWN):
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
rand1=randint(0,255)
rand2=randint(0,255)
rand3=randint(0,255)
color=rand1,rand2,rand3
if pos_y>400 or pos_y<0:
vel_y=-vel_y
rand1=randint(0,255)
rand2=randint(0,255)
rand3=randint(0,255)
color=rand1,rand2,rand3
width=0
pos=pos_x,pos_y,100,100
pygame.draw.rect(screen,color,pos,width)
pygame.display.update()
2、pie游戲
#!/usr/bin/python
#init
import sys
import math
import pygame
from pygame.locals import *
pygame.init()
screen=pygame.display.set_mode((600,500))
pygame.display.set_caption("The Pie Game-Press 1,2,3,4")
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
piece1=False
piece2=False
piece3=False
piece4=False
while True:
for event in pygame.event.get():
if event.type==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))
#draw the four numbers
textimage1=myfont.render("1",True,color)
screen.blit(textimage1,(x+radius/2-20,y-radius/2))
textimage2=myfont.render("2",True,color)
screen.blit(textimage2,(x-radius/2,y-radius/2))
textimage3=myfont.render("3",True,color)
screen.blit(textimage3,(x-radius/2,y+radius/2-20))
textimage4=myfont.render("4",True,color)
screen.blit(textimage4,(x+radius/2-20,y+radius/2-20))
#draw arc,line
if piece1:
start_angle=math.radians(0)
end_angle=math.radians(90)
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 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+radius,y),width)
#if success,display green
if piece1 and piece2 and piece3 and piece4:
color=0,255,0
pygame.display.update()
挑戰
1、繪制橢圓
#!/usr/bin/python
import sys
import pygame
from pygame.locals import *
pygame.init()
screen=pygame.display.set_mode((600,500))
pygame.display.set_caption("Drawing Ellipse")
while True:
for event in pygame.event.get():
if event.type in (QUIT,KEYDOWN):
sys.exit()
screen.fill((0,255,0))
color=255,0,255
position=100,100,400,300
width=8
pygame.draw.ellipse(screen,color,position,width)
pygame.display.update()
這個題目就是讓你認識一下pygame.draw.ellipse()函數的相關使用。
該函數和pygame.draw.rect()函數使用方式十分相似。
2、隨機的繪制1000個線條
#!/usr/bin/python
import random
from random import randint
import sys
import pygame
from pygame import *
pygame.init()
screen=pygame.display.set_mode((800,600))
pygame.display.set_caption("Drawing Line")
screen.fill((0,80,0))
color=100,255,200
width=2
for i in range(1,1001):
pygame.draw.line(screen,color,(randint(0,800),randint(0,600)),(randint(0,800),randint(0,600)),width)
while True:
for event in pygame.event.get():
if event.type in (QUIT,KEYDOWN):
sys.exit()
pygame.display.update()
通過這個題目理解了如果繪制圖形和刷新顯示都在循環中時,while True循環每次都會繪
制圖形並刷新顯示。
調用pygame模塊中的randint()函數。
而在while True循環外繪制圖形,則圖形繪制完成之后保持不變。刷新顯示的是一個已經繪制好
的圖形。
3、修改矩形程序,使矩形碰到屏幕邊界是,矩形會改變顏色
#!/usr/bin/python
import sys
import random
from random import randint
import pygame
from pygame import *
pygame.init()
screen=pygame.display.set_mode((600,500))
pygame.display.set_caption("Drawing Rectangles")
pos_x=300
pos_y=250
vel_x=2
vel_y=1
rand1=randint(0,255)
rand2=randint(0,255)
rand3=randint(0,255)
color=rand1,rand2,rand3
while True:
for event in pygame.event.get():
if event.type in (QUIT,KEYDOWN):
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
rand1=randint(0,255)
rand2=randint(0,255)
rand3=randint(0,255)
color=rand1,rand2,rand3
if pos_y>400 or pos_y<0:
vel_y=-vel_y
rand1=randint(0,255)
rand2=randint(0,255)
rand3=randint(0,255)
color=rand1,rand2,rand3
width=0
pos=pos_x,pos_y,100,100
pygame.draw.rect(screen,color,pos,width)
pygame.display.update()
這里需要用到random模塊。在每次碰到屏幕邊界時,不僅改變矩形的運動方向,而且使用隨機數改變矩形的顏色。
也可以先將color設置為定值,可以少寫三行代碼。
底層(嵌套的層數較多)代碼塊初次使用的變量在頂層代碼塊中依然生效。
以上問題屬於變量的作用域問題。說明我在這一方面認識不夠清晰。