在前面的文章《python小歡喜(六)動畫 (1) pygame的安裝與初步使用》中介紹了如何安裝pygame。
《python小歡喜(七)游戲編程 (1) 擋球》實現了一個小游戲。接下來對這個小游戲做兩方面的改進
1 使用中文提示
2 添加上碰撞時的聲音效果
游戲界面如下:
可以看到,窗口上方的提示變成了中文,如果戴上耳機或使用音箱,可以聽到足球碰到擋板或下邊界時發出的聲音。
python代碼如下:
# -*- coding:utf-8 -*-
# 擋球游戲
import
pygame
#導入pygame模塊
pygame
.
init
(
)
screen
=
pygame
.
display
.
set_mode
(
[
800
,
600
]
)
#設置圖形窗口大小為800*600
pygame
.
display
.
set_caption
(
"擋球"
)
#設置圖形窗口標題
BLACK
=
(
0
,
0
,
0
)
# 用RGB值定義黑色
WHITE
=
(
255
,
255
,
255
)
# 用RGB值定義白色
BROWN
=
(
166
,
134
,
95
)
# 用RGB值定義棕色
ball
=
pygame
.
image
.
load
(
"ball.png"
)
# 加載足球圖片
picx
=
0
#圖片的x坐標的初始值設為0
picy
=
0
#圖片的y坐標的初始值設為0
picw
=
100
#圖片的寬度
pich
=
100
#圖片的高度
speedx
=
5
#足球的水平速度值
speedy
=
5
#足球的垂直速度值
paddlex
=
300
#擋板的x坐標的初始值設為300
paddley
=
550
#擋板的y坐標的初始值設為550 ,窗口高度為600,檔板靠近窗口底部
paddlew
=
200
#擋板的寬度
paddleh
=
25
#擋板的高度
points
=
0
#游戲得分
lives
=
5
#玩家生命值
font
=
pygame
.
font
.
Font
(
"C:\Windows\Fonts\STSONG.TTF"
,
24
)
#設置輸出文本所用的字體
pygame
.
mixer
.
init
(
)
# 初始化混音器
pop
=
pygame
.
mixer
.
Sound
(
"pop.wav"
)
#加載聲音文件
blip
=
pygame
.
mixer
.
Sound
(
"blip.wav"
)
#加載聲音文件
timer
=
pygame
.
time
.
Clock
(
)
#生成一個定時器對象
keepGoing
=
True
while
keepGoing
:
# 事件處理循環
for
event
in
pygame
.
event
.
get
(
)
:
if
event
.
type
==
pygame
.
QUIT
:
keepGoing
=
False
#根據速度值修正足球圖片的當前位置,如果碰到邊緣,則將速度值取反,得到回彈的效果
picx
+=
speedx
picy
+=
speedy
if
picx
<=
0
or
picx
+
ball
.
get_width
(
)
>=
800
:
speedx
=
-
speedx
if
picy
<=
0
:
speedy
=
-
speedy
if
picy
>=
500
:
lives
-=
1
#碰到底邊,生命值減1
speedy
=
-
speedy
blip
.
play
(
)
#播放聲音
screen
.
fill
(
BLACK
)
screen
.
blit
(
ball
,
(
picx
,
picy
)
)
# 根據鼠標的當前位置繪制擋板,只使用的鼠標當前位置的x坐標
paddlex
=
pygame
.
mouse
.
get_pos
(
)
[
0
]
paddlex
-=
paddlew
/
2
pygame
.
draw
.
rect
(
screen
,
BROWN
,
(
paddlex
,
paddley
,
paddlew
,
paddleh
)
)
# 檢查足球是否與檔板發生了碰撞,如果是,則得分數加1,并使足球回彈
if
picy
+
pich
>=
paddley
and
picy
+
pich
<=
paddley
+
paddleh \
and
speedy
>
0
:
if
picx
+
picw
/
2
>=
paddlex
and
picx
+
picw
/
2
<=
paddlex
+
\
paddlew
:
points
+=
1
speedy
=
-
speedy
pop
.
play
(
)
#播放聲音
# 輸出提示信息
tip
=
"生命值: "
+
str
(
lives
)
+
", 得分: "
+
str
(
points
)
text
=
font
.
render
(
tip
,
True
,
WHITE
)
text_rect
=
text
.
get_rect
(
)
text_rect
.
centerx
=
screen
.
get_rect
(
)
.
centerx
text_rect
.
y
=
10
screen
.
blit
(
text
,
text_rect
)
pygame
.
display
.
update
(
)
#刷新窗口
timer
.
tick
(
60
)
#設置幀率不超過60
pygame
.
quit
(
)
# 退出pygame
為了顯示中文,做了如下一些改動:
font
=
pygame
.
font
.
SysFont
(
"Times"
,
24
)
#設置輸出文本所用的字體
改為:
font
=
pygame
.
font
.
Font
(
"C:\Windows\Fonts\STSONG.TTF"
,
24
)
#設置輸出文本所用的字體
其中 “C:\Windows\Fonts\STSONG.TTF” 是中文宋體的字體文件,24 是字號
tip
=
"Lives: "
+
str
(
lives
)
+
" Points: "
+
str
(
points
)
改為:
tip
=
"生命值: "
+
str
(
lives
)
+
", 得分: "
+
str
(
points
)
值得注意的是,如果前面不設置中文字體,直接修改 tip = "生命值: " + str(lives) + ", 得分: " + str(points) ,會導致中文輸出的內容為亂碼
為了添加聲音效果,做了如下一些改動
添加下面三行,加載聲音文件
pygame
.
mixer
.
init
(
)
# 初始化混音器
pop
=
pygame
.
mixer
.
Sound
(
"pop.wav"
)
#加載聲音文件
blip
=
pygame
.
mixer
.
Sound
(
"blip.wav"
)
#加載聲音文件
然后在發生碰撞時執行
blip
.
play
(
)
#播放聲音
或者:
pop
.
play
(
)
#播放聲音
可見有了pygame.mixer之后,播放聲音變得相當的簡單。
補充說明:
程序中用到的兩個聲音文件可在此處下載
提取碼:34yu
更多文章、技術交流、商務合作、聯系博主
微信掃碼或搜索:z360901061

微信掃一掃加我為好友
QQ號聯系: 360901061
您的支持是博主寫作最大的動力,如果您喜歡我的文章,感覺我的文章對您有幫助,請用微信掃描下面二維碼支持博主2元、5元、10元、20元等您想捐的金額吧,狠狠點擊下面給點支持吧,站長非常感激您!手機微信長按不能支付解決辦法:請將微信支付二維碼保存到相冊,切換到微信,然后點擊微信右上角掃一掃功能,選擇支付二維碼完成支付。
【本文對您有幫助就好】元
