微信公众号搜"智元新知"关注
微信扫一扫可直接关注哦!

游戏问题

如何解决游戏问题

我是一名年轻的 Python 程序员。我决定使用 pygame 创建一个 2D 游戏 游戏目的:在汽车上行驶尽可能远的距离,而不会撞到会在游戏过程中“产生”的物体。汽车将驶过田野。

我的装饰精灵有问题(在游戏过程中,树木会“倒”在窗户的边缘)图 1.pic1

因此,树应该在前一棵树到达窗口中间后立即生成,但是当新树生成时,这就是我的情况:图 2。然后游戏开始冻结pic2

这是我的代码

from superwires import games,color
from random import randrange
games.init(screen_width = 530,screen_height = 600,fps = 60)

#Car sprite

class Car(games.Sprite):
    image = games.load_image("C:/python/car.bmp")
    def __init__(self):
        super(Car,self).__init__(image = Car.image,x = games.mouse.x,bottom = games.screen.height - 10)
        self.score = games.Text(value = 0,size = 25,color = color.yellow,top = 5,right = games.screen.width/2)
        games.screen.add(self.score)
    def update(self):
        self.x = games.mouse.x
        if self.left < 65:
            self.left = 65
        if self.right > games.screen.width - 65:
            self.right = games.screen.width - 65

#Tree sprite

class Bush1(games.Sprite):
    image = games.load_image("C:/python/bush.bmp")
    speed = 1
    def __init__(self,x = 20,y = 100):
        super(Bush1,self).__init__(image = Bush1.image,x = x,y = y,dy = Bush1.speed)
    def update(self):
        if self.bottom > games.screen.height/2:
            newbush = Bush1()
            newbush.__init__(x = 20,y = -100)
            games.screen.add(newbush)

class Bush2(games.Sprite):
    image = games.load_image("C:/python/bush.bmp")
    speed = 1
    def __init__(self,x = 515,y = 100):
        super(Bush2,self).__init__(image = Bush2.image,dy = Bush2.speed)

    #Spawning new trees

    def update(self):
        if self.bottom > games.screen.height/2:
            newbush = Bush2()
            newbush.__init__(x = 515,y = -100)
            games.screen.add(newbush)

#Start
def main(): 
    road = games.load_image("road.jpg",transparent = False)
    games.screen.background = road
    bush1 = Bush1()
    bush2 = Bush2()
    car = Car()
    games.screen.add(bush1)
    games.screen.add(bush2)
    games.screen.add(car)
    games.mouse.is_visible = False
    games.screen.event_grab = True
    games.screen.mainloop()

main()

我很高兴知道我在哪里犯了错误

使用:Python 3.9、superwires、游戏

解决方法

这是你的问题。一旦您的第一个灌木丛到达中点,您就会在每个框架上创建两个新灌木丛。这就是你在 pic2 中看到的——你有数百个稍微重叠的灌木丛。只有当旧灌木正好位于中点时,您才需要创建新灌木,而不是在中点或低于中点:

    if self.bottom == games.screen.height//2:

一旦灌木丛从底部脱落,您也可以考虑删除它们

,

这可能无法解决您遇到的具体问题,但它可能有助于了解正在发生的事情并归零。

您在这里创建了一个新的 Bush1Bush2 对象:

bush1 = Bush1()
bush2 = Bush2()

请注意,这两个类的代码实际上是相同的。不同之处仅在于 init 默认值。相当于这样做:

bush1 = Bush1()
bush2 = Bush1(515,100)

基于此,您似乎对 Python 中的类的工作方式有误解。这被两个几乎等效但为常量的 update 块强化了:

    def update(self):
        if self.bottom > games.screen.height/2:
            newbush = Bush1()
            newbush.__init__(x = 20,y = -100)
            games.screen.add(newbush)

在这些块的第三行,您正在创建一个新对象。然后您重新调用该对象的 __init__ 方法。创建新对象时,会运行几个隐藏的“私有”方法,最后一个 __init__ 方法。通常,如果您需要更改对象的实例化,则需要更改该方法。您永远不需要重新调用该方法。

最后,据我所知,您实际上并没有移动灌木丛。我怀疑当您说“开始冻结”时,发生的情况是您有许多灌木丛的实例相互重叠,并且游戏需要越来越长的时间来处理每个循环。在每个 init 方法中放置一个打印语句可能有助于识别这个问题,但您的框架可能有更好的方法来做到这一点。 (我不熟悉他们。)

稍微重构一下以清理类和范围变量的正确使用应该会产生一些类似的东西(尽管几乎可以肯定我们可以做更多):

from superwires import games,color
from random import randrange
games.init(screen_width = 530,screen_height = 600,fps = 60)

#Car sprite

class Car(games.Sprite):
    
    def __init__(self,image: str):
        self.__image = games.load_image(image)
        #  Is the car really instantiated at the point the mouse currently is at?
        super().__init__(image = self.__image,x = games.mouse.x,bottom = games.screen.height - 10)
        #  Is 'score' really a field of 'car'?
        self.score = games.Text(value = 0,size = 25,color = color.yellow,top = 5,right = games.screen.width/2)
        games.screen.add(self.score)

    def update(self):
        self.x = games.mouse.x
        self.left = 65 if self.left < 65 else self.left
        # This should probably be defined in a broader scope
        max_right = games.screen.width - 65
        self.right = self.right if self.right <= max_right else max_right 


class Bush(games.Sprite):
    speed = 1
    def __init__(self,image: str 
                 x: int,y: int):
        self.__image = games.load_image(image)
        self.__start_x = x
        self.__start_y = y
        super().__init__(image = self.__image,x = x,y = y,dy = Bush.speed)

    def update(self):
        if self.bottom > games.screen.height/2:
            # uses the same initial values as this object
            newbush = Bush(self.__image,self.__start_x,-100)  # start vertically above the screen
            games.screen.add(newbush)

#Start
def main(): 
    road = games.load_image("road.jpg",transparent = False)
    games.screen.background = road
    bush1 = Bush("C:/python/bush.bmp",20,100)
    bush2 = Bush("C:/python/bush.bmp",515,100)
    car = Car("C:/python/car.bmp")
    games.screen.add(bush1)
    games.screen.add(bush2)
    games.screen.add(car)
    games.mouse.is_visible = False
    games.screen.event_grab = True
    games.screen.mainloop()

main()

版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。