pygame壁球游戏之节奏型&操控型

此博客内容源于mooc北京理工大学python游戏开发入门课程及本人思考。

在不进行速度限制的情况下,程序中的while循环会以CPU的最大能力进行循环。所以要使while循环速度减慢,就需要在代码中有所体现。

话不多说,先上代码(节奏型)。

#壁球小游戏(节奏型)
import pygame,sys
pygame.init()
size = width,height = 600,400
speed = [1,1]
BLACK = 0,0,0
screen = pygame.display.set_mode(size)
pygame.display.set_caption('Pygame壁球')
ball = pygame.image.load('ball.jpg')
ballrect = ball.get_rect()
fps = 300
fclock = pygame.time.Clock()
while True:
    for event in pygame.event.get():
        if event.type == pygame.QUIT:
            sys.exit()
    ballrect = ballrect.move(speed[0],speed[1])
    if ballrect.left<0 or ballrect.right>width:
        speed[0] = -speed[0]
    if ballrect.top<0 or ballrect.bottom>height:
        speed[1] = -speed[1]
    screen.fill(BLACK)
    screen.blit(ball,ballrect)
    pygame.display.update()
    fclock.tick(fps)

与展示型的壁球游戏代码(https://blog.csdn.net/LuniqueX/article/details/122203012)相比,节奏型的壁球游戏代码增加了三行,分别为:

fps = 300             #Frames per Second每秒帧率参数
fclock = pygame.time.Clock()

其中,pygame.time.Clock()指令,创建一个Clock对象,用于操作时间。

fclock.tick(fps)

clock.tick(framerate)指令,控制帧速度,即窗口刷新速度。例如:clock.tick(300)表示每秒钟300次帧刷新。视频中每次展示的静态图像称为帧。


要使用户能够参与游戏其中,就需要增加surface, 这里我们引入键盘的方向控制键来操控小球的运动速度。而增加对象,就以为着第三部分(获取时间并逐类响应)需要增加代码。

上代码(操控型)

#壁球小游戏(节奏型)
import pygame,sys
pygame.init()
size = width,height = 600,400
speed = [1,1]
BLACK = 0,0,0
screen = pygame.display.set_mode(size)
pygame.display.set_caption('Pygame壁球')
ball = pygame.image.load('ball.jpg')
ballrect = ball.get_rect()
fps = 300             #Frames per Second每秒帧率参数
fclock = pygame.time.Clock()
while True:
    for event in pygame.event.get():
        if event.type == pygame.QUIT:
            sys.exit()
        elif event.type == pygame.KEYDOWN:
            if event.key == pygame.K_LEFT:
                speed[0] = speed[0] if speed[0] == 0 else (abs(speed[0])-1)*int(speed[0]/abs(speed[0]))
            elif event.key == pygame.K_RIGHT:
                speed[0] = speed[0] +1 if speed[0] >0 else speed[0] - 1
            elif event.key == pygame.K_UP:
                speed[1] = speed[1] + 1 if speed[1] > 0 else speed[1] - 1
            elif event.key == pygame.K_DOWN:
                speed[1] = speed[1] if speed[1] == 0 else (abs(speed[1]) - 1)*int(speed[1]/abs(speed[1]))
    ballrect = ballrect.move(speed)
    if ballrect.left<0 or ballrect.right>width:
        speed[0] = -speed[0]
    if ballrect.top<0 or ballrect.bottom>height:
        speed[1] = -speed[1]
    screen.fill(BLACK)
    screen.blit(ball,ballrect)
    pygame.display.update()
    fclock.tick(fps)

相比于节奏型,操控型代码增加了如下内容:

        elif event.type == pygame.KEYDOWN:
            if event.key == pygame.K_LEFT:
                speed[0] = speed[0] if speed[0] == 0 else (abs(speed[0])-1)*int(speed[0]/abs(speed[0]))
            elif event.key == pygame.K_RIGHT:
                speed[0] = speed[0] +1 if speed[0] >0 else speed[0] - 1
            elif event.key == pygame.K_UP:
                speed[1] = speed[1] + 1 if speed[1] > 0 else speed[1] - 1
            elif event.key == pygame.K_DOWN:
                speed[1] = speed[1] if speed[1] == 0 else (abs(speed[1]) - 1)*int(speed[1]/abs(speed[1]))

其中,

pygame.KEYDOWN指令,pygame对键盘敲击的事件定义,键盘每个键对应一个具体定义。

pygame.K_LEFT(左)、pygame.K_RIGHT(右)、pygame.K_UP(上)、pygame.K_DOWN(下)这四个指令,是控制键盘中方向键的。

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值