分数未更新 Pong Pygame

Score Not Updating Pong Pygame

我不确定为什么分数没有更新。当任一玩家得分时,调试器打印 0。这是我的分数变量。

player_score = 0
opponent_score = 0
basic_font = pygame.font.Font('freesansbold.ttf', 32)

And the variables for rendering the score:

player_text = basic_font.render(f'{player_score}', False, light_grey)
screen.blit(player_text, (660, 470))

opponent_text = basic_font.render(f'{opponent_score}', False, light_grey)
screen.blit(opponent_text, (600, 470))

还有我的更新方法。

    def update(self, left_paddle, right_paddle, player_score, opponent_score):
    self.rect.x += self.vx
    self.rect.y += self.vy

    if self.rect.top <= 0 or self.rect.bottom >= screen_height:
        self.vy *= -1

    if self.rect.left <= 0:
        self.ball_start()
        player_score += 1

    if self.rect.right >= screen_width:
        self.ball_start()
        opponent_score += 1

    if self.rect.colliderect(left_paddle) or self.rect.colliderect(right_paddle):
        self.vx *= -1

def ball_start(self):
    self.rect.center = (screen_width / 2, screen_height / 2)
    self.vy *= random.choice((1, -1))
    self.vx *= random.choice((1, -1))

然后调用更新方法:

ball.update(left_paddle, right_paddle, player_score, opponent_score)

这是项目的代码。非常感谢您的帮助。

import logging
import pygame, sys
from inputs import handle_input
import random


class Paddle:
    def __init__(self):
        self.rect = pygame.Rect(10, screen_height / 2 - 70, 10, 140)

        self.speed = 10

    def draw(self, screen):
        pygame.draw.rect(screen, light_grey, self.rect)

    def move_up(self):
        self.rect.y -= self.speed
        self._keep_in_bounds()

    def move_down(self):
        self.rect.y += self.speed
        self._keep_in_bounds()

    def _keep_in_bounds(self):
        if self.rect.top <= 0:
            self.rect.top = 0

        if self.rect.bottom >= screen_height:
            self.rect.bottom = screen_height


class Ball:
    def __init__(self, x, y, width, height):
        self.rect = pygame.Rect(x, y, width, height)
        self.speed = 7
        self.vx = self.speed * random.choice((1, -1))
        self.vy = self.speed * random.choice((1, -1))

    def draw(self, screen):
        pygame.draw.ellipse(screen, light_grey, self.rect)

    def update(self, left_paddle, right_paddle, player_score, opponent_score):

        self.rect.x += self.vx
        self.rect.y += self.vy

        if self.rect.top <= 0 or self.rect.bottom >= screen_height:
            self.vy *= -1

        if self.rect.left <= 0:
            self.ball_start()
            player_score += 1

        if self.rect.right >= screen_width:
            self.ball_start()
            opponent_score += 1

        if self.rect.colliderect(left_paddle) or self.rect.colliderect(right_paddle):
            self.vx *= -1

    def ball_start(self):
        self.rect.center = (screen_width / 2, screen_height / 2)
        # print(self.rect.center)
        self.vy *= random.choice((1, -1))
        self.vx *= random.choice((1, -1))


# General setup
pygame.init()
clock = pygame.time.Clock()

# Main Window
screen_width = 1280
screen_height = 800
screen = pygame.display.set_mode((screen_width, screen_height))
pygame.display.set_caption('Pong')

# Colors
light_grey = (200, 200, 200)
bg_color = pygame.Color('grey12')

# Game Rectangles
ball = Ball(screen_width / 2 - 15, screen_height / 2 - 15, 30, 30)
left_paddle = Paddle()
right_paddle = Paddle()
right_paddle.rect.x = screen_width - right_paddle.rect.width

player_score = 0
opponent_score = 0
basic_font = pygame.font.Font('freesansbold.ttf', 32)

while True:
    for event in pygame.event.get():
        if event.type == pygame.QUIT:
            pygame.quit()
            sys.exit()

    # Game logic
    handle_input(left_paddle, right_paddle)
    screen.fill(bg_color)
    pygame.draw.aaline(screen, light_grey, (screen_width / 2, 0), (screen_width / 2, screen_height))
    ball.draw(screen)
    left_paddle.draw(screen)
    right_paddle.draw(screen)
    ball.update(left_paddle, right_paddle, player_score, opponent_score)

    player_text = basic_font.render(f'{player_score}', False, light_grey)
    screen.blit(player_text, (660, 470))

    opponent_text = basic_font.render(f'{opponent_score}', False, light_grey)
    screen.blit(opponent_text, (600, 470))

    pygame.display.flip()
    clock.tick(60)

您的更新没有反映在全局变量中,因为您根本没有更新它们。您正在更新它们的本地副本,这是通过将它们传递给 Ball.update 函数获得的。

试试这个:

def update(self, left_paddle, right_paddle):
    global player_score, opponent_score

    ...

    if self.rect.left <= 0:
        self.ball_start()
        player_score += 1

    if self.rect.right >= screen_width:
        self.ball_start()
        opponent_score += 1

    ...
    # function ends here

我认为最好的办法是创建一个 Player class 并仅在其中跟踪分数,然后将此 Player class 的实例传递给 update函数。然后,稍后从这些实例中检索分数。