【问题标题】:How to stop my rect from leaving a trail when moving over my background image移动背景图像时如何阻止我的矩形留下痕迹
【发布时间】:2021-11-04 19:17:06
【问题描述】:

我正在做一个乒乓球游戏,球物体在移动时会留下痕迹,我想知道如何阻止它这样做,任何帮助将不胜感激。先感谢您。 这是我的代码:

import sys
import pygame
pygame.init()
clock = pygame.time.Clock()
# screen setup
screen_width = 1200
screen_height = 700
screen = pygame.display.set_mode((screen_width, screen_height))
background = pygame.image.load("spacebackground.png").convert_alpha()
pygame.mouse.set_visible(False)
pygame.display.set_caption('pong')


# set up player and opponent and ball rect
player = pygame.Rect(screen_width - 20,screen_height/2 - 70,10,140)
opponent = pygame.Rect(10,screen_height/2 - 70,10,140 )
ball = pygame.Rect(screen_width/2-15, screen_height/2 - 15,30, 30)

ball_speed_x = 7
ball_speed_y = 7
# defining clock and colour
red = (0,0,0)

clock = pygame.time.Clock()

while True:

    pygame.display.update(ball)
    pygame.display.flip()
    screen.blit(background, (0, 0))
    for event in pygame.event.get():
        if event.type == pygame.QUIT:
            pygame.quit()
            sys.exit()

    ball.x += ball_speed_x
    ball.y += ball_speed_y
    # Drawing the rects
    pygame.draw.rect(background, red, player)
    pygame.draw.rect(background, red, opponent)
    pygame.draw.ellipse(background, red, ball)
    pygame.display.update(ball)

    clock.tick(60)


    

【问题讨论】:

    标签: python python-3.x pygame


    【解决方案1】:

    screen 上而不是background 上绘制对象:

    run = True
    while run:
        for event in pygame.event.get():
            if event.type == pygame.QUIT:
                run = False 
    
        ball.x += ball_speed_x
        ball.y += ball_speed_y
       
        # draw background on the screen
        screen.blit(background, (0, 0))
    
        # draw objects on the screen
        pygame.draw.rect(screen, red, player)
        pygame.draw.rect(screen, red, opponent)
        pygame.draw.ellipse(screen, red, ball)
        
        # update the display
        pygame.display.flip()
    
        clock.tick(60)
    
    pygame.quit()
    sys.exit()
    

    【讨论】:

      猜你喜欢
      • 2019-06-09
      • 2021-07-07
      • 2018-05-13
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2011-09-09
      • 1970-01-01
      • 2019-04-15
      相关资源
      最近更新 更多