python 我如何更新我的P1_score和P2_score,使其为任一值增加一分?

b1payxdu  于 2023-04-28  发布在  Python
关注(0)|答案(2)|浏览(97)

我正在制作一个乒乓游戏,基于Clearcode的2020年视频,虽然我已经能够适应的代码工作w/ 3。9 python,我还没有弄清楚如何更新文本以将+1添加到玩家侧或计算机侧。
代码:

import pygame, sys, random

pygame.init()
clock = pygame.time.Clock()

Width = 1600
Length = 900
Line_Width = 15
Line_Color = (255, 255, 255)





Color = (0, 0, 0)
NetColor = (150, 150, 150)

screen = pygame.display.set_mode((Width, Length))
pygame.display.set_caption('Spy-pong!')
screen.fill(Color)

ball = pygame.Rect(Width/2 - 15, Length/2 - 15, 30, 30)

play_paddle = pygame.Rect(Width - 20, Length/2 - 70, 10, 140)

com_paddle = pygame.Rect(10, Length/2 - 70, 10, 140)

def ball_animation():   
    global ball_mspeed_x, ball_mspeed_y, P1_score, P2_score
    ball.x += ball_mspeed_x
    
    ball.y += ball_mspeed_y
    
    if ball.top <= 0 or ball.bottom >= Length:
        ball_mspeed_y *= -1
        
    if ball.left <= 0:
        P1_score += 1
        ball_restart()
        
    if ball.right >= Width:     
        P2_score += 1
        ball_restart()             
    
    if ball.colliderect(play_paddle) or ball.colliderect(com_paddle):
        ball_mspeed_x *= -1
        
ball_mspeed_x = 15 * random.choice((1, -1))
ball_mspeed_y = 15 * random.choice((1, -1))
player_speed = 0
com_speed = 13        

P1_score = 0
P2_score = 0
game_font = pygame.font.Font("freesansbold.ttf",32)

def play_paddle_animation():
    play_paddle.y += player_speed
    if play_paddle.top <= 0:
        play_paddle.top = 0
    if play_paddle.bottom >= Length:
        play_paddle.bottom = Length

def com_paddle_ai():
   
    if com_paddle.top < ball.y:
        com_paddle.top += com_speed
    if com_paddle.bottom > ball.y:
        com_paddle.bottom -= com_speed
    if com_paddle.top <= 0:
        com_paddle.top = 0
    if com_paddle.bottom >= Length:
        com_paddle.bottom = Length  

def ball_restart():
    global ball_mspeed_x, ball_mspeed_y
    ball.center = (Width/2, Length/2)
    ball_mspeed_x *= random.choice((1,-1))
    ball_mspeed_y *= random.choice((1,-1))
 
def draw_rect():
        
        pygame.draw.rect(screen, Line_Color, play_paddle)
        pygame.draw.rect(screen, Line_Color, com_paddle)
        
def draw_ellipse():
            
        pygame.draw.ellipse(screen, Line_Color, ball)
            
def draw_aaline():
    
        pygame.draw.aaline(screen, NetColor, (Width/2, 0), (Width/2, Length))

                

P1_text = game_font.render(f"{P1_score}",False,Line_Color)
screen.blit(P1_text,(835, 450))

P2_text = game_font.render(f"{P2_score}",False,Line_Color)
screen.blit(P2_text,(750, 450))    
     
    
while True:
    
    screen.fill(Color)
    
    screen.blit(P1_text, (835, 450))
    
    screen.blit(P2_text, (750, 450))
    
    draw_rect()

    draw_ellipse()

    draw_aaline()
    

    for event in pygame.event.get():
         if event.type == pygame.QUIT:
             pygame.QUIT()
             sys.exit()
             
         if event.type == pygame.KEYDOWN:    
             if event.key == pygame.K_DOWN: 
                 player_speed += 15
             if event.key == pygame.K_UP:
                 player_speed -= 15
         if event.type == pygame.KEYUP:    
             if event.key == pygame.K_DOWN: 
                 player_speed -= 15
             if event.key == pygame.K_UP:
                 player_speed += 15  
    
    
    
    ball_animation()
    play_paddle_animation()
    com_paddle_ai()           
    
   
                  
                
                
                
                
    pygame.display.update()
    clock.tick(60)

我试着重复屏幕。blit,但这确实工作,我打算做一个单独的屏幕。blit为player += 1变量,但效果并不好。我只需要文本更新,因为我已经得到了修改1的句柄。9.6由clearcode使用的代码将由3.9 python

w46czmvw

w46czmvw1#

为了更新分数文本,您需要在每次调用ball_animation()函数时呈现更新的分数。
以下是更新分数文本的方法:
在ball_animation()函数中创建新的P1_text和P2_text曲面,其中包括更新后的分数。
用包含更新分数的新曲面替换主游戏循环中的现有P1_text和P2_text曲面。
下面是更新后的ball_animation()函数:def ball_animation():global ball_mspeed_x、ball_mspeed_y、P1_score、P2_score

# Update the ball position
ball.x += ball_mspeed_x
ball.y += ball_mspeed_y

# Check for collisions with walls and paddles
if ball.top <= 0 or ball.bottom >= Length:
    ball_mspeed_y *= -1
    
if ball.left <= 0:
    P1_score += 1
    P1_text = game_font.render(f"{P1_score}",False,Line_Color)
    ball_restart()
    
if ball.right >= Width:
    P2_score += 1
    P2_text = game_font.render(f"{P2_score}",False,Line_Color)
    ball_restart()

if ball.colliderect(play_paddle) or ball.colliderect(com_paddle):
    ball_mspeed_x *= -1
    
# Update the score text
P1_text = game_font.render(f"{P1_score}", False, Line_Color)
P2_text = game_font.render(f"{P2_score}", False, Line_Color)

以下是如何在主游戏循环中更新分数文本:while True:

# ...

# Update the screen with the new score text
screen.blit(P1_text, (835, 450))
screen.blit(P2_text, (750, 450))

# ...
taor4pac

taor4pac2#

我看到的是,事实上P1_textP2_text只渲染一次,这是在你运行主游戏循环之前,有可能在你再次调用blit之前重新渲染它们,就像这样:

# these lines can be commented out singe you are anyway printing the text 
#   at every cycle of the loop
#P1_text = game_font.render(f"{P1_score}",False,Line_Color)
#screen.blit(P1_text,(835, 450))

#P2_text = game_font.render(f"{P2_score}",False,Line_Color)
#screen.blit(P2_text,(750, 450))    
 

while True:
    screen.fill(Color)

    P1_text = game_font.render(f"{P1_score}",False,Line_Color)
    screen.blit(P1_text, (835, 450))
    P2_text = game_font.render(f"{P2_score}",False,Line_Color)
    screen.blit(P2_text, (750, 450))

    draw_rect()

相关问题