0

I have a simulator where you chop trees and I wanted it to be so that when I clicked a tree it would count down from 3 seconds, then disappear. I sorted the trees into a Tree class, but now I don't know how to get their position in an if statement as they are no longer an actual pygame surface. Can someone tell me how to get their position if they are in a class? The code is below:

import pygame
import time
pygame.init()
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)
class Tree:
    def __init__(self, tree_x, tree_y, tree_property_name):
        self.tree_x = tree_x
        self.tree_y = tree_y
        self.tree_property_name = tree_property_name
    def destroy(self):
        count = pygame.font.SysFont('Tahoma', 18, True, False)
        countdown = count.render('3', True, (0, 0, 0))
        root.blit(countdown, (583, 553))
        pygame.display.update()
        time.sleep(1)
        pygame.draw.rect(root, (255, 255, 255), (583, 553, 20, 30))
        pygame.display.update()
        count = pygame.font.SysFont('Tahoma', 18, True, False)
        countdown = count.render('2', True, (0, 0, 0))
        root.blit(countdown, (583, 553))
        pygame.display.update()
        time.sleep(1)
        pygame.draw.rect(root, (255, 255, 255), (583, 553, 20, 30))
        pygame.display.update()
        count = pygame.font.SysFont('Tahoma', 18, True, False)
        countdown = count.render('1', True, (0, 0, 0))
        root.blit(countdown, (583, 553))
        pygame.display.update()
        time.sleep(1)
        pygame.draw.rect(root, (255, 255, 255), (583, 553, 20, 30))
        pygame.display.update()
        self.tree_property_name = False
    def create_tree(self):
        if self.tree_property_name:
            trunk_x = self.tree_x + 10
            trunk_y = self.tree_y + 10
            pygame.draw.rect(root, brown, (trunk_x, trunk_y, width, height))
            pygame.draw.rect(root, green, (self.tree_x, self.tree_y, leaves_width, leaves_height))
    def redraw(self):
        self.create_tree()
tree_one_property = True
tree_two_property = True
tree_three_property = True
tree_four_property = True
tree_five_property = True
tree_six_property = True
tree_seven_property = True
tree_eight_property = True
tree_nine_property = True
tree_ten_property = True
tree_eleven_property = True
tree_twelve_property = True
tree_thirteen_property = True
tree_fourteen_property = True
tree_fifteen_property = True
tree_sixteen_property = True
tree_seventeen_property = True
tree_eighteen_property = True
tree_nineteen_property = True
tree_twenty_property = True
tree_twenty_one_property = True
tree_twenty_two_property = True
tree_twenty_three_property = True
tree_twenty_four_property = True
tree_twenty_five_property = True
tree_one = Tree(0, 0, tree_one_property)
tree_two = Tree(50, 0, tree_two_property)
tree_three = Tree(100, 0, tree_three_property)
tree_four = Tree(150, 0, tree_four_property)
tree_five = Tree(200, 0, tree_five_property)
tree_six = Tree(0, 50, tree_six_property)
tree_eight = Tree(100, 50, tree_eight_property)
tree_seven = Tree(50, 50, tree_seven_property)
tree_nine = Tree(150, 50, tree_nine_property)
tree_ten = Tree(200, 50, tree_ten_property)
tree_eleven = Tree(0, 100, tree_eleven_property)
tree_twelve = Tree(50, 100, tree_twelve_property)
tree_thirteen = Tree(100, 100, tree_thirteen_property)
tree_fourteen = Tree(150, 100, tree_fourteen_property)
tree_fifteen = Tree(200, 100, tree_fifteen_property)
tree_sixteen = Tree(0, 150, tree_sixteen_property)
tree_seventeen = Tree(50, 150, tree_seventeen_property)
tree_eighteen = Tree(100, 150, tree_eighteen_property)
tree_nineteen = Tree(150, 150, tree_nineteen_property)
tree_twenty = Tree(200, 150, tree_twenty_property)
tree_twenty_one = Tree(0, 200, tree_twenty_one_property)
tree_twenty_two = Tree(50, 200, tree_twenty_two_property)
tree_twenty_three = Tree(100, 200, tree_twenty_three_property)
tree_twenty_four = Tree(150, 200, tree_twenty_four_property)
tree_twenty_five = Tree(200, 200, tree_twenty_five_property)
root.fill(white)
while window_is_open:
    pygame.time.delay(100)
    for event in pygame.event.get():
        if event.type == pygame.QUIT:
            window_is_open = False
        if pygame.mouse.get_pressed()[0] and tree_two.rect.collidepoint(pygame.mouse.get_pos()): # This bit doesn't work
            tree_two.destroy()
    keys = pygame.key.get_pressed()
    if keys[pygame.K_RIGHT]:
        x += vel
    if keys[pygame.K_LEFT]:
        x -= vel
    if keys[pygame.K_UP]:
        y -= vel
    if keys[pygame.K_DOWN]:
        y += vel
    if keys[pygame.K_q]:
        tree_one.destroy()

    root.fill(white)
    font = pygame.font.SysFont('Tahoma', 18, True, False)
    score = font.render('Score:', True, (0, 0, 0))
    root.blit(score, (410, 0))
    rectangle = pygame.draw.rect(root, (0, 0, 0), (x, y, width, 10)) 
    tree_one.redraw()
    tree_two.redraw()
    tree_three.redraw()
    tree_four.redraw()
    tree_five.redraw()
    tree_six.redraw()
    tree_seven.redraw()
    tree_eight.redraw()
    tree_nine.redraw()
    tree_ten.redraw()
    tree_eleven.redraw()
    tree_twelve.redraw()
    tree_thirteen.redraw()
    tree_fourteen.redraw()
    tree_fifteen.redraw()
    tree_sixteen.redraw()
    tree_seventeen.redraw()
    tree_eighteen.redraw()
    tree_nineteen.redraw()
    tree_twenty.redraw()
    tree_twenty_one.redraw()
    tree_twenty_two.redraw()
    tree_twenty_three.redraw()
    tree_twenty_four.redraw()
    tree_twenty_five.redraw()
    pygame.display.update()    
