1

I was making a game where you chop wood in a forest every day and was doing the function to destroy a tree after 3 seconds by putting a SysFont at the bottom of the screen, counting down from three seconds which by then it would remove the tree. I binded the key Q to chop down the first tree(for testing purposes), but instead I got no removal of the tree, and no SysFont counting down - all it did was lag the game because of the time.sleep(1) i had put in. The entire code is below:

import pygame
import time
pygame.init()
root = pygame.display.set_mode((603, 573))
pygame.display.set_caption("Homework")
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)
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
def create_tree(tree_x, tree_y, tree):
    if tree:
        trunk_x = tree_x + 10
        trunk_y = tree_y + 10
        pygame.draw.rect(root, brown, (trunk_x, trunk_y, width, height))
        pygame.draw.rect(root, green, (tree_x, tree_y, leaves_width, leaves_height))
def destroy_tree(tree_property_name):
    count = pygame.font.SysFont('Tahoma', 18, True, False)
    countdown = count.render('3', True, (0, 0, 0))
    root.blit(countdown, (590, 569))
    pygame.display.update()
    time.sleep(1)
    count = pygame.font.SysFont('Tahoma', 18, True, False)
    countdown = count.render('2', True, (0, 0, 0))
    root.blit(countdown, (590, 569))
    pygame.display.update()
    time.sleep(1)
    count = pygame.font.SysFont('Tahoma', 18, True, False)
    countdown = count.render('1', True, (0, 0, 0))
    root.blit(countdown, (590, 569))
    pygame.display.update()
    time.sleep(1)
    tree_property_name = False
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
    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]:
        destroy_tree(tree_one_property)
    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 = create_tree(0, 0, tree_one_property)
    tree_two = create_tree(50, 0, tree_two_property)
    tree_three = create_tree(100, 0, tree_three_property)
    tree_four = create_tree(150, 0, tree_four_property)
    tree_five = create_tree(200, 0, tree_five_property)
    tree_six = create_tree(0, 50, tree_six_property)
    tree_seven = create_tree(50, 50, tree_seven_property)
    tree_eight = create_tree(100, 50, tree_eight_property)
    tree_nine = create_tree(150, 50, tree_nine_property)
    tree_ten = create_tree(200, 50, tree_ten_property)
    tree_eleven = create_tree(0, 100, tree_eleven_property)
    tree_twelve = create_tree(50, 100, tree_twelve_property)
    tree_thirteen = create_tree(100, 100, tree_thirteen_property)
    tree_fourteen = create_tree(150, 100, tree_fourteen_property)
    tree_fifteen = create_tree(200, 100, tree_fifteen_property)
    tree_sixteen = create_tree(0, 150, tree_sixteen_property)
    tree_seventeen = create_tree(50, 150, tree_seventeen_property)
    tree_eighteen = create_tree(100, 150, tree_eighteen_property)
    tree_nineteen = create_tree(150, 150, tree_nineteen_property)
    tree_twenty = create_tree(200, 150, tree_twenty_property)
    tree_twenty_one = create_tree(0, 200, tree_twenty_one_property)
    tree_twenty_two = create_tree(50, 200, tree_twenty_two_property)
    tree_twenty_three = create_tree(100, 200, tree_twenty_three_property)
    tree_twenty_four = create_tree(150, 200, tree_twenty_four_property)
    tree_twenty_five = create_tree(200, 200, tree_twenty_five_property)
    pygame.display.update()    
pygame.quit()




BoxifyMC
  • 43
  • 10

1 Answers1

1

It looks like you are drawing the font (almost) entirely off the bottom of the screen.The lines below:

root = pygame.display.set_mode((603, 573))

  ...

    root.blit(countdown, (590, 569))

indicate that your screen has a height of 573 pixels. You are blit'ing your text with its top at 569, which is only 4 pixels onto the screen. In reality that likely means that the text is entirely off the bottom of the screen.

Try moving it a bit higher.

In fact you can actually get the size of the bounding box of the text surface countdown like this:

text_box_size = countdown.get_size()

See docs here. You can get the height and width of the text box like this and use that to determine the offset from the bottom of the screen you want to pass to blit().

I have edited this answer to add a response to the question in your comment asking why your tree was still rendering. This would be to much to add in a follow on comment.

In your code you have a method def destroy_tree(tree_property_name) and in it you try to update the variable tree_property_name and make it False. That would work if the variable was passed to the function as pass by reference, but in python that is not how arguments are passed. So when you set it to False inside the method all that does is change the value seen inside the scope of the method. The value seen by the caller of the function is not changed.

You can get more explanation by looking at the answer to How do I pass a variable by reference?:

Your code would be significantly cleaner if you made the trees into a class. It would also allow you to be able to change the internal values in the class when the class is passed into a method.

Glenn Mackintosh
  • 2,765
  • 1
  • 10
  • 18