亚洲在线久爱草,狠狠天天香蕉网,天天搞日日干久草,伊人亚洲日本欧美

為了賬號安全,請及時綁定郵箱和手機立即綁定
已解決430363個問題,去搜搜看,總會有你想問的

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條經驗 獲得超6個贊

對象沒有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 關注
  • 149 瀏覽
慕課專欄
更多

添加回答

舉報

0/150
提交
取消
微信客服

購課補貼
聯系客服咨詢優惠詳情

幫助反饋 APP下載

慕課網APP
您的移動學習伙伴

公眾號

掃描二維碼
關注慕課網微信公眾號