划桨移动时留下痕迹(乒乓游戏)

2024-06-26 13:53:31 发布

您现在位置:Python中文网/ 问答频道 /正文

我的球拍有问题。每次我试图移动它,划桨留下一条“痕迹”。我想是因为我的代码没有删除以前的旧位置的划桨。如果是,那么如何删除前一个?我应该使用blit()? 代码:

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)

Tags: keyselfeventdistdefdisplayscreenpygame
1条回答
网友
1楼 · 发布于 2024-06-26 13:53:31

问题是你只画了一次背景。这意味着你的划桨,当它移动时,会留下像素,它已经变成白色,而没有任何东西来掩盖它们。要解决这个问题,用黑色填充背景,并用while循环的每次迭代重新绘制中心线。在

以下是正确的代码:

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)

相关问题 更多 >