为什么游戏内得分不更新?

0 投票
1 回答
34 浏览
提问于 2025-04-12 04:08

这是代码。

import random
import arcade


SCREEN_WIDTH = 960
SCREEN_HEIGHT = 720
SCREEN_TITLE = "Гонки"
CAR_ANGLE = 20
CAR_SPEED = 5
WALL_SPEED = 5


class Car(arcade.Sprite):
    def update(self):
        self.center_x += self.change_x


class Wall(arcade.Sprite):
    def __init__(self, filename, scale):
        super().__init__(filename, scale)
        self.score = 0  # Initialize the score attribute

    def update(self):
        self.center_y -= self.change_y
        if self.top < 0:
            self.bottom = SCREEN_HEIGHT + random.randint(0, SCREEN_HEIGHT)
            self.center_x = random.randint(168, SCREEN_WIDTH - 168)
            self.score += 1  # Increment the score
        return self.score  # Return the updated score


class MyGame(arcade.Window):
    def __init__(self, width, height, title):
        super().__init__(width, height, title)
        self.bg = arcade.load_texture('bg.png')
        self.car = Car("yellow_car.png", 0.8)
        self.car.center_x = SCREEN_WIDTH / 2
        self.car.center_y = 100
        self.score = 0  # Initialize score attribute for the game
        self.wall = Wall('wall.png', 0.8)
        self.wall.center_y = SCREEN_HEIGHT
        self.wall.center_x = random.randint(168, SCREEN_WIDTH - 168)
        self.wall.change_y = WALL_SPEED
        self.game = True

    def update(self, delta_time: float):
        if self.game:
            self.car.update()
            self.score = self.wall.update()  # Update score from Wall class

    def on_draw(self):
        self.clear()
        arcade.draw_texture_rectangle(SCREEN_WIDTH / 2, SCREEN_HEIGHT / 2, SCREEN_WIDTH, SCREEN_HEIGHT, self.bg)
        self.car.draw()
        self.wall.draw()
        if self.game:
            arcade.draw_text(f"Your score is {self.score}", 10, 10, arcade.color.AFRICAN_VIOLET, font_size=26)
        else:
            arcade.draw_text("Авария!", SCREEN_WIDTH / 2 - 150, SCREEN_HEIGHT / 2, arcade.color.CYAN, font_size=60)

        if self.score == 10:
            arcade.draw_text("Выигрыш!", SCREEN_WIDTH / 2 - 150, SCREEN_HEIGHT / 2, arcade.color.GOLD, font_size=60)

    def update(self, delta_time: float):
        if self.game:
            self.car.update()
            self.wall.update()
            if arcade.check_for_collision(self.car, self.wall):
                self.game = False

    def on_key_press(self, symbol: int, modifiers: int):
        if self.game:
            if symbol == arcade.key.LEFT:
                self.car.change_x = -CAR_SPEED
                self.car.angle = CAR_ANGLE
            if symbol == arcade.key.RIGHT:
                self.car.change_x = CAR_SPEED
                self.car.angle = -CAR_ANGLE

    def on_key_release(self, symbol: int, modifiers: int):
        if symbol == arcade.key.LEFT:
            self.car.change_x = 0
            self.car.angle = 0
        if symbol == arcade.key.RIGHT:
            self.car.change_x = 0
            self.car.angle = 0


window = MyGame(SCREEN_WIDTH, SCREEN_HEIGHT, SCREEN_TITLE)
arcade.run()

这里面有个问题,特别是在这一部分:

    def on_draw(self):
        self.clear()
        arcade.draw_texture_rectangle(SCREEN_WIDTH / 2, SCREEN_HEIGHT / 2, SCREEN_WIDTH, SCREEN_HEIGHT, self.bg)
        self.car.draw()
        self.wall.draw()
        if self.game:
            arcade.draw_text(f"Your score is {self.score}", 10, 10, arcade.color.AFRICAN_VIOLET, font_size=26)

当我开始游戏时,屏幕上应该显示分数,当木板的y坐标低于0的时候。但是当它经过这个位置时,什么都没有发生,分数还是零。

1 个回答

暂无回答

撰写回答