简体   繁体   English

Pygame,如何在移动时从玩家身上移除轨迹,我尝试填充屏幕但仍然不适合我

[英]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循环开始时使用blitfill来用背景颜色填充屏幕。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 我试图让玩家在 pygame 中移动,但它没有用,它给了我这个错误: - I tried to make a player move in pygame but it didn't work and it's giving me this error: 我想显示第一个列表中的第一个单词并显示第二个列表中的 10 个单词等等-python。我尝试使用范围和 zip。它仍然不起作用 - I want to display first word from 1st list and display 10 words from 2nd list and so on-python.I tried it using range and zip.it didnt work still Pygame Class 按键时播放器不动 - Pygame Class Player not moving when I press key 如何让我的 object 在 Pygame 中移动时不留下痕迹? - How do I make my object not leave a trail while moving in Pygame? Pygame 当我没有使用 screen.fill(color) 时文本重叠,为什么? - Pygame Text overlap when i didnt use screen.fill(color),why? 为什么我的播放器 animation 在我不动的时候仍然出现? - Why is my player animation still happening when I am not moving? 如何在 pygame 中移动球而不是在整个屏幕上留下痕迹? - How can I move the ball instead of leaving a trail all over the screen in pygame? 我如何让 pygame 中的播放器不显示屏幕 - How do i keep my player in pygame from going off of the screen 如何从屏幕上删除 pygame 按钮图像? - How to remove pygame button image from screen? 如何从屏幕pygame中完全删除精灵 - How to remove sprite completely from screen pygame
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM