为了账号安全,请及时绑定邮箱和手机立即绑定

Pygame 表面突然反转变量

Pygame 表面突然反转变量

慕森王 2022-11-29 17:19:05
我试图制作一个游戏,你可以砍掉一些树并卖掉它,但在这样做的过程中我设法发现了一个令人心碎的错误——第一棵树会倒转所有其他树!我知道这没有意义,但我的意思是我已经将树分类到一个类中,并且我创建了一个名为的变量实例self.tree_property,它确定树是否已被砍伐。当您砍倒第一棵树时,所有其他树都会重新tree_property设置True。当您砍倒任何其他树时,第一棵树tree_property会True再次设置为 任何人都可以告诉我如何修复它以及为什么会这样吗?代码如下:import pygameimport timepygame.init()print('Loading game...')icon = pygame.image.load('C:\Program Files\Foraging Simulator\icon.png')market = pygame.image.load('C:\Program Files\Foraging Simulator\market.png')house = pygame.image.load('C:\Program Files\Foraging Simulator\house.png')pygame.display.set_icon(icon)market = pygame.transform.scale(market, (100, 100))house = pygame.transform.scale(house, (100, 100))root = pygame.display.set_mode((603, 573))pygame.display.set_caption("Foraging Simulator")window_is_open = Truewhite = (255, 255, 255)black = (0, 0, 0)width = 10leaves_width = 30height = 20leaves_height = 10x = 0tree_trunk_x = 10y = 0tree_trunk_y = 10vel = 5brown = (150, 75, 0)green = (58, 95, 11)grass = (124, 252, 0)score = 0chop_wood = 'C:\Program Files\Foraging Simulator\chopping.mp3'clock = pygame.time.Clock()time = 0happiness = 10def test(string):    print(string)def reset_trees():    for tree in trees:        tree.tree_property = Truedef open_house():    reset_trees()    score = 0def open_market():    happiness = score / 2class Tree: # The class for the trees    def __init__(self, tree_x, tree_y):        self.tree_x = tree_x        self.tree_y = tree_y        self.tree_property = True # Creating instance tree_property        self.trunk = None        self.leaves = None    def destroy(self):        self.tree_property = False    def create_tree(self):        if self.tree_property:            trunk_x = self.tree_x + 10            trunk_y = self.tree_y + 10            self.trunk = pygame.draw.rect(root, brown, (trunk_x, trunk_y, width, height))            self.leaves = pygame.draw.rect(root, green, (self.tree_x, self.tree_y, leaves_width, leaves_height))
查看完整描述

1 回答

?
ibeautiful

TA贡献1993条经验 获得超5个赞

对象没有pygame.Surface位置。返回的矩形的位置get_rect()总是 (0, 0)。
请注意,当表面为 时,您指定一个位置blit

root.blit(house, (400, 100))

当您检索碰撞的矩形时,您必须设置相同的位置。您可以将关键字参数值传递给此函数,这些值设置为pygame.Rect对象:

house.get_rect(topleft = (400, 100))

例如:

while window_is_open:

    # [...]


    for event in pygame.event.get():

        if event.type == pygame.QUIT:

            window_is_open = False


        if event.type == pygame.MOUSEBUTTONDOWN:

            mouse_x, mouse_y = event.pos

            dx = mouse_x - x

            dy = mouse_y - y


            if market.get_rect(topleft = (400, 300)).collidepoint(mouse_x, mouse_y):

                if abs(dx) <= 50 and abs(dy) <= 50:

                    open_market()


            if house.get_rect(topleft = (400, 100)).collidepoint(mouse_x, mouse_y):

                if abs(dx) <= 50 and abs(dy) <= 50:

                    open_house()


            for tree in trees:

                if tree.trunk.collidepoint(mouse_x, mouse_y):

                    if abs(dx) <= 50 and abs(dy) <= 50:

                        countdown = 3

                        destroy_tree = tree

    # [...]

此外,您必须在函数中使用global语句将其happiness视为全局命名空间中的变量open_market


def open_market():

    global happiness

    happiness = score / 2


查看完整回答
反对 回复 2022-11-29
  • 1 回答
  • 0 关注
  • 70 浏览
慕课专栏
更多

添加回答

举报

0/150
提交
取消
意见反馈 帮助中心 APP下载
官方微信