更新比分Pygame Pong

电子笔

无论出于什么原因,我的乒乓球比赛中的比分显示都没有更新。它只是一直说“ 0”。我检查了分数是否按照游戏的逻辑进行了实际更新,并将其打印出来(打印到游戏机上)。每次绘制新的显示时,我的文本都会被“涂黑”,所以有人可以告诉我为什么它不会更新吗?

import pygame
import random
pygame.init()

# Create colors
BLACK = (0, 0, 0)
WHITE = (255, 255, 255)

# Create screen and set screen caption
size = (700, 500)
screen = pygame.display.set_mode(size)
pygame.display.set_caption("Pong")

# Loop until user clicks close button
done = False

# Used to manage how fast the screen is updated
clock = pygame.time.Clock()

# Create player class
class Player():

    # Initialize player paddles
    def __init__(self, x, y, color, height, width):
        self.x = x
        self.y = y
        self.color = color
        self.height = height
        self.width = width
        self.y_speed = 0
        self.score = 0
        self.font = pygame.font.SysFont('Calibri', 24, True, False)
        self.display_score = self.font.render("Score: %d " % (self.score), True, WHITE)

    # Updates with new position of paddle every frame
    def draw(self, y):
        pygame.draw.rect(screen, self.color, [self.x, y, self.height, self.width])

    # Keeps paddle from going off screen
    def keepOnScreen(self):
        if self.y < 0:
            self.y = 0
        elif self.y > 410:
            self.y = 410

# Create Ball class
class Ball():

    # Initialize ball in the middle of the screen with no movement
    def __init__(self, color, height, width):
        self.x = 325
        self.y = random.randrange(150, 350)
        self.color = color
        self.height = height
        self.width = width
        self.y_speed = 0
        self.x_speed = 0

    # Updates new position of ball every frame
    def draw(self, x, y):
        pygame.draw.rect(screen, self.color, [x, y, self.height, self.width])

# Create instances of both players and ball
player1 = Player(50, 100, WHITE, 25, 90)
player2 = Player(625, 100, WHITE, 25, 90)   
ball = Ball(WHITE, 20, 20)

# --- Main Program Loop --- 
while not done:
    # --- Main event loop
    for event in pygame.event.get(): # User did something
        if event.type == pygame.QUIT: # If user clicked close
            done = True # We are done so we exit this loop

        if event.type == pygame.KEYDOWN: # Players utilize keyboard to move paddles
            if event.key == pygame.K_w:
                player1.y_speed = -6
            if event.key == pygame.K_UP:
                player2.y_speed = -6
            if event.key == pygame.K_s:
                player1.y_speed = 6
            if event.key == pygame.K_DOWN:
                player2.y_speed = 6
            if event.key == pygame.K_SPACE: # Starts the ball movement
                ball.x_speed = 3 * random.randrange(-1, 1, 2)
                ball.y_speed = 3 * random.randrange(-1, 1, 2)
        if event.type == pygame.KEYUP:
            if event.key == pygame.K_w:
                player1.y_speed = 0
            if event.key == pygame.K_UP:
                player2.y_speed = 0
            if event.key == pygame.K_s:
                player1.y_speed = 0
            if event.key == pygame.K_DOWN:
                player2.y_speed = 0

    # Calculate the movement of the players
    player1.y += player1.y_speed
    player2.y += player2.y_speed

    # Prevents paddles from going off-screen
    player1.keepOnScreen()
    player2.keepOnScreen()

    # Checks to see if ball has made contact with paddle, then reverses direction of the ball
    # Had to give a 4 pixel buffer since the ball won't always exactly hit the same part of paddle in x direction
    if ball.x <= player1.x + 27 and (ball.x >= player1.x + 23):
        if ball.y >= player1.y and (ball.y <= player1.y + 100):
            ball.x_speed *= -1  
    if ball.x >= player2.x - 27 and (ball.x <= player2.x - 23):
        if ball.y >= player2.y and (ball.y <= player2.y + 100):
            ball.x_speed *= -1

    # Checks to see if ball has made contact with top or bottom of screen
    if ball.y <= 0 or ball.y >= 480:
        ball.y_speed *= -1

    # Calculates movement of the ball
    ball.x += ball.x_speed
    ball.y += ball.y_speed

    # Updates score
    if ball.x < 0:
        player2.score += 1
        ball.__init__(WHITE, 20, 20)

    if ball.x > 700:
        player1.score += 1
        ball.__init__(WHITE, 20, 20)

    # Set background
    screen.fill(BLACK)

    # Draw players and ball on screen
    player1.draw(player1.y)
    player2.draw(player2.y)
    ball.draw(ball.x, ball.y)
    screen.blit(player1.display_score, [0, 0])
    screen.blit(player2.display_score, [615, 0])

    # Update display
    pygame.display.flip()

    # Limit to 60 frames per second
    clock.tick(60)