pygame.quit()




Rabbid76
  • 202,892
  • 27
  • 131
  • 174
BoxifyMC
  • 43
  • 10

2 Answers2

0

Your Tree class can be better organized. One thing you need to do is put the rectangle for the tree into their own class variables so you can access them. Here is an example of how to do it. See code comments for the changes I made:

import pygame
import time
pygame.init()
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)
class Tree:
    def __init__(self, tree_x, tree_y, tree_property_name):
        self.tree_x = tree_x
        self.tree_y = tree_y
        self.tree_property_name = tree_property_name

    def destroy(self):
        count = pygame.font.SysFont('Tahoma', 18, True, False)
        countdown = count.render('3', True, (0, 0, 0))
        root.blit(countdown, (583, 553))
        pygame.display.update()
        time.sleep(1)
        pygame.draw.rect(root, (255, 255, 255), (583, 553, 20, 30))
        pygame.display.update()
        count = pygame.font.SysFont('Tahoma', 18, True, False)
        countdown = count.render('2', True, (0, 0, 0))
        root.blit(countdown, (583, 553))
        pygame.display.update()
        time.sleep(1)
        pygame.draw.rect(root, (255, 255, 255), (583, 553, 20, 30))
        pygame.display.update()
        count = pygame.font.SysFont('Tahoma', 18, True, False)
        countdown = count.render('1', True, (0, 0, 0))
        root.blit(countdown, (583, 553))
        pygame.display.update()
        time.sleep(1)
        pygame.draw.rect(root, (255, 255, 255), (583, 553, 20, 30))
        pygame.display.update()
        self.tree_property_name = False
    def create_tree(self):
        if self.tree_property_name:
            trunk_x = self.tree_x + 10
            trunk_y = self.tree_y + 10
            # add trunk & leaves variable to Tree class
            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))
    def redraw(self):
        self.create_tree()

    def is_clicked(self):
        return pygame.mouse.get_pressed()[0] and self.rect.collidepoint(pygame.mouse.get_pos())
