Paddle leaves trail when moving (Pygame Pong game) [duplicate]

天涯浪子 提交于 2020-07-03 13:54:28

问题


I have problem with the paddle in my game. Every time I'm trying to move it, paddle leave a "trail". I guess it's because my code don't delete previous paddle with old position. If yes, then how can I delete previous one? Should I use blit()? Code:

import pygame, sys, random
from pygame.locals import *

pygame.init()

gamename = pygame.display.set_caption('Pong')

clock = pygame.time.Clock()
FPS = 60

black = (0, 0, 0)
white = (255, 255, 255)

screen = pygame.display.set_mode((800, 800))
screen.fill(black)

line = pygame.draw.line(screen, white, (400, 800), (400, 0), 5)


class Player(object):
    def __init__(self, screen):
        pady = 350
        padx = 40
        padh = 100
        padw = 35
        dist = 5
        self.pady = pady
        self.padx = padx
        self.padh = padh
        self.padw = padw
        self.dist = dist
        self.screen = screen

    def draw(self):
        playerpaddle = pygame.rect.Rect((self.padx, self.pady, self.padw, self.padh))
        pygame.draw.rect(self.screen, white, playerpaddle)

    def controlkeys(self):
        key = pygame.key.get_pressed()
        if key[pygame.K_s]:
            self.pady += self.dist
        elif key[pygame.K_w]:
            self.pady -= self.dist


pygame.display.update()

player = Player(screen)

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

    player.controlkeys()
    player.draw()
    pygame.display.update()
    clock.tick(FPS)

回答1:


The problem is that you are drawing your background only once. This means that your paddle, as it moves, leaves behind the pixels it has turned white without anything to cover them up. To solve this, fill your background with black and redraw your center line with each iteration of the while loop.

Here is the corrected code:

import pygame, sys, random
from pygame.locals import *

pygame.init()

gamename = pygame.display.set_caption('Pong')

clock = pygame.time.Clock()
FPS = 60

black = (0, 0, 0)
white = (255, 255, 255)

screen = pygame.display.set_mode((800, 800))
screen.fill(black)

pygame.draw.line(screen, white, (400, 800), (400, 0), 5)
#You don't need to set this to a variable, it's just a command


class Player(object):
    def __init__(self, screen):
        pady = 350
        padx = 40
        padh = 100
        padw = 35
        dist = 5
        self.pady = pady
        self.padx = padx
        self.padh = padh
        self.padw = padw
        self.dist = dist
        self.screen = screen

    def draw(self):
        playerpaddle = pygame.rect.Rect((self.padx, self.pady, self.padw, self.padh))
        pygame.draw.rect(self.screen, white, playerpaddle)

    def controlkeys(self):
        key = pygame.key.get_pressed()
        if key[pygame.K_s]:
            self.pady += self.dist
        elif key[pygame.K_w]:
            self.pady -= self.dist


pygame.display.update()

player = Player(screen)

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

    player.controlkeys()
    screen.fill(black)#Covers up everything with black
    pygame.draw.line(screen, white, (400, 800), (400, 0), 5)#Redraws your line
    player.draw()
    pygame.display.update()
    clock.tick(FPS)


来源:https://stackoverflow.com/questions/42056472/paddle-leaves-trail-when-moving-pygame-pong-game

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!