Pygame 表面突然反转变量

我试图制作一个游戏,你可以砍掉一些树并卖掉它,但在这样做的过程中我设法发现了一个令人心碎的错误——第一棵树会倒转所有其他树!我知道这没有意义,但我的意思是我已经将树分类到一个类中,并且我创建了一个名为的变量实例self.tree_property,它确定树是否已被砍伐。当您砍倒第一棵树时,所有其他树都会重新tree_property设置True。当您砍倒任何其他树时,第一棵树tree_propertyTrue再次设置为 任何人都可以告诉我如何修复它以及为什么会这样吗?代码如下:

import pygame

import time

pygame.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 = True

white = (255, 255, 255)

black = (0, 0, 0)

width = 10

leaves_width = 30

height = 20

leaves_height = 10

x = 0

tree_trunk_x = 10

y = 0

tree_trunk_y = 10

vel = 5

brown = (150, 75, 0)

green = (58, 95, 11)

grass = (124, 252, 0)

score = 0

chop_wood = 'C:\Program Files\Foraging Simulator\chopping.mp3'

clock = pygame.time.Clock()

time = 0

happiness = 10

def test(string):

    print(string)

def reset_trees():

    for tree in trees:

        tree.tree_property = True

def open_house():

    reset_trees()

    score = 0

def open_market():

    happiness = score / 2

class 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))

慕森王
浏览 73回答 1
1回答

ibeautiful

对象没有pygame.Surface位置。返回的矩形的位置get_rect()总是 (0, 0)。请注意,当表面为 时,您指定一个位置blit:root.blit(house,&nbsp;(400,&nbsp;100))当您检索碰撞的矩形时,您必须设置相同的位置。您可以将关键字参数值传递给此函数,这些值设置为pygame.Rect对象:house.get_rect(topleft&nbsp;=&nbsp;(400,&nbsp;100))例如:while window_is_open:&nbsp; &nbsp; # [...]&nbsp; &nbsp; for event in pygame.event.get():&nbsp; &nbsp; &nbsp; &nbsp; if event.type == pygame.QUIT:&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; window_is_open = False&nbsp; &nbsp; &nbsp; &nbsp; if event.type == pygame.MOUSEBUTTONDOWN:&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; mouse_x, mouse_y = event.pos&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; dx = mouse_x - x&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; dy = mouse_y - y&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; if market.get_rect(topleft = (400, 300)).collidepoint(mouse_x, mouse_y):&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; if abs(dx) <= 50 and abs(dy) <= 50:&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; open_market()&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; if house.get_rect(topleft = (400, 100)).collidepoint(mouse_x, mouse_y):&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; if abs(dx) <= 50 and abs(dy) <= 50:&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; open_house()&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; for tree in trees:&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; if tree.trunk.collidepoint(mouse_x, mouse_y):&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; if abs(dx) <= 50 and abs(dy) <= 50:&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; countdown = 3&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; destroy_tree = tree&nbsp; &nbsp; # [...]此外,您必须在函数中使用global语句将其happiness视为全局命名空间中的变量open_market:def open_market():&nbsp; &nbsp; global happiness&nbsp; &nbsp; happiness = score / 2
打开App,查看更多内容
随时随地看视频慕课网APP

相关分类

Python