得分未更新Pong PyGame

Score Not Updating Pong Pygame(得分未更新Pong PyGame)
本文介绍了得分未更新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))

然后调用更新方法:

BAL.UPDATE(Left_Paddle,Right_Paddle,PERAYER_SCORE,ACTRATER_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类,并且只跟踪其中的分数,然后将这个Player类的实例传递给update函数。然后,稍后从这些实例中检索分数。

这篇关于得分未更新Pong PyGame的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持编程学习网!

本站部分内容来源互联网,如果有图片或者内容侵犯您的权益请联系我们删除!

相关文档推荐

Leetcode 234: Palindrome LinkedList(Leetcode 234:回文链接列表)
How do I read an Excel file directly from Dropbox#39;s API using pandas.read_excel()?(如何使用PANDAS.READ_EXCEL()直接从Dropbox的API读取Excel文件?)
subprocess.Popen tries to write to nonexistent pipe(子进程。打开尝试写入不存在的管道)
I want to realize Popen-code from Windows to Linux:(我想实现从Windows到Linux的POpen-code:)
Reading stdout from a subprocess in real time(实时读取子进程中的标准输出)
How to call type safely on a random file in Python?(如何在Python中安全地调用随机文件上的类型?)