Home > Blockchain >  Why are the trees not displaying?
Why are the trees not displaying?

Time:11-18

I want to add the tree sprite to the sprite group named tree_group I expect the distance of the trees spawning is calculated like this: if 800 - (tree.x TREE_HEIGHT) > Tree.get_distance(score): tree_group.draw(tree)

For some reason, the trees are not appearing and I believe that this is because the tree sprite is not in the group yet, that is why I want to use the add() function.

I want the draw_display function to iterate over all the trees currently on the screen and move them using the get_speed equation.

My code is this:


import pygame
import sys
import random
import os
import time

from pygame.event import post

# initializes pygame libraries
pygame.init()
pygame.font.init()
pygame.mixer.init()

score = 0
FPS = 60
VEL = 5

# region | main window attributes and background
WIDTH, HEIGHT = 800, 800
display = pygame.display.set_mode((WIDTH, HEIGHT))
pygame.display.set_caption("Ski Game")

BACKGROUND = pygame.transform.scale(pygame.image.load(os.path.join("background.jpg")), (WIDTH, HEIGHT))
# endregion

# region | fonts and colors
SCORE_FONT = pygame.font.SysFont('comicsans', 40)
GAME_OVER_FONT = pygame.font.SysFont('comicsans', 100)
BLACK = (0, 0, 0)
WHITE = (255, 255, 255)
# endregion

# region | linking sprites and resizing then
SKIER_WIDTH = 65
SKIER_HEIGHT = 105
SKIER = pygame.transform.scale(pygame.image.load(
    os.path.join("skier.png")), (SKIER_WIDTH, SKIER_HEIGHT))
# endregion


clock = pygame.time.Clock()
done = False


# create a custom event
CRASH_TREE = pygame.USEREVENT   1


def draw_display(score, skier):
    # blits the background
    display.blit(BACKGROUND, (0, 0))
    score_text = SCORE_FONT.render("Score: "   str(score), 1, BLACK)
    display.blit(score_text, (WIDTH / 2 - score_text.get_width()/2, 10))
    # blit skier
    display.blit(SKIER, (skier.x, skier.y))
    # blit trees
    tree = Tree(score)
    tree_group = pygame.sprite.Group()
    tree_group.draw(display)
    if score == 0:
        tree_group.add(tree)
        score  = 1
    elif 800 - (tree.rect.x   150) > tree.get_distance(score):
        tree_group.add(tree)
        score  = 1
    pygame.display.update()


class Tree(pygame.sprite.Sprite):
    def __init__(self, score):
        super().__init__()
        self.TREE_WIDTH = 80
        self.TREE_HEIGHT = 150
        self.image = pygame.transform.scale(pygame.image.load(os.path.join("tree.png")), (self.TREE_WIDTH, self.TREE_HEIGHT))
        self.rect = self.image.get_rect()
        self.rect.x = random.randrange(0, WIDTH)
        self.rect.y = 700
        
        print("init")
        
    def get_speed(self, score, base=2, interval=10, multiplier=0.05):
        return base   ((score//interval) * multiplier)

    def get_distance(self, score, base=100, interval=10, multiplier=5):
        return base   ((score//interval) * multiplier)
        
    def update(self):
        self.rect.y -= self.get_speed(score)
        print("update")
        

def handle_skier_movement(keys_pressed, skier):
    if keys_pressed[pygame.K_LEFT] and skier.x - VEL > 0:  # LEFT
        skier.x -= VEL
    if keys_pressed[pygame.K_RIGHT] and skier.x   VEL   skier.width < WIDTH:  # RIGHT
        skier.x  = VEL
    if keys_pressed[pygame.K_UP] and skier.y - VEL > 0:  # UP
        skier.y -= VEL
    if keys_pressed[pygame.K_DOWN] and skier.y   VEL   skier.width < WIDTH:  # DOWN
        skier.y  = VEL


def game_over():
    game_over_text = GAME_OVER_FONT.render("GAME OVER", 1, BLACK)
    display.blit(game_over_text, (WIDTH/2 - game_over_text.get_width() /
                                  2, HEIGHT/2 - game_over_text.get_height()/2))
    pygame.display.update()
    pygame.time.delay(5000)


def main():
    skier = pygame.Rect(700, 300, SKIER_WIDTH, SKIER_HEIGHT)

    score = 0

    clock = pygame.time.Clock()
    run = True
    while run:

        clock.tick(FPS)
        for event in pygame.event.get():
            if event.type == pygame.QUIT:
                run = False
                pygame.quit()

        keys_pressed = pygame.key.get_pressed()

        handle_skier_movement(keys_pressed, skier)

        draw_display(score, skier)
        
        tree = Tree(score)
        tree.update()

        if event.type == CRASH_TREE:
            game_over()
            score = 0
            break


if __name__ == "__main__":
    main()



CodePudding user response:

In your draw_display function, you are first drawing the group and then adding the elements. That way, you are only drawing an empty group, and thus nothing happens. You should therefore place the tree_group.draw(display) line after the elif statement, right before pygame.display.update() line.

  • Related