# Close the window and quit
pygame.quit()
戴维·里夫

看来问题在于您是在设置每个玩家的display_score唯一__init__功能。

# Initialize player paddles
def __init__(self, x, y, color, height, width):
    ...
    self.score = 0
    ...
    self.display_score = self.font.render("Score: %d " % (self.score), True, WHITE)

由于初始化变量的方式,更改的值player.score不会更改的值player.display_score

解决方案1

您可以display_score在更改球员得分时更改其值,这可以通过函数调用轻松完成:

def player_scores( player, ball ):
    player.score += 1
    player.display_score = player.font.render("Score: %d " % (player.score), True, WHITE)
    ball.__init__(WHITE, 20, 20)

然后在您的游戏循环中:

# Updates score
if ball.x < 0:
    player_scores( player2, ball )

if ball.x > 700:
    player_scores( player1, ball )

解决方案2

您可以在显示乐谱文本时呈现它,而不是在播放器上创建它。在您的游戏循环中:

    screen.blit(player1.font.render("Score: %d " % (player1.score), True, WHITE), [0, 0])
    screen.blit(player2.font.render("Score: %d " % (player2.score), True, WHITE), [615, 0])

这将使display_score变量完全无效

本文收集自互联网,转载请注明来源。

如有侵权,请联系[email protected] 删除。

编辑于
0

我来说两句

0条评论
登录后参与评论

相关文章

来自分类Dev

为什么在调用更新时(pygame Pong)球不动?

来自分类Dev

在Pong Game中移动球-Pygame

来自分类Dev

我简单的pygame pong游戏落后(python)

来自分类Dev

如何使桨在pygame pong中运动?

来自分类Dev

Ping Pong Pygame 与打击垫一侧发生碰撞

来自分类Dev

MATLAB键盘输入在“ Pong”游戏中未更新

来自分类Dev

Pygame上的Pong,尽管不与球拍碰撞,球始终会弹跳

来自分类Dev

Arduino Explore Pong游戏

来自分类Dev

JavaScript Pong游戏延迟

来自分类Dev

帆布“ Pong”游戏桨

来自分类Dev

Tkinter中的Pong碰撞方法

来自分类Dev

组装中的Pong项目8086

来自分类Dev

JavaScript画布:Pong-动画

来自分类Dev

JavaFX Pong 垂直游戏问题

来自分类Dev

如何使用python在pong中增加摩擦?

来自分类Dev

使实时socket.io Pong游戏更快

来自分类Dev

用canvas javascript开发的pong游戏的问题

来自分类Dev

使实时socket.io Pong游戏更快

来自分类Dev

C ++创建Pong游戏时出错

来自分类Dev

kivy官方pong教程:“ NoneType”对象没有属性“ center”

来自分类常见问题

WebSockets ping / pong,为什么不进行TCP keepalive?

来自分类Dev

如何在Netty 4中处理PING / PONG帧?

来自分类Dev

移动AI的最佳方法是什么?LibGDX-PONG游戏

来自分类Dev

在我的Pong重拍中,桨没有在python中移动

来自分类Dev

python中的Pong游戏。得分和屏幕外检查

来自分类Dev

UIAttachmentBehavior无法用于可能会丢失的Pong AI

来自分类Dev

在 PixiJS 的帮助下重写一个普通的 JS Pong

来自分类Dev

如何在我的 Pong 游戏中找到错误?

来自分类Dev

如何更改 JavaScript Pong 游戏 onClick 的内部属性