桨在移动时留下痕迹(Pygame Pong 游戏) [英] Paddle leaves trail when moving (Pygame Pong game)
问题描述
我的游戏中的桨有问题.每次我试图移动它时,划桨都会留下痕迹".我想这是因为我的代码没有删除旧位置的前一个桨.如果是,那么我怎样才能删除以前的?我应该使用 blit()
吗?代码:
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)
推荐答案
问题是您只绘制了一次背景.这意味着您的桨在移动时会留下它变成白色的像素,而没有任何东西可以掩盖它们.要解决此问题,请用黑色填充背景并在 while 循环的每次迭代中重新绘制中心线.
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.
这是更正后的代码:
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)
这篇关于桨在移动时留下痕迹(Pygame Pong 游戏)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!