zoukankan      html  css  js  c++  java
  • pygame-KidsCanCode系列jumpy-part6-主角挂掉重新开始

    游戏的虚拟世界中,最让人happy的一个因素就是主角挂了,而且重来,只要restart就行了,不象现实中人的生命只有1次。回顾上节的效果,如果方块向下落时,挡板没接住,整个游戏就跪了:

    如果我们希望方块挂了之后,可以重新来过,可以这样做,修改Game类的update方法:

        def update(self):
            self.all_sprites.update()
            if self.player.vel.y > 0:
                hits = pg.sprite.spritecollide(self.player, self.platforms, False)
                if hits:
                    self.player.pos.y = hits[0].rect.top
                    self.player.vel.y = 0
            if self.player.rect.top < HEIGHT / 4:
                self.player.pos.y += abs(self.player.vel.y)
                for plat in self.platforms:
                    plat.rect.top += abs(self.player.vel.y)
                    if plat.rect.top > HEIGHT:
                        plat.kill()
            # 如果方块跌落到屏幕之外
            if self.player.rect.bottom > HEIGHT:
                # 为了让体验更好,整个屏幕上滚,然后将所有方块干掉
                for sprite in self.all_sprites:
                    sprite.rect.top -= max(self.player.vel.y, 5)
                    if sprite.rect.bottom < 0:
                        sprite.kill()
                # 如果1个档板都没有了,游戏结束,然后run()本次运行结束,下一轮主循环进来时,new()重新初始化,所有sprite实例重新初始化,满血复活
                if len(self.platforms) <= 0:
                    self.playing = False
    
            while len(self.platforms) <= 5:
                width = random.randint(50, 100)
                p = Platform(random.randint(0, WIDTH - width),
                             random.randint(-70, -30),
                             width, 10)
                self.platforms.add(p)
                self.all_sprites.add(p)
    

    效果如下:

    可以看到,方块挂了后,屏幕自动下滚,然后重新开始了。

    再来讨论另一个问题:游戏得分。 每跳一级,应该给于玩家一定的奖励(比如:得分),然后在屏幕上显示出来。

    先定义一个显示文字的函数:(main.py中)

        def draw_text(self, text, size, color, x, y):
            font = pg.font.SysFont(self.font_name, size)
            text_surface = font.render(text, True, color)
            text_rect = text_surface.get_rect()
            text_rect.midtop = (x, y)
            self.screen.blit(text_surface, text_rect)
    

    self.font_name在初始化时指定:

        def __init__(self):
            pg.init()
           ...
            self.font_name = FONT_NAME
    

    为了方便调整,可以在settings.py中定义字体名:

    # Player properties
    ...
    FONT_NAME = "Menlo"
    ...
    

    得分值score在new()中初始化:(main.py中)

        def new(self):
            self.score = 0
            ...
            self.run()
    

    跳跃过程中,屏幕会下滚(包括所有档板),如果档板下移到屏幕外,得分+10(注:不能在方块与档板碰撞时+分,不然如果方块跳上一块档板,再跳下来,再跳上去,反复上下跳,可以不断刷得分)

        def update(self):
            self.all_sprites.update()
            ...
            if self.player.rect.top < HEIGHT / 4:
                self.player.pos.y += abs(self.player.vel.y)
                for plat in self.platforms:
                    plat.rect.top += abs(self.player.vel.y)
                    if plat.rect.top > HEIGHT:
                        plat.kill()
                        # 得分+10
                        self.score += 10
           ...
    

    最后main.py中的draw函数中,实时显示得分:

        def draw(self):
            self.screen.fill(BLACK)
            ...
            self.draw_text(str(self.score), 22, WHITE, WIDTH / 2, 15)
            ...
    

  • 相关阅读:
    自定义key解决zabbix端口监听取值不准确的问题
    Redis——主从同步原理
    Leetcode 24——Swap Nodes in Pairs
    Struts2——第一个helloworld页面
    Leetcode 15——3Sum
    Leetcode 27——Remove Element
    C#简单入门
    Leetcode 12——Integer to Roman
    Leetcode 6——ZigZag Conversion
    eclipse如何debug调试jdk源码(任何源码)并显示局部变量
  • 原文地址:https://www.cnblogs.com/yjmyzz/p/ygame-kidscancode-part6-restart.html
Copyright © 2011-2022 走看看