用pygame写游戏——遇到的坑和收获

2017-05-05  本文已影响0人  蒲编
游戏运行中时的图片

游戏规则:

鼠标控制你的白球,红球每隔两秒从四个边界随机出现,速度的大小和速度也是随机的,红球每隔5秒就消失,红球碰到你的白球就gameover。

写游戏中要思考的地方:

游戏运行的逻辑:

      while True:
          if self.state != self.nextState:
              self.state = self.nextState
              self.state.firstDisplay(screen)

          for event in pygame.event.get():
              self.state.handle(event)
          self.state.update(self)

          self.state.display(screen)
from PIL import Image
white_ball = Image.open('images/white_ball.png')
white_img = white_ball.convert("RGBA")
bands = white_img.split()

black_img = Image.new("RGBA", white_ball.size, (0, 0, 0))
other_bands = black_img.split()

red_ball = Image.merge("RGBA", (bands[0], other_bands[1], other_bands[2], bands[3]))
red_ball.save("images/red_ball.png", 'png')
class Vector2(object):
    def __init__(self, x=0.0, y=0.0):
        self.x = x
        self.y = y

    def __str__(self):
        return "(%s, %s)" % (self.x, self.y)
    __repr__ = __str__

    def __add__(self, other):
        return Vector2(self.x+other.x, self.y+other.y)

    def __sub__(self, other):
        return Vector2(self.x - other.x, self.y - other.y)

    def __mul__(self, num):
        return Vector2(num*self.x, num*self.y)

    def __divmod__(self, scalar):
        return Vector2(self.x / scalar, self.y / scalar)

    def get_length(self):
        from math import sqrt
        return sqrt(self.x**2 + self.y**2)

    def normolize(self):
        length = self.get_length()
        return Vector2(self.x/length, self.y/length)

    @classmethod
    def from_points(cls, P1, P2):
        return cls(P2[0] - P1[0], P2[1] - P1[1])
    def touches(self, other):
        return self.rect.colliderect(other.rect)

用这个判断白球死亡没问题,因为红白有重叠后游戏就结束了。
但用这个来让红球互相碰撞后反弹就有问题了:

        for a_red_ball in self.red_sprites:
            for another_red_ball in self.red_sprites:
                if a_red_ball is not another_red_ball and a_red_ball.touches(another_red_ball):
                    a_red_ball.speed = a_red_ball.speed * (-1)
                    another_red_ball.speed = another_red_ball.speed * (-1)

                    a_red_ball.play_bounce_sound()

遍历所有的红球,看有没有相交的。但这样有个问题,当时显示出来的就是红球抖动但不反弹。我推测是乘了多次-1。解决办法是给球加个be_bounce+False的属性,然后:

        for a_red_ball in self.red_sprites:
            for another_red_ball in self.red_sprites:
                if a_red_ball is not another_red_ball and a_red_ball.touches(another_red_ball) and \
                        not (a_red_ball.be_bounce and another_red_ball.be_bounce):
                    a_red_ball.speed = a_red_ball.speed * (-1)
                    another_red_ball.speed = another_red_ball.speed * (-1)
                    a_red_ball.be_bounce = True
                    another_red_ball.be_bounce = True
                    a_red_ball.play_bounce_sound()

        for red_ball in self.red_sprites:
            red_ball.be_bounce = False

代码很好懂,就不解释了。

self.gap += time_passed_seconds
        if int(self.gap) == 2:
            new_ball = RedBall(self.red_ball_image)
            now_sprite = pygame.sprite.RenderUpdates(new_ball)
            self.red_sprites.add(now_sprite)
            self.gap = 0

附上此游戏的github地址:https://github.com/petalsofcherry/ball_game

上一篇 下一篇

猜你喜欢

热点阅读