![00195b03ea8f2e0aad83c2d5c4221b9a.png](https://img-blog.csdnimg.cn/img_convert/00195b03ea8f2e0aad83c2d5c4221b9a.png)
我们在玩游戏的时候,通常都会有游戏得分,这样会使得游戏更加的有趣。
这次我们需要增加得分、生命值。每次笑脸碰到挡板的时候,用户就会得到1分,当用户漏掉了笑脸并且碰到了屏幕的底部,用户就会丢掉一条命。
游戏开始的时候,设置初始得分为0,给玩家5次的生命机会:
points = 0 # 得分
lives = 5 # 生命值
得分:
当笑脸下落触碰到挡板的时候,得分加1分:
points += 1
在笑脸和挡板碰撞检测处添加得分代码:
# 笑脸和挡板碰撞检测,得分加1
if pic_y + pic.get_height() >= paddle_y and pic_y + pic.get_height() <=
paddle_y + paddle_height and speedy > 0:
if pic_x + pic.get_width()/2 >= paddle_x and pic_x + pic.get_width()/2
<= paddle_x + paddle_width:
points += 1 # 得分加1
speedy = -speedy
减少生命值:
如果挡板没有触碰到笑脸,笑脸掉落触碰到了屏幕的底部,这就用户就会损失一条命:
lives -= 1
笑脸触碰到屏幕的底部:
if pic_y >= 600 - pic.get_height():
lives -= 1 # 减少一条命
speedy = -speedy
显示生命值和得分
这里我们需要将用户的生命值和游戏得分显示到我们的游戏屏幕上,这样玩家在玩游戏的时候,就可以很轻松的看到自己的得分以及生命值。
一、将要显示的文本字符串组合
draw_text ="生命值:" + str(lives) + " 得分: " + str(points)
str()函数将数字转化为字符串,例如5——‘5’。然后使用符号“+”连接字符串,赋值给字符串变量draw_text。
二、文本绘制——pygame.font模块
在Pygame中将文本显示到屏幕上,通常都是将文本作为一个Surface对象绘制到屏幕上。
1、设置字体和大小
SysFont(name, size, bold=False, italic=False) -> Font
从系统字体库中加载并返回一个新的字体对象。该字体将会匹配 bold(加粗)和 italic(斜体)参数的要求。
font = pygame.font.SysFont("simhei",24)
从系统字库中创建一个Font对象,字体"simhei"(黑体),大小24。
我们可以从电脑中查看系统自带的字体:
![1f0ca1136b826d5338150e9cf3194ef5.png](https://img-blog.csdnimg.cn/img_convert/1f0ca1136b826d5338150e9cf3194ef5.png)
2、在Surface对象上绘制文本(屏幕)
render(text, antialias, color, background=None) -> Surface
- 参数text:文本字符串;
- 参数antialias:为True时文本图像显示更光滑,为False时文本图像显示有锯齿状
- 参数color:文本的颜色
- 参数background:为文本背景颜色,默认为小黑屏
返回值:返回一个surface对象(字体的渲染成的图像),即Pygame处理文本的时候,是将文本作为一个Surface对象进行处理,文本字符串将会在一个矩形的界面上绘制。
text = font.render(draw_text,True,WHITE)
变量text存储渲染后的文本界面,是一个新的Surface对象(矩形图像),下一步我们就可以获得到这个界面的宽度和高度,文本内容越多,绘制的就会越宽。
text_rect = text.get_rect()
get_rect()方法获取矩形图像的所有属性,将获得的属性存储到变量text_rect中。绘制出来的文本矩形的初始位置:
print(text_rect)
<rect(0, 0, 204, 25)>
我们想要将文本显示到屏幕的中央,那就需要设置文本矩形的centerx属性在屏幕的水平居中。
text_rect.centerx = screen.get_rect().centerx # 水平居中
将屏幕中心的水平位置赋值给文本矩形的水平位置,这样文本矩形就在屏幕上水平居中。
修改文本矩形的竖直方向的距离,距离屏幕顶端以下10px的位置:
text_rect.y = 10
此时文本矩形的位置就发生了改变,不再是(0,0):
<rect(298, 10, 204, 25)>
3、绘制到屏幕上
和处理图片一样,使用blit()方法,因为我们渲染之后,文本字符串已经是一个Surface对象了。
screen.blit(text,text_rect)
代码:
# 显示文本字符串
draw_text ="生命值:" + str(lives) + " 得分: " + str(points)
text = font.render(draw_text,True,WHITE)
text_rect = text.get_rect()
text_rect.centerx = screen.get_rect().centerx # 水平居中
text_rect.y = 10
screen.blit(text,text_rect)
完整代码:
import pygame # 导入模块
from pygame.locals import *
pygame.init() # 初始化模块
screen = pygame.display.set_mode((800,600)) # 创建一个Surface对象窗口
pygame.display.set_caption("Pong游戏") # 设置标题
keep_going = True # 循环标志
BLACK = (0,0,0)
clock = pygame.time.Clock()
# 挡板
WHITE = (255,255,255)
paddle_width = 200 # 挡板宽度
paddle_height = 25 # 挡板高度
paddle_x = 300 # 挡板left属性
paddle_y = 550 # 挡板top属性
# 笑脸图片
pic = pygame.image.load("Smile.bmp").convert_alpha()
colorkey = pic.get_at((0,0))
pic.set_colorkey(colorkey)
# 速度和位置
speedx = 5
speedy = 5
pic_x = 0
pic_y = 0
# 得分和生命值
points = 0
lives = 5
# 从系统字库创建一个Font对象
font = pygame.font.SysFont("simhei",24)
while keep_going:
for event in pygame.event.get():
if event.type == QUIT:
keep_going = False
pic_x += speedx
pic_y += speedy
# 墙壁碰撞检测
if pic_x <= 0 or pic_x + pic.get_width() >= 800:
speedx = - speedx
if pic_y <= 0:
speedy = -speedy
if pic_y >= 600 - pic.get_height():
lives -= 1 # 减少一条命
speedy = -speedy
screen.fill(BLACK) # 填充背景
# 绘制挡板
paddle_x = pygame.mouse.get_pos()[0]
paddle_x -= paddle_width/2
pygame.draw.rect(screen,WHITE,(paddle_x,paddle_y,paddle_width,paddle_height))
# 绘制笑脸图片
screen.blit(pic,(pic_x,pic_y))
# 笑脸和挡板碰撞检测,得分加1
if pic_y + pic.get_height() >= paddle_y and pic_y + pic.get_height() <=
paddle_y + paddle_height and speedy > 0:
if pic_x + pic.get_width()/2 >= paddle_x and pic_x + pic.get_width()/2
<= paddle_x + paddle_width:
points += 1
speedy = -speedy
# 显示文本字符串
draw_text ="生命值:" + str(lives) + " 得分: " + str(points)
text = font.render(draw_text,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.flip()
clock.tick(60) # 设置帧速率
pygame.quit()
![c0089c5ca3880467729c72e70b9365ef.gif](https://img-blog.csdnimg.cn/img_convert/c0089c5ca3880467729c72e70b9365ef.gif)
好了,可以自己动起手来尝试一下!
下一次我们准备增加游戏结束,即当生命值消耗完了,游戏结束。
更多内容可以关注微信公众号【陪孩子一起学编程】
http://weixin.qq.com/r/hymfhyjEF3VorWzk93yV (二维码自动识别)
Pygame游戏制作——Pong游戏
陪孩子一起学编程:Pygame游戏——Pong游戏(一)zhuanlan.zhihu.com![9b2558ee56b2c999d25eb8b6ad86671b.png](https://img-blog.csdnimg.cn/img_convert/9b2558ee56b2c999d25eb8b6ad86671b.png)
![8212450e61c8da0ea5ed47cb86344349.png](https://img-blog.csdnimg.cn/img_convert/8212450e61c8da0ea5ed47cb86344349.png)
![30d27e54c0a5103844adfb06711a4aa9.png](https://img-blog.csdnimg.cn/img_convert/30d27e54c0a5103844adfb06711a4aa9.png)