tree_one_property = True
tree_two_property = True
tree_three_property = True
tree_four_property = True
tree_five_property = True
tree_six_property = True
tree_seven_property = True
tree_eight_property = True
tree_nine_property = True
tree_ten_property = True
tree_eleven_property = True
tree_twelve_property = True
tree_thirteen_property = True
tree_fourteen_property = True
tree_fifteen_property = True
tree_sixteen_property = True
tree_seventeen_property = True
tree_eighteen_property = True
tree_nineteen_property = True
tree_twenty_property = True
tree_twenty_one_property = True
tree_twenty_two_property = True
tree_twenty_three_property = True
tree_twenty_four_property = True
tree_twenty_five_property = True
tree_one = Tree(0, 0, tree_one_property)
tree_two = Tree(50, 0, tree_two_property)
tree_three = Tree(100, 0, tree_three_property)
tree_four = Tree(150, 0, tree_four_property)
tree_five = Tree(200, 0, tree_five_property)
tree_six = Tree(0, 50, tree_six_property)
tree_eight = Tree(100, 50, tree_eight_property)
tree_seven = Tree(50, 50, tree_seven_property)
tree_nine = Tree(150, 50, tree_nine_property)
tree_ten = Tree(200, 50, tree_ten_property)
tree_eleven = Tree(0, 100, tree_eleven_property)
tree_twelve = Tree(50, 100, tree_twelve_property)
tree_thirteen = Tree(100, 100, tree_thirteen_property)
tree_fourteen = Tree(150, 100, tree_fourteen_property)
tree_fifteen = Tree(200, 100, tree_fifteen_property)
tree_sixteen = Tree(0, 150, tree_sixteen_property)
tree_seventeen = Tree(50, 150, tree_seventeen_property)
tree_eighteen = Tree(100, 150, tree_eighteen_property)
tree_nineteen = Tree(150, 150, tree_nineteen_property)
tree_twenty = Tree(200, 150, tree_twenty_property)
tree_twenty_one = Tree(0, 200, tree_twenty_one_property)
tree_twenty_two = Tree(50, 200, tree_twenty_two_property)
tree_twenty_three = Tree(100, 200, tree_twenty_three_property)
tree_twenty_four = Tree(150, 200, tree_twenty_four_property)
tree_twenty_five = Tree(200, 200, tree_twenty_five_property)
root.fill(white)
while window_is_open:
    pygame.time.delay(100)
    for event in pygame.event.get():
        if event.type == pygame.QUIT:
            window_is_open = False
        # If Tree trunk is clicked
        if pygame.mouse.get_pressed()[0] and tree_two.trunk.collidepoint(pygame.mouse.get_pos()): # This bit doesn't work
            tree_two.destroy()

    keys = pygame.key.get_pressed()
    if keys[pygame.K_RIGHT]:
        x += vel
    if keys[pygame.K_LEFT]:
        x -= vel
    if keys[pygame.K_UP]:
        y -= vel
    if keys[pygame.K_DOWN]:
        y += vel
    if keys[pygame.K_q]:
        tree_one.destroy()

    root.fill(white)
    font = pygame.font.SysFont('Tahoma', 18, True, False)
    score = font.render('Score:', True, (0, 0, 0))
    root.blit(score, (410, 0))
    rectangle = pygame.draw.rect(root, (0, 0, 0), (x, y, width, 10))
    tree_one.redraw()
    tree_two.redraw()
    tree_three.redraw()
    tree_four.redraw()
    tree_five.redraw()
    tree_six.redraw()
    tree_seven.redraw()
    tree_eight.redraw()
    tree_nine.redraw()
    tree_ten.redraw()
    tree_eleven.redraw()
    tree_twelve.redraw()
    tree_thirteen.redraw()
    tree_fourteen.redraw()
    tree_fifteen.redraw()
    tree_sixteen.redraw()
    tree_seventeen.redraw()
    tree_eighteen.redraw()
    tree_nineteen.redraw()
    tree_twenty.redraw()
    tree_twenty_one.redraw()
    tree_twenty_two.redraw()
    tree_twenty_three.redraw()
    tree_twenty_four.redraw()
    tree_twenty_five.redraw()
    pygame.display.update()
pygame.quit()
Thaer A
  • 2,243
  • 1
  • 10
  • 14
0

You have to add an instance attribute self.rect To the class Tree. Each of your trees consists of 2 rectangles. Use pygame.Rect and union() joins two rectangles and to create a new rectangle that completely covers the area of the two rectangles. For instance:

class Tree:
    def __init__(self, tree_x, tree_y, tree_property_name):
        self.tree_x = tree_x
        self.tree_y = tree_y
        self.tree_property_name = tree_property_name

        trunk_x = self.tree_x + 10
        trunk_y = self.tree_y + 10
        rect1 = pygame.Rect(trunk_x, trunk_y, width, height)
        rect2 = pygame.Rect(self.tree_x, self.tree_y, leaves_width, leaves_height)
        self.rect = rect1.union(rect2)

    # [...]
Rabbid76
  • 202,892
  • 27
  • 131
  • 174