【问题标题】:Change the speed of a single unchanging sprite object in pygame改变pygame中单个不变精灵对象的速度
【发布时间】:2016-05-20 21:01:05
【问题描述】:

我正在从一本书中编写一个 pygame 程序,其中厨师掉落比萨饼,您必须用平底锅接住它们。厨师和平移精灵只创建一次,而披萨精灵显然会不断创建。我正在努力做到这一点,以便随着分数的提高,厨师开始更快地移动(仅在 x 中移动)。我想我在类属性和实例属性方面遇到了麻烦。即使我尝试将 score 设为全局变量,甚至只是将其分配给虚拟全局变量,我也无法找到从 chef 类访问分数的方法(这将允许我在更新方法)。或者,我尝试访问 pan 类中的 chefs dx,因为那是得分所在。即使使用 getattr 方法,我也无法访问它。非常感谢对此提出的任何建议。这是 pan 和 chef 类的代码。我已经注释掉了我尝试过但没有成功的部分内容。

from livewires import games, color
import random

games.init(screen_width = 640, screen_height = 480, fps = 50)


class Pan(games.Sprite):
    """
    A pan controlled by player to catch falling pizzas.
    """
    image = games.load_image("pan.bmp")

    def __init__(self):
        """ Initialize Pan object and create Text object for score. """
        super(Pan, self).__init__(image = Pan.image,
                                  x = games.mouse.x,
                                  bottom = games.screen.height)


        self.score = games.Text(value = 0, size = 25, color = color.black,
                                top = 5, right = games.screen.width - 10)
        games.screen.add(self.score)

    def update(self):
        """ Move to mouse x position. """
        self.x = games.mouse.x

        if self.left < 0:
            self.left = 0

        if self.right > games.screen.width:
            self.right = games.screen.width

        self.check_catch()
    def check_catch(self):
        """ Check if catch pizzas. """
        for pizza in self.overlapping_sprites:
            self.score.value += 10

#increase the speed of the pizza

 #           if self.score.value % 100 == 0:
#             Pizza.speed += 0.1
     #           print(Pizza.speed)

#increase the speed of the chef


   #         if self.score.value % 100 == 0:
     #          print(Chef.dx)
      #         print(x)
       #        y = int(x)*2
        #       print(y)

            self.score.right = games.screen.width - 10 
            pizza.handle_caught()




class Chef(games.Sprite):
    """
    A chef which moves left and right, dropping pizzas.
    """
    image = games.load_image("chef.bmp")
    speed  = 2

    def __init__(self, y = 55, odds_change = 200):
        """ Initialize the Chef object. """
        super(Chef, self).__init__(image = Chef.image,
                                   x = games.screen.width / 2,
                                   y = y,
                                   dx = Chef.speed)

        self.odds_change = odds_change
        self.time_til_drop = 0

    def update(self):
        #x = getattr(Pan,"score")
        #print(x)
        """ Determine if direction needs to be reversed. """
        if self.left < 0 or self.right > games.screen.width:
            self.dx = -self.dx
        elif random.randrange(self.odds_change) == 0:
           self.dx = -self.dx       
        self.check_drop()


    def check_drop(self):
        """ Decrease countdown or drop pizza and reset countdown. """
        if self.time_til_drop > 0:
            self.time_til_drop -= 1
        else:
            new_pizza = Pizza(x = self.x)
            games.screen.add(new_pizza)

            # set buffer to approx 30% of pizza height, regardless of pizza speed   
            self.time_til_drop = int(new_pizza.height * 1.3 / Pizza.speed) + 1      


def main():
    """ Play the game. """
    wall_image = games.load_image("wall.jpg", transparent = False)
    games.screen.background = wall_image

    the_chef = Chef()
    games.screen.add(the_chef)

    the_pan = Pan()
    games.screen.add(the_pan)

    games.mouse.is_visible = False

    games.screen.event_grab = True
    games.screen.mainloop()

# start it up!
main()   

【问题讨论】:

    标签: python class oop attributes pygame


    【解决方案1】:

    由于厨师速度是一个类变量,您需要添加类名才能访问它,如:Chef.speed。披萨课在哪里?我不知道 Livewire,所以无法解释为什么您无法访问分数,但您肯定可以以某种方式将其设置为数字并以这种方式使用它?

    【讨论】:

      【解决方案2】:

      感谢您的宝贵时间。我能够通过在 Pan 类中创建一个属性标志来检查分数,然后在 Chef 的更新方法中访问该属性标志来解决它,然后才能更改 dx. Chef.speed 只是初始厨师速度,因此更改它不会更新厨师的 dx。

      【讨论】:

        猜你喜欢
        • 2018-08-20
        • 2018-03-08
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2020-09-14
        相关资源
        最近更新 更多