简体   繁体   English

为什么树不显示?

[英]Why are the trees not displaying?

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)我想将tree精灵添加到名为tree_group的精灵组我希望树木生成的距离是这样计算的: 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.由于某种原因,树没有出现,我相信这是因为树精灵还没有在组中,这就是为什么我想使用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.我希望 draw_display function 遍历屏幕上当前的所有树并使用 get_speed 方程移动它们。

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



In your draw_display function, you are first drawing the group and then adding the elements.在您的draw_display function 中,您首先绘制组,然后添加元素。 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.因此,您应该将tree_group.draw(display)行放在 elif 语句之后,就在pygame.display.update()行之前。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM