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. I want to achieve a simple moving ascii letter. - 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.
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()
CodePudding user response:
tile = pygame.draw.rect(screen, (255,255,255), [0,0,10,10])
That returns a pygame.Rect object, so tile is a rect.
Then you use tile in the blit function.
screen.blit(screen,(x*16,y*16),tile)
But from pygame's documentation, blit takes a pygame.Surface not a rect.
To fix the problem.
Make tile a 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))
CodePudding user response:
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])
). 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
. So you could either create a new surface (or use an image) or just use draw
function as I have shown.
This allows then to use blit
or fill
at the start of the while
loop to fill the screen with background color.