80+行代码实现简单的“飞行的小鸟”游戏(2)

上一篇 / 下一篇  2018-04-27 09:47:56 / 个人分类:博为峰原创技术文章

接上篇

#定义游戏开始键

#参数为显示的地方,颜色,面板的位置和大小 

    pygame.draw.rect(screen,GROUND,(300,300,200,100))#开始键的面板

    #参数为显示的文字,是否显示,颜色

    text = arial18.render("Press space to play",True,SKY)#文字
    #文字的位置设置
    textX = text.get_rect().width
    textY = text.get_rect().height
    screen.blit(text,((400 - (textX / 2)),(350 - (textY / 2))))#字的填充位置

    同样还要设置游戏结束的按钮,只是文案稍微加上gameover。

    然后要考虑到整个页面的刷新,用pygame.display.flip方法。画面刷新时间控制由pygame.timeClock.tick控制。

    以下是游戏运行的主要函数:

# -------- Main Program Loop -----------
#当游戏没结束
while not done:
    # --- Main event loop
    # pygameevent方法
    for event in pygame.event.get():
        if event.type == pygame.QUIT:  # 退出游戏
            done = True
        # 游戏键位设置
        if event.type == pygame.KEYDOWN:
            if event.key == pygame.K_SPACE:  # 设置为空格键
                if gameState == 1:  # 游戏未开始状态为1
                    gameState = 2  # 游戏中状态为2
                elif gameState == 3:  # 游戏结束状态为3
                    bird.reset()  # 小鸟重置
                    # 管子list重置
                    pipes = []
                    pipes.append(Pipe())
                    # 游戏中
                    gameState = 2
                else:  # 没有管子的时候,小鸟飞行
                    bird.flap()
    screen.fill(SKY)  # 屏幕填色
    if gameState == 1: #游戏未开始时
        #定义游戏开始键
        pygame.draw.rect(screen,GROUND,(300,300,200,100))#开始键的面板,参数为xy轴位置,xy方向的大小
        text = arial18.render("Press space to play",True,SKY)#文字
        #文字的位置设置
        textX = text.get_rect().width
        textY = text.get_rect().height
        screen.blit(text,((400 - (textX / 2)),(350 - (textY / 2))))#字的填充位置
    # 当游戏运行中
    if gameState == 2:
        # 小鸟的飞行更新
        bird.update()
        bird.draw()
        # 管子的更新显示
        for pipe in pipes:
            pipe.update()
            pipe.draw()
    # 当游戏结束
    if gameState == 3:
        for pipe in pipes:
            pipe.draw()
        bird.draw()
        pygame.draw.rect(screen,GROUND,(300,300,250,100))#开始键的面板
        text = arial18.render("Game Over,Press space to play",True,SKY)#文字
        #文字的位置设置
        textX = text.get_rect().width
        textY = text.get_rect().height
        screen.blit(text,((420 - (textX / 2)),(350 - (textY / 2))))#字的填充位置
# 刷新页面
    pygame.display.flip()
# 画面更新时间,单位:秒
    clock.tick(50)
pygame.quit()

    游戏的实现效果是这样的:用方块来代替小鸟。

    以上就是这个小游戏的实现过程了,大家可以自己研究加上小鸟穿过管子后的计分功能,也可以把小鸟美化一下,或者给游戏设置由易到难的玩法,可以各种尝试。大家一起试试吧


TAG:

 

评分:0

我来说两句

日历

« 2024-04-26  
 123456
78910111213
14151617181920
21222324252627
282930    

数据统计

  • 访问量: 34101
  • 日志数: 43
  • 建立时间: 2018-01-25
  • 更新时间: 2018-11-09

RSS订阅

Open Toolbar