为什么树不显示?

Why are the trees not displaying?

我想将 tree 精灵添加到名为 tree_group 的精灵组 我希望树木产卵的距离是这样计算的:if 800 - (tree.x + TREE_HEIGHT) > Tree.get_distance(score): tree_group.draw(tree)

出于某种原因,树没有出现,我认为这是因为树精灵还没有出现在组中,这就是我想使用 add() 功能的原因。

我希望 draw_display 函数遍历当前屏幕上的所有树并使用 get_speed 方程移动它们。

我的代码是这样的:


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



在您的 draw_display 函数中,您首先绘制组,然后添加元素。这样,您只绘制一个空组,因此什么也不会发生。因此,您应该将 tree_group.draw(display) 行放在 elif 语句之后,就在 pygame.display.update() 行之前。