0

我想将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(
    "assets", "background", "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("assets", "sprites", "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))

    display.blit(SKIER, (skier.x, skier.y))

    tree_group = pygame.sprite.Group()
    tree_group.draw(display)

    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(
            "assets", "sprites", "tree.png")), (self.TREE_WIDTH, self.TREE_HEIGHT))
        self.rect = self.image.get_rect()
        self.rect.x = random.randrange(0, WIDTH)
        self.rect.y = 800
        self.rect.y += self.get_speed(score)

    def get_speed(score, base=2, interval=10, multiplier=0.05):
        return base + ((score//interval) * multiplier)

    def get_distance(score, base=2, interval=10, multiplier=5):
        return base + ((score//interval) * multiplier)


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)

    trees = 1
    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)

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


if __name__ == "__main__":
    main()

4

0 回答 0