[英]Pygame, how to remove trail from a player when moving, I tried filling screen but still didnt work for me
So I am trying to remove the trail from player while it moves.所以我试图在玩家移动时从玩家身上移除踪迹。 I tried to do screen.fill, but when I do it, then the whole map disappears.我试图做screen.fill,但是当我这样做时,整个map就消失了。 I want to achieve a simple moving ascii letter.我想实现一个简单的移动ASCII字母。 - tiles(map1) is supposed to draw the map, however if I put screen.fill before it, then the map will disappear i have no idea why. - 瓷砖(map1)应该绘制map,但是如果我把screen.fill放在它之前,那么map会消失我不知道为什么。
import pygame
from pygame import key
from pygame.draw import rect
from pygame.locals import *
pygame.init()
fpsClock = pygame.time.Clock()
resmulti=3
spritesize=16*resmulti
resolution=(256*resmulti,224*resmulti)
screen = pygame.display.set_mode((resolution[0], resolution[1]))
pygame.display.set_caption("test")
player_character = "@"
player_color = (255, 0, 0)
player_size = 20
current_position = [0, 1]
player = pygame.font.Font(None, player_size).render(player_character,False, player_color)
tile = pygame.draw.rect(screen, (255,255,255), [0,0,10,10])
map1 = """
wwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwww
w w
w wwwww w ww w
w w w
w w w
w www www w
w w w w w
w w w w w
w w wwwww www w
w w
wwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwww
"""
map1 = map1.splitlines()
gameRunning = True
background = pygame.image.load("bg.png")
def tiles(map1):
global tile
for y,line in enumerate(map1):
for x,c in enumerate(line):
if c == "w":
screen.blit(screen,(x*16,y*16),tile)
p_x = 20
p_y = 90
while gameRunning:
for event in pygame.event.get():
if event.type == pygame.QUIT:
gameRunning = False
break
keys = pygame.key.get_pressed()
if keys[K_DOWN]:
p_y += 2
elif keys[K_UP]:
p_y = p_y - 2
elif keys[K_LEFT]:
p_x = p_x - 2
elif keys[K_RIGHT]:
p_x += 2
tiles(map1)
screen.blit(player,(p_x,p_y))
fpsClock.tick(60)
pygame.display.update()
tile = pygame.draw.rect(screen, (255,255,255), [0,0,10,10])
That returns a pygame.Rect object, so tile is a rect.这将返回一个 pygame.Rect object,所以 tile 是一个矩形。
Then you use tile in the blit function.然后在 blit function 中使用 tile。
screen.blit(screen,(x*16,y*16),tile)
But from pygame's documentation, blit takes a pygame.Surface not a rect.但是从 pygame 的文档来看, blit 需要一个 pygame.Surface 不是一个矩形。
To fix the problem.解决问题。
Make tile a pygame.Surface.使瓷砖成为 pygame.Surface。
#tile = pygame.draw.rect(screen, (255,255,255), [0,0,10,10])
tile = pygame.Surface((10, 10)).convert()
tile.fill((255, 255, 255))
Draw the tile as follows.绘制瓷砖如下。
if c == "w":
screen.blit(tile,(x*16,y*16))
Then clear screen, draw tiles, draw player.然后清屏,画瓷砖,画玩家。
screen.fill((0, 0, 0))
tiles(map1)
screen.blit(player,(p_x,p_y))
The easiest is to simply replace最简单的就是简单地更换
screen.blit(screen, (x * 16, y * 16), tile)
with和
pygame.draw.rect(screen, (255, 255, 255), (x * 16, y * 16, 10, 10))
The issue you had was that you were blit
ing an area of the screen
, the same area that you had colored white previously ( tile = pygame.draw.rect(screen, (255,255,255), [0,0,10,10])
).您遇到的问题是您正在对screen
的一个区域进行blit
,这与您之前涂成白色的区域相同( tile = pygame.draw.rect(screen, (255,255,255), [0,0,10,10])
)。 The issue is that if you for example used fill
to fill the screen
with some color, it would also fill in that drawn square you had and it would no more be white but the filled color and since your tiles would be then created in that same color, they would be the same color as the entire screen
.问题是,例如,如果您使用fill
来用某种颜色填充screen
,它也会填充您拥有的绘制的正方形,并且它不再是白色而是填充的颜色,并且因为您的图块随后会以相同的颜色创建颜色,它们将与整个screen
的颜色相同。 So you could either create a new surface (or use an image) or just use draw
function as I have shown.因此,您可以创建一个新表面(或使用图像),也可以只使用draw
function,如我所展示的。
This allows then to use blit
or fill
at the start of the while
loop to fill the screen with background color.这允许在while
循环开始时使用blit
或fill
来用背景颜色填充屏幕。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.